-
Notifications
You must be signed in to change notification settings - Fork 9
/
AttributeMatching.java
4687 lines (4131 loc) · 183 KB
/
AttributeMatching.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.io.*;
import java.util.Collections;
import javax.swing.JOptionPane;
import java.util.Vector;
import java.util.List;
/******************************
* 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: TransformationSynthesis
*/
public class AttributeMatching
{ Attribute src;
Attribute trg;
String srcname = "";
String trgname = "";
Expression srcvalue = null; // in cases where a value is mapped to the target
Expression trgvalue = null;
EntityMatching dependsOn; // in cases where src : E or Collection(E) and E maps to F
Attribute elementVariable = null; // for expr -> trg
Vector auxVariables = new Vector(); // for expr -> trg
boolean unionSemantics = false; // src' should be added to trg, not assigned
public AttributeMatching(Attribute source, Attribute target)
{ src = source;
trg = target;
srcname = src.getName();
trgname = trg.getName();
}
public AttributeMatching(Expression source, Attribute target)
{ src = new Attribute("" + source, source.getType(),
ModelElement.INTERNAL);
src.setElementType(source.getElementType());
srcvalue = source;
trg = target;
trgvalue = new BasicExpression(target);
srcname = srcvalue + "";
trgname = trg.getName();
}
public AttributeMatching(Expression source, Attribute target, Attribute var,
Vector auxvars)
{ src = new Attribute("" + source, source.getType(), ModelElement.INTERNAL);
src.setType(source.getType());
src.setElementType(source.getElementType());
srcvalue = source;
trg = target;
trgvalue = new BasicExpression(target);
elementVariable = var;
srcname = srcvalue + "";
trgname = trg.getName();
auxVariables = auxvars;
}
public AttributeMatching(Expression source, Expression target)
{ src = new Attribute("" + source, source.getType(),
ModelElement.INTERNAL);
src.setElementType(source.getElementType());
srcvalue = source;
trgvalue = target;
trg = new Attribute("" + target, target.getType(),
ModelElement.INTERNAL);
trg.setElementType(target.getElementType());
srcname = srcvalue + "";
trgname = trg.getName();
}
public AttributeMatching(Expression source, Expression target, Attribute var,
Vector auxvars)
{ src = new Attribute("" + source, source.getType(), ModelElement.INTERNAL);
src.setType(source.getType());
src.setElementType(source.getElementType());
srcvalue = source;
trgvalue = target;
trg = new Attribute("" + target, target.getType(),
ModelElement.INTERNAL);
elementVariable = var;
srcname = srcvalue + "";
trgname = trg.getName();
auxVariables = auxvars;
}
public boolean isVacuous()
{ // Only has _1 |-->_1 or _0 |-->_0
if (srcvalue != null && trgvalue != null &&
srcvalue instanceof BasicExpression &&
trgvalue instanceof BasicExpression)
{ String rulelhs = ((BasicExpression) srcvalue).toCSTL();
BasicExpression rbe = (BasicExpression) trgvalue;
String rulerhs = rbe.toCSTL();
if (rulelhs.trim().equals("_1") &&
rulerhs.trim().equals("_1"))
{ return true; }
}
if (srcname.trim().equals("_1") &&
trgname.trim().equals("_1"))
{ return true; }
if (srcname.trim().equals("_0") &&
trgname.trim().equals("_0"))
{ return true; }
if ((srcvalue + "").equals("_1") &&
(trgvalue + "").equals("_1"))
{ return true; }
if ((srcvalue + "").equals("_0") &&
(trgvalue + "").equals("_0"))
{ return true; }
return false;
}
public boolean isBasic()
{ // Only variable is _1 on rhs; with no `mf
if (trgvalue instanceof BasicExpression)
{ BasicExpression be = (BasicExpression) trgvalue;
Vector vars = trgvalue.metavariables();
Vector mfs = CGRule.metafeatures(be.toLiteralCSTL() + "");
if (vars.size() == 1 && vars.contains("_1") &&
mfs.size() == 0)
{ return true; }
return false;
}
if (trgvalue != null)
{ Vector tvars = CGRule.metavariables(trgvalue + "");
Vector mfs = CGRule.metafeatures(trgvalue + "");
if (tvars.size() == 1 && tvars.contains("_1") &&
mfs.size() == 0)
{ return true; }
}
return false;
}
public boolean isIterative()
{ // LHS has _*
String lhs = srcvalue + "";
if (lhs.indexOf("_*") >= 0)
{ return true; }
return false;
}
public void setElementVariable(Attribute var)
{ elementVariable = var; }
public void addAuxVariable(Attribute var)
{ if (auxVariables.contains(var)) { }
else
{ auxVariables.add(var); }
}
public void setExpressionMatch(Expression source, Attribute target, Attribute var)
{ src = new Attribute("" + source, source.getType(), ModelElement.INTERNAL);
src.setType(source.getType());
src.setElementType(source.getElementType());
srcvalue = source;
trg = target;
trgvalue = new BasicExpression(target);
elementVariable = var;
srcname = srcvalue + "";
trgname = trg.getName();
}
public Attribute getSource()
{ return src; }
public Expression getSourceExpression()
{ if (srcvalue != null)
{ return srcvalue; }
return new BasicExpression(src);
}
public Attribute getTarget()
{ return trg; }
public String toString()
{ return " " + srcname + " |--> " + trgname; }
public Vector usesCSTLfunctions()
{ Vector res = new Vector();
if (trgvalue != null &&
trgvalue instanceof BasicExpression)
{ res =
((BasicExpression) trgvalue).usesCSTLfunctions();
}
return res;
}
public Vector allTagsArities(String category)
{ Vector res = new Vector();
if (srcvalue != null)
{ String rulelhs = "" + srcvalue;
Vector vars = CGRule.metavariables(rulelhs);
if (vars != null)
{ Vector pair = new Vector();
pair.add(category);
pair.add(vars.size());
res.add(pair);
}
if (srcvalue instanceof BasicExpression)
{ BasicExpression be = (BasicExpression) srcvalue;
Vector pars = be.getParameters();
if (pars != null)
{ Vector pair = new Vector();
pair.add(category);
pair.add(pars.size());
res.add(pair);
}
}
}
return res;
}
public String toCSTL(String category, Expression cond,
CGSpec cg, Vector typematches)
{ if (srcvalue != null && trgvalue != null &&
srcvalue instanceof BasicExpression &&
trgvalue instanceof BasicExpression)
{ String rulelhs = ((BasicExpression) srcvalue).toCSTL();
BasicExpression rbe = (BasicExpression) trgvalue;
// System.out.println(">>> Mapping: " + this + " " + rbe.isFunctionApplication());
if (rbe.isFunctionApplication() &&
"_1".equals(rulelhs))
{ String func = rbe.getAppliedFunction();
TypeMatching tm =
TypeMatching.lookupByName(func,typematches);
if (tm != null)
{ String res = tm.toCSTL(category,cg);
System.out.println(">>> Mapping: " + this + " is " + res);
return res;
}
} // It is simply _1 |-->_1`func
String rulerhs =
((BasicExpression) trgvalue).toLiteralCSTL();
CGRule rle = new CGRule(rulelhs, rulerhs);
Vector vars = rle.getVariables();
if (cond != null && vars.size() > 0)
{ // CGCondition cc =
// CGCondition.newCGCondition(category,cond,vars);
// if (cc != null)
// { rle.addCondition(cc); }
rle.addNewCGConditions(category,cond,vars);
}
cg.addCategoryRuleInOrder(category,rle);
String res = rulelhs + " |--> " + rulerhs;
return res;
}
return "";
}
boolean isDirect() // either source or target is a direct attribute
{ if (src.getNavigation().size() <= 1)
{ return true; }
if (trg.getNavigation().size() <= 1)
{ return true; }
return false;
}
boolean isDirectSource() // source is a direct attribute
{ if (src.getNavigation().size() <= 1)
{ return true; }
return false;
}
boolean isDirectTarget() // target is a direct attribute
{ if (trg.getNavigation().size() <= 1)
{ return true; }
return false;
}
boolean is1MultiplicityTarget() // target not a collection
{ if (trg.isCollection())
{ return false; }
return true;
}
boolean isIdentityTarget() // target is an identity
{ if (trg.isIdentity())
{ return true; }
Attribute fatt = trg.getFinalFeature();
if (fatt != null && fatt.isIdentity())
{ return true; }
return false;
}
boolean notReferenceTarget() // target is not a reference
{ if (trg.isReference())
{ return false; }
return true;
}
boolean isComposedTarget() // target is a composed attribute
{ if (trg.getNavigation().size() > 1)
{ return true; }
return false;
}
boolean isExpressionMapping()
{ return srcvalue != null; }
boolean isValueAssignment()
{ return srcvalue != null && elementVariable == null; }
boolean isExpressionAssignment()
{ return srcvalue != null && elementVariable != null; }
boolean isStringAssignment()
{ return srcvalue != null && auxVariables.size() > 1; }
// But could occur even if srcvalue is not a string.
boolean isStringMapping()
{ return srcvalue != null && auxVariables.size() > 1 &&
srcvalue.getType().isString();
}
boolean isValueTyped()
{ // target and source are of a value type
Type st = src.getType();
Type tt = trg.getType();
if (Type.isValueType(st) && Type.isValueType(tt))
{ return true; }
return false;
}
void displayMappingKind()
{ if (isExpressionMapping() &&
!(srcvalue.getType().isString()))
{ System.out.println(">>> Expression mapping: " + this); }
if (isStringAssignment() && srcvalue.getType().isString())
{ System.out.println(">>> String mapping: " + this); }
if (isValueAssignment())
{ System.out.println(">>> Value mapping: " + this); }
}
boolean isPossibleFeatureMerge()
{ // of form s->before(sep) or s->after(sep), etc
if (srcvalue != null &&
(srcvalue instanceof BinaryExpression))
{ BinaryExpression be = (BinaryExpression) srcvalue;
if ("->before".equals(be.operator) || "->after".equals(be.operator))
{ return true; }
if ("->front".equals(be.operator) || "->last".equals(be.operator))
{ return true; }
if ("->first".equals(be.operator) || "->tail".equals(be.operator))
{ return true; }
}
return false;
}
Vector findMergeFamily(Vector ams, Vector seen)
{ // of form s->before(sep) or s->after(sep), etc
Vector res = new Vector();
res.add(this);
if (srcvalue != null &&
(srcvalue instanceof BinaryExpression))
{ BinaryExpression be = (BinaryExpression) srcvalue;
if ("->before".equals(be.operator))
{ Expression sep = be.getRight();
for (int i = 0; i < ams.size(); i++)
{ AttributeMatching amx = (AttributeMatching) ams.get(i);
if (amx.isExpressionAssignment() && !(seen.contains(amx)) &&
amx.srcvalue instanceof BinaryExpression)
{ BinaryExpression be2 = (BinaryExpression) amx.srcvalue;
if ("->after".equals(be2.getOperator()) &&
(sep + "").equals(be2.getRight() + "") &&
(be.getLeft() + "").equals(be2.getLeft() + ""))
{ res.add(amx);
return res;
}
}
}
}
else if ("->after".equals(be.operator))
{ Expression sep = be.getRight();
for (int i = 0; i < ams.size(); i++)
{ AttributeMatching amx = (AttributeMatching) ams.get(i);
if (amx.isExpressionAssignment() && !(seen.contains(amx)) &&
amx.srcvalue instanceof BinaryExpression)
{ BinaryExpression be2 = (BinaryExpression) amx.srcvalue;
if ("->before".equals(be2.getOperator()) &&
(sep + "").equals(be2.getRight() + "") &&
(be.getLeft() + "").equals(be2.getLeft() + ""))
{ res.add(amx);
return res;
}
}
}
}
}
return res;
}
Vector findMergeFamilySequence1(Vector ams, Vector seen)
{ // of form s->front() or s->last(), etc
Vector res = new Vector();
res.add(this);
if (srcvalue != null &&
(srcvalue instanceof UnaryExpression))
{ UnaryExpression be = (UnaryExpression) srcvalue;
if ("->front".equals(be.operator))
{ for (int i = 0; i < ams.size(); i++)
{ AttributeMatching amx = (AttributeMatching) ams.get(i);
if (amx.isExpressionAssignment() && !(seen.contains(amx)) &&
amx.srcvalue instanceof UnaryExpression)
{ UnaryExpression be2 = (UnaryExpression) amx.srcvalue;
if ("->last".equals(be2.getOperator()) &&
(be.getArgument() + "").equals(be2.getArgument() + ""))
{ res.add(amx);
return res;
}
}
}
}
else if ("->last".equals(be.operator))
{ for (int i = 0; i < ams.size(); i++)
{ AttributeMatching amx = (AttributeMatching) ams.get(i);
if (amx.isExpressionAssignment() && !(seen.contains(amx)) &&
amx.srcvalue instanceof UnaryExpression)
{ UnaryExpression be2 = (UnaryExpression) amx.srcvalue;
if ("->front".equals(be2.getOperator()) &&
(be.getArgument() + "").equals(be2.getArgument() + ""))
{ res.add(amx);
return res;
}
}
}
}
}
return res;
}
Vector findMergeFamilySequence2(Vector ams, Vector seen)
{ // of form s->tail() or s->first(), etc
Vector res = new Vector();
res.add(this);
if (srcvalue != null &&
(srcvalue instanceof UnaryExpression))
{ UnaryExpression be = (UnaryExpression) srcvalue;
if ("->first".equals(be.operator))
{ for (int i = 0; i < ams.size(); i++)
{ AttributeMatching amx = (AttributeMatching) ams.get(i);
if (amx.isExpressionAssignment() && !(seen.contains(amx)) &&
amx.srcvalue instanceof UnaryExpression)
{ UnaryExpression be2 = (UnaryExpression) amx.srcvalue;
if ("->tail".equals(be2.getOperator()) &&
(be.getArgument() + "").equals(be2.getArgument() + ""))
{ res.add(amx);
return res;
}
}
}
}
else if ("->tail".equals(be.operator))
{ for (int i = 0; i < ams.size(); i++)
{ AttributeMatching amx = (AttributeMatching) ams.get(i);
if (amx.isExpressionAssignment() && !(seen.contains(amx)) &&
amx.srcvalue instanceof UnaryExpression)
{ UnaryExpression be2 = (UnaryExpression) amx.srcvalue;
if ("->first".equals(be2.getOperator()) &&
(be.getArgument() + "").equals(be2.getArgument() + ""))
{ res.add(amx);
return res;
}
}
}
}
}
return res;
}
public boolean usedAsIntermediateClass(Entity e)
{ // E is the entity element type of some p : trg.path
if (e == null)
{ return false; }
String nme = e.getName();
if (trg.isComposed())
{ Vector path = trg.getNavigation();
for (int i = 0; i < path.size() - 1; i++)
{ Attribute p = (Attribute) path.get(i);
if ((p.getElementType() + "").equals(nme))
{ return true; }
}
}
return false;
}
public boolean unusedInSource(Attribute v)
{ String nme = v.getName();
if (nme.equals(srcname))
{ return false; }
if (src.isComposed())
{ Vector path = src.getNavigation();
for (int i = 0; i < path.size(); i++)
{ Attribute p = (Attribute) path.get(i);
if (p.getName().equals(nme))
{ return false; }
}
return true;
}
if (srcvalue != null)
{ if (srcvalue.hasVariable(nme))
{ return false; }
}
return true;
}
public boolean usedInSource(Attribute v)
{ if (v == src || v.equalByNameAndOwner(src) || v.equalToReverseDirection(src))
{ return true; }
if (src.isComposed())
{ Vector path = src.getNavigation();
for (int i = 0; i < path.size(); i++)
{ Attribute p = (Attribute) path.get(i);
if (v == p || v.equalByNameAndOwner(p))
{ return true; }
}
return false;
}
String nme = v.getName();
if (srcvalue != null)
{ if (srcvalue.hasVariable(nme))
{ return true; }
}
return false;
} // or src is the reverse
public double similarity(Map mm, Vector entities, Vector thesaurus)
{ // NMS similarity between src and trg
double namesim = ModelElement.similarity(srcname, trgname);
double namesemsim = Entity.nmsSimilarity(srcname, trgname, thesaurus);
double nsim = (namesim + 2*namesemsim - namesim*namesemsim);
double tsim = Attribute.partialTypeMatch(src,trg,mm,entities);
return nsim*tsim;
} // should be namesemsim not 2*namesemsim
public Vector enumConversions()
{ // Conversions from one enum type to a different one
Vector res = new Vector();
Type stype = src.getType();
Type ttype = trg.getType();
if (stype != null && stype.isEnumeration() &&
ttype != null && ttype.isEnumeration())
{ Maplet mm = new Maplet(stype,ttype);
res.add(mm);
}
return res;
}
public Vector boolEnumConversions(Vector names)
{ // Conversions from boolean type to enum
Vector res = new Vector();
Type stype = src.getType();
Type ttype = trg.getType();
if (stype != null && stype.isBoolean() &&
ttype != null && ttype.isEnumeration())
{ res.add(ttype);
names.add(src.getName());
}
return res;
}
public Vector enumBoolConversions(Vector names)
{ // Conversions from boolean type to enum
Vector res = new Vector();
Type stype = src.getType();
Type ttype = trg.getType();
if (ttype != null && ttype.isBoolean() &&
stype != null && stype.isEnumeration())
{ res.add(stype);
names.add(trg.getName());
}
return res;
}
public Vector stringEnumConversions()
{ // Conversions from String type to enum
Vector res = new Vector();
Type stype = src.getType();
Type ttype = trg.getType();
if (stype != null && stype.isString() &&
ttype != null && ttype.isEnumeration())
{ res.add(ttype); }
return res;
}
public Vector enumStringConversions()
{ // Conversions from enum type to String
Vector res = new Vector();
Type stype = src.getType();
Type ttype = trg.getType();
if (stype != null && stype.isEnumeration() &&
ttype != null && ttype.isString())
{ res.add(stype); }
return res;
}
public boolean isBidirectionalassociationMatching()
{ return src.isBidirectionalassociation() &&
trg.isBidirectionalassociation();
}
public Vector inverts(Entity srcent, Entity trgent, Vector ems)
{ Vector res = new Vector();
if (isStringAssignment())
{ // assume auxVariables.size() == 2
// v1 + " ~ " + v2 --> v inverts as
// v->before(" ~ ") --> v1, v->after(" ~ ") --> v2
Expression delim = new BasicExpression("\" ~ \"");
if (srcvalue instanceof BinaryExpression)
{ BinaryExpression sbe = (BinaryExpression) srcvalue;
if ("+".equals(sbe.operator))
{ Expression lbe = sbe.getLeft();
Expression rbe = sbe.getRight();
// Attribute var1 = (Attribute) auxVariables.get(0);
// Attribute var2 = (Attribute) auxVariables.get(1);
if (rbe instanceof BinaryExpression &&
"+".equals(((BinaryExpression) rbe).operator))
{ delim = (BasicExpression) ((BinaryExpression) rbe).getLeft();
System.out.println(">> Delimiter = " + delim);
}
else if (lbe instanceof BinaryExpression &&
"+".equals(((BinaryExpression) lbe).operator))
{ // (var1 + sep) + var2
delim = ((BinaryExpression) lbe).getRight();
System.out.println(">> Delimiter = " + delim);
}
}
}
delim.setType(new Type("String",null));
delim.setElementType(new Type("String",null));
BasicExpression etrg = new BasicExpression(trg);
etrg.setUmlKind(Expression.ATTRIBUTE);
BinaryExpression be1 = new BinaryExpression("->before", etrg, delim);
BinaryExpression be2 = new BinaryExpression("->after", etrg, delim);
be1.setType(new Type("String",null));
be1.setElementType(new Type("String",null));
be2.setType(new Type("String",null));
be2.setElementType(new Type("String",null));
Attribute v1 = (Attribute) auxVariables.get(0);
Attribute v2 = (Attribute) auxVariables.get(1);
res.add(new AttributeMatching(be1,v1));
res.add(new AttributeMatching(be2,v2));
return res;
} // In general could be several of these.
else if (isExpressionAssignment())
{ if ("self".equals(srcvalue + ""))
{ return null; } // inverted to condition instead
if (srcvalue instanceof SetExpression)
{ SetExpression sse = (SetExpression) srcvalue;
if (sse.isOrdered() && sse.size() >= 2 && trg.isOrdered())
{ // Sequence{r1,...,rn} --> trg inverts to trg->at(1) --> r1, ...,
// trg->at(n) --> rn
Expression v1 = sse.getExpression(0);
Expression v2 = sse.getExpression(1);
Attribute a1 = new Attribute(v1 + "",v1.getType(),
ModelElement.INTERNAL);
Attribute a2 = new Attribute(v2 + "",v2.getType(),
ModelElement.INTERNAL);
a1.setElementType(v1.getElementType());
a2.setElementType(v2.getElementType());
BasicExpression ind1 = new BasicExpression(1);
BasicExpression ind2 = new BasicExpression(2);
BasicExpression trge = new BasicExpression(trg);
trge.setUmlKind(Expression.ATTRIBUTE);
BinaryExpression at1 = new BinaryExpression("->at",trge,ind1);
at1.setType(trg.getElementType());
at1.setElementType(trg.getElementType());
BinaryExpression at2 = new BinaryExpression("->at",trge,ind2);
at2.setType(trg.getElementType());
at2.setElementType(trg.getElementType());
AttributeMatching am1 = new AttributeMatching(at1,a1);
AttributeMatching am2 = new AttributeMatching(at2,a2);
res.add(am1);
res.add(am2);
return res;
}
} // no elementVariable needed?
// if r1->including(r2) --> trg with trg ordered then invert to
// trg->front() --> r1, trg->last() --> r2
if (srcvalue instanceof BinaryExpression)
{ BinaryExpression sbe = (BinaryExpression) srcvalue;
if (sbe.operator.equals("->including") &&
sbe.left instanceof BasicExpression &&
sbe.right instanceof BasicExpression && trg.isOrdered())
{ BasicExpression lft = (BasicExpression) sbe.left;
BasicExpression rgt = (BasicExpression) sbe.right;
Attribute a1 = new Attribute(lft.data,lft.getType(),
ModelElement.INTERNAL);
Attribute a2 = new Attribute(rgt.data,rgt.getType(),
ModelElement.INTERNAL);
a1.setElementType(lft.getElementType());
a2.setElementType(rgt.getElementType());
Expression trge = new BasicExpression(trg);
UnaryExpression at1 = new UnaryExpression("->front",trge);
UnaryExpression at2 = new UnaryExpression("->last",trge);
at1.setType(trg.getType());
at1.setElementType(trg.getElementType());
at2.setType(trg.getElementType());
at2.setElementType(trg.getElementType()); // can't be a nested sequence?
res.add(new AttributeMatching(at1,a1));
res.add(new AttributeMatching(at2,a2));
return res;
}
} // no element variable needed?
// if r1->union(r2) --> trg with trg or r1 being sets then invert to
// trg --> r1, trg --> r2
if (srcvalue instanceof BinaryExpression)
{ BinaryExpression sbe = (BinaryExpression) srcvalue;
if (sbe.operator.equals("->union") &&
sbe.left instanceof BasicExpression &&
sbe.right instanceof BasicExpression &&
!(sbe.left.isOrdered() && trg.isOrdered()))
{ BasicExpression lft = (BasicExpression) sbe.left;
BasicExpression rgt = (BasicExpression) sbe.right;
Attribute a1 = new Attribute(lft.data,lft.getType(),
ModelElement.INTERNAL);
a1.setElementType(lft.getElementType());
Attribute a2 = new Attribute(rgt.data,rgt.getType(),
ModelElement.INTERNAL);
a2.setElementType(rgt.getElementType());
res.add(new AttributeMatching(trg,a1));
res.add(new AttributeMatching(trg,a2));
return res;
}
}
// if r1->select(p) --> r2 invert to
// A'->unionAll(r2)->includes(self) --> p, for B' --> B
if (srcvalue instanceof BinaryExpression)
{ BinaryExpression sbe = (BinaryExpression) srcvalue;
if (sbe.operator.equals("->select") &&
sbe.left instanceof BasicExpression &&
sbe.right instanceof BasicExpression &&
sbe.right.isBooleanValued())
{ BasicExpression lft = (BasicExpression) sbe.left;
BasicExpression rgt = (BasicExpression) sbe.right;
Type etval = srcvalue.getElementType();
if (etval != null && etval.isEntity() && trg.getElementType() != null &&
trg.getElementType().isEntity())
{ Entity b = etval.getEntity();
Entity b1 = trg.getElementType().getEntity();
BasicExpression selfexp = new BasicExpression("self");
// selfexp.setUmlkind(Expression.VARIABLE);
selfexp.setType(new Type(b1));
selfexp.setElementType(new Type(b1));
Attribute p = new Attribute(rgt.data,rgt.getType(),
ModelElement.INTERNAL);
p.setElementType(rgt.getElementType());
BasicExpression realtrgs = new BasicExpression(trgent);
BasicExpression r2exp = new BasicExpression(trg);
BinaryExpression allr2 = new BinaryExpression("->unionAll",realtrgs,r2exp);
BinaryExpression newleft = new BinaryExpression("->includes",allr2,selfexp);
EntityMatching bb1 = ModelMatching.getRealEntityMatching(b1,b,ems);
if (bb1 != null)
{ bb1.addAttributeMatch(new AttributeMatching(newleft,p)); }
// and it is an expression match
return null;
}
}
}
if (srcvalue instanceof BinaryExpression)
{ BinaryExpression sbe = (BinaryExpression) srcvalue;
// elems->closure(next) --> parts for ordered parts inverts as
// parts --> elems, next computation of parts/container --> next
// System.out.println("INVERSE OF: " + srcvalue + " --> " + trg);
// System.out.println(trg.isOrdered() + " " + trg.hasOpposite() + " " +
// ((BasicExpression) sbe.right).variable + " " +
// ((BasicExpression) sbe.right).isZeroOneRole());
if (sbe.operator.equals("->closure") && trg.isOrdered() &&
sbe.left instanceof BasicExpression && trg.hasOpposite() &&
sbe.right instanceof BasicExpression &&
((BasicExpression) sbe.right).variable != null &&
((BasicExpression) sbe.right).isZeroOneRole())
{ BasicExpression lft = (BasicExpression) sbe.left;
BasicExpression rgt = (BasicExpression) sbe.right;
String role1 = trg.getRole1(); // container
Attribute newrhs1 = new Attribute(sbe.left + "", sbe.left.getType(),
ModelElement.INTERNAL); // elems
newrhs1.setElementType(sbe.left.getElementType());
AttributeMatching newam = new AttributeMatching(trg,newrhs1);
res.add(newam);
Type targetref = src.getElementType();
if (targetref != null && targetref.isEntity())
{ Entity trgref = targetref.getEntity();
BasicExpression ownerparts = new BasicExpression(trg);
BasicExpression ownr = new BasicExpression(role1);
ownerparts.setUmlKind(Expression.ATTRIBUTE);
ownr.setUmlKind(Expression.ATTRIBUTE);
ownerparts.setObjectRef(ownr);
BasicExpression selfexp = new BasicExpression("self");
// should have umlkind = VARIABLE
selfexp.setType(new Type(trgref));
selfexp.setElementType(new Type(trgref));
BinaryExpression selfindex = new BinaryExpression("->indexOf", ownerparts, selfexp);
UnaryExpression ownerpartssize = new UnaryExpression("->size",ownerparts);
BinaryExpression test = new BinaryExpression("<", selfindex, ownerpartssize);
BinaryExpression nextindex = new BinaryExpression("+",selfindex,new BasicExpression(1));
BinaryExpression ownerpartsat = new BinaryExpression("->at", ownerparts, nextindex);
SetExpression emptyset = new SetExpression();
SetExpression nextset = new SetExpression();
nextset.addElement(ownerpartsat);
ConditionalExpression newlhs = new ConditionalExpression(test,nextset,emptyset);
Type sourceref = trg.getElementType();
if (sourceref != null && sourceref.isEntity())
{ Entity srcref = sourceref.getEntity();
Type restype = new Type("Set", null);
restype.setElementType(new Type(srcref)); // not trgref
ownerparts.setType(restype);
ownerparts.setElementType(new Type(srcref));
newlhs.setType(restype);
newlhs.setElementType(new Type(srcref));
newlhs.setBrackets(true);
AttributeMatching newatm =
new AttributeMatching(newlhs,rgt.variable);
Attribute varnew = new Attribute(Identifier.nextIdentifier("var$"),
newlhs.getElementType(),
ModelElement.INTERNAL);
varnew.setElementType(newlhs.getElementType());
newatm.setElementVariable(varnew);
EntityMatching bb1 = ModelMatching.getRealEntityMatching(srcref,trgref,ems);
if (bb1 != null)
{ bb1.addAttributeMatch(newatm); }
}
}
return res;
}
else if (sbe.operator.equals("->unionAll") && sbe.right instanceof BasicExpression &&
sbe.left instanceof BinaryExpression &&
((BinaryExpression) sbe.left).operator.equals("->closure"))
{ BinaryExpression clsre = (BinaryExpression) sbe.left;
BasicExpression f = (BasicExpression) sbe.right;
Expression g = clsre.right;
BasicExpression r = new BasicExpression(trg);
Type Rtype = r.getElementType();
BasicExpression Fclass = new BasicExpression(trgent);
Type Ftype = new Type(trgent);
String avalue = Identifier.nextIdentifier("var$");
BasicExpression ax = new BasicExpression(avalue);
ax.setType(Ftype);
ax.setElementType(Ftype);
BinaryExpression aInFclass = new BinaryExpression(":", ax, Fclass);
BasicExpression ar = new BasicExpression(trg);
ar.setObjectRef(ax);
BinaryExpression aAncestor = new BinaryExpression("->includesAll", r, ar);
BinaryExpression aStrict = new BinaryExpression("/=", r, ar);
BinaryExpression aStrictAncestor = new BinaryExpression("&",aAncestor,aStrict);
BinaryExpression ancestors = new BinaryExpression("|", aInFclass, aStrictAncestor);
// ancestors->selectMaximals(r.size) |--> g
// r - ancestors->unionAll(r) |--> f
UnaryExpression rsize = new UnaryExpression("->size", r);
BinaryExpression lhsgmap = new BinaryExpression("->selectMaximals", ancestors, rsize);
Type FSetType = new Type("Set",null);
FSetType.setElementType(Ftype);
lhsgmap.setType(FSetType);
lhsgmap.setElementType(Ftype);
AttributeMatching gmatch = new AttributeMatching(lhsgmap,g);
Attribute gmatchvar = new Attribute(Identifier.nextIdentifier("var$"), Ftype, ModelElement.INTERNAL);
gmatchvar.setElementType(Ftype);
gmatch.setElementVariable(gmatchvar);
res.add(gmatch);
BinaryExpression ancestorsUnionAllr = new BinaryExpression("->unionAll", ancestors, r);
BinaryExpression fmatchlhs = new BinaryExpression("-", r, ancestorsUnionAllr);
Type FRefSetType = new Type("Set",null);
FRefSetType.setElementType(Rtype);
fmatchlhs.setType(FRefSetType);
fmatchlhs.setElementType(Rtype);
AttributeMatching fmatch = new AttributeMatching(fmatchlhs,f);
Attribute fmatchvar = new Attribute(Identifier.nextIdentifier("var$"), Rtype, ModelElement.INTERNAL);
fmatchvar.setElementType(Rtype);
fmatch.setElementVariable(fmatchvar);
res.add(fmatch);
return res;
}
}
}
AttributeMatching invam = invert();
res.add(invam);
return res;
}
public AttributeMatching invert()
{ return new AttributeMatching(trg,src); }
// ignores any expression/value match
public static Vector invertCondition(Expression cond, Entity realsrc, Entity realtrg)
{ Vector res = new Vector();
if (cond instanceof BinaryExpression)
{ BinaryExpression condbe = (BinaryExpression) cond;
if (condbe.operator.equals("="))
{ // feature = value inverts as value --> feature
if ((condbe.left.umlkind == Expression.VARIABLE ||
condbe.left.umlkind == Expression.ATTRIBUTE) &&
condbe.left instanceof BasicExpression &&
((BasicExpression) condbe.left).variable != null &&
condbe.right.umlkind != Expression.VARIABLE &&
condbe.right.umlkind != Expression.ATTRIBUTE)
{ Attribute trgfeature = ((BasicExpression) condbe.left).variable;
Attribute var = null;
// System.out.println(">> Target feature is " + trgfeature +
// " " + trgfeature.getType());
Expression newleft = null;
if ((condbe.right + "").equals("self"))
{ newleft = new BasicExpression("self");
newleft.setType(new Type(realtrg));
newleft.setElementType(new Type(realtrg));
var = new Attribute("self",
new Type(realtrg),
ModelElement.INTERNAL);