Skip to content

Commit

Permalink
disable rules in the simulator when the guard is false and add locati…
Browse files Browse the repository at this point in the history
…on information to guards
  • Loading branch information
reb-ddm committed Feb 14, 2025
1 parent 4d46e1a commit 5215298
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion core/KaSa_rep/frontend/handler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ let guard_to_bdu parameters error handler_bdu guard bdu_restriction =
Ckappa_sig.Views_bdu.mvbdu_true parameters handler_bdu error
| LKappa.False ->
Ckappa_sig.Views_bdu.mvbdu_false parameters handler_bdu error
| LKappa.Param a ->
| LKappa.Param (a, _) ->
let error, handler_bdu, association_list =
Ckappa_sig.Views_bdu.build_association_list parameters handler_bdu error
[
Expand Down
8 changes: 4 additions & 4 deletions core/KaSa_rep/frontend/prepreprocess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -826,8 +826,8 @@ let add_conflict_site_to_rule parameters error agent site1 site2 rule =
let conflicts_guard_p_name agent site1 site2 =
"@co-" ^ agent ^ "-" ^ site1 ^ "-" ^ site2

let add_conflict_to_guard guard_opt agent site1 site2 negate =
let guardp = LKappa.Param (conflicts_guard_p_name agent site1 site2) in
let add_conflict_to_guard guard_opt agent site1 site2 negate loc =
let guardp = LKappa.Param (conflicts_guard_p_name agent site1 site2, loc) in
let guardp =
if negate then
LKappa.Not guardp
Expand Down Expand Up @@ -867,10 +867,10 @@ let add_rules_with_conflicts parameters error (rule_string, guard, (rule, p))
in
if was_changed then (
let guard_new_rule =
add_conflict_to_guard guard agent site1 site2 false
add_conflict_to_guard guard agent site1 site2 false p
in
let guard_og_rule =
add_conflict_to_guard guard agent site1 site2 true
add_conflict_to_guard guard agent site1 site2 true p
in
( error,
(id, guard_og_rule, (rule, p))
Expand Down
4 changes: 2 additions & 2 deletions core/KaSa_rep/frontend/preprocess.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1166,9 +1166,9 @@ let rec guard_param_conversion convert error guard_params g =
match g with
| LKappa.True -> error, LKappa.True
| LKappa.False -> error, LKappa.False
| LKappa.Param p ->
| LKappa.Param (p, loc) ->
let error, conv_p = convert p error guard_params in
error, LKappa.Param conv_p
error, LKappa.Param (conv_p, loc)
| LKappa.Not g1 ->
let error, conv_g1 = guard_param_conversion convert error guard_params g1 in
error, LKappa.Not conv_g1
Expand Down
2 changes: 1 addition & 1 deletion core/grammar/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ let merge_guards g1 g2 =

let rec guard_params_list_from_guard = function
| LKappa.True | LKappa.False -> []
| Param id -> [ id ]
| Param (id, _) -> [ id ]
| Not guard -> guard_params_list_from_guard guard
| And (g1, g2) | Or (g1, g2) ->
let gp1 = guard_params_list_from_guard g1 in
Expand Down
12 changes: 7 additions & 5 deletions core/grammar/cst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
(* |_|\_\ * GNU Lesser General Public License Version 3 *)
(******************************************************************************)

let add_working_set_guard guard k =
let add_working_set_guard guard k loc =
let guard_name = "@rule-" ^ string_of_int k in
let guard_param = LKappa.Param guard_name in
let guard_param = LKappa.Param (guard_name, loc) in
match guard with
| None -> Some guard_param
| Some guard -> Some (LKappa.And (guard_param, guard))
Expand All @@ -17,16 +17,18 @@ let append_to_ast_compil rev_instr compil =
fst
@@ List.fold_left
(fun (r, k) -> function
| Ast.RULE (label, guard, rule, is_in_working_set) ->
| Ast.RULE (label, guard, (rule, loc), is_in_working_set) ->
if is_in_working_set then
( {
r with
Ast.rules =
(label, add_working_set_guard guard k, rule) :: r.Ast.rules;
(label, add_working_set_guard guard k loc, (rule, loc))
:: r.Ast.rules;
},
k + 1 )
else
{ r with Ast.rules = (label, guard, rule) :: r.Ast.rules }, k
( { r with Ast.rules = (label, guard, (rule, loc)) :: r.Ast.rules },
k )
| Ast.SIG ag -> { r with Ast.signatures = ag :: r.Ast.signatures }, k
| Ast.TOKENSIG str_pos ->
{ r with Ast.tokens = str_pos :: r.Ast.tokens }, k
Expand Down
2 changes: 1 addition & 1 deletion core/grammar/kparser4.mly
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ small_guard_bool_expr:
| OP_PAR annoted guard_bool_expr CL_PAR annoted { $3 }
| TRUE annoted { LKappa.True }
| FALSE annoted { LKappa.False }
| ID annoted { LKappa.Param $1 }
| ID annoted { LKappa.Param ($1, rhs_pos 1) }
| NOT annoted small_guard_bool_expr
{ LKappa.Not $3 }
;
Expand Down
39 changes: 31 additions & 8 deletions core/grammar/lKappa_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,24 @@ let conflicts_to_id agents_sig conflicts =
(agent_id, snd agent), (site1_id, snd site1), (site2_id, snd site2))
conflicts

let evaluate_guard_opt guard guard_param_values =
let rec evaluate_guard = function
| LKappa.True -> true
| LKappa.False -> false
| LKappa.Param (p, pos) ->
(try Ast.StringMap.find p guard_param_values
with Not_found ->
raise
(ExceptionDefn.Malformed_Decl
("Undefined value for guard parameter ", pos)))
| Not guard -> not (evaluate_guard guard)
| And (g1, g2) -> evaluate_guard g1 && evaluate_guard g2
| Or (g1, g2) -> evaluate_guard g1 || evaluate_guard g2
in
match guard with
| None -> true
| Some guard -> evaluate_guard guard

let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil
=
(* TODO test this *)
Expand Down Expand Up @@ -2704,14 +2722,19 @@ let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil
in

let rules =
List.rev_map
(fun (rule : rule_inter_rep) ->
( rule.label_opt,
rule.guard,
( assemble_rule ~warning ~syntax_version rule agents_sig counters_info
tokens_finder alg_vars_finder,
rule.pos ) ))
cleaned_rules
List.rev
(List.filter_map
(fun (rule : rule_inter_rep) ->
if evaluate_guard_opt rule.guard ast_compil.guard_param_values then
Some
( rule.label_opt,
rule.guard,
( assemble_rule ~warning ~syntax_version rule agents_sig
counters_info tokens_finder alg_vars_finder,
rule.pos ) )
else
None)
cleaned_rules)
in

let variables =
Expand Down
6 changes: 3 additions & 3 deletions core/term/lKappa.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type rule_mixture = rule_agent list
type 'id guard =
| True
| False
| Param of 'id
| Param of 'id Loc.annoted
| Not of 'id guard
| And of 'id guard * 'id guard
| Or of 'id guard * 'id guard
Expand Down Expand Up @@ -513,7 +513,7 @@ let rec string_of_guard g =
match g with
| True -> "TRUE"
| False -> "FALSE"
| Param i -> i
| Param (i, _) -> i
| And (a, b) -> string_of_guard a ^ " && " ^ string_of_guard b
| Or (a, b) -> string_of_guard a ^ " || " ^ string_of_guard b
| Not a -> "[not] " ^ string_of_guard a
Expand All @@ -522,7 +522,7 @@ let rec print_guard f g =
match g with
| True -> Format.fprintf f "TRUE"
| False -> Format.fprintf f "FALSE"
| Param i -> Format.fprintf f "%s" i
| Param (i, _) -> Format.fprintf f "%s" i
| And (a, b) -> Format.fprintf f "@[%a && %a@]" print_guard a print_guard b
| Or (a, b) -> Format.fprintf f "@[(%a || %a)@]" print_guard a print_guard b
| Not a -> Format.fprintf f "@[[not] %a@]" print_guard a
Expand Down
2 changes: 1 addition & 1 deletion core/term/lKappa.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type rule_mixture = rule_agent list
type 'id guard =
| True
| False
| Param of 'id
| Param of 'id Loc.annoted
| Not of 'id guard
| And of 'id guard * 'id guard
| Or of 'id guard * 'id guard
Expand Down

0 comments on commit 5215298

Please sign in to comment.