Skip to content

Commit

Permalink
disable initial mixture in KaSim if the guard is false
Browse files Browse the repository at this point in the history
  • Loading branch information
reb-ddm committed Feb 17, 2025
1 parent 386550e commit f0e2064
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 45 deletions.
5 changes: 3 additions & 2 deletions core/cli/cli_init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ let preprocess_ast ~warning ~debug_mode ?kasim_args cli_args
ast_compiled_data.agents_sig ast_compiled_data.counters_info
ast_compiled_data.contact_map
ast_compiled_data.token_names.NamedDecls.finder
ast_compiled_data.alg_vars_finder compil.Ast.init),
ast_compiled_data.alg_vars_finder compil.Ast.init
compil.Ast.guard_param_values),
conf.Configuration.initial )
in
{
Expand Down Expand Up @@ -193,7 +194,7 @@ let get_pack_from_marshalizedfile ~warning kasim_args cli_args marshalized_file
compilation_result.counters_info compilation_result.contact_map
(Model.tokens_finder compilation_result.env)
(Model.algs_finder compilation_result.env)
compil.Ast.init
compil.Ast.init compil.Ast.guard_param_values
in
let init_l =
Eval.compile_inits ~debug_mode:!Parameter.debug_modeOn ~warning
Expand Down
91 changes: 48 additions & 43 deletions core/grammar/lKappa_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1907,15 +1907,54 @@ let create_sigs (l : Ast.agent_sig list) : Signature.s =
(* TODO see agent_sigs namings *)
Signature.create ~counters_per_agent agent_sigs

type bool_or_error = Value of bool | Error of Loc.t

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

let init_of_ast ~warning ~syntax_version sigs counters_info contact_map tok algs
inits =
List.map
(fun (g, (*rTODO*) expr, ini) ->
( g,
alg_expr_of_ast ~warning ~syntax_version sigs counters_info tok algs
expr,
init_of_ast ~warning ~syntax_version sigs counters_info tok contact_map
ini ))
inits guard_param_values =
List.filter_map
(fun (guard, expr, ini) ->
if evaluate_guard_opt guard guard_param_values then
Some
( guard,
alg_expr_of_ast ~warning ~syntax_version sigs counters_info tok algs
expr,
init_of_ast ~warning ~syntax_version sigs counters_info tok
contact_map ini )
else
None)
inits

type ast_compiled_data = {
Expand Down Expand Up @@ -2542,41 +2581,6 @@ let conflicts_to_id agents_sig conflicts =
(agent_id, snd agent), (site1_id, snd site1), (site2_id, snd site2))
conflicts

type bool_or_error = Value of bool | Error of Loc.t

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

let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil
=
(* TODO test this *)
Expand Down Expand Up @@ -2780,6 +2784,7 @@ let compil_of_ast ~warning ~debug_mode ~syntax_version ~var_overwrite ast_compil
let init =
init_of_ast ~warning ~syntax_version agents_sig counters_info contact_map
tokens_finder alg_vars_finder ast_compil.init
ast_compil.guard_param_values
in
{
agents_sig;
Expand Down
1 change: 1 addition & 0 deletions core/grammar/lKappa_compiler.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ val init_of_ast :
int Mods.StringMap.t ->
int Mods.StringMap.t ->
(Ast.mixture, Ast.mixture, string) Ast.init_statement list ->
bool Ast.StringMap.t ->
(LKappa.rule_agent list, Raw_mixture.t, int) Ast.init_statement list

type ast_compiled_data = {
Expand Down

0 comments on commit f0e2064

Please sign in to comment.