-
Notifications
You must be signed in to change notification settings - Fork 9
/
ASTTerm.java
10810 lines (8762 loc) · 333 KB
/
ASTTerm.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
/******************************
* 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
* *****************************/
import java.util.Vector;
import java.io.*;
import javax.swing.*;
import java.lang.reflect.Method;
public abstract class ASTTerm
{ String id = "";
// Vector stereotypes = new Vector();
// A programming language AST can be represented by
// one of the following UML/OCL things:
Expression expression = null;
Statement statement = null;
ModelElement modelElement = null;
Vector modelElements = null;
Vector expressions = null; // for parameter/argument lists
static Vector requiredLibraries = new Vector();
static String packageName = null;
static Vector enumtypes;
static Vector entities;
static Vector functionsInScope;
static Vector cobolClasses = new Vector();
static Entity currentClass = null; // Current context class
static java.util.Map metafeatures;
static { metafeatures = new java.util.HashMap(); }
// String --> Vector(String),
// eg., recording the conceptual
// type of the element & stereotypes.
static java.util.Map types = new java.util.HashMap();
// String --> String for general type of identifiers
// valid at the scope of the current term.
static java.util.Map elementTypes = new java.util.HashMap();
// String --> String for general elementType
// of identifiers
// valid at the scope of the current term.
static java.util.Map cg_cache = new java.util.HashMap();
// CGSpec --> (ASTTerm --> String)
// But invalid to do this if the denotation needs to
// change, eg., a[x] is different for array/map a
static java.util.Map mathoclvars;
static
{ mathoclvars = new java.util.HashMap(); }
// String --> ASTTerm
static Vector mathocltheorems;
static
{ mathocltheorems = new Vector(); }
// pairs [concl,premise]
static Vector mathoclrewrites;
static
{ mathoclrewrites = new Vector(); }
// pairs [lhs,rhs]
static int mathoclfunctionIndex;
static String cobolHyphenReplacement;
static
{ cobolHyphenReplacement = "_"; } // or "$" for Java
static String cobolCommaReplacement;
static
{ cobolCommaReplacement = "."; }
static int cobolFillerCount;
static
{ cobolFillerCount = 0; }
static Vector cobolDataDescriptionDataNames;
static
{ cobolDataDescriptionDataNames = new Vector(); }
static Vector cobolAmbiguousDataNames;
static
{ cobolAmbiguousDataNames = new Vector(); }
static Vector cqueryfunctions = new Vector();
static
{ cqueryfunctions.add("sin");
cqueryfunctions.add("cos");
cqueryfunctions.add("tan");
cqueryfunctions.add("asin");
cqueryfunctions.add("acos");
cqueryfunctions.add("atan");
cqueryfunctions.add("sinh");
cqueryfunctions.add("cosh");
cqueryfunctions.add("tanh");
cqueryfunctions.add("exp");
cqueryfunctions.add("log");
cqueryfunctions.add("log10");
cqueryfunctions.add("sqrt");
cqueryfunctions.add("ceil");
cqueryfunctions.add("floor");
cqueryfunctions.add("fabs");
cqueryfunctions.add("abs");
cqueryfunctions.add("labs");
cqueryfunctions.add("pow");
cqueryfunctions.add("atan2");
cqueryfunctions.add("ldexp");
cqueryfunctions.add("fmod");
cqueryfunctions.add("atof");
cqueryfunctions.add("atoi");
cqueryfunctions.add("atol");
cqueryfunctions.add("isalnum");
cqueryfunctions.add("isalpha");
cqueryfunctions.add("isspace");
cqueryfunctions.add("isdigit");
cqueryfunctions.add("isupper");
cqueryfunctions.add("islower");
cqueryfunctions.add("iscntrl");
cqueryfunctions.add("isgraph");
cqueryfunctions.add("isprint");
cqueryfunctions.add("ispunct");
cqueryfunctions.add("isxdigit");
cqueryfunctions.add("isnan");
cqueryfunctions.add("calloc");
cqueryfunctions.add("malloc");
// cqueryfunctions.add("realloc");
cqueryfunctions.add("strcmp");
cqueryfunctions.add("strncmp");
cqueryfunctions.add("strchr");
cqueryfunctions.add("strrchr");
cqueryfunctions.add("strlen");
cqueryfunctions.add("strstr");
cqueryfunctions.add("strpsn");
cqueryfunctions.add("strcpsn");
cqueryfunctions.add("strpbrk");
cqueryfunctions.add("strerror");
cqueryfunctions.add("getenv");
cqueryfunctions.add("bsearch");
cqueryfunctions.add("toupper");
cqueryfunctions.add("tolower");
cqueryfunctions.add("fopen");
cqueryfunctions.add("div");
cqueryfunctions.add("ldiv");
cqueryfunctions.add("time");
cqueryfunctions.add("difftime");
cqueryfunctions.add("localtime");
cqueryfunctions.add("mktime");
cqueryfunctions.add("asctime");
cqueryfunctions.add("ctime");
cqueryfunctions.add("gmtime");
}
public abstract String toString();
public abstract String getTag();
public boolean hasNestedTag(String tg)
{ java.util.Set alltags = allTagsIn();
if (alltags.contains(tg))
{ return true; }
return false;
}
public boolean hasNestedTags(Vector tgs)
{ java.util.Set alltags = allTagsIn();
for (int i = 0; i < tgs.size(); i++)
{ String tg = (String) tgs.get(i);
if (alltags.contains(tg))
{ }
else
{ return false; }
}
return true;
}
public boolean hasExactNestedTags(Vector tgs)
{ java.util.Set alltags = allTagsIn();
if (tgs.containsAll(alltags) &&
alltags.containsAll(tgs))
{ return true; }
return false;
}
public abstract String literalForm();
public abstract String literalFormSpaces();
public abstract String evaluationLiteralForm();
public static Vector getLiteralForms(Vector trms)
{ Vector res = new Vector();
for (int i = 0; i < trms.size(); i++)
{ ASTTerm trm = (ASTTerm) trms.get(i);
res.add(trm.literalForm());
}
return res;
}
public static void putCg_cache(CGSpec cgs, ASTTerm trm, String res)
{ java.util.Map cgsmap = (java.util.Map) cg_cache.get(cgs);
if (cgsmap == null)
{ cgsmap = new java.util.HashMap(); }
cgsmap.put(trm, res);
cg_cache.put(cgs, cgsmap);
}
public static String getCg_cache(CGSpec cgs, ASTTerm trm)
{ java.util.Map cgsmap = (java.util.Map) cg_cache.get(cgs);
if (cgsmap == null)
{ return null; }
String res = (String) cgsmap.get(trm);
return res;
}
public abstract ASTTerm removeWhitespaceTerms();
public abstract ASTTerm removeExtraNewlines();
public abstract ASTTerm replaceCobolIdentifiers();
public abstract ASTTerm replaceAmbiguousCobolNames(Vector rnames);
public abstract ASTTerm substituteEq(String str, ASTTerm newtrm);
public abstract String tagFunction();
public static Vector allNestedTagsArities(Vector sasts)
{ // [tag,arity] for each inner term of sasts.
Vector res = new Vector();
for (int i = 0; i < sasts.size(); i++)
{ ASTTerm trm = (ASTTerm) sasts.get(i);
Vector trms = trm.getTerms();
for (int j = 0; j < trms.size(); j++)
{ ASTTerm tj = (ASTTerm) trms.get(j);
Vector alltags = tj.allNestedTagsArities();
res.addAll(alltags);
}
}
return res;
}
public static Vector allTagsArities(Vector sasts)
{ // [tag,arity] for each term of sasts and subterm,
// recursively.
Vector res = new Vector();
for (int i = 0; i < sasts.size(); i++)
{ ASTTerm trm = (ASTTerm) sasts.get(i);
Vector alltags = trm.allTagsArities();
res.addAll(alltags);
}
return res;
}
public static Vector allTagsAtIndex(int index, ASTTerm[] sasts)
{ // tags for each i'th term of sasts
Vector alltags = new Vector();
Vector res = new Vector();
for (int i = 0; i < sasts.length; i++)
{ ASTTerm trm = (ASTTerm) sasts[i];
ASTTerm termi = (ASTTerm) trm.getTerm(index);
String tagi = termi.getTag();
if (tagi != null)
{ if (alltags.contains(tagi)) { }
else
{ alltags.add(tagi); }
}
}
return alltags;
}
public static Vector allTagSetsAtIndex(int index, ASTTerm[] sasts)
{ // tags for each i'th term of sasts
Vector alltags = new Vector();
Vector res = new Vector();
for (int i = 0; i < sasts.length; i++)
{ ASTTerm trm = (ASTTerm) sasts[i];
ASTTerm termi = (ASTTerm) trm.getTerm(index);
java.util.Set tagi = termi.allTagsIn();
if (tagi != null)
{ if (alltags.contains(tagi)) { }
else
{ alltags.add(tagi); }
}
}
return alltags;
}
public abstract Vector allNestedTagsArities();
public abstract Vector allTagsArities();
public abstract java.util.Set allTagsIn();
public abstract Vector allIdentifiers(Vector tags);
public static Vector rulesFromTagsArities(Vector tagsarities)
{ // For each (tag,n) : tagsarities
// define CGRule tag:: _1 |-->_1 or _* |-->_*
java.util.Map cgrules = new java.util.HashMap();
for (int k = 0; k < tagsarities.size(); k++)
{ Vector tgar = (Vector) tagsarities.get(k);
String tg = (String) tgar.get(0);
int ar = (int) tgar.get(1);
Vector rls = (Vector) cgrules.get(tg);
if (rls == null)
{ rls = new Vector(); }
if (ar == 1)
{ if (rls.contains("_1 |-->_1")) { }
else
{ rls.add("_1 |-->_1"); }
}
else
{ if (rls.contains("_* |-->_*")) { }
else
{ rls.add("_* |-->_*"); }
}
cgrules.put(tg,rls);
}
Vector res = new Vector();
for (Object obj : cgrules.keySet())
{ String key = (String) obj;
Vector rls = (Vector) cgrules.get(key);
String rle = key + "::\n";
for (int p = 0; p < rls.size(); p++)
{ rle = rle + rls.get(p) + "\n"; }
res.add(rle);
}
return res;
}
public static boolean isSubterm(ASTTerm t1, ASTTerm t2)
{ if (t1 == null || t2 == null)
{ return false; }
String lit1 = t1.literalForm();
String lit2 = t2.literalForm();
if (lit1.equals(lit2))
{ return true; } // Only for t2 : ASTSymbolTerm, ASTBasicTerm
if (t2 instanceof ASTCompositeTerm)
{ ASTCompositeTerm ct = (ASTCompositeTerm) t2;
Vector trms = ct.getTerms();
for (int i = 0; i < trms.size(); i++)
{ ASTTerm subtrm =
(ASTTerm) trms.get(i);
if (ASTTerm.isSubterm(t1, subtrm))
{ return true; }
}
return false;
}
return false;
}
public static boolean isTermOf(ASTTerm sub, ASTTerm sup)
{ // sub is literally equal to a subterm of sup
String sublit = sub.literalForm();
Vector trms = sup.getTerms();
for (int i = 0; i < trms.size(); i++)
{ ASTTerm trm = (ASTTerm) trms.get(i);
if (sublit.equals(trm.literalForm()))
{ return true; }
}
return false;
}
public Vector allTagSubterms(String tagx)
{ Vector res = new Vector();
if (getTag().equals(tagx))
{ res.add(this); }
Vector trms = getTerms();
for (int i = 0; i < trms.size(); i++)
{ ASTTerm trm = (ASTTerm) trms.get(i);
Vector nres = trm.allTagSubterms(tagx);
res.addAll(nres);
}
return res;
}
public void addStereotype(String str)
{ String lit = literalForm();
Object stereo = ASTTerm.metafeatures.get(lit);
Vector stereotypes = new Vector();
if (stereo == null)
{ stereotypes.add(str);
ASTTerm.metafeatures.put(lit,stereotypes);
return;
}
else if (!(stereo instanceof Vector))
{ return; } // single-valued metafeature.
// System.out.println(">++++> Adding stereotype " + str + " to " + lit);
stereotypes = (Vector) stereo;
if (stereotypes.contains(str)) {}
else
{ stereotypes.add(str);
ASTTerm.metafeatures.put(lit,stereotypes);
}
// JOptionPane.showMessageDialog(null,
// "*** addStereotype " + str + " to " + lit + " Metafeature values of " + lit + " are " + stereotypes, "",
// JOptionPane.INFORMATION_MESSAGE);
}
public static void addStereo(ASTTerm ast, String str)
{ if (ast == null) { return; }
String lit = ast.literalForm();
addStereo(lit,str);
}
public static void addStereo(String lit, String str)
{ Object mfs = ASTTerm.metafeatures.get(lit);
if (mfs == null)
{ mfs = new Vector();
ASTTerm.metafeatures.put(lit, mfs);
}
if (mfs instanceof Vector)
{
Vector stereotypes = (Vector) mfs;
if (stereotypes.contains(str)) {}
else
{ stereotypes.add(0,str);
ASTTerm.metafeatures.put(lit, stereotypes);
}
}
// JOptionPane.showMessageDialog(null,
// "*** addStereo " + str + " to " + lit + " Metafeature values of " + lit + " are " + mfs, "",
// JOptionPane.INFORMATION_MESSAGE);
System.out.println("*** " + lit + " metafeature values set to " +
ASTTerm.metafeatures.get(lit));
}
public static void setTaggedValue(ASTTerm ast, String mf, String val)
{ // updates stereotypes of ast so that mf=val
String lit = ast.literalForm();
setTaggedValue(lit, mf, val);
}
public static void setTaggedValue(String lit, String mf, String val)
{ Object mfs = ASTTerm.metafeatures.get(lit);
// System.out.println("*** " + lit + " has tagged values: " +
// mfs);
if (mfs == null)
{ mfs = new Vector();
ASTTerm.metafeatures.put(lit,mfs);
}
if (mfs instanceof Vector)
{ Vector stereos = (Vector) mfs;
Vector newstereos = new Vector();
for (int x = 0; x < stereos.size(); x++)
{ String stereo = (String) stereos.get(x);
if (stereo.startsWith(mf + "=") ||
stereo.startsWith(mf + " ="))
{ }
else
{ newstereos.add(stereo); }
}
newstereos.add(mf + "=" + val);
ASTTerm.metafeatures.put(lit,newstereos);
System.out.println("*** Set " + lit +
" tagged values: " +
newstereos);
}
}
public void removeStereotype(String str)
{ String lit = literalForm();
Object mfs = ASTTerm.metafeatures.get(lit);
if (mfs == null)
{ mfs = new Vector();
ASTTerm.metafeatures.put(lit,mfs);
}
if (mfs instanceof Vector)
{
Vector stereotypes = (Vector) mfs;
Vector removed = new Vector();
removed.add(str);
stereotypes.removeAll(removed);
ASTTerm.metafeatures.put(lit,stereotypes);
}
System.out.println(">>> " + lit + " metafeature values = " +
ASTTerm.metafeatures.get(lit));
}
public static void removeStereo(String lit, String str)
{ if (ASTTerm.metafeatures.get(lit) instanceof Vector)
{
Vector stereotypes =
(Vector) ASTTerm.metafeatures.get(lit);
if (stereotypes == null)
{ stereotypes = new Vector();
ASTTerm.metafeatures.put(lit,stereotypes);
}
Vector removed = new Vector();
removed.add(str);
stereotypes.removeAll(removed);
}
System.out.println("*** " + lit + " metafeature values= " +
ASTTerm.metafeatures.get(lit));
}
public boolean hasStereotype(String str)
{ String lit = literalForm();
Object mfs = ASTTerm.metafeatures.get(lit);
if (mfs == null)
{ return false; }
if (mfs instanceof Vector)
{ Vector stereotypes = (Vector) mfs;
return stereotypes.contains(str);
}
return false;
}
public static String getStereotypeValue(String lit)
{ Object stereo =
ASTTerm.metafeatures.get(lit);
System.out.println(">>>--- Global variable " + lit +
" has value " + stereo);
System.out.println();
if (stereo == null)
{ return ""; }
else
{ return "" + stereo; }
}
public static void setStereotypeValue(String lit, String val)
{ System.out.println(">>>--- Global variable " + lit +
" set to " + val);
System.out.println();
ASTTerm.metafeatures.put(lit,val);
}
public static Vector getStereotypes(String lit)
{ Object mfs = ASTTerm.metafeatures.get(lit);
if (mfs == null)
{ return new Vector(); }
if (mfs instanceof Vector)
{ return (Vector) mfs; }
return null;
}
public static Vector getStereotypes(ASTTerm t)
{ return getStereotypes(t.literalForm()); }
public static boolean hasTaggedValue(ASTTerm trm, String str)
{ String lit = trm.literalForm();
System.out.println("*** " + lit + " has tagged values: " +
ASTTerm.metafeatures.get(lit));
if (ASTTerm.metafeatures.get(lit) instanceof Vector)
{
Vector stereotypes =
(Vector) ASTTerm.metafeatures.get(lit);
if (stereotypes == null)
{ stereotypes = new Vector();
ASTTerm.metafeatures.put(lit,stereotypes);
}
for (int x = 0; x < stereotypes.size(); x++)
{ String stereo = (String) stereotypes.get(x);
if (stereo.startsWith(str + "=")) // or " ="
{ return true; }
}
return false;
}
return false;
}
public static String getTaggedValue(ASTTerm trm, String str)
{ String lit = trm.literalForm();
Object mfs = ASTTerm.metafeatures.get(lit);
if (mfs == null)
{ mfs = new Vector();
ASTTerm.metafeatures.put(lit,mfs);
}
// JOptionPane.showMessageDialog(null,
// "Looking up: " + str + " of: " + lit +
// " in: " + ASTTerm.metafeatures, "",
// JOptionPane.INFORMATION_MESSAGE);
// System.out.println("*** " + lit +
// " gets tagged values: " +
// mfs);
if (mfs instanceof Vector)
{ Vector stereotypes = (Vector) mfs;
for (int x = 0; x < stereotypes.size(); x++)
{ String stereo = (String) stereotypes.get(x);
if (stereo.startsWith(str + "=")) // or " ="
{ int indx = stereo.indexOf("=");
return stereo.substring(indx + 1);
}
}
}
return null;
}
public static String getTaggedValue(String lit, String str)
{ Object mfs = ASTTerm.metafeatures.get(lit);
if (mfs instanceof Vector)
{ Vector stereotypes = (Vector) mfs;
for (int x = 0; x < stereotypes.size(); x++)
{ String stereo = (String) stereotypes.get(x);
if (stereo.startsWith(str + "=")) // or " ="
{ int indx = stereo.indexOf("=");
return stereo.substring(indx + 1);
}
}
}
return null;
}
public static String cgtlOperation(String opname, Vector eargs)
{ System.out.println(">>> External operation: " + opname + " on " + eargs);
/* System.out.println(">>> metafeatures: " + ASTTerm.metafeatures);
System.out.println(); */
if ("symbolicAddition".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.symbolicAddition(e1,e2);
}
if ("symbolicSubtraction".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.symbolicSubtraction(e1,e2);
}
if ("symbolicMultiplication".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.symbolicMultiplication(e1,e2);
}
if ("symbolicDivision".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.symbolicDivision(e1,e2);
}
if ("symbolicNegateMultiplication".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.symbolicNegateMultiplication(e1,e2);
}
if ("symbolicLess".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.symbolicLess(e1,e2);
}
if ("symbolicLeq".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.symbolicLeq(e1,e2);
}
if ("symbolicDeterminant".equals(opname) &&
eargs.size() == 1)
{ ASTTerm m = (ASTTerm) eargs.get(0);
return ASTTerm.symbolicDeterminant(m);
}
if ("expressAsPolynomial".equals(opname) &&
eargs.size() == 2)
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
ASTTerm e2 = (ASTTerm) eargs.get(1);
return ASTTerm.expressAsPolynomial(e1,e2);
}
// Also Eq and Neq
if ("equationSolution".equals(opname) &&
eargs.size() == 2)
{ // Solve exprs for ids
ASTTerm exprs = (ASTTerm) eargs.get(0);
ASTTerm ids = (ASTTerm) eargs.get(1);
return ASTTerm.solveEquations(exprs,ids);
}
if ("attemptProof".equals(opname) &&
eargs.size() == 2)
{ // Prove expr if assump
ASTTerm exprs = (ASTTerm) eargs.get(0);
ASTTerm ids = (ASTTerm) eargs.get(1);
return ASTTerm.attemptProof(exprs,ids);
}
if ("attemptSubstitution".equals(opname) &&
eargs.size() == 2)
{ // Prove expr if assump
ASTTerm vbl = (ASTTerm) eargs.get(0);
ASTTerm expr = (ASTTerm) eargs.get(1);
String var = vbl.literalForm();
ASTTerm def = (ASTTerm) ASTTerm.mathoclvars.get(var);
if (def == null)
{ return " Substitute " + var + " in " +
expr.literalFormSpaces();
}
ASTTerm res = expr.mathOCLSubstitute(var,def);
return res.literalFormSpaces();
}
/* _2<when> _2 expression<action> _1`value / _1
Substitute _1 in _2 |-->Substitute _1 in _2
*/
if ("groupTermsByPower".equals(opname) &&
eargs.size() == 2)
{ // Prove expr if assump
ASTTerm exprs = (ASTTerm) eargs.get(0);
ASTTerm ids = (ASTTerm) eargs.get(1);
return ASTTerm.groupTermsByPower(exprs,ids);
}
if ("pythonEval".equals(opname) &&
eargs.size() == 1)
{ // (trailer (arguments (
// (arglist (argument ... "text" ...)) ) ))
ASTTerm e2 = (ASTTerm) eargs.get(0);
String arg = e2.literalForm();
if (arg.startsWith("(") &&
arg.endsWith(")"))
{ arg = arg.substring(1,arg.length()-1); }
if (arg.startsWith("\"") &&
arg.endsWith("\""))
{ arg = arg.substring(1,arg.length()-1); }
String astexpr =
PreProcessModels.applyAntlr("Python",
"expr",
arg);
if (astexpr == null ||
"".equals(astexpr))
{ return "null"; }
Compiler2 c = new Compiler2();
ASTTerm xx =
c.parseGeneralAST(astexpr);
if (xx == null)
{ return "null"; }
return PreProcessModels.applyCGTL(xx,
"cg/python2UML.cstl");
}
if ("pythonExec".equals(opname) &&
eargs.size() == 1)
{ // (trailer (arguments (
// (arglist (argument ... "text" ...)) ) ))
ASTTerm e2 = (ASTTerm) eargs.get(0);
String arg = e2.literalForm();
if (arg.startsWith("(") &&
arg.endsWith(")"))
{ arg = arg.substring(1,arg.length()-1); }
if (arg.startsWith("\"") &&
arg.endsWith("\""))
{ arg = arg.substring(1,arg.length()-1); }
String astexpr =
PreProcessModels.applyAntlr("Python",
"stmt",
arg);
if (astexpr == null ||
"".equals(astexpr))
{ return "null"; }
Compiler2 c = new Compiler2();
ASTTerm xx =
c.parseGeneralAST(astexpr);
if (xx == null)
{ return "null"; }
return PreProcessModels.applyCGTL(xx,
"cg/python2UML.cstl");
}
int dotind = opname.indexOf(".");
// System.out.println(dotind);
if (dotind > 0)
{ String cname = opname.substring(0,dotind);
String mname = opname.substring(dotind + 1);
try {
Class cl = Class.forName(cname);
Object xinst = cl.newInstance();
Method[] mets = cl.getDeclaredMethods();
// System.out.println(">> Found class: " + cl + " methods " + mets.length);
for (int i = 0; i < mets.length; i++)
{ Method m = mets[i];
// System.out.println(m.getName());
if (m.getName().equals(mname))
{ Object[] args = eargs.toArray();
return (String) m.invoke(xinst,args);
}
}
}
catch (Throwable _x)
{ System.err.println("!! No class/method: " + cname + " " + mname); }
}
return opname + "(" + eargs + ")";
}
public static void addRequiredLibrary(String lib)
{ if (requiredLibraries.contains(lib)) {}
else
{ requiredLibraries.add(lib); }
}
public static Vector getRequiredLibraries()
{ return requiredLibraries; }
public abstract ASTTerm removeOuterTag();
/* For symbol terms return null; basic terms the
symbol of value; composite terms - the first term */
public abstract ASTTerm getTerm(int i);
public abstract Vector tokenSequence();
public abstract int termSize();
public abstract int size();
public abstract Vector getTerms();
public abstract Vector symbolTerms();
public abstract Vector nonSymbolTerms();
public abstract String toJSON();
public abstract String asTextModel(PrintWriter out);
public abstract String cg(CGSpec cgs);
public abstract String cgRules(CGSpec cgs, Vector rules);
public abstract java.util.Set allMathMetavariables();
public abstract java.util.HashMap hasMatch(ASTTerm rterm,
java.util.HashMap res);
public abstract java.util.HashMap fullMatch(ASTTerm rterm,
java.util.HashMap res);
public abstract ASTTerm instantiate(
java.util.HashMap res);
// Only for programming languages.
public abstract boolean updatesObject(ASTTerm t);
public abstract ASTTerm updatedObject();
public abstract boolean callSideEffect();
// Only for programming languages.
public abstract boolean hasSideEffect();
public abstract boolean isIdentifier();
public static boolean isMathOCLIdentifier(String str)
{ int n = str.length();
if (n > 0 && Character.isJavaIdentifierStart(str.charAt(0)))
{ for (int i = 1; i < n; i++)
{ if (Character.isJavaIdentifierPart(str.charAt(i))) { }
else
{ return false; }
}
return true;
}
return false;
} // also g{p} etc.
public abstract String preSideEffect();
public abstract String postSideEffect();
public boolean hasMetafeature(String f)
{ Object mf = metafeatures.get(f);
if (mf instanceof String)
{ String val = (String) mf;
return val != null;
}
return false;
}
public void setMetafeature(String f, String val)
{ metafeatures.put(f,val); }
public String getMetafeatureValue(String f)
{ Object mf = metafeatures.get(f);
if (mf instanceof String)
{ String val = (String) mf;
return val;
}
return null;
}
public static void setType(ASTTerm t, String val)
{ String f = t.literalForm();
types.put(f,val);
}
public static void setElementType(ASTTerm t, String val)
{ String f = t.literalForm();
elementTypes.put(f,val);
}
public static void setType(String f, String val)
{ types.put(f,val); }
public static void setElementType(String f, String val)
{ elementTypes.put(f,val); }
public static String getType(String f)
{ String val = (String) types.get(f);
return val;
}
public static String getType(ASTTerm t)
{ String val = (String) types.get(t.literalForm());