-
Notifications
You must be signed in to change notification settings - Fork 7
/
BOp.java
1150 lines (1026 loc) · 42.8 KB
/
BOp.java
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
import java.util.Vector;
import java.util.List;
/* package: B */
/******************************
* Copyright (c) 2003,2019 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
* *****************************/
public class BOp extends ModelElement
{ private String result = null;
private List params = new Vector(); // of String
private BExpression pre;
private BStatement code;
private String signature;
public BOp(String nme, String res, List pars,
BExpression p, BStatement s)
{ super(nme);
result = res;
params = pars;
pre = p;
code = s;
}
public void setSignature(String sig)
{ signature = sig; }
public String getSignature()
{ return signature; }
public String getResult()
{ return result; }
public List getParameters()
{ return params; }
public BExpression getPre()
{ return pre; }
public void addPre(BExpression p)
{ pre = BBinaryExpression.simplify("&",pre,p,false); }
public BStatement getCode()
{ return code; }
public void generateJava(java.io.PrintWriter out) // not used
{ out.println(" public void " + getName() + "()"); }
public static BOp buildGetOperation(
String nme, String cx,
BExpression cs)
// nmex <-- getnme(cx) =
// PRE cx: cs THEN nmex := nme(cx) END
{ Vector pars = new Vector();
String opname = "get" + nme;
String attx = nme + "x";
pars.add(cx);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression attxbe =
new BBasicExpression(attx);
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BExpression rhs = new BApplyExpression(nme,cxbe);
BStatement code = new BAssignStatement(attxbe,rhs);
return new BOp(opname,attx,pars,pre1,code);
}
public static BOp buildGetQualOperation(
String nme, String cx,
BExpression cs)
// nmex <-- getnme(cx,ind) =
// PRE cx: cs THEN nmex := nme(cx)(ind) END
{ Vector pars = new Vector();
String opname = "get" + nme;
String attx = nme + "x";
pars.add(cx);
pars.add("ind");
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression indbe = new BBasicExpression("ind");
BBasicExpression attxbe =
new BBasicExpression(attx);
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",indbe,new BBasicExpression("STRING"));
BBinaryExpression oppre =
new BBinaryExpression("&",pre1,pre2);
BExpression rhs = new BApplyExpression(nme,cxbe);
BStatement code = new BAssignStatement(attxbe,
new BApplyExpression(rhs,indbe));
return new BOp(opname,attx,pars,oppre,code);
}
public static BOp buildStaticGetOperation(String nme)
// nmex <-- getnme =
// nmex := nme;
{ Vector pars = new Vector();
String opname = "get" + nme;
String attx = nme + "x";
BBasicExpression attxbe =
new BBasicExpression(attx);
BExpression rhs = new BBasicExpression(nme);
BStatement code = new BAssignStatement(attxbe,rhs);
return new BOp(opname,attx,pars,null,code);
}
public static BOp buildUpdateOperation(String nme, BExpression cs,
Association ast, BExpression btbe, boolean inj)
// updatenme(nmex) =
// PRE nmex: cs +-> T THEN nme := nme <+ nmex END
{ Vector pars = new Vector();
String opname = "update" + nme;
String attx = nme + "x";
pars.add(attx);
BBasicExpression nmebe = new BBasicExpression(nme);
BBasicExpression attxbe =
new BBasicExpression(attx);
BExpression pfun = new BBinaryExpression("+->",cs,btbe);
BBinaryExpression pre =
new BBinaryExpression(":",attxbe,pfun);
BExpression rhs = new BBinaryExpression("<+",nmebe,attxbe);
BStatement code = new BAssignStatement(nmebe,rhs);
if (inj)
{ // rhs: cs >-> btbe
BExpression inje = new BBinaryExpression(">->",cs,btbe);
BExpression injpre = new BBinaryExpression(":",rhs,inje);
pre = new BBinaryExpression("&",pre,injpre);
}
/* if (ast != null)
{ String role1 = ast.getRole1();
if (role1 != null && role1.length() > 0)
{ int card1 = ast.getCard1();
int card2 = ast.getCard2();
Entity ent1 = ast.getEntity1();
Entity ent2 = ast.getEntity2();
BParallelStatement body = new BParallelStatement();
code.setWriteFrame(nme);
body.addStatement(code);
if (card2 != ONE)
{ // role1 := { bxx,axx | axx : dom(attxbe) & bxx : attxbe(axx) }, ...
BExpression role1be = new BBasicExpression(role1);
String e1name = ent1.getName();
String e2name = ent2.getName(); // should be different
String e1var = e1name.toLowerCase() + "_xx";
String e2var = e2name.toLowerCase() + "_xx";
String e1s = e1name.toLowerCase() + "s";
BExpression e1sbe = new BBasicExpression(e1s);
Vector scompvars = new Vector();
scompvars.add(e2var);
scompvars.add(e1var);
BExpression e2varbe = new BBasicExpression(e2var);
BExpression e1varbe = new BBasicExpression(e1var);
BExpression attxapp = new BApplyExpression(attx,e1varbe);
BExpression domattx = new BUnaryExpression("dom",attxbe);
BExpression pred11 = new BBinaryExpression(":",e1varbe,domattx);
BExpression pred12 = new BBinaryExpression(":",e2varbe,attxapp);
BExpression pred1 = new BBinaryExpression("&",pred11,pred12);
BExpression comp1 = new BSetComprehension(scompvars,pred1);
BExpression role2app = new BApplyExpression(nme, e1varbe);
BExpression role1sub = new BBinaryExpression(":",e1varbe,e1sbe);
BExpression pred21 = new BBinaryExpression("/:",e1varbe,domattx);
BExpression pred22 = new BBinaryExpression(":",e2varbe,role2app);
BExpression pred2 = new BBinaryExpression("&",pred22,pred21);
BExpression comp2 = new BSetComprehension(scompvars,
new BBinaryExpression("&",role1sub,pred2));
BExpression invrhs = new BBinaryExpression("\\/", comp1, comp2);
BStatement invcode = new BAssignStatement(role1be,invrhs);
invcode.setWriteFrame(role1);
body.addStatement(invcode);
return new BOp(opname,null,pars,pre,body);
}
}
} */
/* if (card2 == ModelElement.ZEROONE)
{ String cname = cs.substring(0,cs.length()-1);
BExpression zpre = zeroOneConstraint(nme,cname,"update",attx);
pre = new BBinaryExpression("&",pre,zpre);
} */
return new BOp(opname,null,pars,pre,code);
}
public static BOp buildSetOperation(
String nme, String cx,
BExpression cs,
BExpression btbe, boolean inj, String role1, int card1, int card2,
Entity ent, Vector entities, Vector types, Vector invs)
// setnme(cx,nmex) =
// PRE cx: cs & nmex: T THEN nme(cx) := attx END
{ Vector pars = new Vector();
String opname = "set" + nme;
String attx = nme + "xx";
String e2name = "";
Type t;
Attribute att = ent.getDefinedAttribute(nme);
if (att != null)
{ t = att.getType(); }
else
{ Association ast = ent.getRole(nme);
Entity ent2 = ast.getEntity2();
e2name = ent2 + "";
if (ast != null && card2 == ModelElement.ONE)
{ t = new Type(ent2); }
else
{ t = new Type("Set",null); }
} // and element type is Entity2 in 2nd case?
pars.add(cx);
pars.add(attx);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression attxbe =
new BBasicExpression(attx);
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",attxbe,btbe);
BExpression pre =
new BBinaryExpression("&",pre1,pre2); // and card(attxbe) = card2 for n
if (inj)
{ String cname = cx.substring(0,cx.length()-1);
BExpression inje = injConstraint(nme,cname,attx);
pre = new BBinaryExpression("&",pre,inje);
}
if (card2 == ModelElement.ZEROONE)
{ String cname = cx.substring(0,cx.length()-1);
BExpression zpre = zeroOneConstraint(nme,cname,"set",attx);
pre = new BBinaryExpression("&",pre,zpre);
}
BExpression lhs = new BApplyExpression(nme,cxbe);
BStatement code = new BAssignStatement(lhs,attxbe);
code.setWriteFrame(nme);
BParallelStatement body = new BParallelStatement();
body.addStatement(code);
if (role1 != null && role1.length() > 0)
{ BExpression role1be = new BBasicExpression(role1);
String e1name = ent.getName();
String e1var = e1name.toLowerCase() + "_xx";
String e2var = e2name.toLowerCase() + "_xx";
String e2s = e2name.toLowerCase() + "s";
BExpression e2sbe = new BBasicExpression(e2s);
Vector cxset = new Vector();
cxset.add(cxbe);
BExpression cxsetbe = new BSetExpression(cxset,false);
if (card2 == MANY)
{ if (card1 == ONE)
{ // role1 := (attxbe <<| role1) \\/ attxbe*{cxbe}
BExpression rhs1 = new BBinaryExpression("<<|",attxbe,role1be);
rhs1.setBrackets(true);
BExpression rhs2 = new BBinaryExpression("*",attxbe,cxsetbe);
BExpression invrhs = new BBinaryExpression("\\/",rhs1,rhs2);
BStatement invcode = new BAssignStatement(role1be,invrhs);
invcode.setWriteFrame(role1);
body.addStatement(invcode);
}
else
{ Vector scompvars = new Vector();
scompvars.add(e2var);
scompvars.add(e1var);
BExpression e2varbe = new BBasicExpression(e2var);
BExpression e1varbe = new BBasicExpression(e1var);
BExpression pred11 = new BBinaryExpression(":",e2varbe,attxbe);
BExpression role1app = new BApplyExpression(role1,e2varbe);
BExpression role1add = new BBinaryExpression("\\/",role1app,cxsetbe);
BExpression pred12 = new BBinaryExpression("=",e1varbe,role1add);
BExpression pred1 = new BBinaryExpression("&",pred11,pred12);
BExpression comp1 = new BSetComprehension(scompvars,pred1);
BExpression role1sub = new BBinaryExpression("-",role1app,cxsetbe);
BExpression pred21 = new BBinaryExpression("/:",e2varbe,attxbe);
BExpression pred22 = new BBinaryExpression(":",e2varbe,e2sbe);
BExpression pred2 = new BBinaryExpression("&",pred22,pred21);
BExpression pred23 = new BBinaryExpression("=",e1varbe,role1sub);
BExpression comp2 = new BSetComprehension(scompvars,
new BBinaryExpression("&",pred2,pred23));
BExpression invrhs = new BBinaryExpression("\\/", comp1, comp2);
BStatement invcode = new BAssignStatement(role1be,invrhs);
invcode.setWriteFrame(role1);
body.addStatement(invcode);
} // role1 := { bxx,axx | bxx : brx & axx = ar(bxx) \\/ { ax } } \\/ ...
}
else if (card2 == ONE && card1 != ONE)
{ // role1 := { bx |-> ar(bx) \\/ {ax} } \\/ { bb,aa | bb : bs & bb /= bx & aa : ar(bx) - { ax } }
Vector scompvars = new Vector();
scompvars.add(e2var);
scompvars.add(e1var);
BExpression e2varbe = new BBasicExpression(e2var);
BExpression e1varbe = new BBasicExpression(e1var);
BExpression pred11 = new BBinaryExpression("=",e2varbe,attxbe);
BExpression role1app = new BApplyExpression(role1,e2varbe);
BExpression role1add = new BBinaryExpression("\\/",role1app,cxsetbe);
BExpression pred12 = new BBinaryExpression("=",e1varbe,role1add);
BExpression pred1 = new BBinaryExpression("&",pred11,pred12);
BExpression comp1 = new BSetComprehension(scompvars,pred1);
BExpression role1sub = new BBinaryExpression("-",role1app,cxsetbe);
BExpression pred21 = new BBinaryExpression("/=",e2varbe,attxbe);
BExpression pred22 = new BBinaryExpression(":",e2varbe,e2sbe);
BExpression pred2 = new BBinaryExpression("&",pred22,pred21);
BExpression pred23 = new BBinaryExpression("=",e1varbe,role1sub);
BExpression comp2 = new BSetComprehension(scompvars,
new BBinaryExpression("&",pred2,pred23));
BExpression invrhs = new BBinaryExpression("\\/", comp1, comp2);
BStatement invcode = new BAssignStatement(role1be,invrhs);
invcode.setWriteFrame(role1);
body.addStatement(invcode);
}
}
Vector vpars = new Vector();
// and the attx parameter
Attribute p1 = new Attribute(attx,t,ModelElement.INTERNAL);
vpars.add(p1);
BehaviouralFeature event =
new BehaviouralFeature("set" + nme,vpars,false,null);
String ename = ent.getName();
java.util.Map env = new java.util.HashMap();
env.put(ename, new BBasicExpression(cx));
Vector contexts = new Vector();
if (ent != null)
{ contexts.add(ent); }
for (int i = 0; i < invs.size(); i++)
{ Constraint con = (Constraint) invs.get(i);
Constraint ccnew = con.bmatches("set",nme,ent,attx,event);
if (ccnew != null)
{ boolean typed = ccnew.typeCheck(types,entities,contexts);
BStatement stat;
if (typed)
{ stat = ccnew.synthesiseBCode(ent,nme,true);
body.addStatement(stat);
}
}
else if (con.getEvent() == null && con.involvesFeature(nme))
{ BExpression inv = con.binvariantForm(env,false);
inv.setBrackets(true);
BExpression inv1 = inv.substituteEq(nme + "(" + cx + ")", attxbe);
pre = new BBinaryExpression("&",pre,inv1);
} // must ensure that not both an invariant and its contrapositive are used!
}
BStatement newbody = BStatement.separateUpdates(body.getStatements());
return new BOp(opname,null,pars,pre,newbody);
}
public static BOp buildStaticSetOperation(
String nme, String cx, BExpression cs,
BExpression btbe,
boolean inj, int card2,
Entity ent, Vector entities,
Vector types, Vector invs)
// setnme(nmex) =
// PRE nmex: T THEN nme := nmex END
{ Vector pars = new Vector();
String opname = "set" + nme;
String attx = nme + "x";
pars.add(attx);
BBasicExpression attxbe =
new BBasicExpression(attx);
BExpression pre = new BBinaryExpression(":",attxbe,btbe);
BExpression lhs = new BBasicExpression(nme);
BStatement code = new BAssignStatement(lhs,attxbe);
BParallelStatement body = new BParallelStatement();
body.addStatement(code);
Type t;
Attribute att = ent.getAttribute(nme);
if (att != null)
{ t = att.getType(); }
else
{ return null; }
Vector vpars = new Vector();
// and the attx parameter
Attribute p1 = new Attribute(attx,t,ModelElement.INTERNAL);
vpars.add(p1);
BehaviouralFeature event = new BehaviouralFeature("set" + nme,vpars,false,null);
Vector contexts = new Vector();
if (ent != null)
{ contexts.add(ent); }
for (int i = 0; i < invs.size(); i++)
{ Constraint con = (Constraint) invs.get(i);
Constraint ccnew = con.bmatches("set",nme,ent,attx,event);
if (ccnew != null)
{ boolean typed = ccnew.typeCheck(types,entities,contexts);
BStatement stat;
if (typed)
{ stat = ccnew.staticSynthesiseBCode(ent,nme,true);
body.addStatement(stat);
}
}
}
BStatement newbody = BStatement.separateUpdates(body.getStatements());
return new BOp(opname,null,pars,pre,newbody);
}
// Not valid for {unique} attributes:
public static BOp buildSetAllOperation(String nme,
String cx, BExpression cs,
BExpression btbe)
// setAllnme(cx,nmex) =
// PRE cx <: cs & nmex: T
// THEN nme := nme <+ (cx*{nmex}) END
{ Vector pars = new Vector();
String opname = "setAll" + nme;
String nmex = nme + "x";
pars.add(cx);
pars.add(nmex);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression nmexbe =
new BBasicExpression(nmex);
BBasicExpression nmebe = new BBasicExpression(nme);
BBinaryExpression pre1 =
new BBinaryExpression("<:",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",nmexbe,btbe);
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
Vector nvals = new Vector();
nvals.add(nmexbe);
BExpression nmeset = new BSetExpression(nvals);
BExpression update =
new BBinaryExpression("*",cxbe,nmeset);
BExpression rhs =
new BBinaryExpression("<+",nmebe,update);
BStatement code =
new BAssignStatement(nmebe,rhs);
return new BOp(opname,null,pars,pre,code);
}
public static BOp buildSetIndOp(
String nme, String cx,
BExpression cs,
BExpression btbe, Entity ent)
// setnme(cx,ind,e2x) =
// PRE cx: cs & ind: dom(nme(cx)) & e2x: btbe
// THEN nme(cx)(ind) := e2x END
{ Vector pars = new Vector();
String opname = "set" + nme;
Type t;
Association ast = ent.getRole(nme);
t = new Type(ast.getEntity2());
String e2x = ast.getEntity2().getName().toLowerCase() + "xx";
pars.add(cx);
pars.add("ind");
pars.add(e2x);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression e2xbe =
new BBasicExpression(e2x);
BBasicExpression indbe = new BBasicExpression("ind");
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",e2xbe,btbe);
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
BExpression lhs = new BApplyExpression(nme,cxbe);
BExpression indom = new BBinaryExpression(":",indbe,
new BUnaryExpression("dom",lhs));
BExpression lhs1 = new BApplyExpression(lhs,indbe);
BStatement code = new BAssignStatement(lhs1,e2xbe);
code.setWriteFrame(nme);
pre = new BBinaryExpression("&",pre,indom);
return new BOp(opname,null,pars,pre,code);
}
public static BOp buildSetQualOp(
String nme, String cx,
BExpression cs,
BExpression btbe, Entity ent)
// setnme(cx,ind,e2x) =
// PRE cx: cs & ind: STRING & e2x: btbe
// THEN nme(cx)(ind) := e2x END
{ Vector pars = new Vector();
String opname = "set" + nme;
Type t;
Association ast = ent.getRole(nme);
t = new Type(ast.getEntity2());
String e2x = ast.getEntity2().getName().toLowerCase() + "xx";
pars.add(cx);
pars.add("ind");
pars.add(e2x);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression e2xbe =
new BBasicExpression(e2x);
BBasicExpression indbe = new BBasicExpression("ind");
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",e2xbe,btbe);
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
BExpression lhs = new BApplyExpression(nme,cxbe);
BExpression indom = new BBinaryExpression(":",indbe,
new BBasicExpression("STRING"));
BExpression lhs1 = new BApplyExpression(lhs,indbe);
BStatement code = new BAssignStatement(lhs1,e2xbe);
code.setWriteFrame(nme);
pre = new BBinaryExpression("&",pre,indom);
return new BOp(opname,null,pars,pre,code);
}
public static BOp buildDelOperation(
String nme, String cx,
BExpression cs,
BExpression btbe, String role1, int card1,
Entity ent, Vector entities, Vector types, Vector invs)
// removenme(cx,nmex) =
// PRE cx: cs & nmex: T
// THEN nme(cx) := nme(cx) - { nmex } END, or more complex if nme is ordered
{ Vector pars = new Vector();
String opname = "remove" + nme;
String nmex = nme + "xx";
pars.add(cx);
pars.add(nmex);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression nmexbe =
new BBasicExpression(nmex);
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",nmexbe,btbe);
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
BExpression lhs = new BApplyExpression(nme,cxbe);
Vector vals = new Vector();
vals.add(nmexbe);
BSetExpression nmese = new BSetExpression(vals);
BExpression rhs =
new BBinaryExpression("-",lhs,nmese);
rhs.setBrackets(true);
if (ent.getRole(nme).isOrdered())
{ BBasicExpression nmebe = new BBasicExpression(nme);
BBinaryExpression rresf = new BBinaryExpression("|>>",nmebe,nmese);
Vector subparams = new Vector();
subparams.add("_ii_");
subparams.add("_yy_");
BUnaryExpression domf = new BUnaryExpression("dom", rresf);
BBasicExpression jjbe = new BBasicExpression("_jj_");
BBinaryExpression rge = new BBinaryExpression("..", new BBasicExpression("1"), jjbe);
BBinaryExpression intrsec = new BBinaryExpression("/\\", domf, rge);
BUnaryExpression cardintrsec = new BUnaryExpression("card", intrsec);
BBasicExpression iibe = new BBasicExpression("_ii_");
BBinaryExpression iieq = new BBinaryExpression("=", iibe, cardintrsec);
BBinaryExpression jjindom = new BBinaryExpression(":", jjbe, domf);
BApplyExpression appf = new BApplyExpression(nmebe, jjbe);
BBasicExpression yybe = new BBasicExpression("_yy_");
BBinaryExpression yyeq = new BBinaryExpression("=", yybe, appf);
BBinaryExpression and1 = new BBinaryExpression("&", jjindom, yyeq);
BBinaryExpression and2 = new BBinaryExpression("&", and1, iieq);
BQuantifierExpression exists1 = new BQuantifierExpression("#", "_jj_", and2);
rhs = new BSetComprehension(subparams, exists1);
lhs = nmebe;
}
java.util.Map env = new java.util.HashMap();
env.put(ent.getName(), new BBasicExpression(cx));
BStatement code = new BAssignStatement(lhs,rhs);
code.setWriteFrame(nme);
BParallelStatement body = new BParallelStatement();
body.addStatement(code);
if (role1 != null && role1.length() > 0)
{ if (card1 != ONE)
{ BExpression invlhs = new BApplyExpression(role1,nmexbe);
Vector invvals = new Vector();
invvals.add(cxbe);
BSetExpression role1se = new BSetExpression(invvals,false);
BExpression invrhs = new BBinaryExpression("-",invlhs,role1se);
invrhs.setBrackets(true);
BStatement inversecode = new BAssignStatement(invlhs,invrhs);
inversecode.setWriteFrame(role1);
body.addStatement(inversecode);
}
else // role1 := role1 domain anti-restriction { nmexbe }
{ BExpression role1be = new BBasicExpression(role1);
Vector invvals = new Vector();
invvals.add(nmexbe);
BSetExpression role1se = new BSetExpression(invvals,false);
BExpression invrhs = new BBinaryExpression("<<|",role1se,role1be);
BStatement inversecode = new BAssignStatement(role1be,invrhs);
inversecode.setWriteFrame(role1);
body.addStatement(inversecode);
}
}
Vector vpars = new Vector();
// and the attx parameter
Association ast = ent.getDefinedRole(nme);
Type t = new Type(ast.getEntity2());
Attribute p1 = new Attribute(nmex,t,ModelElement.INTERNAL);
vpars.add(p1);
BehaviouralFeature event = new BehaviouralFeature("remove" +
nme,vpars,false,null);
Vector contexts = new Vector();
if (ent != null)
{ contexts.add(ent); }
for (int i = 0; i < invs.size(); i++)
{ Constraint con = (Constraint) invs.get(i);
Constraint ccnew = con.bmatches("remove",nme,ent,nmex,event);
if (ccnew != null)
{ boolean typed = ccnew.typeCheck(types,entities,contexts);
if (typed)
{ BStatement stat = ccnew.bupdateOperation(ent,true);
body.addStatement(stat);
}
}
else if (con.getEvent() == null && con.involvesFeature(nme))
{ BExpression inv = con.binvariantForm(env,false);
inv.setBrackets(true);
BExpression inv1 = inv.substituteEq(nme + "(" + cx + ")", rhs);
pre = new BBinaryExpression("&",pre,inv1);
} // must ensure that not both an invariant and its contrapositive are used!
}
BStatement newbody = BStatement.separateUpdates(body.getStatements());
return new BOp(opname,null,pars,pre,newbody);
}
public static BOp buildDelQualOperation(
String nme, String cx,
BExpression cs,
BExpression btbe,
Entity ent, Vector entities, Vector types, Vector invs)
// removenme(cx,ind,nmex) =
// PRE cx: cs & ind : STRING & nmex: T
// THEN nme(cx)(ind) := nme(cx)(ind) - { nmex } END
{ Vector pars = new Vector();
String opname = "remove" + nme;
String nmex = nme + "xx";
pars.add(cx);
pars.add("ind");
pars.add(nmex);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression nmexbe =
new BBasicExpression(nmex);
BBasicExpression indbe = new BBasicExpression("ind");
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",nmexbe,btbe);
BBinaryExpression pre3 =
new BBinaryExpression(":",indbe,new BBasicExpression("STRING"));
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
pre = new BBinaryExpression("&",pre,pre3);
BExpression lhs = new BApplyExpression(nme,cxbe);
lhs = new BApplyExpression(lhs,indbe);
Vector vals = new Vector();
vals.add(nmexbe);
BSetExpression nmese = new BSetExpression(vals);
BExpression rhs =
new BBinaryExpression("-",lhs,nmese);
rhs.setBrackets(true);
java.util.Map env = new java.util.HashMap();
env.put(ent.getName(), new BBasicExpression(cx));
BStatement code = new BAssignStatement(lhs,rhs);
code.setWriteFrame(nme);
BParallelStatement body = new BParallelStatement();
body.addStatement(code);
Vector vpars = new Vector();
// and the attx parameter
Association ast = ent.getDefinedRole(nme);
Type t = new Type(ast.getEntity2());
Attribute p1 = new Attribute(nmex,t,ModelElement.INTERNAL);
vpars.add(p1);
BehaviouralFeature event = new BehaviouralFeature("remove" +
nme,vpars,false,null);
Vector contexts = new Vector();
if (ent != null)
{ contexts.add(ent); }
for (int i = 0; i < invs.size(); i++)
{ Constraint con = (Constraint) invs.get(i);
Constraint ccnew = con.bmatches("remove",nme,ent,nmex,event);
if (ccnew != null)
{ boolean typed = ccnew.typeCheck(types,entities,contexts);
if (typed)
{ BStatement stat = ccnew.bupdateOperation(ent,true);
body.addStatement(stat);
}
}
else if (con.getEvent() == null && con.involvesFeature(nme))
{ BExpression inv = con.binvariantForm(env,false);
inv.setBrackets(true);
BExpression inv1 = inv.substituteEq(nme + "(" + cx + ")", rhs);
pre = new BBinaryExpression("&",pre,inv1);
} // must ensure that not both an invariant and its contrapositive are used!
}
BStatement newbody = BStatement.separateUpdates(body.getStatements());
return new BOp(opname,null,pars,pre,newbody);
}
public static BOp buildAddOperation(
String nme, String cx,
BExpression cs,
BExpression btbe,
String role1, int card1, int card2, boolean isseq,
Entity ent, Vector entities, Vector types, Vector invs)
// addnme(cx,nmex) =
// PRE cx: cs & nmex: T &
// card(nme(cx) \/ {nmex}) <= 1 (for ZEROONE)
// THEN nme(cx) := nme(cx) \/ { nmex } END or ^ [nmex] for a sequence
// inverse: role1(nmex) := role1(nmex) \/ { cx }
{ Vector pars = new Vector();
String opname = "add" + nme;
String nmex = nme + "xx";
pars.add(cx);
pars.add(nmex);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression nmexbe =
new BBasicExpression(nmex);
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",nmexbe,btbe);
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
BExpression lhs = new BApplyExpression(nme,cxbe);
Vector vals = new Vector();
vals.add(nmexbe);
BSetExpression nmese = new BSetExpression(vals,isseq);
BExpression rhs;
if (isseq)
{ rhs = new BBinaryExpression("^",lhs,nmese); }
else
{ rhs = new BBinaryExpression("\\/",lhs,nmese); }
rhs.setBrackets(true);
java.util.Map env = new java.util.HashMap();
env.put(ent.getName(), new BBasicExpression(cx));
if (card2 == ZEROONE)
{ BExpression cardleq =
new BBinaryExpression("<=",
new BUnaryExpression("card",rhs),
new BBasicExpression("1"));
pre = new BBinaryExpression("&",pre,cardleq);
}
BStatement code = new BAssignStatement(lhs,rhs);
code.setWriteFrame(nme);
BParallelStatement body = new BParallelStatement();
body.addStatement(code);
if (role1 != null && role1.length() > 0)
{ if (card1 != ONE)
{ BExpression invlhs = new BApplyExpression(role1,nmexbe);
Vector invvals = new Vector();
invvals.add(cxbe);
BSetExpression role1se = new BSetExpression(invvals,false);
BExpression invrhs = new BBinaryExpression("\\/",invlhs,role1se);
invrhs.setBrackets(true);
BStatement inversecode = new BAssignStatement(invlhs,invrhs);
inversecode.setWriteFrame(role1);
body.addStatement(inversecode);
}
else // role1(nmexbe) := cx
{ BExpression invlhs = new BApplyExpression(role1,nmexbe);
BStatement inversecode = new BAssignStatement(invlhs,cxbe);
inversecode.setWriteFrame(role1);
body.addStatement(inversecode);
}
}
Vector vpars = new Vector();
// and the attx parameter
Association ast = ent.getDefinedRole(nme);
Type t = new Type(ast.getEntity2());
Attribute p1 = new Attribute(nmex,t,ModelElement.INTERNAL);
vpars.add(p1);
BehaviouralFeature event = new BehaviouralFeature("add" + nme,vpars,false,null);
if (isseq)
{ event.setOrdered(true); }
Vector contexts = new Vector();
if (ent != null)
{ contexts.add(ent); }
for (int i = 0; i < invs.size(); i++)
{ Constraint con = (Constraint) invs.get(i);
Constraint ccnew = con.bmatches("add",nme,ent,nmex,event);
if (ccnew != null)
{ boolean typed = ccnew.typeCheck(types,entities,contexts);
if (typed)
{ BStatement stat = ccnew.bupdateOperation(ent,true);
body.addStatement(stat);
}
}
else if (con.getEvent() == null && con.involvesFeature(nme))
{ BExpression inv = con.binvariantForm(env,false);
inv.setBrackets(true);
BExpression inv1 = inv.substituteEq(nme + "(" + cx + ")", rhs);
pre = new BBinaryExpression("&",pre,inv1);
} // must ensure that not both an invariant and its contrapositive are used!
}
BStatement newbody = BStatement.separateUpdates(body.getStatements());
return new BOp(opname,null,pars,pre,newbody);
}
public static BOp buildAddQualOperation(
String nme, String cx,
BExpression cs,
BExpression btbe,
int card2, boolean isseq,
Entity ent, Vector entities, Vector types, Vector invs)
// addnme(cx,ind,nmex) =
// PRE cx: cs & ind : STRING & nmex: T &
// card(nme(cx)(ind) \/ {nmex}) <= 1 (for ZEROONE)
// THEN nme(cx)(ind) := nme(cx)(ind) \/ { nmex } END or ^ [nmex] for a sequence
{ Vector pars = new Vector();
String opname = "add" + nme;
String nmex = nme + "xx";
pars.add(cx);
pars.add("ind");
pars.add(nmex);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression nmexbe =
new BBasicExpression(nmex);
BBasicExpression indbe = new BBasicExpression("ind");
BBinaryExpression pre1 =
new BBinaryExpression(":",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",nmexbe,btbe);
BBinaryExpression pre3 =
new BBinaryExpression(":",indbe,new BBasicExpression("STRING"));
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
pre = new BBinaryExpression("&",pre,pre3);
BExpression lhs = new BApplyExpression(nme,cxbe);
lhs = new BApplyExpression(lhs,indbe);
Vector vals = new Vector();
vals.add(nmexbe);
BSetExpression nmese = new BSetExpression(vals,isseq);
BExpression rhs;
if (isseq)
{ rhs = new BBinaryExpression("^",lhs,nmese); }
else
{ rhs = new BBinaryExpression("\\/",lhs,nmese); }
rhs.setBrackets(true);
java.util.Map env = new java.util.HashMap();
env.put(ent.getName(), new BBasicExpression(cx));
if (card2 == ZEROONE)
{ BExpression cardleq =
new BBinaryExpression("<=",
new BUnaryExpression("card",rhs),
new BBasicExpression("1"));
pre = new BBinaryExpression("&",pre,cardleq);
}
BStatement code = new BAssignStatement(lhs,rhs);
code.setWriteFrame(nme);
BParallelStatement body = new BParallelStatement();
body.addStatement(code);
Vector vpars = new Vector();
// and the attx parameter
Association ast = ent.getDefinedRole(nme);
Type t = new Type(ast.getEntity2());
Attribute p1 = new Attribute(nmex,t,ModelElement.INTERNAL);
vpars.add(p1);
BehaviouralFeature event = new BehaviouralFeature("add" + nme,vpars,false,null);
if (isseq)
{ event.setOrdered(true); }
Vector contexts = new Vector();
if (ent != null)
{ contexts.add(ent); }
for (int i = 0; i < invs.size(); i++)
{ Constraint con = (Constraint) invs.get(i);
Constraint ccnew = con.bmatches("add",nme,ent,nmex,event);
if (ccnew != null)
{ boolean typed = ccnew.typeCheck(types,entities,contexts);
if (typed)
{ BStatement stat = ccnew.bupdateOperation(ent,true);
body.addStatement(stat);
}
}
else if (con.getEvent() == null && con.involvesFeature(nme))
{ BExpression inv = con.binvariantForm(env,false);
inv.setBrackets(true);
BExpression inv1 = inv.substituteEq(nme + "(" + cx + ")", rhs);
pre = new BBinaryExpression("&",pre,inv1);
} // must ensure that not both an invariant and its contrapositive are used!
}
BStatement newbody = BStatement.separateUpdates(body.getStatements());
return new BOp(opname,null,pars,pre,newbody);
}
public static BOp buildAddAllOperation(String nme,
String cx, BExpression cs,
BExpression btbe)
// addAllnme(cx,nmex) =
// PRE cx <: cs & nmex: T
// THEN
// nme := nme <+ { x:cx | x |-> nme(x) \/ {nmex}}
// END
{ Vector pars = new Vector();
String opname = "addAll" + nme;
String nmex = nme + "x";
pars.add(cx);
pars.add(nmex);
BBasicExpression cxbe = new BBasicExpression(cx);
BBasicExpression nmexbe =
new BBasicExpression(nmex);
BBasicExpression nmebe = new BBasicExpression(nme);
BBinaryExpression pre1 =
new BBinaryExpression("<:",cxbe,cs);
BBinaryExpression pre2 =
new BBinaryExpression(":",nmexbe,btbe);
BExpression pre =
new BBinaryExpression("&",pre1,pre2);
Vector nvals = new Vector();
nvals.add(nmexbe);
BExpression nmeset = new BSetExpression(nvals);
String xxvar = "xx";
BExpression xxbe = new BBasicExpression(xxvar);
BExpression xapp =
new BApplyExpression(nme,xxbe);
BExpression pred =
new BBinaryExpression("|->",xxbe,
new BBinaryExpression("\\/",xapp,nmeset));
BExpression update =
new BSetComprehension(xxvar,cxbe,pred);
BExpression rhs =
new BBinaryExpression("<+",nmebe,update);
BStatement code =
new BAssignStatement(nmebe,rhs);
return new BOp(opname,null,pars,pre,code);
}