Skip to content

Commit

Permalink
added guard field to lkappa rule
Browse files Browse the repository at this point in the history
  • Loading branch information
reb-ddm committed Dec 9, 2024
1 parent f6aebc0 commit f0cfd4c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
2 changes: 2 additions & 0 deletions core/grammar/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ let compile_inits ~debug_mode ~warning ?rescale ~compile_mode_on contact_map env
LKappa.r_rate = Alg_expr.const Nbr.zero;
LKappa.r_un_rate = None;
LKappa.r_edit_style = true;
LKappa.r_guard = None;
}
in
let preenv'', state' =
Expand Down Expand Up @@ -544,6 +545,7 @@ let compile_inits ~debug_mode ~warning ?rescale ~compile_mode_on contact_map env
LKappa.r_rate = Alg_expr.const Nbr.zero;
LKappa.r_un_rate = None;
LKappa.r_edit_style = false;
LKappa.r_guard = None;
}
in
(match
Expand Down
1 change: 1 addition & 0 deletions core/grammar/lKappa_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ let assemble_rule ~warning ~syntax_version (rule : rule_inter_rep)
r_delta_tokens;
r_rate;
r_un_rate;
r_guard = rule.guard;
}

let modif_expr_of_ast ~warning ~syntax_version sigs counters_info tok algs
Expand Down
3 changes: 3 additions & 0 deletions core/symmetries/patterns_extra.ml
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ let lkappa_init =
LKappa.r_rate = Alg_expr.int 0;
LKappa.r_un_rate = None;
LKappa.r_edit_style = true;
LKappa.r_guard = None;
}

let raw_mixture_to_lkappa_rule raw_mixture =
Expand All @@ -583,6 +584,7 @@ let raw_mixture_to_lkappa_rule raw_mixture =
LKappa.r_rate = Alg_expr.int 0;
LKappa.r_un_rate = None;
LKappa.r_edit_style = true;
LKappa.r_guard = None;
}

let rule_mixture_to_lkappa_rule rule_mixture =
Expand All @@ -593,6 +595,7 @@ let rule_mixture_to_lkappa_rule rule_mixture =
LKappa.r_rate = Alg_expr.int 0;
LKappa.r_un_rate = None;
LKappa.r_edit_style = true;
LKappa.r_guard = None;
}

(*convert a species into lkappa rule signature*)
Expand Down
34 changes: 25 additions & 9 deletions core/term/lKappa.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ type rule_agent = {

type rule_mixture = rule_agent list

type 'id guard =
| True
| False
| Param of 'id
| Not of 'id guard
| And of 'id guard * 'id guard
| Or of 'id guard * 'id guard

type rule = {
r_mix: rule_mixture;
r_created: Raw_mixture.t;
Expand All @@ -46,16 +54,9 @@ type rule = {
* (rule_mixture, int) Alg_expr.e Loc.annoted option)
option;
r_edit_style: bool;
r_guard: string guard option;
}

type 'id guard =
| True
| False
| Param of 'id
| Not of 'id guard
| And of 'id guard * 'id guard
| Or of 'id guard * 'id guard

let print_link pr_port pr_type pr_annot f = function
| ANY_FREE -> Format.pp_print_string f "#"
| LNK_TYPE (p, a) -> Format.fprintf f "%a.%a" (pr_port a) p pr_type a
Expand Down Expand Up @@ -508,8 +509,22 @@ let print_rates ~noCounters sigs counters_info pr_tok pr_var f r =
md))
max_dist)

let rec print_guard f g =
(*rTODO test*)
match g with
| True -> Format.fprintf f "TRUE"
| False -> Format.fprintf f "FALSE"
| 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

let print_rule ~noCounters ~full sigs counters_info pr_tok pr_var f r =
Format.fprintf f "@[<h>%t%t%a%t@]"
Format.fprintf f "@[<h>%t%t%t%a%t@]"
(fun f ->
match r.r_guard with
| None -> ()
| Some g -> print_guard f g)
(fun f ->
if full || r.r_edit_style then
Format.fprintf f "%a%a"
Expand Down Expand Up @@ -679,6 +694,7 @@ let rule_of_json ~filenames = function
(List.assoc "unary_rate" l)
with Not_found -> None);
r_edit_style = Yojson.Basic.Util.to_bool (List.assoc "edit_style" l);
r_guard = None (*TODO List.assoc_opt "guard" l;*);
}
with Not_found ->
raise (Yojson.Basic.Util.Type_error ("Incorrect rule", x)))
Expand Down
20 changes: 12 additions & 8 deletions core/term/lKappa.mli
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ Array.copy ra_ints)]. *)
type rule_mixture = rule_agent list
(** [rule_mixture] is the mixture description from the initial state of a rule *)

type 'id guard =
| True
| False
| Param of 'id
| Not of 'id guard
| And of 'id guard * 'id guard
| Or of 'id guard * 'id guard

type rule = {
r_mix: rule_mixture; (** Initial mixture state *)
r_created: Raw_mixture.t; (** Mixture state after rule is applied *)
Expand All @@ -62,16 +70,10 @@ type rule = {
option;
r_edit_style: bool;
(** If rule was written in edit style, else it's rewrite style *)
r_guard: string guard option;
(** There could be a guard that defines if this rule is active or not. *)
}

type 'id guard =
| True
| False
| Param of 'id
| Not of 'id guard
| And of 'id guard * 'id guard
| Or of 'id guard * 'id guard

val agent_to_erased : Signature.s -> rule_agent -> rule_agent
val to_erased : Signature.s -> rule_mixture -> rule_mixture
val to_maintained : rule_mixture -> rule_mixture
Expand Down Expand Up @@ -132,6 +134,8 @@ val print_rates :
rule ->
unit

val print_guard : Format.formatter -> string guard -> unit

val print_rule :
noCounters:bool ->
full:bool ->
Expand Down

0 comments on commit f0cfd4c

Please sign in to comment.