Skip to content

Commit f8bf176

Browse files
authored
Improved Python to Java mapping
1 parent 9b7587a commit f8bf176

File tree

7 files changed

+181
-10
lines changed

7 files changed

+181
-10
lines changed

Attribute.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,76 @@ public boolean typeCheck(Vector types, Vector entities)
428428
return true;
429429
}
430430

431+
public boolean typeInference(Vector types, Vector entities,
432+
java.util.Map vtypes)
433+
{ if (initialExpression != null)
434+
{ Vector cntx = new Vector();
435+
if (entity != null)
436+
{ cntx.add(entity); }
437+
Vector env = new Vector();
438+
initialExpression.typeCheck(types,entities,cntx,env);
439+
440+
System.out.println(">> Type of attribute: " + name + " is " + type + "(" + elementType + ")");
441+
442+
if (Type.isVacuousType(type) &&
443+
!Type.isVacuousType(initialExpression.type))
444+
{ type = initialExpression.type;
445+
elementType = initialExpression.elementType;
446+
type.elementType = elementType;
447+
}
448+
449+
System.out.println(">> Type of initialiser: " + initialExpression + " is " + initialExpression.type + "(" + initialExpression.elementType + ")");
450+
451+
if (initialExpression.type == null)
452+
{ System.err.println("!! Invalid initial expression !!");
453+
initialExpression =
454+
Type.defaultInitialValueExpression(type);
455+
}
456+
457+
return true;
458+
}
459+
460+
if (type == null)
461+
{ type = new Type("OclAny", null);
462+
return true;
463+
}
464+
465+
String tname = type + "";
466+
Type t = Type.getTypeFor(tname, types, entities);
467+
if (t == null)
468+
{ System.err.println("!! Warning: null type for attribute " + name);
469+
// type = new Type("OclAny", null);
470+
return true;
471+
}
472+
473+
type = t;
474+
475+
Type et = elementType;
476+
if (elementType != null)
477+
{ String etname = elementType + "";
478+
et = Type.getTypeFor(etname, types, entities);
479+
if (et == null)
480+
{ System.err.println("!! Warning: null element type for attribute " + name);
481+
et = elementType;
482+
type.elementType = elementType;
483+
}
484+
else
485+
{ elementType = et;
486+
type.elementType = et;
487+
}
488+
}
489+
490+
System.out.println(">> Updated type of attribute: " + name + " is " + type + "(" + elementType + ")");
491+
492+
if (initialExpression != null &&
493+
initialExpression.type == null)
494+
{ initialExpression.type = type;
495+
initialExpression.elementType = et;
496+
}
497+
498+
return true;
499+
}
500+
431501
public boolean isMany()
432502
{ return upper == 0; }
433503

BSystemTypes.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,7 +4952,9 @@ public static String generateSubrangeOp() // for Java
49524952
" }\n\n";
49534953

49544954
res = res + " public static List subrange(List l, int i, int j)\n";
4955-
res = res + " { List tmp = new Vector(); \n" +
4955+
res = res + " { List tmp = new Vector(); \n" +
4956+
" if (i < 0) { i = l.size() + i; }\n" +
4957+
" if (j < 0) { j = l.size() + j; }\n" +
49564958
" for (int k = i-1; k < j; k++)\n" +
49574959
" { tmp.add(l.get(k)); } \n" +
49584960
" return tmp; \n" +
@@ -4980,6 +4982,8 @@ public static String generateSubrangeOpJava6() // for Java6
49804982

49814983
res = res + " public static ArrayList subrange(ArrayList l, int i, int j)\n";
49824984
res = res + " { ArrayList tmp = new ArrayList(); \n" +
4985+
" if (i < 0) { i = l.size() + i; }\n" +
4986+
" if (j < 0) { j = l.size() + j; }\n" +
49834987
" for (int k = i-1; k < j; k++)\n" +
49844988
" { tmp.add(l.get(k)); } \n" +
49854989
" return tmp; \n" +
@@ -5007,6 +5011,8 @@ public static String generateSubrangeOpJava7() // for Java7
50075011

50085012
res = res + " public static <T> ArrayList<T> subrange(ArrayList<T> l, int i, int j)\n";
50095013
res = res + " { ArrayList<T> tmp = new ArrayList<T>(); \n" +
5014+
" if (i < 0) { i = l.size() + i; }\n" +
5015+
" if (j < 0) { j = l.size() + j; }\n" +
50105016
" for (int k = i-1; k < j; k++)\n" +
50115017
" { tmp.add(l.get(k)); } \n" +
50125018
" return tmp; \n" +
@@ -5034,6 +5040,8 @@ public static String generateSubrangeOpCSharp() // for CSharp
50345040

50355041
res = res + " public static ArrayList subrange(ArrayList l, int i, int j)\n";
50365042
res = res + " { ArrayList tmp = new ArrayList(); \n" +
5043+
" if (i < 0) { i = l.Count + i; }\n" +
5044+
" if (j < 0) { j = l.Count + j; }\n" +
50375045
" if (i < 1) { i = 1; }\n" +
50385046
" for (int k = i-1; k < j; k++)\n" +
50395047
" { tmp.Add(l[k]); } \n" +
@@ -7694,8 +7702,8 @@ public static String generateAsSetOp()
76947702
" }\n\n";
76957703

76967704
res = res +
7697-
" public static List mapAsSequence(Map m)\n" +
7698-
" { List range = new Vector();\n" +
7705+
" public static Vector mapAsSequence(Map m)\n" +
7706+
" { Vector range = new Vector();\n" +
76997707
" java.util.Set ss = m.entrySet();\n" +
77007708
" for (Object x : ss)\n" +
77017709
" { Map.Entry ee = (Map.Entry) x;\n" +
@@ -7707,8 +7715,8 @@ public static String generateAsSetOp()
77077715
" }\n\n";
77087716

77097717
res = res +
7710-
" public static List mapAsSet(Map m)\n" +
7711-
" { List range = mapAsSequence(m); \n" +
7718+
" public static Vector mapAsSet(Map m)\n" +
7719+
" { Vector range = mapAsSequence(m); \n" +
77127720
" return asSet(range); \n" +
77137721
" }\n\n";
77147722

@@ -7764,7 +7772,7 @@ public static String generateAsSetOpCSharp()
77647772
" }\n\n";
77657773

77667774
return res;
7767-
} // and map
7775+
} // and map as a set
77687776

77697777
public static String refOps()
77707778
{ String res = "";
@@ -9938,6 +9946,25 @@ public static String generateAsSetOpJava6()
99389946
" return res; \n" +
99399947
" }\n\n";
99409948

9949+
res = res +
9950+
" public static ArrayList mapAsSequence(Map m)\n" +
9951+
" { ArrayList range = new ArrayList();\n" +
9952+
" java.util.Set ss = m.entrySet();\n" +
9953+
" for (Object x : ss)\n" +
9954+
" { Map.Entry ee = (Map.Entry) x;\n" +
9955+
" HashMap mx = new HashMap(); \n" +
9956+
" mx.put(ee.getKey(), ee.getValue());\n" +
9957+
" range.add(mx); \n" +
9958+
" } \n" +
9959+
" return range;\n" +
9960+
" }\n\n";
9961+
9962+
res = res +
9963+
" public static HashSet mapAsSet(Map m)\n" +
9964+
" { ArrayList range = mapAsSequence(m); \n" +
9965+
" return asSet(range); \n" +
9966+
" }\n\n";
9967+
99419968
return res;
99429969
}
99439970

@@ -9949,6 +9976,11 @@ public static String generateAsSetOpJava7()
99499976
" return res;\n" +
99509977
" }\n\n";
99519978

9979+
res = res + " public static <S,T> HashSet<HashMap<S,T>> asSet(Map<S,T> m)\n" +
9980+
" { ArrayList<HashMap<S,T>> res = Ocl.asSequence(m);\n" +
9981+
" return Ocl.asSet(res);\n" +
9982+
" }\n\n";
9983+
99529984
res = res + " public static <T> ArrayList<T> asOrderedSet(Collection<T> c)\n" +
99539985
" { ArrayList<T> res = new ArrayList<T>();\n" +
99549986
" for (T x : c)\n" +
@@ -9959,6 +9991,17 @@ public static String generateAsSetOpJava7()
99599991
" return res;\n" +
99609992
" }\n\n";
99619993

9994+
res = res + " public static <S,T> ArrayList<HashMap<S,T>> asSequence(Map<S,T> m)\n" +
9995+
" { Set<Map.Entry<S,T>> ss = m.entrySet();\n" +
9996+
" ArrayList<HashMap<S,T>> res = new ArrayList<HashMap<S,T>>();\n" +
9997+
" for (Map.Entry<S,T> item : ss)\n" +
9998+
" { HashMap<S,T> maplet = new HashMap<S,T>();\n" +
9999+
" maplet.put(item.getKey(), item.getValue()); \n" +
10000+
" res.add(maplet); \n" +
10001+
" } \n" +
10002+
" return res;\n" +
10003+
" }\n";
10004+
996210005
return res;
996310006
}
996410007

BasicExpression.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7855,6 +7855,14 @@ public String queryFormJava7(java.util.Map env, boolean local)
78557855
{ return "new Date()"; }
78567856
}
78577857

7858+
if (data.equals("formattedString") &&
7859+
"StringLib".equals(objectRef + ""))
7860+
{ Expression par = (Expression) parameters.get(0);
7861+
String pqf = par.queryFormJava7(env,local);
7862+
String cmd = "(new Object() { public String call(Object _x) { return " + Expression.formattedString(pqf) + "; } }).call(this)";
7863+
return cmd;
7864+
}
7865+
78587866
if (umlkind == VALUE || umlkind == CONSTANT)
78597867
{ if (data.equals("{}") || data.equals("Set{}"))
78607868
{ return "new HashSet<Object>()"; } // new Set really

Entity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,7 @@ public void typeInferenceAttributes(Vector types, Vector entities, java.util.Map
16291629
for (int i = 0; i < attributes.size(); i++)
16301630
{ Attribute att = (Attribute) attributes.get(i);
16311631
// System.out.println(">> Type-checking " + att + " with " + localtypes + " " + entities);
1632-
att.typeCheck(localtypes,entities);
1632+
att.typeInference(localtypes,entities,vartypes);
16331633
vartypes.put(att.getName(), att.getType());
16341634
}
16351635
}

Expression.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,42 @@ public void setElementType(Type t)
258258

259259
public abstract String toAST();
260260

261+
public static String formattedString(String f)
262+
{ /* s written with {v:fmt} to format v element */
263+
264+
String res = "";
265+
boolean inElement = false;
266+
boolean inFormat = false;
267+
String var = "";
268+
for (int i = 0; i < f.length(); i++)
269+
{ char c = f.charAt(i);
270+
if (inElement)
271+
{ if (':' == c)
272+
{ res = res + "\" + " + var + " + \"";
273+
var = "";
274+
inFormat = true;
275+
}
276+
else if ('}' == c)
277+
{ inElement = false;
278+
inFormat = false;
279+
var = "";
280+
}
281+
else if (inFormat)
282+
{ } // skip the format
283+
else
284+
{ var = var + c; } // var name character
285+
}
286+
else if ('{' == c)
287+
{ inElement = true; }
288+
else
289+
{ res = res + c; } // literal character
290+
}
291+
292+
return res + "";
293+
}
294+
295+
296+
261297
public Vector mutants()
262298
{ Vector res = new Vector();
263299
res.add(this);

Type.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,12 @@ else if (nme.equals("int") || nme.equals("long"))
28682868
return res;
28692869
}
28702870

2871+
public static Expression defaultInitialValueExpression(Type t)
2872+
{ if (t == null)
2873+
{ return new BasicExpression("null"); }
2874+
return t.getDefaultValueExpression();
2875+
}
2876+
28712877
public Expression getDefaultValueExpression()
28722878
{ return getDefaultValueExpression(elementType); }
28732879

UnaryExpression.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,7 +2416,9 @@ else if (type == null)
24162416
{ type = new Type("Sequence",null);
24172417
elementType = argument.elementType;
24182418
if (argument.isMap())
2419-
{ elementType = argument.type; }
2419+
{ elementType = argument.type; }
2420+
// It is a sequence of individual maplets
2421+
24202422
type.setElementType(elementType);
24212423
multiplicity = ModelElement.MANY;
24222424
return res;
@@ -4093,11 +4095,17 @@ else if (argument instanceof UnaryExpression)
40934095
}
40944096

40954097
if (operator.equals("->asSequence"))
4096-
{ return "Set.asSequence(" + qf + ")"; }
4098+
{ if (argument.isMap())
4099+
{ return "Set.mapAsSequence(" + qf + ")"; }
4100+
return "Set.asSequence(" + qf + ")";
4101+
}
40974102
// but maps cannot be converted
40984103

40994104
if (operator.equals("->asSet"))
4100-
{ return "Set.asSet(" + qf + ")"; }
4105+
{ if (argument.isMap())
4106+
{ return "Set.mapAsSet(" + qf + ")"; }
4107+
return "Set.asSet(" + qf + ")";
4108+
}
41014109

41024110
if (operator.equals("->asOrderedSet"))
41034111
{ return "Set.asOrderedSet(" + qf + ")"; }

0 commit comments

Comments
 (0)