-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbt2spec.v
1352 lines (1253 loc) · 57.3 KB
/
bt2spec.v
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
Require Import bt micro_smv spec_extr.
Require Import Arith List ListSet String.
Open Scope string_scope.
(* Utility functions to convert between nat and string *)
Definition string_of_digit (n: nat) :=
(String (Ascii.ascii_of_nat (n + 48)) EmptyString).
Definition string_of_nat (n: nat) :=
(fix rec_string_of_nat (i n: nat) (acc: string): string :=
let d := string_of_digit (n mod 10) in
let acc' := d ++ acc in
match i with
| 0 => acc'
| S p => match n / 10 with
| 0 => acc'
| n' => rec_string_of_nat p n' acc'
end
end) n n "".
Fixpoint dlist_of_string (s: string): list (option nat) :=
match s with
| EmptyString => nil
| String a rest =>
let v := Ascii.nat_of_ascii a in
(if andb (48 <=? v) (v <=? 57) then (Some (v - 48)) else None)
:: (dlist_of_string rest)
end.
Definition nat_of_dlist (dl: list (option nat)): option nat :=
match dl with
| nil => None
| l' => (fix loop (l: list (option nat)) (i acc: nat): option nat :=
match l with
| nil => Some acc
| maybe_digit :: rest =>
match maybe_digit with
| None => None
| Some d => loop rest (i - 1) (acc + d * (10 ^ i))
end
end) l' ((List.length l') - 1) 0
end.
Definition nat_of_string (s: string) := nat_of_dlist (dlist_of_string s).
(* Some micro-SMV types *)
Definition bt_input_type :=
(TEnum ("Runn"::"Fail"::"Succ"::nil)).
Definition bt_output_type :=
(TEnum ("none"::"disabled"::"running"::"failed"::"succeeded"::nil)).
Definition bt_action_type :=
(TEnum ("no"::"enable"::"disable"::nil)).
Definition bt_seq_state :=
(TEnum ("initial"::"enabling_left"::"enabling_right"::"return_left"::
"return_right"::"disabling_left"::"disabling_right"::nil)).
(* Boilerplate modules *)
Definition bp_tick_generator :=
Build_smv_module
"bt_tick_generator"
("top_level_bt"::nil)
None
None
None
(Some (AddA (init (Mod "top_level_bt" (Id "enable"))
(BConst smvT))
(LastA (next (Mod "top_level_bt" (Id "enable"))
(Neg (Equal (Qual (Mod "top_level_bt"
(Id "output")))
(SConst "none"))))))).
Definition bp_skill_autonomous :=
(* This module is useful for autonomous generation of an SMV specification.
When used with OCRA, the following (simpler) module is sufficient: input
variables and transitions are supplied by the main module. *)
Build_smv_module
"bt_skill"
nil
(Some (AddV "output" (TSimp bt_output_type)
(LastV "enable" (TSimp TBool))))
(Some (LastI "input" bt_input_type))
None
(Some (AddA (init (Id "output") (SConst "none"))
(LastA
(next (Id "output")
(Case
(AddCexp (Neg (Qual (Id "enable")))
(SConst "none")
(AddCexp (Equal (Qual (Id "input"))
(SConst "Runn"))
(SConst "running")
(AddCexp (Equal (Qual (Id "input"))
(SConst "Fail"))
(SConst "failed")
(Cexp (Equal (Qual (Id "input"))
(SConst "Succ"))
(SConst "succeeded")))))))))).
Definition bp_skill :=
Build_smv_module
"bt_skill"
nil
(Some (AddV "output" (TSimp bt_output_type)
(LastV "enable" (TSimp TBool))))
None
None
None.
Definition bp_TRUE :=
Build_smv_module
"bt_TRUE"
nil
(Some (LastV "enable" (TSimp TBool)))
None
(Some (LastD "output" (SConst "succeeded")))
None.
Definition bp_not :=
Build_smv_module
"bt_not"
("child_bt"::nil)
(Some (LastV "enable" (TSimp TBool)))
None
(Some (LastD "output"
(Case
(AddCexp (Equal (Qual (Mod "child_bt"
(Id "output")))
(SConst "failed"))
(SConst "succeeded")
(AddCexp (Equal (Qual (Mod "child_bt"
(Id "output")))
(SConst "succeeded"))
(SConst "failed")
(Cexp (BConst smvT)
(Qual (Mod "child_bt"
(Id "output")))))))))
(Some (LastA (invar (Mod "child_bt" (Id "enable"))
(Qual (Id "enable"))))).
Definition bp_isRunning :=
Build_smv_module
"bt_isRunning"
("child_bt"::nil)
(Some (LastV "enable" (TSimp TBool)))
None
(Some (LastD "output"
(Case
(AddCexp (Equal (Qual (Mod "child_bt"
(Id "output")))
(SConst "running"))
(SConst "succeeded")
(AddCexp (Equal (Qual (Mod "child_bt"
(Id "output")))
(SConst "none"))
(SConst "none")
(Cexp (BConst smvT)
(SConst "failed")))))))
(Some (LastA (invar (Mod "child_bt" (Id "enable"))
(Qual (Id "enable"))))).
(* Binary-branching implementation *)
Module BT_bin_spec (X: BT_SIG).
Include BT_binary X.
(* Possible SMV modules to be generated *)
Inductive modtype :=
Skmod | TRUEmod | Seqmod | Fbmod | Par1mod | Par2mod | Notmod | Runmod.
Theorem modtype_dec: forall x y:modtype, {x = y} + {x <> y}.
decide equality.
Defined.
(* Definition of module names *)
Definition rootName (t: btree) :=
match t with
| Skill s => X.skillName s
| TRUE => "BTSucc"
| Node _ n _ _ => n
| Dec _ n _ => n
end.
Definition nodeName (k: nodeKind) :=
match k with
| Sequence => "SEQUENCE_NODE"
| Fallback => "FALLBACK_NODE"
| Parallel1 => "PARALLEL1_NODE"
| Parallel2 => "PARALLEL2_NODE"
end.
Definition decName (d: decKind) :=
match d with
| Not => "NOT_NODE"
| IsRunning => "ISRUNNING_NODE"
end.
(* Boilerplate modules specific for the binary case *)
Definition bp_bin_seq :=
Build_smv_module
(nodeName Sequence)
("visit"::"from_left_bt"::"from_right_bt"::nil)
(Some (AddV "to_left_bt" (TSimp bt_action_type)
(AddV "to_right_bt" (TSimp bt_action_type)
(AddV "output" (TSimp bt_output_type)
(AddV "cached_left" (TSimp bt_output_type)
(LastV "state" (TSimp bt_seq_state)))))))
None
None
(Some (AddA (init (Id "to_left_bt") (SConst "no"))
(AddA (init (Id "to_right_bt") (SConst "no"))
(AddA (init (Id "output") (SConst "none"))
(AddA (init (Id "cached_left") (SConst "none"))
(AddA (init (Id "state") (SConst "initial"))
(AddA (next (Id "to_left_bt")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "enable")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "disable")))
(SConst "disable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "none")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Neg (Equal (Qual (Id "from_left_bt"))
(SConst "disabled"))))
(SConst "disable")
(Cexp (BConst smvT) (SConst "no"))))))))
(AddA (next (Id "to_right_bt")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "succeeded")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Paren (Or (Equal (Qual (Id "from_left_bt"))
(SConst "running"))
(Equal (Qual (Id "from_left_bt"))
(SConst "failed")))))
(SConst "disable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Equal (Qual (Id "from_left_bt"))
(SConst "none")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "disabled"))))
(SConst "disable")
(Cexp (BConst smvT) (SConst "no"))))))))
(AddA (next (Id "output")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "none"))))
(SConst "from_right_bt")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Equal (Qual (Id "from_right_bt"))
(SConst "disabled")))
(SConst "cached_left")
(Cexp (BConst smvT) (SConst "none"))))))
(AddA (next (Id "cached_left")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Paren (Or (Equal (Qual (Id "from_left_bt"))
(SConst "running"))
(Equal (Qual (Id "from_left_bt"))
(SConst "failed")))))
(SConst "from_left_bt")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "disabled")))
(SConst "from_left_bt")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "disabled"))))
(SConst "cached_left")
(Cexp (BConst smvT) (SConst "none")))))))
(LastA (next (Id "state")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "enable")))
(SConst "enabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "disable")))
(SConst "disabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "succeeded")))
(SConst "enabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "none")))
(SConst "enabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Paren (Or (Equal (Qual (Id "from_left_bt"))
(SConst "running"))
(Equal (Qual (Id "from_left_bt"))
(SConst "failed")))))
(SConst "disabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Equal (Qual (Id "from_right_bt"))
(SConst "none")))
(SConst "enabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "none"))))
(SConst "return_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "disabled"))))
(SConst "disabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Equal (Qual (Id "from_right_bt"))
(SConst "disabled")))
(SConst "return_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Neg (Equal (Qual (Id "from_left_bt"))
(SConst "disabled"))))
(SConst "disabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "disabled")))
(SConst "disabling_right")
(Cexp (BConst smvT) (SConst "initial")))))))))))))))))))))))))).
Definition bp_bin_fb :=
Build_smv_module
(nodeName Fallback)
("visit"::"from_left_bt"::"from_right_bt"::nil)
(Some (AddV "to_left_bt" (TSimp bt_action_type)
(AddV "to_right_bt" (TSimp bt_action_type)
(AddV "output" (TSimp bt_output_type)
(AddV "cached_left" (TSimp bt_output_type)
(LastV "state" (TSimp bt_seq_state)))))))
None
None
(Some (AddA (init (Id "to_left_bt") (SConst "no"))
(AddA (init (Id "to_right_bt") (SConst "no"))
(AddA (init (Id "output") (SConst "none"))
(AddA (init (Id "cached_left") (SConst "none"))
(AddA (init (Id "state") (SConst "initial"))
(AddA (next (Id "to_left_bt")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "enable")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "disable")))
(SConst "disable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "none")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Neg (Equal (Qual (Id "from_left_bt"))
(SConst "disabled"))))
(SConst "disable")
(Cexp (BConst smvT) (SConst "no"))))))))
(AddA (next (Id "to_right_bt")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "failed")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Paren (Or (Equal (Qual (Id "from_left_bt"))
(SConst "running"))
(Equal (Qual (Id "from_left_bt"))
(SConst "succeeded")))))
(SConst "disable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Equal (Qual (Id "from_right_bt"))
(SConst "none")))
(SConst "enable")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "disabled"))))
(SConst "disable")
(Cexp (BConst smvT) (SConst "no"))))))))
(AddA (next (Id "output")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "none"))))
(SConst "from_right_bt")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Equal (Qual (Id "from_right_bt"))
(SConst "disabled")))
(SConst "cached_left")
(Cexp (BConst smvT) (SConst "none"))))))
(AddA (next (Id "cached_left")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Paren (Or (Equal (Qual (Id "from_left_bt"))
(SConst "running"))
(Equal (Qual (Id "from_left_bt"))
(SConst "succeeded")))))
(SConst "from_left_bt")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "disabled")))
(SConst "from_left_bt")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "disabled"))))
(SConst "cached_left")
(Cexp (BConst smvT) (SConst "none")))))))
(LastA (next (Id "state")
(Case
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "enable")))
(SConst "enabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "initial"))
(Equal (Qual (Id "visit"))
(SConst "disable")))
(SConst "disabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "failed")))
(SConst "enabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "none")))
(SConst "enabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_left"))
(Paren (Or (Equal (Qual (Id "from_left_bt"))
(SConst "running"))
(Equal (Qual (Id "from_left_bt"))
(SConst "succeeded")))))
(SConst "disabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Equal (Qual (Id "from_right_bt"))
(SConst "none")))
(SConst "enabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "enabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "none"))))
(SConst "return_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Neg (Equal (Qual (Id "from_right_bt"))
(SConst "disabled"))))
(SConst "disabling_right")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_right"))
(Equal (Qual (Id "from_right_bt"))
(SConst "disabled")))
(SConst "return_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Neg (Equal (Qual (Id "from_left_bt"))
(SConst "disabled"))))
(SConst "disabling_left")
(AddCexp (And (Equal (Qual (Id "state"))
(SConst "disabling_left"))
(Equal (Qual (Id "from_left_bt"))
(SConst "disabled")))
(SConst "disabling_right")
(Cexp (BConst smvT) (SConst "initial")))))))))))))))))))))))))).
Definition bp_par1 :=
Build_smv_module
(nodeName Parallel1)
("left_bt"::"right_bt"::nil)
(Some (LastV "enable" (TSimp TBool)))
None
(Some (AddD "true_output_count"
(Count (AddSexp
(Equal (Qual (Mod "left_bt" (Id "output")))
(SConst "succeeded"))
(Sexp
(Equal (Qual (Mod "right_bt" (Id "output")))
(SConst "succeeded")))))
(AddD "fail_output_count"
(Count (AddSexp
(Equal (Qual (Mod "left_bt" (Id "output")))
(SConst "failed"))
(Sexp
(Equal (Qual (Mod "right_bt" (Id "output")))
(SConst "failed")))))
(LastD "output"
(Case
(AddCexp
(Less (SConst "0")
(Qual (Id "true_output_count")))
(SConst "succeeded")
(AddCexp
(Less (SConst "1")
(Qual (Id "fail_output_count")))
(SConst "failed")
(Cexp
(BConst smvT)
(SConst "running")))))))))
(Some (AddA (invar (Mod "left_bt" (Id "enable"))
(Qual (Id "enable")))
(LastA (invar (Mod "right_bt" (Id "enable"))
(Qual (Id "enable")))))).
Definition bp_par2 :=
Build_smv_module
(nodeName Parallel2)
("left_bt"::"right_bt"::nil)
(Some (LastV "enable" (TSimp TBool)))
None
(Some (AddD "true_output_count"
(Count (AddSexp
(Equal (Qual (Mod "left_bt" (Id "output")))
(SConst "succeeded"))
(Sexp
(Equal (Qual (Mod "right_bt" (Id "output")))
(SConst "succeeded")))))
(AddD "fail_output_count"
(Count (AddSexp
(Equal (Qual (Mod "left_bt" (Id "output")))
(SConst "failed"))
(Sexp
(Equal (Qual (Mod "right_bt" (Id "output")))
(SConst "failed")))))
(LastD "output"
(Case
(AddCexp
(Less (SConst "1")
(Qual (Id "true_output_count")))
(SConst "succeeded")
(AddCexp
(Less (SConst "0")
(Qual (Id "fail_output_count")))
(SConst "failed")
(Cexp
(BConst smvT)
(SConst "running")))))))))
(Some (AddA (invar (Mod "left_bt" (Id "enable"))
(Qual (Id "enable")))
(LastA (invar (Mod "right_bt" (Id "enable"))
(Qual (Id "enable")))))).
(* This function scans a BT compiling a list of the needed modules *)
Fixpoint addmod (t: btree) (s: ListSet.set modtype) :=
match t with
| Skill _ => set_add modtype_dec Skmod s
| TRUE => set_add modtype_dec TRUEmod s
| Node k _ t1 t2 =>
let nodemod := match k with
| Sequence => Seqmod
| Fallback => Fbmod
| Parallel1 => Par1mod
| Parallel2 => Par2mod
end in
let left_modules := addmod t1 s in
let right_modules := addmod t2 s in
set_add modtype_dec nodemod
(set_union modtype_dec left_modules right_modules)
| Dec k _ t' =>
let decmod := match k with
| Not => Notmod
| IsRunning => Runmod
end in
set_add modtype_dec decmod (addmod t' s)
end.
(* This function returns a list of the names of all the inner nodes of a BT,
together with its kind. It is needed for the OSS file generation. *)
Fixpoint inlist (t: btree) (s: list (identifier * string)) :=
match t with
| Skill _ => s
| TRUE => s
| Node k n t1 t2 =>
let s' := inlist t1 s in
let s'' := inlist t2 s' in
(pair n (nodeName k)) :: s''
| Dec k n t' =>
let decmod := match k with
| Not => Notmod
| IsRunning => Runmod
end in
(pair n (decName k)) :: (inlist t' s)
end.
(* Functions to generate the SMV specification *)
Definition make_mod (t: modtype) (aut:bool): smv_module :=
match t with
| Skmod => if aut then bp_skill_autonomous else bp_skill
| TRUEmod => bp_TRUE
| Seqmod => bp_bin_seq
| Fbmod => bp_bin_fb
| Par1mod => bp_par1
| Par2mod => bp_par2
| Notmod => bp_not
| Runmod => bp_isRunning
end.
Fixpoint make_mod_list (l: list modtype) (aut: bool): list smv_module :=
match l with
| nil => nil
| m :: rest =>
(* D5.3 workflow: skills are implemented independently *)
match m with
| Skmod => (make_mod_list rest aut)
| _ => cons (make_mod m aut) (make_mod_list rest aut)
end
end.
Fixpoint make_vars (t: btree): varlist :=
match t with
| Skill s => LastV (X.skillName s) (TComp (TMod "bt_skill"))
| TRUE => LastV "t" (TComp (TMod "bt_TRUE"))
| Node k name t1 t2 =>
let x := varlist_app (make_vars t1) (make_vars t2) in
AddV name
(TComp (TModPar (nodeName k)
(AddP (Qual (Id (rootName t1)))
(LastP (Qual (Id (rootName t2)))))))
x
| Dec d name t =>
let x := make_vars t in
AddV name
(TComp (TModPar (decName d)
(LastP (Qual (Id (rootName t))))))
x
end.
Definition make_main (t: btree) (modname: string) :=
Build_smv_module
modname
nil
(Some (varlist_rev
(AddV "tick_generator"
(TComp (TModPar "bt_tick_generator"
(LastP (Qual (Id (rootName t))))))
(make_vars t))))
None
None
None.
Definition make_spec (t: btree): list smv_module :=
let needed := addmod t (empty_set modtype) in
let modlist := make_mod_list needed true in
app modlist (bp_tick_generator :: (make_main t "main") :: nil).
(* Modules for OCRA inteface *)
Fixpoint mkop (lst: list X.skillSet) :=
match lst with
| nil => nil
| s :: rest => cons ("from_" ++ (X.skillName s)) (mkop rest)
end.
Definition build_parameters (p: identifier) (t1 t2: btree) :=
match t1 with
| Skill s1 =>
match t2 with
| Skill s2 => (* skill, skill *)
(AddP (Qual (Id p))
(AddP (Qual (Id ("from_" ++ X.skillName s1)))
(LastP (Qual (Id ("from_" ++ X.skillName s2))))))
| TRUE => LastP (Qual (Id "dummy")) (* TBD *)
| Node k2 n2 _ _ => (* skill, node *)
(AddP (Qual (Id p))
(AddP (Qual (Id ("from_" ++ X.skillName s1)))
(LastP (Qual (Mod n2 (Id "output"))))))
| Dec k2 _ _ => LastP (Qual (Id "dummy")) (* TBD *)
end
| TRUE => LastP (Qual (Id "dummy")) (* TBD *)
| Node k1 n1 _ _ =>
match t2 with
| Skill s2 => (* node, skill *)
(AddP (Qual (Id p))
(AddP (Qual (Mod n1 (Id "output")))
(LastP (Qual (Id ("from_" ++ X.skillName s2))))))
| TRUE => LastP (Qual (Id "dummy")) (* TBD *)
| Node k2 n2 _ _ => (* node, node *)
(AddP (Qual (Id p))
(AddP (Qual (Mod n1 (Id "output")))
(LastP (Qual (Mod n2 (Id "output"))))))
| Dec k2 _ _ => LastP (Qual (Id "dummy")) (* TBD *)
end
| Dec k1 _ _ => LastP (Qual (Id "dummy")) (* TBD *)
end.
Fixpoint mkov (t: btree) (l: option varlist) (parent: identifier) :=
match t with
| Skill s => l
| TRUE => l
| Node k name t1 t2 =>
match k with
| Sequence =>
let parameters := build_parameters parent t1 t2 in
let newlist :=
let vl_left := mkov t1 l (name ++ ".to_left_bt") in
let vl_right := mkov t2 l (name ++ ".to_right_bt") in
match vl_left, vl_right with
| Some vl, Some vl' => Some (varlist_app vl vl')
| Some _, None => vl_left
| None, _ => vl_right
end in
match newlist with
| Some vl =>
(Some (AddV name
(TComp (TModPar (nodeName Sequence) parameters))
vl))
| None =>
(Some (LastV name
(TComp (TModPar (nodeName Sequence) parameters))))
end
| Fallback =>
let parameters := build_parameters parent t1 t2 in
let newlist :=
let vl_left := mkov t1 l (name ++ ".to_left_bt") in
let vl_right := mkov t2 l (name ++ ".to_right_bt") in
match vl_left, vl_right with
| Some vl, Some vl' => Some (varlist_app vl vl')
| Some _, None => vl_left
| None, _ => vl_right
end in
match newlist with
| Some vl =>
(Some (AddV name
(TComp (TModPar (nodeName Fallback) parameters))
vl))
| None =>
(Some (LastV name
(TComp (TModPar (nodeName Fallback) parameters))))
end
| Parallel1 => None (* TBD *)
| Parallel2 => None (* TBD *)
end
| Dec k _ t' => None (* TBD *)
end.
Inductive direction := From_left | From_right.
Fixpoint mkod (t: btree) (d: deflist) (parent: identifier) (dir: direction) :=
match t with
| Skill s =>
(AddD ("to_" ++ X.skillName s)
(Qual (Mod parent
(Id (match dir with
| From_left => "to_left_bt"
| From_right => "to_right_bt"
end))))
d)
| TRUE => d
| Node _ n t1 t2 =>
let dl_left := mkod t1 d n From_left in
mkod t2 dl_left n From_right
| Dec k _ t' => d
end.
(* (LastD "pippo" (Qual (Id "pluto"))).*)
Definition ocra_bt_fsm (t: btree) :=
Build_smv_module
"BT_FSM"
("visit" :: (mkop (sklist t)))
(mkov t None "visit")
None
(match t with
| Skill s => None (* TBD *)
| TRUE => None (* TBD *)
| Node _ n t1 t2 =>
let dl_left := mkod t1
(LastD "output" (Qual (Mod n (Id "output"))))
n
From_left in
let dl_tot := mkod t2
dl_left
n
From_right in
Some dl_tot
| Dec _ n t' => None (* TBD *)
end)
None.
Fixpoint mkparomain (l: list X.skillSet) :=
match l with
| nil => (* will never happen *)
LastP (Qual (Id ""))
| s :: nil =>
LastP (Qual (Id ("from_" ++ (X.skillName s))))
| s :: rest =>
AddP (Qual (Id ("from_" ++ (X.skillName s))))
(mkparomain rest)
end.
Fixpoint mkvaromain (lst: list X.skillSet) :=
match lst with
| nil => (* will never happen *)
LastV "" (TSimp bt_output_type)
| s :: nil =>
LastV ("from_" ++ (X.skillName s))
(TSimp bt_output_type)
| s :: rest =>
AddV ("from_" ++ (X.skillName s))
(TSimp bt_output_type)
(mkvaromain rest)
end.
Fixpoint mkdefomain (lst: list X.skillSet) :=
match lst with
| nil => (* will never happen *)
LastD "" (Qual (Id ""))
| s :: nil =>
LastD ("to_" ++ (X.skillName s))
(Qual (Mod "BT_FSM_inst"
(Id ("to_" ++ (X.skillName s)))))
| s :: rest =>
AddD ("to_" ++ (X.skillName s))
(Qual (Mod "BT_FSM_inst"
(Id ("to_" ++ (X.skillName s)))))
(mkdefomain rest)
end.
Definition ocra_main (t: btree) :=
Build_smv_module
"main"
nil
(Some (AddV "BT_FSM_inst" (TComp (TModPar "BT_FSM"
(AddP (Qual (Id "visit"))
(mkparomain (sklist t)))))
(AddV "visit" (TSimp bt_action_type)
(mkvaromain (sklist t)))))
None
(Some (AddD "output" (Qual (Mod "BT_FSM_inst" (Id "output")))
(mkdefomain (sklist t))))
None.
Definition make_spec_ocra (t: btree): list smv_module :=
let needed := addmod t (empty_set modtype) in
let modlist := make_mod_list needed false in
app modlist ((ocra_bt_fsm t) :: (ocra_main t) :: nil).
End BT_bin_spec.
(* Arbitrary-branching implementation *)
Module BT_gen_spec (X: BT_SIG).
Include BT_general X.
(* Possible SMV modules to be generated *)
Inductive modtype :=
| Skmod: modtype
| TRUEmod: modtype
| Seqmod: nat -> modtype
| Fbmod: nat -> modtype
| Parmod: nat -> nat -> modtype (* threshold, n. of args *)
| Notmod: modtype
| Runmod: modtype.
Theorem modtype_dec: forall x y:modtype, {x = y} + {x <> y}.
decide equality; apply PeanoNat.Nat.eq_dec.
Defined.
(* This is used for placeholders when translating non-normalized BTs *)
Definition bp_identity (name: string) :=
Build_smv_module
name
("bt"::nil)
(Some (LastV "enable" (TSimp TBool)))
None
(Some (LastD "output" (Qual (Mod "bt" (Id "output")))))
(Some (LastA (invar (Mod "bt" (Id "enable"))
(Qual (Id "enable"))))).
Definition rootName (t: btree) :=
match t with
| Skill s => X.skillName s
| TRUE => "BTSucc"
| Node _ n _ => n
| Dec _ n _ => n
end.
Definition nodeName (k: nodeKind) (n: nat) :=
match k with
| Sequence => append "bt_sequence_"
(string_of_nat n)
| Fallback => append "bt_fallback_"
(string_of_nat n)
| Parallel t => append (append "bt_parallel"
(string_of_nat t))
(append "_"
(string_of_nat n))
end.
Definition decName (d: decKind) :=
match d with
| Not => "bt_not"
| IsRunning => "bt_is_running"
end.
(* This function scans a BT compiling a list of the needed modules *)
Fixpoint addmod (t: btree) (s: ListSet.set modtype) :=
match t with
| Skill _ => set_add modtype_dec Skmod s
| TRUE => set_add modtype_dec TRUEmod s
| Node k _ f => let l := len f in
addmod_f f (set_add modtype_dec
match k with
| Sequence => Seqmod l
| Fallback => Fbmod l
| Parallel n => Parmod n l
end
s)
| Dec k _ t' => addmod t' (set_add modtype_dec
match k with
| Not => Notmod
| IsRunning => Runmod
end
s)
end
with addmod_f (f: btforest) (s: ListSet.set modtype) :=
match f with
| Child t => addmod t s
| Add t1 rest => addmod_f rest (addmod t1 s)
end.
(* Helper functions to generate the non-boilerplate SMV modules *)
Fixpoint mk_param_list (l: nat) :=
match l with
| O => nil
| S p => (cons ("bt" ++ string_of_nat l) (mk_param_list p))
end.
Fixpoint mk_seq_invar (l: nat) :=
match l with
| 0 => (* will never happen *)
(LastA (invar (Id "enable") (BConst smvF)))
| 1 =>
(LastA (invar (Mod "bt1" (Id "enable"))
(Equal (Qual (Mod "bt2" (Id "output")))
(SConst "succeeded"))))
| S p =>
(AddA (invar (Mod ("bt" ++ string_of_nat (S p)) (Id "enable"))
(Equal (Qual (Mod ("bt" ++ string_of_nat (S (S p))) (Id "output")))
(SConst "succeeded")))