-
Notifications
You must be signed in to change notification settings - Fork 7
/
BehaviouralFeature.java
11712 lines (9884 loc) · 404 KB
/
BehaviouralFeature.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.io.*;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import java.util.Set;
import java.util.HashSet;
/******************************
* Copyright (c) 2003--2024 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
* *****************************/
/* package: Class Diagram
*/
public class BehaviouralFeature extends ModelElement
{ private Vector parameters = new Vector(); // Attribute
private Vector typeParameters = new Vector(); // Type
private boolean query = true;
private Type resultType;
private Type elementType; // Of the result
private Expression pre; // For OCL operation specs
private Expression bpre; // For preconditions of B ops
private Expression post;
protected Entity entity; // the owner of the operation, null for global op
private UseCase useCase; // the use case that owns the operation, if any
private boolean instanceScope = true;
private boolean ordered = false; // true for ops on sequences
private boolean sorted = false; // true if op returns a sorted set
private Statemachine sm = null;
private Statement activity = null;
private String text = ""; // The text of the operation
private String comments = null;
private boolean derived = false;
private boolean bx = false;
private Vector readfr = null;
private Vector writefr = null;
private Expression defcond = null;
private Expression detcond = null;
private Vector myMutants = new Vector();
public BehaviouralFeature(String nme, Vector pars,
boolean readstatus,
Type res)
{ super(nme);
parameters = pars;
if (pars == null)
{ parameters = new Vector(); }
query = readstatus;
if (query)
{ addStereotype("query"); }
resultType = res;
if (res != null && Type.isCollectionType(res)) { }
else
{ elementType = res; } // primitive, String, entity type
}
public BehaviouralFeature(String nme, Vector pars,
Statement stat)
{ super(nme);
parameters = pars;
if (pars == null)
{ parameters = new Vector(); }
query = false;
activity = stat;
}
public BehaviouralFeature(String nme, Vector pars,
Vector stats)
{ super(nme);
parameters = pars;
if (pars == null)
{ parameters = new Vector(); }
query = false;
if (stats == null || stats.size() == 0)
{ activity = new InvocationStatement("skip");
return;
}
Vector actualstatements = new Vector();
for (int i = 0; i < stats.size(); i++)
{ if (stats.get(i) instanceof Statement)
{ actualstatements.add(stats.get(i)); }
}
if (actualstatements.size() == 1)
{ activity = (Statement) actualstatements.get(0); }
else
{ activity = new SequenceStatement(actualstatements); }
}
public BehaviouralFeature(String nme)
{ // creates update operation
super(nme);
query = false;
resultType = null;
elementType = null;
}
public void setType(Type rt)
{ resultType = rt; }
public void setReturnType(Type rt)
{
resultType = rt;
if (rt != null && Type.isCollectionType(rt)) { }
else
{ elementType = rt; } // primitive, String, entity type
}
public void setComments(String comms)
{ comments = comms; }
public Expression makeLambdaExpression(Expression inst)
{ // lambda pars : ParTypes in inst.name(pars)
BasicExpression be = new BasicExpression(this);
be.setObjectRef(inst);
return UnaryExpression.newLambdaUnaryExpression(be,this);
}
public void jsClassFromConstructor(Entity ent, Entity cclass, Vector inits, Vector entities)
{ // For each statement self.att := expr in activity
// make att an attribute of ent, with initialisation expr
// move operations from cclass to ent if they are used
// in activity.
Type etype = new Type(ent);
String ename = ent.getName();
BasicExpression res =
BasicExpression.newVariableBasicExpression(
"_res_", etype);
BehaviouralFeature initialiseEnt =
new BehaviouralFeature("initialise" + ename);
initialiseEnt.setActivity((Statement) activity.clone());
initialiseEnt.setOwner(ent);
initialiseEnt.setParameters(parameters);
ent.addOperation(initialiseEnt);
if (activity != null)
{ Vector asgns =
Statement.getAssignments(activity);
Vector calls =
Statement.getOperationCalls(activity);
System.out.println("^^^^^^^ Operation calls: " + calls);
System.out.println();
Vector pars = new Vector();
Vector exprs = new Vector();
for (int i = 0; i < asgns.size(); i++)
{ AssignStatement asgn =
(AssignStatement) asgns.get(i);
Expression lhs = asgn.getLeft();
Expression rhs = asgn.getRight();
// if parameters[ind] equals lhs.data,
// use inits[ind] for initialisation
// else use rhs.
if (lhs instanceof BasicExpression)
{ BasicExpression be = (BasicExpression) lhs;
if ("self".equals(be.objectRef + ""))
{ Expression init = rhs;
if (parameters != null &&
ModelElement.lookupByName(
be.data, parameters) != null)
{ int indx =
ModelElement.indexByName(be.data, parameters);
if (0 <= indx && indx < inits.size())
{ init = (Expression) inits.get(indx); }
Attribute att = new Attribute(be,rhs,init);
ent.addAttribute(att);
pars.add(att);
exprs.add(new BasicExpression(att));
Attribute par =
(Attribute) ModelElement.lookupByName(
be.data, parameters);
par.setType(att.getType());
}
else
{ Expression newinit = init;
if (init instanceof UnaryExpression)
{ String ff =
((UnaryExpression) init).functionOfLambda();
// System.out.println("++++ Function call: " + init + " " + ff);
if (ff != null)
{ BehaviouralFeature oldop =
cclass.getOperation(ff);
if (oldop != null)
{ oldop.setOwner(ent);
cclass.removeOperation(oldop);
ent.addOperation(oldop);
}
}
}
Attribute attf = new Attribute(be,rhs,newinit);
ent.addAttribute(attf);
} // a method in fact.
be.setObjectRef(res);
}
else if (rhs instanceof BasicExpression)
{ BasicExpression rhsbe = (BasicExpression) rhs;
if (rhsbe.isCallBasicExpression())
{ String fnme = rhsbe.getData();
if (fnme != null)
{ BehaviouralFeature oldop =
cclass.getOperation(fnme);
if (oldop != null)
{ oldop.setOwner(ent);
cclass.removeOperation(oldop);
ent.addOperation(oldop);
}
}
}
}
else if (rhs instanceof UnaryExpression)
{ String ff =
((UnaryExpression) rhs).functionOfLambda();
// System.out.println("++++ Function call: " + rhs + " " + ff);
if (ff != null)
{ BehaviouralFeature oldop =
cclass.getOperation(ff);
if (oldop != null)
{ oldop.setOwner(ent);
cclass.removeOperation(oldop);
ent.addOperation(oldop);
}
}
}
}
}
// and there may be calls self.SupEnt(pars)
// of the superclass.
/*
if (ent.hasOperation("new" + ename)) { }
else
{ BehaviouralFeature bf =
newStaticConstructor(ent,pars,exprs);
ent.addOperation(bf);
} */
SequenceStatement code = new SequenceStatement();
CreationStatement cs = new CreationStatement("_res_", etype);
code.addStatement(cs);
BasicExpression createCall =
new BasicExpression("create" + ename);
createCall.setUmlKind(Expression.UPDATEOP);
createCall.setParameters(new Vector());
createCall.setIsEvent();
createCall.setType(etype);
createCall.setStatic(true);
// createCall.entity = e;
AssignStatement cassgn =
new AssignStatement(res,createCall);
code.addStatement(cassgn);
Statement newactivity =
activity.substituteEq("self", res);
code.addStatement(newactivity);
code.addStatement(new ReturnStatement(res));
activity = code;
for (int h = 0; h < calls.size(); h++)
{ InvocationStatement opcall =
(InvocationStatement) calls.get(h);
if (opcall.callExp instanceof BasicExpression)
{ BasicExpression called =
(BasicExpression) opcall.callExp;
String calledname = called.getData();
if (calledname.startsWith("initialise"))
{ String superclassName =
calledname.substring(10);
System.out.println("^^^^^ Superclass: " + superclassName);
Entity superEnt =
(Entity) ModelElement.lookupByName(
superclassName, entities);
if (superEnt != null)
{ ent.setSuperclass(superEnt);
superEnt.addSubclass(ent);
}
}
}
}
}
}
public void addBeforeActivityEnd(Statement st)
{ if (activity != null)
{ Statement.addBeforeEnd(activity,st); }
}
public void addBeforeActivityEnd(Entity ent, Attribute att,
Expression val)
{ if (activity != null)
{ Type etype = new Type(ent);
BasicExpression res =
BasicExpression.newVariableBasicExpression(
"_res_", etype);
BasicExpression lhs = new BasicExpression(att);
if (att.isStatic())
{ lhs.setObjectRef(new BasicExpression(ent));
lhs.setStatic(true);
}
else
{ lhs.setObjectRef(res); }
Statement.addBeforeEnd(activity,
new AssignStatement(lhs,val));
}
}
public void setBx(boolean b)
{ bx = b; }
public boolean isNoRecursion()
{ return hasStereotype("noRecursion"); }
public void setTypeParameters(String generics, Vector entities, Vector types)
{ String gens = generics.trim();
if (gens.length() == 0)
{ return; }
StringTokenizer stok = new StringTokenizer(gens, "<>, ");
Vector pars = new Vector();
while (stok.hasMoreTokens())
{ String par = stok.nextToken();
Type tt = Type.getTypeFor(par,types,entities);
if (tt != null)
{ pars.add(tt); }
else
{ Entity entpar = new Entity(par);
tt = new Type(entpar);
pars.add(tt);
}
System.out.println(">> Added type parameter " + tt);
}
typeParameters = pars;
}
public void setTypeParameters(Vector pars)
{ typeParameters = pars; }
public boolean isGeneric()
{ return typeParameters != null &&
typeParameters.size() > 0;
}
public String getCompleteName()
{ String nme = getName();
if (typeParameters.size() > 0)
{ String tp = "";
for (int i = 0; i < typeParameters.size(); i++)
{ tp = tp +
((ModelElement) typeParameters.get(i)).getName();
if (i < typeParameters.size()-1)
{ tp = tp + ","; }
}
nme = nme + "<" + tp + ">";
}
return nme;
}
public int getArity()
{ if (parameters == null)
{ return 0; }
return parameters.size();
}
public Object clone()
{ Vector newpars = new Vector();
if (parameters == null) { }
else
{ for (int i = 0; i < parameters.size(); i++)
{ Attribute par = (Attribute) parameters.get(i);
Attribute newpar = (Attribute) par.clone();
newpars.add(newpar);
}
}
BehaviouralFeature res = new BehaviouralFeature(getName(),
newpars, query, resultType);
res.setElementType(elementType);
res.setStatechart(sm);
res.setActivity(activity);
res.setEntity(entity);
res.setInstanceScope(instanceScope);
res.setDerived(derived);
res.setOrdered(ordered);
res.setSorted(sorted);
res.setPre(pre); // I assume these
res.setPost(post); // should be copied, or cloned?
res.setBx(bx);
if (typeParameters != null)
{ res.typeParameters = new Vector();
res.typeParameters.addAll(typeParameters);
} // clone them.
return res;
} // and copy the use case, readfr, etc?
public BehaviouralFeature interfaceOperation()
{ BehaviouralFeature res = (BehaviouralFeature) clone();
res.activity = null;
res.setAbstract(true);
return res;
}
public BehaviouralFeature addContainerReference(
BasicExpression ref,
String var, Vector excl)
{ BehaviouralFeature res = new BehaviouralFeature(getName(), parameters, query, resultType);
res.setElementType(elementType);
res.setStatechart(sm);
res.setEntity(entity);
res.setInstanceScope(instanceScope);
res.setDerived(derived);
res.setOrdered(ordered);
res.setSorted(sorted);
res.setBx(bx);
Vector newexcl = new Vector();
newexcl.addAll(excl);
newexcl.add(getName());
if (parameters != null)
{ for (int i = 0; i < parameters.size(); i++)
{ Attribute att = (Attribute) parameters.get(i);
newexcl.add(att.getName());
}
}
if (typeParameters != null)
{ res.typeParameters = new Vector();
res.typeParameters.addAll(typeParameters);
for (int i = 0; i < typeParameters.size(); i++)
{ Type tp = (Type) typeParameters.get(i);
newexcl.add(tp.getName());
}
}
Expression newpre = null;
if (pre != null)
{ newpre = pre.addContainerReference(ref,var,newexcl); }
else
{ newpre = new BasicExpression(true); }
Expression newpost = null;
if (post != null)
{ newpost = post.addContainerReference(ref,var,newexcl); }
else
{ newpost = new BasicExpression(true); }
Statement newact = null;
if (activity != null) {
newact = activity.addContainerReference(ref,var,newexcl);
}
res.setActivity(newact);
res.setPre(newpre);
res.setPost(newpost);
return res;
} // and copy the use case, readfr, etc?
public static BehaviouralFeature newConstructor(String ename,
Entity ent, Vector pars)
{ BehaviouralFeature bf = new BehaviouralFeature("new" + ename);
bf.setParameters(pars);
Entity e = new Entity(ename);
Type etype = new Type(e);
bf.setType(etype);
bf.setPostcondition(new BasicExpression(true));
SequenceStatement code = new SequenceStatement();
BasicExpression res =
BasicExpression.newVariableBasicExpression(
"res", etype);
CreationStatement cs =
new CreationStatement("res", etype);
// cs.setInstanceType(etype);
code.addStatement(cs);
BasicExpression createCall =
new BasicExpression("create" + ename);
createCall.setUmlKind(Expression.UPDATEOP);
createCall.setParameters(new Vector());
createCall.setIsEvent();
createCall.setType(etype);
createCall.setStatic(true);
// createCall.entity = e;
AssignStatement assgn = new AssignStatement(res,createCall);
code.addStatement(assgn);
BasicExpression initialiseCall =
new BasicExpression("initialise");
initialiseCall.setUmlKind(Expression.UPDATEOP);
initialiseCall.setIsEvent();
Vector parNames = bf.getParameterExpressions();
// System.out.println(">>=== " + pars + " " + parNames);
initialiseCall.setParameters(parNames);
initialiseCall.setObjectRef(res);
InvocationStatement callInit =
new InvocationStatement(initialiseCall);
callInit.setParameters(parNames);
code.addStatement(callInit);
// Add each instance _initialiseInstance() operation
// res._initialiseInstance()
if (ent != null)
{ Vector allops = ent.getOperations();
for (int i = 0; i < allops.size(); i++)
{ BehaviouralFeature op = (BehaviouralFeature) allops.get(i);
String opname = op.getName();
if (opname.startsWith("_initialiseInstance"))
{ BasicExpression initialiseInstanceCall =
new BasicExpression(opname);
initialiseInstanceCall.setUmlKind(Expression.UPDATEOP);
initialiseInstanceCall.setIsEvent();
initialiseInstanceCall.setParameters(new Vector());
initialiseInstanceCall.setObjectRef(res);
InvocationStatement callInstInit =
new InvocationStatement(initialiseInstanceCall);
callInstInit.setParameters(new Vector());
code.addStatement(callInstInit);
}
}
}
ReturnStatement rs = new ReturnStatement(res);
code.addStatement(rs);
code.setBrackets(true);
bf.setActivity(code);
bf.setStatic(true);
return bf;
}
public static BehaviouralFeature newConstructor(String ename,
Entity ent, Vector pars,
Vector gpars)
{ BehaviouralFeature bf = new BehaviouralFeature("new" + ename);
bf.setParameters(pars);
Entity e = new Entity(ename);
if (gpars != null)
{ e.setTypeParameters(gpars); }
Type etype = new Type(e);
bf.setType(etype);
System.out.println(">> Generic parameters of " + bf + " are " + gpars);
System.out.println(">> Return type of " + bf + " is " + e.getCompleteName());
bf.setPostcondition(new BasicExpression(true));
SequenceStatement code = new SequenceStatement();
BasicExpression res =
BasicExpression.newVariableBasicExpression(
"res", etype);
CreationStatement cs =
new CreationStatement("res", etype);
cs.setInstanceType(etype);
code.addStatement(cs);
BasicExpression createCall =
new BasicExpression("create" + ename);
createCall.setUmlKind(Expression.UPDATEOP);
createCall.setParameters(new Vector());
createCall.setIsEvent();
createCall.setType(etype);
createCall.setStatic(true);
// createCall.entity = e;
AssignStatement assgn = new AssignStatement(res,createCall);
code.addStatement(assgn);
BasicExpression initialiseCall =
new BasicExpression("initialise");
initialiseCall.setUmlKind(Expression.UPDATEOP);
initialiseCall.setIsEvent();
Vector parNames = bf.getParameterExpressions();
// System.out.println(">>=== " + pars + " " + parNames);
initialiseCall.setParameters(parNames);
initialiseCall.setObjectRef(res);
InvocationStatement callInit =
new InvocationStatement(initialiseCall);
callInit.setParameters(parNames);
code.addStatement(callInit);
// Add each instance _initialiseInstance() operation
// res._initialiseInstance()
Vector allops = ent.getOperations();
for (int i = 0; i < allops.size(); i++)
{ BehaviouralFeature op = (BehaviouralFeature) allops.get(i);
String opname = op.getName();
if (opname.startsWith("_initialiseInstance"))
{ BasicExpression initialiseInstanceCall =
new BasicExpression(opname);
initialiseInstanceCall.setUmlKind(Expression.UPDATEOP);
initialiseInstanceCall.setIsEvent();
initialiseInstanceCall.setParameters(new Vector());
initialiseInstanceCall.setObjectRef(res);
InvocationStatement callInstInit =
new InvocationStatement(initialiseInstanceCall);
callInstInit.setParameters(new Vector());
code.addStatement(callInstInit);
}
}
ReturnStatement rs = new ReturnStatement(res);
code.addStatement(rs);
code.setBrackets(true);
bf.setActivity(code);
bf.setStatic(true);
return bf;
}
public static BehaviouralFeature newStaticConstructor(String ename, Vector pars)
{ BehaviouralFeature bf = new BehaviouralFeature("new" + ename);
bf.setParameters(pars);
Entity e = new Entity(ename);
Type etype = new Type(e);
bf.setType(etype);
bf.setPostcondition(new BasicExpression(true));
SequenceStatement code = new SequenceStatement();
BasicExpression res = BasicExpression.newVariableBasicExpression("result", etype);
CreationStatement cs = new CreationStatement("result", etype);
code.addStatement(cs);
BasicExpression createCall = new BasicExpression("create" + ename);
createCall.setUmlKind(Expression.UPDATEOP);
createCall.setParameters(new Vector());
createCall.setIsEvent();
createCall.setType(etype);
createCall.setStatic(true);
// createCall.entity = e;
AssignStatement assgn = new AssignStatement(res,createCall);
code.addStatement(assgn);
for (int i = 0; i < pars.size(); i++)
{ Attribute attr = (Attribute) pars.get(i);
BasicExpression lhs = new BasicExpression(attr);
lhs.setObjectRef(res);
BasicExpression rhs = new BasicExpression(attr);
AssignStatement assgnpar =
new AssignStatement(lhs,rhs);
code.addStatement(assgnpar);
}
ReturnStatement rs = new ReturnStatement(res);
code.addStatement(rs);
code.setBrackets(true);
bf.setActivity(code);
bf.setStatic(true);
return bf;
}
public static BehaviouralFeature newStaticConstructor(Entity ent)
{ String ename = ent.getName();
BehaviouralFeature bf = new BehaviouralFeature("new" + ename);
bf.setParameters(new Vector());
Type etype = new Type(ent);
bf.setType(etype);
bf.setPostcondition(new BasicExpression(true));
SequenceStatement code = new SequenceStatement();
BasicExpression res = BasicExpression.newVariableBasicExpression("result", etype);
CreationStatement cs = new CreationStatement("result", etype);
code.addStatement(cs);
BasicExpression createCall = new BasicExpression("create" + ename);
createCall.setUmlKind(Expression.UPDATEOP);
createCall.setParameters(new Vector());
createCall.setIsEvent();
createCall.setType(etype);
createCall.setStatic(true);
// createCall.entity = e;
AssignStatement assgn = new AssignStatement(res,createCall);
code.addStatement(assgn);
Vector pars = ent.getAttributes();
for (int i = 0; i < pars.size(); i++)
{ Attribute attr = (Attribute) pars.get(i);
BasicExpression lhs = new BasicExpression(attr);
lhs.setObjectRef(res);
Expression rhs = attr.getInitialisation();
if (rhs != null)
{ AssignStatement assgnpar =
new AssignStatement(lhs,rhs);
code.addStatement(assgnpar);
}
}
BasicExpression initialiseCall =
new BasicExpression("initialise");
initialiseCall.setUmlKind(Expression.UPDATEOP);
initialiseCall.setIsEvent();
// Vector parNames = bf.getParameterExpressions();
// System.out.println(">>=== " + pars + " " + parNames);
initialiseCall.setParameters(new Vector());
initialiseCall.setObjectRef(res);
InvocationStatement callInit =
new InvocationStatement(initialiseCall);
// callInit.setParameters(parNames);
code.addStatement(callInit);
ReturnStatement rs = new ReturnStatement(res);
code.addStatement(rs);
code.setBrackets(true);
bf.setActivity(code);
bf.setStatic(true);
return bf;
}
public static BehaviouralFeature newStaticConstructor(Entity ent, Vector pars, Vector exprs)
{ String ename = ent.getName();
BehaviouralFeature bf = new BehaviouralFeature("new" + ename);
bf.setParameters(pars);
Type etype = new Type(ent);
bf.setType(etype);
bf.setPostcondition(new BasicExpression(true));
SequenceStatement code = new SequenceStatement();
BasicExpression res = BasicExpression.newVariableBasicExpression("result", etype);
CreationStatement cs = new CreationStatement("result", etype);
code.addStatement(cs);
BasicExpression createCall = new BasicExpression("create" + ename);
createCall.setUmlKind(Expression.UPDATEOP);
createCall.setParameters(new Vector());
createCall.setIsEvent();
createCall.setType(etype);
createCall.setStatic(true);
// createCall.entity = e;
AssignStatement assgn = new AssignStatement(res,createCall);
code.addStatement(assgn);
// Vector pars = ent.getAttributes();
for (int i = 0; i < pars.size(); i++)
{ Attribute attr = (Attribute) pars.get(i);
BasicExpression lhs = new BasicExpression(attr);
lhs.setObjectRef(res);
Expression rhs = (Expression) exprs.get(i);
if (rhs != null)
{ AssignStatement assgnpar =
new AssignStatement(lhs,rhs);
code.addStatement(assgnpar);
}
}
// BasicExpression initialiseCall =
// new BasicExpression("initialise");
// initialiseCall.setUmlKind(Expression.UPDATEOP);
// initialiseCall.setIsEvent();
// Vector parNames = bf.getParameterExpressions();
// System.out.println(">>=== " + pars + " " + parNames);
// initialiseCall.setParameters(new Vector());
// initialiseCall.setObjectRef(res);
// InvocationStatement callInit =
// new InvocationStatement(initialiseCall);
// callInit.setParameters(parNames);
// code.addStatement(callInit);
ReturnStatement rs = new ReturnStatement(res);
code.addStatement(rs);
code.setBrackets(true);
bf.setActivity(code);
bf.setStatic(true);
return bf;
}
public static BehaviouralFeature fromAttribute(Attribute att)
{ BehaviouralFeature res =
new BehaviouralFeature(att.getName());
res.parameters = att.getParameters();
res.resultType = att.getInnerElementType();
System.out.println("==== Operation from attribute: " + res.display());
return res;
}
public UseCase toUseCase()
{ // usecase name : resultType
// { parameter ... assumptions ... postconditions }
UseCase res = new UseCase(getName());
res.addParameters(parameters);
res.setResultType(resultType);
res.setElementType(elementType);
if (pre != null)
{ res.addPrecondition(pre); }
if (post != null)
{ res.addPostcondition(post); }
res.setActivity(activity);
res.setTypeParameters(typeParameters);
return res;
} // type parameters become those of the linked class
public boolean isMutatable()
{ if (isDerived())
{ return false; }
if (parameters == null || parameters.size() == 0)
{ return false; }
if (resultType == null || "void".equals(resultType + ""))
{ return false; }
return isQuery();
} // At least one input parameter, and a result parameter
public Vector getMutants()
{ return myMutants; }
public Vector getMutants(Vector allops)
{ Vector res = new Vector();
for (int i = 0; i < myMutants.size(); i++)
{ BehaviouralFeature bf = (BehaviouralFeature) myMutants.get(i);
String nme = bf.getName();
if (ModelElement.lookupByName(nme,allops) != null)
{ res.add(bf); }
}
return res;
} // only mutants that actually are in the class
public Vector formMutantOperations(Vector posts)
{ if (myMutants.size() > 0)
{ return myMutants; } // cached result.
Vector res = new Vector();
String nme = getName();
for (int i = 0; i < posts.size(); i++)
{ Expression postx = (Expression) posts.get(i);
BehaviouralFeature bfclone = (BehaviouralFeature) this.clone();
bfclone.setName(nme + "_mutant_" + i);
bfclone.setPost(postx);
bfclone.setDerived(true);
res.add(bfclone);
}
if ("true".equals(post + "") && activity != null)
{ Vector statmuts = activity.singleMutants();
res = new Vector();
for (int i = 0; i < statmuts.size(); i++)
{ Statement statmut = (Statement) statmuts.get(i);
BehaviouralFeature bfclone = (BehaviouralFeature) this.clone();
bfclone.setName(nme + "_mutant_" + i);
bfclone.setActivity(statmut);
bfclone.setDerived(true);
res.add(bfclone);
}
}
JOptionPane.showMessageDialog(null, "Mutant versions of " + getName() + " are: " + res,
"", JOptionPane.INFORMATION_MESSAGE);
myMutants = new Vector();
myMutants.addAll(res);
return res;
}
public Vector formMutantCalls(String ename, Vector mutants, Vector tests, Vector alltests, Vector testcalls)
{ // for each m : mutants, t : tests, set up a call to
// m(t.values)
String nme = getName();
Vector res = new Vector();
if (resultType != null && !("void".equals(resultType + "")))
{ int tsts = alltests.size();
int muts = mutants.size();
for (int j = 0; j < tsts; j++)
{ Vector tst = (Vector) alltests.get(j);
// String tvals = (String) tst.get(1);
String code = parInitCode(tst);
// parameterInitialisationCode(tvals);
String javaType = resultType.getJava();
String call = javaType + " " + nme + "_result = _self." + this.toString() + ";\n";
call = call +
" System.out.println(\"Test " + j + " of " + nme + " on \" + _self + \" result = \" + " + nme + "_result);\n";
// Should use " try {\n"
String testcode =
" public static void " + nme + "_mutation_tests_" + j + "(" + ename + " _self, int[] _counts, int[] _totals)\n" +
" {\n" +
" " + code + "\n" +
" try {\n " + call + "\n";
for (int i = 0; i < muts; i++)
{ BehaviouralFeature m = (BehaviouralFeature) mutants.get(i);
String mname = m.getName();
String mutantcall = "_self." + m.toString();
testcode = testcode +
" try {\n" +
" " + javaType + " " + mname + "_result = " + mutantcall + ";\n" +
" if ((\"\" + " + nme + "_result).equals(\"\" + " + mname + "_result)) { _totals[" + j + "]++; } else\n" +
" { System.out.println(\"Test " + j + " of " + tsts + " detects " + nme + " mutant " + i + " of " + muts + "\");\n" +
" _counts[" + j + "]++; \n" +
" _totals[" + j + "]++;\n" +
" }\n" +
" } catch (Throwable _e) {\n" +
" _counts[" + j + "]++; \n" +
" _totals[" + j + "]++;\n" +
" }\n";
}
testcode = testcode + " } catch (Throwable _e) { }\n }\n\n";
/* Should be:
" } catch (Throwable _e) { _e.printStackTrace(); }\n"; */
res.add(testcode);
String testcall = " MutationTest." + nme + "_mutation_tests_" + j + "(_self,_counts,_totals);\n";
testcalls.add(testcall);
}
}
return res;
}
public Vector formMutantCallsCSharp(String ename, Vector mutants, Vector tests, Vector alltests, Vector testcalls)
{ // for each m : mutants, t : tests, set up a call to
// m(t.values)
String nme = getName();
Vector res = new Vector();
if (resultType != null && !("void".equals(resultType + "")))
{ int tsts = alltests.size();
int muts = mutants.size();
for (int j = 0; j < tsts; j++)
{ Vector tst = (Vector) alltests.get(j);
// String tvals = (String) tst.get(1);
String code = parInitCodeCSharp(tst);
// parameterInitialisationCode(tvals);
String javaType = resultType.getCSharp();