Source file autoGen.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
(** compilation/generation d'automate
-------------------------------------
La génération statique d'un automate n'est pas forcément
la meilleure solution : l'idée d'un générateur dynamique
qui énumère les contraintes à la volée est sans doute plue
efficace.
Il s'agit donc essentiellement de faire des test de branchement
sur Lucky pour peaufiner le travail. Une solution plus efficace
sera étudiée plus tard.
L'algo général est du type ``calcul des dérivées régulières'',
par opposition aux algos de style reglo/Thompson (trop compliqués
pour le langage étendu avec exception et parallélisme).
On procède en plusieurs étapes :
- génération d'un automate abstrait avec transitions "branchues"
- traduction de cet automate dans la syntaxe Lucky
La compilation en automate branchu est basée sur un schéma simple
qui rappèle le calcul des dérivées des e.r.
*)
open LutErrors;;
open Printf;;
open CoTraceExp ;;
open Expand ;;
let dbg = Verbose.get_flag "AutoGen"
(** N.B. On utilise des AlgExp.t de type "CkTypeEff.weight" pour
représenter les poids dans les transitions *)
(** WEIGHTS *)
type weightexp =
| W_huge
| W_exp of CoAlgExp.t
let opt_weight_of_opt_algexp = function
| None -> None
| Some e -> Some (
if (e = CoAlgExp.of_huge_weight) then W_huge
else W_exp e
)
let huge_weight = W_huge
(** EXCEPTIONS *)
type raise_desc = string
(** GOTO *)
type stable_state = Expand.tbl CoTraceExp.t
type cond = Guard.t
type goto = cond * stable_state
(** TRANSITION TREE *)
type ttree =
Vanish
| Raise of raise_desc
| Goto of goto
| Split of (ttree * weightexp option * cond) list
let rec string_of_ttree topt = (
match topt with
None -> sprintf "%s" LutPredef.kw_deadlock
| Some t -> (
match t with
| Vanish -> "Vanish"
| Raise x -> sprintf "Raise %s" x
| Goto (c, ss) -> (
let cl = List.map CoAlgExp.lus_dumps (Guard.to_exp_list c) in
sprintf "Goto (%s, %s)" (String.concat " and " cl) (CoTraceExp.dumps ss)
)
| Split l -> (
let soc (t, wo, _c) = (
let ws = match wo with
| Some W_huge -> "<INF>"
| None -> "< 1 >"
| Some _ -> "< ? >"
in
sprintf "%s : %s" ws (string_of_ttree (Some t))
) in
String.concat ";\n " (List.map soc l)
)
)
)
type config = { data: Guard.store option; control: string }
let make_config state = { data = None; control = state }
(** STATE MANAGMENT *)
type state_info =
SS_stable of Expand.tbl CoTraceExp.t
| SS_transient
| SS_final of string
type trans = {
src: string;
wgt: weightexp option;
form: cond;
dest: string;
}
module TraceMap = struct
include Map.Make(struct type t = Expand.tbl CoTraceExp.t let compare = compare end)
end
module ConfigMap = struct
include Map.Make(struct type t = config let compare = compare end)
end
open Util
type t = {
source_code : Expand.t;
nb_stables : int;
nb_transients : int;
init_control : string;
final_control : string;
states : state_info StringMap.t ;
transitions : trans list;
nb_sinks : int;
_state2trace : Expand.tbl CoTraceExp.t StringMap.t ;
_trace2state : string TraceMap.t ;
_config2ttree : ttree ConfigMap.t;
todo : string list;
}
let source (it:t) = it.source_code
let states (it:t) = it.states
let init_control (it:t) = it.init_control
let transitions (it:t) = it.transitions
let add_pres (unalias:Guard.unalias) (data:Guard.store option) (pctx:CoTraceExp.escope) = (
match pctx with
| [] -> data
| _ -> (
let ctx = match data with
| Some d -> d
| None -> Guard.empty_store
in
let addp accin (id, eo) = (
match eo with
| None -> accin
| Some e -> (
let v = try (
Guard.value_of_algexp unalias ctx e
) with Guard.Not_constant _e -> raise (Internal_error ("AutoExplore.add_pres",
("initial value of \""^id^"\" must be a constant expression")))
in
Value.OfIdent.add accin (id, v)
)
) in
let new_pres = List.fold_left addp ctx.Guard.pres pctx in
Some {Guard.curs=ctx.Guard.curs; Guard.pres=new_pres}
)
)
let get_cpt i = CoIdent.of_cpt i
let dynamic_weight_exp (f : string) (c : int) (args : CoAlgExp.t list) = (
let pcpt = CoAlgExp.of_pre (get_cpt c) CkTypeEff.integer in
CoAlgExp.of_call (CoIdent.from_string f) CkTypeEff.integer (pcpt::args)
)
let incr_loop_cpt (c : int) = (
let nme = get_cpt c in
let precpt = CoAlgExp.of_pre nme CkTypeEff.integer in
let cpt = CoAlgExp.of_support nme CkTypeEff.integer true in
CoAlgExp.of_call (CoIdent.from_string LutPredef.kw_eq) CkTypeEff.boolean [
cpt ;
CoAlgExp.of_call (CoIdent.from_string LutPredef.kw_plus) CkTypeEff.integer [
precpt;
CoAlgExp.of_iconst "1"
]
]
)
let reset_loop_cpt (c : int) = (
let nme = get_cpt c in
let cpt = CoAlgExp.of_support nme CkTypeEff.integer true in
CoAlgExp.of_call (CoIdent.from_string LutPredef.kw_eq) CkTypeEff.boolean [
cpt ;
CoAlgExp.of_iconst "0"
]
)
(** dump des transition branchues pour debug *)
let dump_weight = ( function
| Some W_huge -> printf "huge"
| Some (W_exp w) -> CoAlgExp.dump w
| None -> printf "1"
)
let dump_ttree topt = (
let rec rdump t pfx = (
match t with
Vanish -> (
printf "%sVANISH\n" pfx;
) |
Raise e -> (
printf "%sRAISE %s\n" pfx e;
) |
Goto (c, n) -> (
printf "%sGOTO:[\n" pfx;
printf "%s cond:(" pfx;
Guard.dumpf stdout c;
printf " )\n%s next: <" pfx;
CoTraceExp.dump n;
printf ">\n%s]\n" pfx
) |
Split twl -> (
printf "%sSPLIT:\n" pfx;
let recpfx = sprintf "%s " pfx in
let dchoice (t,wo,cl) = (
printf "%s[\n" pfx;
(match wo with
Some _w -> (
printf "%sWEIGHT: " recpfx;
dump_weight wo ;
printf "\n"
) | None -> ()
);
printf "%sCOND: (" recpfx;
Guard.dumpf stdout cl;
printf ")\n";
rdump t recpfx ;
printf "%s]\n" pfx
) in
List.iter dchoice twl
)
) in
(
match topt with
None -> printf "%s" LutPredef.kw_deadlock
| Some t -> rdump t ""
);
printf "\n"
)
(** utilitaire : mise en séquence tenant compte du TE_eps *)
let put_in_seq te1 te2 = (
if(te1 = TE_eps) then te2
else if(te2 = TE_eps) then te1
else (TE_fby (te1,te2))
)
let put_in_para te1 te2 = (
match (te1,te2) with
| (TE_eps, x) -> x
| (x, TE_eps) -> x
| (x, TE_para yl) -> TE_para (x::yl)
| (x,y) -> TE_para [x; y]
)
(** type de la fonction de traitement des vanish *)
(** type de la fonction de traitement des raise *)
(** type de la fonction de traitement des goto *)
type callback = ttree option -> ttree option
(** CoTraceExp.t -> ttree *)
let _dump_cont _str tt _x = (
dump_ttree tt;
)
let gentrans
(xenv : Expand.t)
(data : Guard.store option)
(x : Expand.tbl CoTraceExp.t)
= (
let id2trace s = (StringMap.find s (Expand.trace_tab xenv)).ti_def_exp in
let unalias s = (StringMap.find s (Expand.alias_tab xenv)).ai_def_exp in
let rec rec_gentrans
(data : Guard.store option)
(x : Expand.tbl CoTraceExp.t)
(acc : cond)
(cont : callback)
= (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "++rec_gentrans \"%s\"\n" (CoTraceExp.dumps x));
match x with
(TE_erun (_, _, _, _)
| TE_dyn_erun (_, _, _, _, _)
| TE_dyn_erun_ldbg (_, _, _, _, _)
| TE_run (_, _, _, _, _, _)
| TE_dyn_run (_, _, _, _, _, _, _)
| TE_dyn_run_ldbg (_, _, _, _, _, _, _)) -> assert false
| TE_eps -> (
cont (Some Vanish)
)
| TE_ref s -> (
rec_gentrans data (id2trace s) acc cont
)
| TE_constraint (ae,si) -> (
let new_acc = Guard.add ~context:data ae acc si in
cont (Some (Goto (new_acc, TE_eps)))
)
| TE_fby (te1, te2) -> (
let fby_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- fby_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Goto (cl,n) -> cont (Some (Goto (cl, put_in_seq n te2)))
| Vanish -> rec_gentrans data te2 acc cont
| _ -> cont (Some c)
)
) in
rec_gentrans data te1 acc fby_cont
)
| TE_choice wtel -> (
let res = ref [] in
let tc (te, we) = (
match (rec_gentrans data te acc cont) with
None -> ()
| Some tt -> res := (tt, opt_weight_of_opt_algexp we,Guard.empty)::!res
) in
List.iter tc wtel;
match !res with
[] -> None
| ttlist -> Some (Split ttlist)
)
| TE_prio tel -> (
let rec doit = ( function
[] -> assert false
| [te] -> (
rec_gentrans data te acc cont
)
| te::tail -> (
let first = rec_gentrans data te acc cont in
let others = doit tail in
match (first, others) with
(None,None) -> None
| (Some f, None) -> Some f
| (None, Some o) -> Some o
| (Some f, Some o) ->
Some
(Split [(f, Some huge_weight, Guard.empty) ;
(o, None, Guard.empty)])
)
) in doit tel
)
| TE_loop te -> (
let loop_goon_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- loop_goon_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Goto (cl,n) -> cont (Some (Goto (cl, put_in_seq n x)))
| Vanish -> None
| _ -> cont (Some c)
)
) in
let goon = rec_gentrans data te acc loop_goon_cont in
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "==== loop goon branch of %s\n gives: %s\n"
(CoTraceExp.dumps x) (string_of_ttree goon));
let stop = cont (Some Vanish) in
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "==== loop stop branch of %s\n gives: %s\n"
(CoTraceExp.dumps x) (string_of_ttree stop));
match (goon,stop) with
(None,None) -> None
| (Some g, None) -> Some g
| (None, Some s) -> Some s
| (Some g, Some s) -> Some
(Split [(g, Some huge_weight, Guard.empty) ; (s, None, Guard.empty)])
)
| TE_loopi (cpt,min,max,te,si) -> (
let loopi_goon_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- loopi_goon_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Goto (cl,n) -> cont (Some (Goto (cl, put_in_seq n x)))
| Vanish -> None
| _ -> cont (Some c)
)
) in
let goon = rec_gentrans data te acc loopi_goon_cont in
let stop = cont (Some Vanish) in
match (goon,stop) with
(None,None) -> None
| (Some g, None) -> Some g
| (None, Some s) -> Some s
| (Some g, Some s) -> (
let gw = dynamic_weight_exp LutPredef.kw_interval_goon cpt [min; max] in
let ga = incr_loop_cpt cpt in
let sw = dynamic_weight_exp LutPredef.kw_interval_stop cpt [min; max] in
let sa = reset_loop_cpt cpt in
Some
(Split [(g, Some (W_exp gw), Guard.of_exp ga si) ;
(s, Some (W_exp sw), Guard.of_exp sa si)])
)
)
| TE_loopa (cpt,av, ecopt,te,si) -> (
let loopa_goon_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- loopi_goon_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Goto (cl,n) -> cont (Some (Goto (cl, put_in_seq n x)))
| Vanish -> None
| _ -> cont (Some c)
)
) in
let goon = rec_gentrans data te acc loopa_goon_cont in
let stop = cont (Some Vanish) in
match (goon,stop) with
(None,None) -> None
| (Some g, None) -> Some g
| (None, Some s) -> Some s
| (Some g, Some s) -> (
let ec = match ecopt with
Some x -> x
| None -> CoAlgExp.of_iconst "0"
in
let gw = dynamic_weight_exp LutPredef.kw_average_goon cpt [av; ec] in
let ga = incr_loop_cpt cpt in
let sw = dynamic_weight_exp LutPredef.kw_average_stop cpt [av; ec] in
let sa = reset_loop_cpt cpt in
Some
(Split [(g, Some (W_exp gw), Guard.of_exp ga si) ;
(s, Some (W_exp sw), Guard.of_exp sa si)])
)
)
| TE_assert (a, te, si) -> (
let assert_cont act = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- assert_cont (%s)\n in context %s\n"
(string_of_ttree act) (CoTraceExp.dumps x));
match act with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Goto (cl,n) -> cont (Some (Goto (cl, TE_assert (a,n,si))))
| _ -> cont (Some c)
)
) in
let rec_acc = Guard.add ~context:data a acc si in
rec_gentrans data te rec_acc assert_cont
)
| TE_exist (ectx, te) -> (
let new_data = add_pres unalias data ectx in
rec_gentrans new_data te acc cont
)
| TE_raise s -> (
cont (Some (Raise s))
)
| TE_catch (i,e,eco) -> (
let catch_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- catch_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Goto (cl,n) -> cont (Some (Goto (cl, TE_catch(i, n, eco))))
| Raise x -> (
if ( x == i) then (
match eco with
Some ec -> (
rec_gentrans data ec acc cont
) |
None -> cont (Some Vanish)
) else (
cont (Some (Raise x))
)
)
| _ -> cont (Some c)
)
) in
rec_gentrans data e acc catch_cont
)
| TE_try (TE_eps,_) -> cont (Some Vanish)
| TE_try (e,eco) -> (
let try_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- try_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Goto (cl,n) -> cont (Some (Goto (cl, TE_try(n,eco))))
| _ -> cont (Some c)
)
) in
let prio = rec_gentrans data e acc try_cont in
let defaut = ( match eco with
| Some ec -> (
rec_gentrans data ec acc cont
)
| None -> cont (Some Vanish)
) in
match (prio,defaut) with
(None,None) -> None
| (Some p, None) -> Some p
| (None, Some d) -> Some d
| (Some p, Some d) -> (
Some
(Split [(p, Some huge_weight, Guard.empty) ; (d, None, Guard.empty)])
)
)
| TE_para ([]) -> assert false
| TE_para ([e]) -> (
rec_gentrans data e acc cont
)
| TE_para (e::el) -> (
let para_head_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- para_head_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Vanish -> (
rec_gentrans data (TE_para(el)) acc cont
)
| Raise s -> (
cont (Some (Raise s))
)
| Goto (cl,n) -> (
let tail_acc = Guard.merge cl acc in
let para_tail_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf
"-- para_tail_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| Vanish -> (
cont (Some (Goto (cl,n)))
)
| Raise s -> (
LutErrors.warning None
("raising exception "^s^" in a parallel cannot be safely compiled into Lucky");
cont None
)
| Goto (tcl, tn) -> (
cont (Some (Goto (tcl, put_in_para n tn)))
)
)
) in
rec_gentrans data (TE_para(el)) tail_acc para_tail_cont
)
)
) in
rec_gentrans data e acc para_head_cont
)
| TE_strong_assert (_, _, _)
| TE_dyn_loop (_, _, _)
| TE_omega _
| TE_noeps _
| TE_dyn_choice (_, _)
-> assert false
) in
let top_cont a = (
Verbose.exe ~flag:dbg
(fun () -> Printf.printf "-- top_cont (%s)\n in context %s\n"
(string_of_ttree a) (CoTraceExp.dumps x));
match a with
| None -> None
| Some c -> (
match c with
| Split _ -> assert false
| _ -> Some c
)
) in
Verbose.exe ~flag:dbg (fun () -> Printf.printf "=================\ngentrans \"%s\"\n"
(CoTraceExp.dumps x));
let res = rec_gentrans data x Guard.empty top_cont in
res
)
(** dump des transitions *)
let dump_trans tr = (
printf "TRANSITION [\n";
printf " SRC: %s\n" tr.src;
printf " WEIGHT: ";
dump_weight tr.wgt ;
printf "\n";
printf " COND: " ;
Guard.dumpf stdout tr.form ;
printf "\n";
printf " DEST: %s\n" tr.dest;
printf "]\n"
)
let new_stable_state (it: t) (e : Expand.tbl CoTraceExp.t) = (
let ssi = it.nb_stables in
let res = sprintf "state%d" ssi in
let it = { it with
nb_stables = it.nb_stables + 1;
states = StringMap.add res (SS_stable e) it.states
}
in
res, it
)
let new_transient_state (it: t) (father: string) (index: int) = (
let res = sprintf "%s_%d" father index in
let it = { it with
nb_transients = it.nb_transients + 1;
states = StringMap.add res SS_transient it.states;
}
in
res, it
)
(** recherche/crée une association trace/state *)
let get_stable (it:t) e = (
try TraceMap.find e it._trace2state, it
with Not_found -> (
let res, it = new_stable_state it e in
Verbose.exe ~level:3
(fun () -> Printf.printf "##new state=\"%s\" exp=%s\n" res (CoTraceExp.dumps e));
let it = { it with
_trace2state = TraceMap.add e res it._trace2state;
_state2trace = StringMap.add res e it._state2trace;
todo = res :: it.todo
}
in
res, it
)
)
(** recherche/crée un état puits
N.B, on garde tel que l'ident qui est suppose être unique !
*)
let get_sink (it:t) x =
match StringMap.find_opt x it.states with
| None ->
Verbose.put ~level:3 "##new sink=\"%s\"\n" x ;
x, { it with
nb_sinks = it.nb_sinks + 1;
states = StringMap.add x (SS_final x) it.states;
}
| Some _ -> x, it
let init (xenv : Expand.t) =
let res = {
source_code = xenv;
nb_stables = 0;
nb_transients = 0;
init_control = "";
final_control = "";
states = StringMap.empty;
transitions = [];
nb_sinks = 0;
_state2trace = StringMap.empty;
_trace2state = TraceMap.empty;
_config2ttree = ConfigMap.empty;
todo = [];
}
in
let is = Expand.main_trace xenv in
let ie = (Util.StringMap.find is (Expand.trace_tab xenv)).ti_def_exp in
let init_control, res = get_stable res ie in
let final_control, res = get_sink res "vanish" in
{ res with
init_control = init_control;
final_control =final_control;
}
let rec ttree2trans (it:t) (src: string) (tt : ttree) = (
match tt with
| Vanish ->
[ { src = src; wgt = None ; form = Guard.empty; dest = it.final_control } ], it
| Raise x ->
let dest, it = get_sink it x in
[ { src = src; wgt = None ; form = Guard.empty; dest = dest } ], it
| Goto (cl, n) ->
let dest, it = get_stable it n in
[ { src = src; wgt = None ; form = cl; dest = dest ; } ], it
| Split twl -> (
let child_cpt = ref 0 in
let treat_choice (_trs,it) (t, wo, a) =
let dest, it = new_transient_state it src !child_cpt in
incr child_cpt;
let t0 = { src = src; wgt = wo ; form = a ; dest = dest; } in
let trs, it = ttree2trans it dest t in
trs @ (t0 :: trs), it
in
List.fold_left treat_choice ([],it) twl
)
)
let get_state_def (it:t) (ix: string) = StringMap.find ix it._state2trace
let get_state_info (it:t) (ix: string) = StringMap.find ix it.states
let config2ttree (it:t) (cfg: config) = (
let ix = cfg.control in
let e = StringMap.find ix it._state2trace in
let data = cfg.data in
try
let tt = ConfigMap.find cfg it._config2ttree in
Verbose.put ~level:2 "##config2ttree: \"%s\" cached\n" ix ;
if (Utils.paranoid ()) then (
let tt' = gentrans it.source_code data e in
assert (tt' = (Some tt))
);
tt, it
with Not_found -> (
Verbose.exe ~level:2
(fun () -> Printf.printf "##config2ttree: \"%s\" = %s\n"
ix (CoTraceExp.dumps e));
match ( gentrans it.source_code data e) with
Some tt ->
let it = { it with _config2ttree = ConfigMap.add cfg tt it._config2ttree } in
tt, it
| None -> raise (Failure "unexpected toplevel Deadlock")
)
)
type gtree = string * gtree_node
and gtree_node =
| GT_leaf of (cond * string)
| GT_choice of (weightexp option * gtree) list
| GT_stop of string
let rec gtree_size (_,gt) = (
match gt with
| GT_leaf _ -> 1
| GT_stop _ -> 1
| GT_choice ll -> (
List.fold_left (fun a (_, g) -> a + (gtree_size g)) 1 ll
)
)
let rec ttree2gtree (it:t) (src: string) (acc: cond) (tt : ttree) =
match tt with
| Vanish -> (src, GT_stop it.final_control), it
| Raise x ->
let sink, it = get_sink it x in
(src, GT_stop sink), it
| Goto (cl, n) ->
let st, it = get_stable it n in
(src, GT_leaf (Guard.merge acc cl, st)), it
| Split twl -> (
let child_cpt = ref 0 in
let treat_choice : (weightexp option * gtree) list * t ->
(ttree * weightexp option * cond) -> (weightexp option * gtree) list * t =
fun (choices, it) (t, wo, a) ->
let dest, it = new_transient_state it src !child_cpt in
incr child_cpt;
let cht, it = ttree2gtree it dest (Guard.merge acc a) t in
(wo, cht)::choices, it
in
let choices, it = List.fold_left treat_choice ([],it) twl in
(src, GT_choice (List.rev choices)), it
)
let config2gtree (it:t) (cfg: config) = (
let ix = cfg.control in
let tt, it = config2ttree it cfg in
ttree2gtree it ix Guard.empty tt
)
let config2trans (it:t) (cfg: config) = (
let ix = cfg.control in
let tt, it = config2ttree it cfg in
ttree2trans it ix tt
)
let make (xenv : Expand.t) = (
let it = init xenv in
let rec explore (tlist, it) =
match it.todo with
| [] -> (tlist, it)
| s::tail -> (
let it = { it with todo = tail } in
let curconf = { data = None; control = s} in
let trs, it = config2trans it curconf in
let tlist = trs @ tlist in
explore (tlist, it)
)
in
let tlist, it = explore ([],it) in
{ it with transitions = List.rev tlist }
)
let dump (auto : t) = (
printf "AUTOMATON (init: %s stables: %d transients: %d)\n"
auto.init_control auto.nb_stables auto.nb_transients
;
List.iter dump_trans auto.transitions
)