1
|
(* this file is no longer used. It contains the code that enable the use of
|
2
|
the CPS to build an evaluator.
|
3
|
|
4
|
Three pieces of code:
|
5
|
- excerpt from the main that instanciate the functor to the evaluator and run the provided trace
|
6
|
- run_trace function
|
7
|
- Evaluator module
|
8
|
*)
|
9
|
|
10
|
let _main_ _ =
|
11
|
| Eval ->
|
12
|
let module Model = (val model) in
|
13
|
let module T = CPS_transformer.Evaluator in
|
14
|
let module Sem = CPS.Semantics (T) (Model) in
|
15
|
let nb_traces = List.length Model.traces in
|
16
|
if nb_traces = 0 then
|
17
|
failwith ("no available traces for model " ^ Model.name);
|
18
|
if !trace_id >= 0 && !trace_id < nb_traces then
|
19
|
let eval_func =
|
20
|
match !eval_mode with
|
21
|
| CPSEval ->
|
22
|
fun (evt, env) ->
|
23
|
let _, env', actions_performed = Sem.compute modularmode (evt, env, []) in
|
24
|
env', actions_performed
|
25
|
in
|
26
|
run_trace Model.model eval_func (List.nth Model.traces !trace_id)
|
27
|
|
28
|
else
|
29
|
failwith (string_of_int !trace_id ^
|
30
|
" is not a valid trace index in [0.." ^ string_of_int nb_traces ^ "]")
|
31
|
|
32
|
|
33
|
|
34
|
let run_trace model func t =
|
35
|
let init_env = Datatype.SF.init_env model in
|
36
|
let _ = Format.printf "Model definitions@.%a@.Initial state: %s @.####" Datatype.SF.pp_src (snd model) (fst model) in
|
37
|
|
38
|
let final_env, cpt =
|
39
|
List.fold_left (fun (env, cpt) event ->
|
40
|
Format.printf "#### %i@.%a@." cpt ActiveStates.Env.pp_env env;
|
41
|
Format.printf "-- Event %a --@." Basetypes.pp_event event;
|
42
|
let env', actions_performed = func (event, env) in
|
43
|
let _ =
|
44
|
match actions_performed with
|
45
|
| [] -> Format.printf "-- no action performed --@."
|
46
|
| _ -> (
|
47
|
Format.printf "@[<v 2>-- action performed --@ ";
|
48
|
List.iter (fun a -> Format.printf "%s@ " a) actions_performed;
|
49
|
Format.printf "@]@."
|
50
|
) in
|
51
|
(* we do not consider produced events *)
|
52
|
env', cpt+1
|
53
|
) (init_env, 1) t
|
54
|
in
|
55
|
Format.printf "#### %i@.%a@." cpt ActiveStates.Env.pp_env final_env;
|
56
|
()
|
57
|
|
58
|
|
59
|
type truc = A of base_action_t | C of base_condition_t
|
60
|
module Evaluator : TransformerType with type t = (event_t * bool ActiveStates.Env.t * truc list) -> (event_t * bool ActiveStates.Env.t * truc list ) =
|
61
|
struct
|
62
|
include TransformerStub
|
63
|
type env_t = event_t * bool ActiveStates.Env.t * truc list (* Don't care for values yet *)
|
64
|
type t = env_t -> env_t
|
65
|
|
66
|
let null rho = rho
|
67
|
let add_action a (evt, rho, al) = (evt, rho, al@[A a]) (* not very efficient but
|
68
|
avoid to keep track of
|
69
|
action order *)
|
70
|
let add_condition c (evt, rho, al) = (evt, rho, al@[C c]) (* not very efficient but
|
71
|
avoid to keep track of
|
72
|
action order *)
|
73
|
|
74
|
let bot _ = assert false
|
75
|
|
76
|
let ( >> ) tr1 tr2 = fun rho -> rho |> tr1 |> tr2
|
77
|
|
78
|
let ( ?? ) b tr = if b then tr else null
|
79
|
|
80
|
let eval_open p (evt, rho, al) = (evt, ActiveStates.Env.add p true rho, al)
|
81
|
let eval_close p (evt, rho, al) = (evt, ActiveStates.Env.add p false rho, al)
|
82
|
let eval_call : type c. (module ThetaType with type t = t) -> c call_t -> c -> t =
|
83
|
fun kenv ->
|
84
|
let module Theta = (val kenv : ThetaType with type t = t) in
|
85
|
fun call -> match call with
|
86
|
| Ecall -> (fun (p, p', f) -> Theta.theta E p p' f)
|
87
|
| Dcall -> (fun p -> Theta.theta D p)
|
88
|
| Xcall -> (fun (p, f) -> Theta.theta X p f)
|
89
|
|
90
|
let eval_act kenv action =
|
91
|
(* Format.printf "----- action = %a@." Action.pp_act action; *)
|
92
|
match action with
|
93
|
| Action.Call (c, a) -> eval_call kenv c a
|
94
|
| Action.Quote a -> add_action a
|
95
|
| Action.Open p -> eval_open p
|
96
|
| Action.Close p -> eval_close p
|
97
|
| Action.Nil -> null
|
98
|
|
99
|
(*if (match trans.event with None -> true | _ -> e = trans.event) && trans.condition rho*)
|
100
|
let rec eval_cond condition ok ko : t =
|
101
|
(* Format.printf "----- cond = %a@." Condition.pp_cond condition; *)
|
102
|
match condition with
|
103
|
| Condition.True -> ok
|
104
|
| Condition.Active p -> (fun ((evt, env, al) as rho) -> if ActiveStates.Env.find p env then ok rho else ko rho)
|
105
|
| Condition.Event e -> (fun ((evt, env, al) as rho) -> match evt with None -> ko rho | Some e' -> if e=e' then ok rho else ko rho)
|
106
|
| Condition.Neg cond -> eval_cond cond ko ok
|
107
|
| Condition.And (cond1, cond2) -> eval_cond cond1 (eval_cond cond2 ok ko) ko
|
108
|
| Condition.Quote c -> add_condition c >> ok (* invalid behavior but similar to the other: should evaluate condition *)
|
109
|
|
110
|
let pp_transformer fmt tr =
|
111
|
Format.fprintf fmt "<transformer>"
|
112
|
|
113
|
let pp_principal fmt tr =
|
114
|
Format.fprintf fmt "principal =@.%a" pp_transformer tr
|
115
|
|
116
|
let pp_component : type c. Format.formatter -> c call_t -> c -> t -> unit =
|
117
|
fun fmt call -> match call with
|
118
|
| Ecall -> (fun (p, p', f) tr ->
|
119
|
Format.fprintf fmt "component %a(%a, %a, %a) =@.%a" pp_call call pp_path p pp_path p' pp_frontier f pp_transformer tr)
|
120
|
| Dcall -> (fun p tr ->
|
121
|
Format.fprintf fmt "component %a(%a) =@.%a" pp_call call pp_path p pp_transformer tr)
|
122
|
| Xcall -> (fun (p, f) tr ->
|
123
|
Format.fprintf fmt "component %a(%a, %a) =@.%a" pp_call call pp_path p pp_frontier f pp_transformer tr)
|
124
|
end
|
125
|
|