Skip to content

Commit 573f0f1

Browse files
authored
Updated CGBE
1 parent 5a78718 commit 573f0f1

11 files changed

+548
-25
lines changed

ASTCompositeTerm.java

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class ASTCompositeTerm extends ASTTerm
3434
public static Type doubleType = new Type("double", null);
3535
public static Type voidType = new Type("void", null);
3636

37+
public static Statement skipStatement =
38+
new InvocationStatement("skip");
3739

3840
public ASTCompositeTerm(String t)
3941
{ tag = t;
@@ -5612,6 +5614,83 @@ public Statement cfunctioncallPresideeffect(String fname,
56125614
sqstat.addStatement(forstat);
56135615
return sqstat;
56145616
}
5617+
else if ("strtok".equals(fname) && args.size() == 2)
5618+
{ // if s /= "" then _tok_string := s else skip ;
5619+
// if _tok_string->hasMatch(ct)
5620+
// then tok_token := _tok_string->firstMatch(ct);
5621+
// _tok_ind := _tok_string->indexOf(_tok_token) + _tok_token.size;
5622+
// _tok_string := _tok_string.substring(_tok_ind)
5623+
// else _tok_token := ""; _tok_ind := 0;
5624+
// _tok_string := ""
5625+
5626+
Expression s = (Expression) args.get(0);
5627+
Expression ct = (Expression) args.get(1);
5628+
s.setType(new Type("String", null));
5629+
ct.setType(new Type("String", null));
5630+
5631+
Expression indexexpr =
5632+
BasicExpression.newVariableBasicExpression("_tok_ind");
5633+
indexexpr.setType(new Type("int", null));
5634+
Expression tokenexpr =
5635+
BasicExpression.newVariableBasicExpression(
5636+
"_tok_token");
5637+
tokenexpr.setType(new Type("String", null));
5638+
Expression tokstringexpr =
5639+
BasicExpression.newVariableBasicExpression(
5640+
"_tok_string");
5641+
tokstringexpr.setType(new Type("String", null));
5642+
5643+
Expression nonempty =
5644+
new BinaryExpression("/=",s,emptyStringExpression);
5645+
nonempty.setType(new Type("boolean", null));
5646+
AssignStatement assigns =
5647+
new AssignStatement(tokstringexpr, s);
5648+
ConditionalStatement cond1 =
5649+
new ConditionalStatement(
5650+
nonempty,assigns,skipStatement);
5651+
5652+
SequenceStatement stat0 = new SequenceStatement();
5653+
AssignStatement asgn1 =
5654+
new AssignStatement(tokenexpr,
5655+
new BinaryExpression("->firstMatch",
5656+
tokstringexpr, ct));
5657+
stat0.addStatement(asgn1);
5658+
Expression indof =
5659+
new BinaryExpression("->indexOf", tokstringexpr, ct);
5660+
Expression sizetok =
5661+
new UnaryExpression("->size", tokenexpr);
5662+
AssignStatement asgn2 =
5663+
new AssignStatement(indexexpr,
5664+
new BinaryExpression("+", indof, sizetok));
5665+
stat0.addStatement(asgn2);
5666+
Expression substr =
5667+
BasicExpression.newFunctionBasicExpression("substring",
5668+
tokstringexpr, indexexpr);
5669+
AssignStatement asgn3 =
5670+
new AssignStatement(tokstringexpr, substr);
5671+
stat0.addStatement(asgn3);
5672+
5673+
SequenceStatement stat1 = new SequenceStatement();
5674+
AssignStatement asgn4 =
5675+
new AssignStatement(tokenexpr, emptyStringExpression);
5676+
stat1.addStatement(asgn4);
5677+
AssignStatement asgn5 =
5678+
new AssignStatement(indexexpr, zeroExpression);
5679+
stat1.addStatement(asgn5);
5680+
AssignStatement asgn6 =
5681+
new AssignStatement(
5682+
tokstringexpr, emptyStringExpression);
5683+
stat1.addStatement(asgn6);
5684+
5685+
Expression test =
5686+
new BinaryExpression("->hasMatch", tokstringexpr, ct);
5687+
ConditionalStatement cond2 =
5688+
new ConditionalStatement(test,stat0,stat1);
5689+
SequenceStatement res = new SequenceStatement();
5690+
res.addStatement(cond1);
5691+
res.addStatement(cond2);
5692+
return res;
5693+
}
56155694

56165695
return null;
56175696
}
@@ -5629,7 +5708,7 @@ public Statement cfunctioncallUpdateForm(String fname,
56295708
Expression arg1 = (Expression) args.get(1);
56305709
arg1.setType(new Type("String", null));
56315710
return new AssignStatement(arg0, arg1);
5632-
} // and side-effect
5711+
}
56335712
else if ("strncpy".equals(fname) && args.size() == 3)
56345713
{ Expression arg0 = (Expression) args.get(0);
56355714
Expression arg1 = (Expression) args.get(1);
@@ -5642,7 +5721,7 @@ else if ("strncpy".equals(fname) && args.size() == 3)
56425721
BasicExpression.newFunctionBasicExpression("subrange", arg1, pars);
56435722
fbe.setType(new Type("String", null));
56445723
return new AssignStatement(arg0, fbe);
5645-
} // and side-effect
5724+
}
56465725
else if (("memcpy".equals(fname) ||
56475726
"memmove".equals(fname)) && args.size() == 3)
56485727
{ // for i : 0..argn-1
@@ -6950,6 +7029,13 @@ else if ("strncmp".equals(fname) && args.size() == 3)
69507029
res.setType(new Type("int", null));
69517030
return res;
69527031
}
7032+
else if ("strtok".equals(fname) && args.size() == 2)
7033+
{ Expression tokenexpr =
7034+
BasicExpression.newVariableBasicExpression(
7035+
"_tok_token");
7036+
tokenexpr.setType(new Type("String", null));
7037+
return tokenexpr;
7038+
}
69537039
else if ("memcmp".equals(fname) && args.size() == 3)
69547040
{ // arg1.subrange(1,n)->compareTo(arg2.subrange(1,n))
69557041
Expression arg1 = (Expression) args.get(0);

ASTTerm.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,33 @@ public static boolean sameNonSymbolArity(ASTTerm[] xs, ASTTerm[] ys)
12911291
return false;
12921292
}
12931293

1294+
public static boolean lowerNonSymbolArity(ASTTerm[] xs, ASTTerm[] ys)
1295+
{ // Is number of non-symbols in ys[i].terms <=
1296+
// number of non-symbols in xs[i].terms?
1297+
1298+
if (ys.length > 1 && xs.length == ys.length)
1299+
{ for (int i = 0; i < xs.length; i++)
1300+
{ ASTTerm xx = xs[i];
1301+
ASTTerm yvect = ys[i];
1302+
1303+
if (xx == null || yvect == null)
1304+
{ return false; }
1305+
1306+
int n = xx.nonSymbolArity();
1307+
System.out.println(">***> Non-symbol arity of " + xx + " = " + n);
1308+
1309+
int m = yvect.nonSymbolArity();
1310+
System.out.println(">***> Non-symbol arity of " + yvect + " = " + m);
1311+
1312+
if (n < m)
1313+
{ return false; }
1314+
}
1315+
return true;
1316+
}
1317+
return false;
1318+
}
1319+
1320+
12941321
/*
12951322
public static boolean embeddedTrees(ASTTerm[] xs, ASTTerm[] ys, ModelSpecification mod)
12961323
{ // Is each ys[i] = (tag xs[i]' ..terms..) for same tag?
@@ -1686,6 +1713,39 @@ public static Vector entityMatchingsFromASTs(Vector sasts,
16861713

16871714
Vector ems = new Vector();
16881715

1716+
for (int i = 0; i < sasts.size() && i < tasts.size(); i++)
1717+
{ ASTTerm s = (ASTTerm) sasts.get(i);
1718+
ASTTerm t = (ASTTerm) tasts.get(i);
1719+
1720+
int n = s.arity();
1721+
int m = t.arity();
1722+
1723+
if (n > 0 && m > 0)
1724+
{ String sentname = s.getTag() + "_" + n;
1725+
Entity sent =
1726+
(Entity) ModelElement.lookupByName(sentname,es);
1727+
String tentname = t.getTag() + "$T_" + m;
1728+
Entity tent =
1729+
(Entity) ModelElement.lookupByName(tentname,es);
1730+
if (sent != null && tent != null)
1731+
{ EntityMatching em =
1732+
new EntityMatching(sent,tent);
1733+
if (ems.contains(em)) { }
1734+
else
1735+
{ ems.add(em); }
1736+
}
1737+
}
1738+
}
1739+
return ems;
1740+
}
1741+
1742+
public static Vector deepEntityMatchingsFromASTs(Vector sasts,
1743+
Vector tasts, Vector es)
1744+
{ // For corresponding s, t create entity matching
1745+
// s.tag_s.arity |--> t.tag$T_t.arity.
1746+
1747+
Vector ems = new Vector();
1748+
16891749
for (int i = 0; i < sasts.size() && i < tasts.size(); i++)
16901750
{ ASTTerm s = (ASTTerm) sasts.get(i);
16911751
ASTTerm t = (ASTTerm) tasts.get(i);
@@ -1722,6 +1782,47 @@ public static void modelSpecificationFromASTs(Vector sasts,
17221782
// t_inst.ast = t
17231783
// s_inst |-> t_inst
17241784

1785+
for (int i = 0; i < sasts.size() && i < tasts.size(); i++)
1786+
{ ASTTerm s = (ASTTerm) sasts.get(i);
1787+
ASTTerm t = (ASTTerm) tasts.get(i);
1788+
1789+
int n = s.arity();
1790+
int m = t.arity();
1791+
1792+
if (n > 0 && m > 0)
1793+
{ String sentname = s.getTag() + "_" + n;
1794+
String tentname = t.getTag() + "$T_" + m;
1795+
String sinst = sentname.toLowerCase() + "_" + i;
1796+
String tinst = tentname.toLowerCase() + "_" + i;
1797+
Entity sent =
1798+
(Entity) ModelElement.lookupByName(sentname,es);
1799+
ObjectSpecification sobj =
1800+
new ObjectSpecification(sinst,sentname);
1801+
sobj.setEntity(sent);
1802+
sobj.setValue("ast", s);
1803+
Entity tent =
1804+
(Entity) ModelElement.lookupByName(tentname,es);
1805+
ObjectSpecification tobj =
1806+
new ObjectSpecification(tinst,tentname);
1807+
tobj.setEntity(tent);
1808+
tobj.setValue("ast", t);
1809+
mod.addObject(sobj);
1810+
mod.addObject(tobj);
1811+
mod.addCorrespondence(sobj,tobj);
1812+
}
1813+
}
1814+
}
1815+
1816+
public static void deepModelSpecificationFromASTs(Vector sasts,
1817+
Vector tasts, Vector es,
1818+
ModelSpecification mod)
1819+
{ // For corresponding s, t create instances
1820+
// s_inst : s.tag_s.arity
1821+
// s_inst.ast = s
1822+
// t_inst : t.tag$T_t.arity
1823+
// t_inst.ast = t
1824+
// s_inst |-> t_inst
1825+
17251826
for (int i = 0; i < sasts.size() && i < tasts.size(); i++)
17261827
{ ASTTerm s = (ASTTerm) sasts.get(i);
17271828
ASTTerm t = (ASTTerm) tasts.get(i);

AttributeMatching.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public String toCSTL(String category, Expression cond,
153153
{ String rulelhs = ((BasicExpression) srcvalue).toCSTL();
154154
BasicExpression rbe = (BasicExpression) trgvalue;
155155

156-
System.out.println(">>> Mapping: " + this + " " + rbe.isFunctionApplication());
156+
// System.out.println(">>> Mapping: " + this + " " + rbe.isFunctionApplication());
157157

158158
if (rbe.isFunctionApplication())
159159
{ String func = rbe.getAppliedFunction();
@@ -166,8 +166,9 @@ public String toCSTL(String category, Expression cond,
166166
} // It is simply _1 |-->_1`func
167167

168168
String rulerhs = ((BasicExpression) trgvalue).toLiteralCSTL();
169-
CGRule rle = new CGRule(rulelhs, rulerhs);
170-
if (cond != null)
169+
CGRule rle = new CGRule(rulelhs, rulerhs);
170+
Vector vars = rle.getVariables();
171+
if (cond != null && vars.size() > 0)
171172
{ CGCondition cc = new CGCondition(cond);
172173
rle.addCondition(cc);
173174
}

EntityMatching.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4462,6 +4462,9 @@ public void checkModelConditions(ModelSpecification mod, ModelMatching mm, Vecto
44624462
// checks that the condition of rule is valid, or if
44634463
// no condition, creates a valid condition.
44644464

4465+
if (realsrc == null || realtrg == null)
4466+
{ return; }
4467+
44654468
String srcname = realsrc.getName();
44664469
String trgname = realtrg.getName();
44674470

KM3Editor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import javax.swing.text.*;
1515
import javax.swing.event.*;
1616

17-
/* K. Lano 2010-2021
17+
/* K. Lano 2010-2022
1818
1919
Adapted from Oracle example of JTextPane
2020
@@ -102,7 +102,7 @@ public KM3Editor(UCDArea parent, Vector ents, Vector ucs)
102102
JScrollPane scrollPane = new JScrollPane(textPane);
103103
scrollPane.setPreferredSize(new Dimension(100, 300));
104104

105-
messageArea = new JTextArea(10, 50);
105+
messageArea = new JTextArea(20, 35);
106106
messageArea.setEditable(false);
107107
JScrollPane scrollPaneForLog = new JScrollPane(messageArea);
108108

ModelMatching.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public String toString()
153153
return res;
154154
}
155155

156+
157+
156158
public String toCSTL(CGSpec cg)
157159
{ String res = "\n";
158160
/* for (int i = 0; i < helpers.size(); i++)
@@ -167,9 +169,16 @@ public String toCSTL(CGSpec cg)
167169
usedFunctions.addAll(em.usesCSTLfunctions());
168170
}
169171

172+
173+
for (int i = 0; i < typematches.size(); i++)
174+
{ TypeMatching tm = (TypeMatching) typematches.get(i);
175+
if (usedFunctions.contains(tm.getName()))
176+
{ usedFunctions.addAll(tm.usesCSTLfunctions()); }
177+
} // *only* include them if used in some rule.
178+
170179
System.out.println(">>> Used functions are: " +
171180
usedFunctions);
172-
181+
173182
for (int i = 0; i < typematches.size(); i++)
174183
{ TypeMatching tm = (TypeMatching) typematches.get(i);
175184
if (usedFunctions.contains(tm.getName()))

0 commit comments

Comments
 (0)