Skip to content

Commit 50d3f39

Browse files
authored
Improved Pascal abstractor
1 parent fc6ba6d commit 50d3f39

File tree

11 files changed

+258
-34
lines changed

11 files changed

+258
-34
lines changed

ASTCompositeTerm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6640,7 +6640,7 @@ else if ("free".equals(fname) && args.size() == 1)
66406640
ImplicitInvocationStatement ee =
66416641
new ImplicitInvocationStatement(delarg);
66426642
return ee;
6643-
}
6643+
} // But !p is deleted, not p?
66446644
else if ("abort".equals(fname) && args.size() == 0)
66456645
{ Expression fail =
66466646
BasicExpression.newStaticCallBasicExpression("exit",

ASTTerm.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public abstract class ASTTerm
3939

4040
static Entity currentClass = null; // Current context class
4141

42-
static java.util.Map metafeatures = new java.util.HashMap();
42+
static java.util.Map metafeatures;
43+
static { metafeatures = new java.util.HashMap(); }
4344
// String --> Vector(String),
4445
// eg., recording the conceptual
4546
// type of the element & stereotypes.
@@ -55,6 +56,8 @@ public abstract class ASTTerm
5556

5657
static java.util.Map cg_cache = new java.util.HashMap();
5758
// CGSpec --> (ASTTerm --> String)
59+
// But invalid to do this if the denotation needs to
60+
// change, eg., a[x] is different for array/map a
5861

5962
static java.util.Map mathoclvars;
6063
static
@@ -551,6 +554,11 @@ public static String getTaggedValue(ASTTerm trm, String str)
551554
ASTTerm.metafeatures.put(lit,mfs);
552555
}
553556

557+
JOptionPane.showMessageDialog(null,
558+
"Looking up: " + str + " of: " + lit +
559+
" in: " + ASTTerm.metafeatures, "",
560+
JOptionPane.INFORMATION_MESSAGE);
561+
554562
// System.out.println("*** " + lit +
555563
// " gets tagged values: " +
556564
// mfs);
@@ -624,6 +632,12 @@ public static String cgtlOperation(String opname, Vector eargs)
624632
return ASTTerm.symbolicLeq(e1,e2);
625633
}
626634

635+
if ("symbolicDeterminant".equals(opname) &&
636+
eargs.size() == 1)
637+
{ ASTTerm m = (ASTTerm) eargs.get(0);
638+
return ASTTerm.symbolicDeterminant(m);
639+
}
640+
627641
if ("expressAsPolynomial".equals(opname) &&
628642
eargs.size() == 2)
629643
{ ASTTerm e1 = (ASTTerm) eargs.get(0);
@@ -5650,6 +5664,25 @@ public static String symbolicEvaluation(ASTTerm e1)
56505664
return ASTTerm.symbolicEvaluation(trm);
56515665
}
56525666

5667+
if (tg.equals("setExpression"))
5668+
{ ASTTerm kind = e1.getTerm(0);
5669+
ASTTerm elems = e1.getTerm(1);
5670+
if ("}".equals(elems.literalForm()))
5671+
{ return kind.literalForm() + "}"; }
5672+
5673+
Vector elemterms = elems.getTerms();
5674+
String res = kind.literalForm();
5675+
5676+
for (int j = 0; j < elemterms.size(); j++)
5677+
{ ASTTerm et = (ASTTerm) elemterms.get(j);
5678+
String eval = ASTTerm.symbolicEvaluation(et);
5679+
res = res + eval;
5680+
if (j < elemterms.size()-1)
5681+
{ res = res + ", "; }
5682+
}
5683+
return res + "}";
5684+
}
5685+
56535686
if (tg.equals("additiveExpression") &&
56545687
n > 2 &&
56555688
"+".equals(e1.getTerm(1) + ""))
@@ -5718,6 +5751,43 @@ else if (ASTTerm.isMathOCLBracketed(e1))
57185751
return a;
57195752
}
57205753

5754+
public static String symbolicDeterminant(ASTTerm m)
5755+
{ // convert m to a Vector of Vector of numbers
5756+
// and apply AuxMath.determinant
5757+
5758+
Vector mrows = ASTTerm.mathOCLsequenceElements(m);
5759+
return "" + AuxMath.determinant(mrows.size(), mrows);
5760+
}
5761+
5762+
public static Vector mathOCLsequenceElements(ASTTerm t)
5763+
{ String tg = t.getTag();
5764+
Vector res = new Vector();
5765+
5766+
if (tg.equals("setExpression"))
5767+
{ ASTTerm elems = t.getTerm(1);
5768+
if ("}".equals(elems.literalForm()))
5769+
{ return new Vector(); }
5770+
5771+
Vector elemterms = elems.getTerms();
5772+
5773+
for (int j = 0; j < elemterms.size(); j++)
5774+
{ ASTTerm et = (ASTTerm) elemterms.get(j);
5775+
String eval = ASTTerm.symbolicEvaluation(et);
5776+
res.add(AuxMath.generalNumericValue(eval));
5777+
} // but for matrix, do recursively
5778+
5779+
return res;
5780+
}
5781+
5782+
Vector trms = t.getTerms();
5783+
if (trms.size() == 1)
5784+
{ ASTTerm elems = (ASTTerm) trms.get(0);
5785+
return ASTTerm.mathOCLsequenceElements(elems);
5786+
}
5787+
5788+
return res;
5789+
}
5790+
57215791
public static String symbolicLess(ASTTerm e1, ASTTerm e2)
57225792
{ String a = ASTTerm.symbolicEvaluation(e1);
57235793
String b = ASTTerm.symbolicEvaluation(e2);

CGCondition.java

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class CGCondition
2020
// e/x means replace x by e
2121
boolean isMatches = false;
2222
// compare variable to stereotype
23+
boolean isWith = false;
2324

2425
public CGCondition()
2526
{ }
@@ -131,6 +132,9 @@ public void setExistential()
131132
public void setUniversal()
132133
{ quantifier = "all"; }
133134

135+
public void setIsWith(boolean w)
136+
{ isWith = w; }
137+
134138
public String toString()
135139
{ String res = variable;
136140

@@ -142,6 +146,8 @@ else if (isSubstitute)
142146
{ res = res + " /"; }
143147
else if (isMatches)
144148
{ res = res + " matches"; }
149+
else if (isWith)
150+
{ res = res + " with"; }
145151

146152
if (positive) { }
147153
else
@@ -227,6 +233,8 @@ public void applyAction(Vector vars, Vector eargs,
227233
// _* all stereo
228234
// sets the stereotype for each term in the _* list
229235

236+
// _i`f with _j uses _j as the parameter _$ in f::
237+
230238
if (isSubstitute || isMatches)
231239
{ return; }
232240

@@ -268,11 +276,11 @@ public void applyAction(Vector vars, Vector eargs,
268276
if (varValue != null)
269277
{ stereo = stereo.replace(rvar,varValue);
270278

271-
/* JOptionPane.showMessageDialog(null,
279+
JOptionPane.showMessageDialog(null,
272280
"Global variable " + rvar + " value is " + stereo, "",
273281
JOptionPane.INFORMATION_MESSAGE);
274282

275-
System.out.println(">--> Replacing global variable " + rvar + " by " + varValue); */
283+
System.out.println(">--> Replacing global variable " + rvar + " by " + varValue);
276284
}
277285
}
278286

@@ -307,6 +315,12 @@ public void applyAction(Vector vars, Vector eargs,
307315
return;
308316
}
309317

318+
if (isWith)
319+
{ ASTTerm.setStereotypeValue("_$", stereo);
320+
JOptionPane.showMessageDialog(null,
321+
"Global variable _$ value is " + stereo, "",
322+
JOptionPane.INFORMATION_MESSAGE);
323+
}
310324

311325
Vector metafs = CGRule.metafeatures(variable);
312326

@@ -341,23 +355,35 @@ else if (obj instanceof ASTTerm)
341355
// if there is a metafeature of variable, apply it:
342356

343357
if (mffeat != null)
344-
{ String repl = CGRule.applyMetafeature(
358+
{ if (cgs.hasRuleset(mffeat))
359+
{ String repl = CGRule.applyMetafeature(
345360
mffeat,ast,cgs,entities);
346361

347-
// System.out.println("***>> Action " + ast + "`" + mffeat + " = " + repl);
362+
// Evaluate ast`mffeat and set its stereo
348363

349-
if (positive && repl != null)
350-
{ ASTTerm.setType(repl,stereo);
351-
ASTTerm.addStereo(repl,stereo);
352-
}
353-
else if (repl != null)
354-
{ ASTTerm.setType(repl,null);
355-
ASTTerm.removeStereo(repl,stereo);
356-
}
357-
else // repl == null; stereotype is mffeat=stereo
364+
JOptionPane.showMessageDialog(null,
365+
"repl: " + repl + " Ruleset mffeat: " + mffeat +
366+
" stereo: " + stereo, "",
367+
JOptionPane.INFORMATION_MESSAGE);
368+
369+
if (isWith) { }
370+
else if (positive && repl != null)
371+
{ ASTTerm.setType(repl,stereo);
372+
ASTTerm.addStereo(repl,stereo);
373+
}
374+
else if (repl != null)
375+
{ ASTTerm.setType(repl,null);
376+
ASTTerm.removeStereo(repl,stereo);
377+
}
378+
}
379+
else // No ruleset, set ast`mffeat=stereo
358380
{ ASTTerm.setTaggedValue(ast, mffeat, stereo);
359-
System.out.println("***>>> Executed action " + ast + "`" + mffeat + " = " + stereo);
360-
repl = varx + "`" + mffeat;
381+
382+
JOptionPane.showMessageDialog(null,
383+
"Executed action " + ast + "`" + mffeat +
384+
" = " + stereo + " Tagged values = " + ASTTerm.metafeatures,
385+
"", JOptionPane.INFORMATION_MESSAGE);
386+
// repl = stereo;
361387
}
362388

363389
/* System.out.println(">>> Executed action " + repl + " (" + positive + ") " + stereo);
@@ -366,7 +392,8 @@ else if (repl != null)
366392
JOptionPane.INFORMATION_MESSAGE); */
367393
}
368394
else
369-
{ if (positive)
395+
{ if (isWith) { }
396+
else if (positive)
370397
{ // ast.addStereotype(stereo);
371398
ASTTerm.addStereo(lit,stereo);
372399
}
@@ -380,10 +407,10 @@ else if (repl != null)
380407
}
381408
}
382409
}
383-
else // global variable
384-
{ /* JOptionPane.showMessageDialog(null,
410+
else // varx is a global variable
411+
{ JOptionPane.showMessageDialog(null,
385412
"Set global variable " + varx + " " + mffeat + " " + stereo, "",
386-
JOptionPane.INFORMATION_MESSAGE); */
413+
JOptionPane.INFORMATION_MESSAGE);
387414

388415
if (mffeat == null || mffeat.length() == 0)
389416
{ ASTTerm.setStereotypeValue(varx,stereo); }

CGRule.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.Vector;
1616
import java.util.regex.Matcher;
1717
import java.util.regex.Pattern;
18+
19+
import javax.swing.*;
1820
import java.io.*;
1921

2022

@@ -24,7 +26,7 @@ public class CGRule
2426
Vector variables; // The _i or _*, _+ in the lhs --
2527
// used to hold matched source elements
2628
Vector rhsVariables = new Vector();
27-
// The additional _i in the rhs --
29+
// The additional _i or _$ in the rhs --
2830
// these are global variables.
2931
Vector metafeatures; // The _i`f in rhs
3032
String lhsop = "";
@@ -45,7 +47,7 @@ public CGRule(Expression lexp, Expression rexp, Vector whens)
4547

4648
if (lvars.containsAll(rvars)) {}
4749
else
48-
{ System.err.println("!! Error: some extra metavariables on RHS of " + lexp + " |--> " + rexp); }
50+
{ System.err.println("! Warning: some extra metavariables on RHS of " + lexp + " |--> " + rexp); }
4951
lhs = lexp + "";
5052
rhs = rexp + "";
5153
variables = lvars;
@@ -62,7 +64,7 @@ public CGRule(Expression lexp, String rgt, Vector whens)
6264
Vector rvariables = metavariables(rgt);
6365
if (variables.containsAll(rvariables)) { }
6466
else
65-
{ System.err.println("!! Error: some extra metavariables on RHS of " + this); }
67+
{ System.err.println("! Warning: some extra metavariables on RHS of " + this); }
6668

6769
conditions = whens;
6870
metafeatures = metafeatures(rhs);
@@ -77,7 +79,7 @@ public CGRule(Expression lexp, String rgt)
7779
Vector rvariables = metavariables(rgt);
7880
if (variables.containsAll(rvariables)) { }
7981
else
80-
{ System.err.println("!! Error: some extra metavariables on RHS of " + this); }
82+
{ System.err.println("! Warning: some extra metavariables on RHS of " + this); }
8183

8284
conditions = new Vector();
8385
metafeatures = metafeatures(rhs);
@@ -91,7 +93,7 @@ public CGRule(Statement lexp, String rgt, Vector whens)
9193
Vector rvariables = metavariables(rgt);
9294
if (variables.containsAll(rvariables)) { }
9395
else
94-
{ System.err.println("!! Error: some extra metavariables on RHS of " + this); }
96+
{ System.err.println("! Warning: some extra metavariables on RHS of " + this); }
9597

9698
conditions = whens;
9799
metafeatures = metafeatures(rhs);
@@ -105,7 +107,7 @@ public CGRule(Statement lexp, String rgt)
105107
Vector rvariables = metavariables(rgt);
106108
if (variables.containsAll(rvariables)) { }
107109
else
108-
{ System.err.println("!! Error: some extra metavariables on RHS of " + this); }
110+
{ System.err.println("! Warning: some extra metavariables on RHS of " + this); }
109111

110112
conditions = new Vector();
111113
metafeatures = metafeatures(rhs);
@@ -119,7 +121,7 @@ public CGRule(Type lexp, String rgt, Vector whens)
119121
Vector rvariables = metavariables(rgt);
120122
if (variables.containsAll(rvariables)) { }
121123
else
122-
{ System.err.println("!! Error: some extra metavariables on RHS of " + this); }
124+
{ System.err.println("! Warning: some extra metavariables on RHS of " + this); }
123125

124126
conditions = whens;
125127
metafeatures = metafeatures(rhs);
@@ -134,7 +136,7 @@ public CGRule(Type lexp, String rgt)
134136
Vector rvariables = metavariables(rgt);
135137
if (variables.containsAll(rvariables)) { }
136138
else
137-
{ System.err.println("!! Error: some extra metavariables on RHS of " + this); }
139+
{ System.err.println("! Warning: some extra metavariables on RHS of " + this); }
138140

139141
conditions = new Vector();
140142
metafeatures = metafeatures(rhs);
@@ -246,7 +248,9 @@ public static Vector metavariables(String str)
246248
if ('_' == c & i + 2 < str.length() &&
247249
Character.isDigit(str.charAt(i+1)) &&
248250
Character.isDigit(str.charAt(i+2)))
249-
{ res.add(c + "" + str.charAt(i+1) + "" + str.charAt(i+2)); }
251+
{ res.add(c + "" + str.charAt(i+1) + "" +
252+
str.charAt(i+2));
253+
}
250254
else if ('_' == c &&
251255
Character.isDigit(str.charAt(i+1)))
252256
{ res.add(c + "" + str.charAt(i+1)); }
@@ -258,6 +262,9 @@ else if ('_' == c &&
258262
if (str.indexOf("_+") > -1)
259263
{ res.add("_+"); }
260264

265+
if (str.indexOf("_$") > -1)
266+
{ res.add("_$"); }
267+
261268
return res;
262269
}
263270

@@ -591,14 +598,19 @@ public static String applyMetafeature(String mffeat,
591598
CGSpec cgs, Vector entities)
592599
{ System.out.println(">***> Applying " + mffeat + " to ASTTerm " + term);
593600
System.out.println();
601+
JOptionPane.showMessageDialog(null,
602+
"Trying to compute: " + mffeat + " of: " + term +
603+
" in: " + ASTTerm.metafeatures, "",
604+
JOptionPane.INFORMATION_MESSAGE);
605+
594606

595607
ASTTerm obj = term;
596608

597609
if (CSTL.hasTemplate(mffeat + ".cstl"))
598610
{ CGSpec template = CSTL.getTemplate(mffeat + ".cstl");
599611

600612
if (template != null)
601-
{ System.out.println(">>> Applying CSTL template " + mffeat + ".cstl to " + term);
613+
{ System.out.println(">>> Applying CSTL script " + mffeat + ".cstl to " + term);
602614

603615
String repl = null;
604616
repl = term.cg(template);
@@ -913,7 +925,8 @@ else if (obj instanceof ASTSymbolTerm)
913925
if (repl != null)
914926
{ return repl; }
915927
else
916-
{ System.out.println(">!!!> no ruleset: " + mffeat);
928+
{ System.out.println(">!!!> cannot apply ruleset: " + mffeat + " to " + term);
929+
917930
if (term.hasMetafeature(mffeat))
918931
{ String replx = term.getMetafeatureValue(mffeat);
919932
if (replx != null)

0 commit comments

Comments
 (0)