Skip to content

Commit 2b5d5bf

Browse files
authored
Added ->selectElements for Python np
1 parent d0a4d9b commit 2b5d5bf

23 files changed

+275
-26
lines changed

ATLFileFilter.class

0 Bytes
Binary file not shown.

BasicExpression.class

305 Bytes
Binary file not shown.

BasicExpression.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,30 @@ else if (objectRef != null)
897897
return res;
898898
}
899899

900+
public Expression transformPythonSelectExpressions()
901+
{ BasicExpression res = (BasicExpression) clone();
902+
if (arrayIndex != null)
903+
{ res.arrayIndex =
904+
arrayIndex.transformPythonSelectExpressions();
905+
}
906+
907+
if (parameters != null)
908+
{ res.parameters = new Vector();
909+
for (int i = 0; i < parameters.size(); i++)
910+
{ if (parameters.get(i) instanceof Expression)
911+
{ Expression par = (Expression) parameters.get(i);
912+
Expression pclone =
913+
par.transformPythonSelectExpressions();
914+
res.parameters.add(pclone);
915+
}
916+
else
917+
{ res.parameters.add(parameters.get(i)); }
918+
}
919+
}
920+
921+
return res;
922+
}
923+
900924
public boolean containsSubexpression(Expression expr)
901925
{ if (arrayIndex != null)
902926
{ if (arrayIndex.containsSubexpression(expr))

BinaryExpression.class

595 Bytes
Binary file not shown.

BinaryExpression.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,33 @@ public Expression determinate()
291291
// return new BasicExpression("false",0);
292292
}
293293

294+
public Expression transformPythonSelectExpressions()
295+
{
296+
// s->selectRows(P) is transformed to
297+
// s->select( $x | P[$x/s] )
298+
// s->restrict(P) for Sequence s is transformed to
299+
// s->select( _x | P[_x/s] )
300+
// but actually nested.
301+
302+
if ("->restrict".equals(operator) && left.isSequence())
303+
{ String fvar = left + "";
304+
Expression avar =
305+
BasicExpression.newVariableBasicExpression("$x");
306+
Expression newright =
307+
right.substituteEq(fvar, avar);
308+
Expression newleft =
309+
new BinaryExpression(":", avar, left);
310+
return new BinaryExpression("|",
311+
newleft, newright);
312+
}
313+
314+
BinaryExpression res = (BinaryExpression) clone();
315+
res.left = left.transformPythonSelectExpressions();
316+
res.right = right.transformPythonSelectExpressions();
317+
return res;
318+
}
319+
320+
294321
public Expression firstConjunct()
295322
{ if ("&".equals(operator))
296323
{ return left.firstConjunct(); }
@@ -3677,7 +3704,7 @@ public int typeCheck(Vector sms)
36773704
else if (operator.equals("&") || operator.equals("or"))
36783705
{ return typeCheckAnd(sms); }
36793706
else
3680-
{ System.out.println("Unexpected operator: " + operator);
3707+
{ System.out.println("!! ERROR: Unexpected operator: " + operator);
36813708
modality = ERROR;
36823709
return ERROR;
36833710
}
@@ -3731,6 +3758,16 @@ public boolean typeInference(final Vector types,
37313758
return true;
37323759
}
37333760

3761+
if ("->restrict".equals(operator) && left.isSequence())
3762+
{ JOptionPane.showInputDialog("Converting Python expression " + this);
3763+
BinaryExpression pyexpr =
3764+
(BinaryExpression) transformPythonSelectExpressions();
3765+
operator = pyexpr.operator;
3766+
left = pyexpr.left;
3767+
right = pyexpr.right;
3768+
return true;
3769+
}
3770+
37343771
if (operator.equals("->exists") ||
37353772
operator.equals("->exists1") ||
37363773
"->existsLC".equals(operator) ||
@@ -5122,6 +5159,16 @@ public boolean typeCheck(final Vector types,
51225159
return true;
51235160
}
51245161

5162+
if ("->restrict".equals(operator) && left.isSequence())
5163+
{ BinaryExpression pyexpr =
5164+
(BinaryExpression) transformPythonSelectExpressions();
5165+
JOptionPane.showInputDialog("Converting Python expression " + this + " to " + pyexpr);
5166+
operator = pyexpr.operator;
5167+
left = pyexpr.left;
5168+
right = pyexpr.right;
5169+
return true;
5170+
}
5171+
51255172
if (operator.equals("->iterate") &&
51265173
iteratorVariable != null &&
51275174
accumulator != null)

Compiler2.class

1.06 KB
Binary file not shown.

Compiler2.java

Lines changed: 131 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -713,20 +713,20 @@ public void nospacelexicalanalysis(String str)
713713
{ char c = str.charAt(i);
714714
if (in == INUNKNOWN)
715715
{ if (isSymbolCharacter(c))
716-
{ sb = new StringBuffer(); // start new buffer for the symbol
716+
{ sb = new StringBuffer(); // new buffer for symbol
717717
lexicals.addElement(sb);
718718
in = INSYMBOL;
719719
sb.append(c);
720720
previous = c;
721721
}
722722
else if (isBasicExpCharacter(c))
723-
{ sb = new StringBuffer(); // start new buffer for the expression
723+
{ sb = new StringBuffer(); // new buffer for expression
724724
lexicals.addElement(sb);
725725
in = INBASICEXP;
726726
sb.append(c);
727727
}
728728
else if (isStringDelimiter(c))
729-
{ sb = new StringBuffer(); // start new buffer for the string
729+
{ sb = new StringBuffer(); // new buffer for the string
730730
lexicals.addElement(sb);
731731
in = INSTRING;
732732
sb.append('"');
@@ -742,18 +742,19 @@ else if (c == ' ' || c == '\n' || c == '\t' || c == '\r')
742742
in = INBASICEXP;
743743
}
744744
else
745-
{ System.err.println("Unrecognised literal: " + c); }
745+
{ System.err.println("!! Unrecognised UML/OCL token: " + c); }
746746
}
747747
}
748748
else if (in == INBASICEXP)
749-
{ if (isBasicExpCharacter(c) || c == '"') // Why allow " in a basic exp???
750-
{ sb.append(c); } // carry on adding to current basic exp
749+
{ if (isBasicExpCharacter(c) ||
750+
c == '"') // Why allow " in a basic exp???
751+
{ sb.append(c); } // carry on adding to current basic exp
751752
else if (c == '_')
752753
{ // System.out.println("Metavariable symbol: " + c);
753754
sb.append(c);
754755
}
755756
else if (isSymbolCharacter(c))
756-
{ sb = new StringBuffer(); // start new buffer for the symbol
757+
{ sb = new StringBuffer(); // new buffer for symbol
757758
lexicals.addElement(sb);
758759
in = INSYMBOL;
759760
sb.append(c);
@@ -765,27 +766,27 @@ else if (c == ' ' || c == '\n' || c == '\t' || c == '\r')
765766
{ sb = new StringBuffer(); // unrecognised lexical
766767
lexicals.addElement(sb);
767768
in = INUNKNOWN;
768-
System.err.println("Unrecognised literal in expression: " + c);
769+
System.err.println("!! Unrecognised token in expression: " + c);
769770
sb.append(c);
770771
}
771772
}
772773
else if (in == INSYMBOL)
773774
{ if (isStringDelimiter(c))
774-
{ sb = new StringBuffer(); // start new buffer for the string
775+
{ sb = new StringBuffer(); // new buffer for the string
775776
lexicals.addElement(sb);
776777
in = INSTRING;
777778
sb.append('"');
778779
}
779780
else if (c == '(' || c == ')')
780-
{ sb = new StringBuffer(); // start new buffer for the new symbol
781+
{ sb = new StringBuffer(); // new buffer for new symbol
781782
lexicals.addElement(sb);
782783
in = INSYMBOL;
783784
previous = c;
784785
sb.append(c);
785786
}
786787
else if (c == '_')
787788
{ // System.out.println("Metavariable symbol: " + c);
788-
sb = new StringBuffer(); // start new buffer for the string
789+
sb = new StringBuffer(); // new buffer for the string
789790
lexicals.addElement(sb);
790791
in = INBASICEXP;
791792
sb.append(c);
@@ -1091,6 +1092,50 @@ else if (c == ' ' || c == '\n' ||
10911092
}
10921093
}
10931094

1095+
public void filterLexicals()
1096+
{ // removes invalid sequences such as ; ; and ; )
1097+
1098+
Vector newlexicals = new Vector();
1099+
boolean previousSemi = false; // last token was ';'
1100+
StringBuffer semi = new StringBuffer();
1101+
semi.append(";");
1102+
1103+
for (int i = 0; i < lexicals.size(); i++)
1104+
{ StringBuffer sb = (StringBuffer) lexicals.get(i);
1105+
String ss = sb + "";
1106+
if (";".equals(ss))
1107+
{ if (previousSemi) { } // skip the token
1108+
else
1109+
{ previousSemi = true; }
1110+
}
1111+
else if (")".equals(ss))
1112+
{ if (previousSemi)
1113+
{ previousSemi = false;
1114+
newlexicals.add(sb);
1115+
}
1116+
else
1117+
{ newlexicals.add(sb); }
1118+
}
1119+
else if ("else".equals(ss))
1120+
{ if (previousSemi)
1121+
{ previousSemi = false;
1122+
newlexicals.add(sb);
1123+
}
1124+
else
1125+
{ newlexicals.add(sb); }
1126+
}
1127+
else if (previousSemi)
1128+
{ newlexicals.add(semi);
1129+
previousSemi = false;
1130+
newlexicals.add(sb);
1131+
}
1132+
else
1133+
{ newlexicals.add(sb); }
1134+
}
1135+
1136+
lexicals = newlexicals;
1137+
}
1138+
10941139
public void nospacelexicalanalysisAST(String str)
10951140
{ int in = INUNKNOWN;
10961141
char previous = ' ';
@@ -2771,6 +2816,13 @@ public BinaryExpression parse_implies_expression(int bcount, int pstart,
27712816
}
27722817
}
27732818

2819+
2820+
public void showLexicals()
2821+
{ int sze = lexicals.size();
2822+
String lexs = showLexicals(0,sze-1);
2823+
System.out.println(lexs);
2824+
}
2825+
27742826
public String showLexicals(int st, int en)
27752827
{ String res = "";
27762828
for (int i = st; i <= en; i++)
@@ -3408,10 +3460,19 @@ else if (i + 3 <= pend) // && "(".equals(lexicals.get(i+2) + "") &&
34083460
{ // System.out.println(">> Trying to parse at " + ss2);
34093461
Expression ee1 = parse_expression(bc+1,i+3,pend-1,entities,types);
34103462
if (ee1 == null) { continue; }
3411-
Expression ee2 = parse_factor_expression(bc,pstart,i-1,entities,types);
3463+
3464+
Expression ee2 =
3465+
parse_factor_expression(bc,pstart,i-1,
3466+
entities,types);
3467+
34123468
if (ee2 == null) { continue; }
3469+
34133470
if ("selectRows".equals(ss2))
3414-
{ // convert it
3471+
{ // convert it to
3472+
// MathLib.dataTableFromRows(
3473+
// MathLib.rowsOfDataTable(ee2)->select(
3474+
// $row | ee1[$row/ee2] ) )
3475+
34153476
BasicExpression realarg =
34163477
BasicExpression.newStaticCallBasicExpression(
34173478
"rowsOfDataTable", "MathLib", ee2);
@@ -3429,8 +3490,32 @@ else if (i + 3 <= pend) // && "(".equals(lexicals.get(i+2) + "") &&
34293490
"dataTableFromRows", "MathLib", be);
34303491
return res;
34313492
}
3493+
3494+
if ("selectElements".equals(ss2))
3495+
{ // convert it to
3496+
// MatrixLib.selectElements(ee2,
3497+
// lambda $x : double in ee1[$x/ee2])
3498+
3499+
BasicExpression svar =
3500+
BasicExpression.newVariableBasicExpression("$x");
3501+
svar.isEvent = false;
3502+
Type dtype = new Type("double", null);
3503+
Expression subee1 =
3504+
ee1.substituteEq("" + ee2, svar);
3505+
Expression func =
3506+
UnaryExpression.newLambdaUnaryExpression(
3507+
"$x", dtype, subee1);
3508+
Vector pars = new Vector();
3509+
pars.add(ee2);
3510+
pars.add(func);
3511+
BasicExpression res =
3512+
BasicExpression.newStaticCallBasicExpression(
3513+
"selectElements", "MatrixLib", pars);
3514+
return res;
3515+
}
34323516

3433-
BinaryExpression be = new BinaryExpression(ss+ss2,ee2,ee1);
3517+
BinaryExpression be =
3518+
new BinaryExpression(ss+ss2,ee2,ee1);
34343519
// System.out.println(">>> Parsed binary -> expression: " + be);
34353520
return be;
34363521
}
@@ -3648,7 +3733,8 @@ public Expression parse_basic_expression(int bc,
36483733
// return null;
36493734
}
36503735

3651-
if (pstart < pend && "]".equals(lexicals.get(pend) + "") &&
3736+
if (pstart < pend &&
3737+
"]".equals(lexicals.get(pend) + "") &&
36523738
"[".equals(lexicals.get(pstart+1) + ""))
36533739
{ // iden[indexes]
36543740

@@ -11257,7 +11343,7 @@ public NLPSentence parseRoot(int st, int en)
1125711343
{ String tag = "" + lexicals.get(st+1);
1125811344
NLPSentence sent = parseSentence(st+2,en-1);
1125911345
if (sent == null)
11260-
{ System.err.println("!! Not a sentence: " + showLexicals(st+2,en-1));
11346+
{ System.err.println("!! Error: Not a sentence: " + showLexicals(st+2,en-1));
1126111347
return null;
1126211348
}
1126311349
return sent;
@@ -11299,10 +11385,36 @@ public static void main(String[] args)
1129911385

1130011386
// c.nospacelexicalanalysis("Map{ \"Name\" |-> Sequence{\"Braund, Mr. Owen Harris\"}->union(Sequence{\"Allen, Mr. William Henry\"}->union(Sequence{ \"Bonnell, Miss. Elizabeth\" })) }->union(Map{ \"Age\" |-> Sequence{22}->union(Sequence{35}->union(Sequence{ 58 })) }->union(Map{ \"Sex\" |-> Sequence{\"male\"}->union(Sequence{\"male\"}->union(Sequence{ \"female\" })) }->union(Map{ \"Fare\" |-> Sequence{102.0}->union(Sequence{99.0}->union(Sequence{ 250.0 })) }) ) )");
1130111387

11302-
c.nospacelexicalanalysis("table->selectRows(table->at(p) > v)");
11303-
Expression zz = c.parseExpression();
11388+
11389+
11390+
c.nospacelexicalanalysis("table->restrict(table > v)");
11391+
BinaryExpression zz = (BinaryExpression) c.parseExpression();
11392+
11393+
Expression zleft = zz.getLeft();
11394+
zleft.setType(new Type("Sequence", null));
11395+
11396+
System.out.println(zz);
11397+
11398+
Expression yy = zz.transformPythonSelectExpressions();
11399+
11400+
System.out.println(yy);
11401+
11402+
/*
11403+
c.nospacelexicalanalysis("execute (OclFile[\"system.in\"]).println(x)");
1130411404
11305-
System.out.println(zz);
11405+
Statement stat = c.parseStatement();
11406+
11407+
System.out.println(stat);
11408+
11409+
c.showLexicals(); */
11410+
11411+
// c.filterLexicals();
11412+
11413+
// c.showLexicals();
11414+
11415+
// stat = c.parseStatement();
11416+
11417+
// System.out.println(stat);
1130611418

1130711419
/* zz.typeCheck(new Vector(), new Vector(), new Vector(), new Vector());
1130811420

ConditionalExpression.class

130 Bytes
Binary file not shown.

ConditionalExpression.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public Expression getElse()
5454
public Vector getParameters()
5555
{ return new Vector(); }
5656

57+
public Expression transformPythonSelectExpressions()
58+
{ Expression tqf = test.transformPythonSelectExpressions();
59+
Expression lqf = ifExp.transformPythonSelectExpressions();
60+
Expression rqf = elseExp.transformPythonSelectExpressions();
61+
return new ConditionalExpression(tqf, lqf, rqf);
62+
}
63+
5764
public String queryForm(java.util.Map env, boolean local)
5865
{ String tqf = test.queryForm(env, local);
5966
String lqf = ifExp.queryForm(env, local);

ETLFileFilter.class

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)