| |
---|
| | import models.Node; |
---|
| | import models.algebra.Expression; |
---|
| | import models.algebra.InvalidMessage; |
---|
| | import models.algebra.ParameterizedIdentifierIsFutureWork; |
---|
| | import models.algebra.Symbol; |
---|
| | import models.algebra.Term; |
---|
| | import models.algebra.Type; |
---|
| | import models.algebra.UnificationFailed; |
---|
| | import models.algebra.ValueUndefined; |
---|
| | import models.algebra.Variable; |
---|
| | import models.dataConstraintModel.ChannelGenerator; |
---|
| | import models.dataConstraintModel.ChannelMember; |
---|
| | import models.dataConstraintModel.DataConstraintModel; |
---|
| | import models.dataConstraintModel.IdentifierTemplate; |
---|
| | import models.dataFlowModel.DataFlowModel; |
---|
| | import models.dataFlowModel.DataflowChannelGenerator; |
---|
| | import models.dataFlowModel.DataflowChannelGenerator.IResourceStateAccessor; |
---|
| |
---|
| | import models.dataFlowModel.StoreAttribute; |
---|
| | |
---|
| | public class JavaMethodBodyGenerator { |
---|
| | public static ArrayList<CompilationUnit> doGenerate(ResourceDependencyGraph graph, DataFlowModel model, ArrayList<CompilationUnit> codes) { |
---|
| | Symbol floor = model.getSymbol("floor"); |
---|
| | Symbol.Memento floorMem = null; |
---|
| | if (floor != null) { |
---|
| | floorMem = floor.createMemento(); |
---|
| | floor.setImplName("(int)Math.floor"); |
---|
| | floor.setImplOperatorType(Symbol.Type.PREFIX); |
---|
| | } |
---|
| | Symbol sum = model.getSymbol("sum"); |
---|
| | Symbol.Memento sumMem = null; |
---|
| | if (sum != null) { |
---|
| | sumMem = sum.createMemento(); |
---|
| | final int[] count = new int[] {0}; |
---|
| | sum.setGenerator(new Symbol.IImplGenerator() { |
---|
| | @Override |
---|
| | public String generate(Type type, String[] children, String[] sideEffects) { |
---|
| | String compType = "Integer"; |
---|
| | if (type != null) { |
---|
| | String interfaceType = type.getInterfaceTypeName(); |
---|
| | if (interfaceType.contains("<")) { |
---|
| | compType = interfaceType.substring(interfaceType.indexOf("<") + 1, interfaceType.lastIndexOf(">")); |
---|
| | } |
---|
| | } |
---|
| | count[0]++; |
---|
| | String impl = compType + " " + "temp_sum" + count[0] + " = 0;\n"; |
---|
| | impl += "for (" + compType + " x: " + children[0] + ") {\n"; |
---|
| | impl += "\t" + "temp_sum" + count[0] + " += x;\n"; |
---|
| | impl += "}\n"; |
---|
| | sideEffects[0] = sideEffects[0] + impl; |
---|
| | return "temp_sum" + count[0]; |
---|
| | } |
---|
| | }); |
---|
| | sum.setImplOperatorType(Symbol.Type.GENERATIVE); |
---|
| | sum.setSignature(new Type[] {DataConstraintModel.typeInt, DataConstraintModel.typeList}); |
---|
| | // sum.setImplName("stream().mapToInt(x->x).sum"); |
---|
| | // sum.setImplOperatorType(Symbol.Type.METHOD); |
---|
| | } |
---|
| | Symbol merge = model.getSymbol("merge"); |
---|
| | Symbol.Memento mergeMem = null; |
---|
| | if (merge != null) { |
---|
| | mergeMem = merge.createMemento(); |
---|
| | merge.setArity(2); |
---|
| | final int[] count = new int[] {0}; |
---|
| | merge.setGenerator(new Symbol.IImplGenerator() { |
---|
| | @Override |
---|
| | public String generate(Type type, String[] childrenImpl, String[] sideEffects) { |
---|
| | String implType = "Arrayist<>"; |
---|
| | String interfaceType = "List<Integer>"; |
---|
| | String compType = "Integer"; |
---|
| | if (type != null) { |
---|
| | implType = type.getImplementationTypeName(); |
---|
| | interfaceType = type.getInterfaceTypeName(); |
---|
| | if (interfaceType.contains("<")) { |
---|
| | compType = interfaceType.substring(interfaceType.indexOf("<") + 1, interfaceType.lastIndexOf(">")); |
---|
| | } |
---|
| | } |
---|
| | String idxGetter = ""; |
---|
| | if (compType.startsWith("Map.Entry")) { |
---|
| | idxGetter = ".getKey()"; |
---|
| | } |
---|
| | count[0]++; |
---|
| | String impl = ""; |
---|
| | impl += "" + interfaceType + " temp_l" + count[0] + " = new " + implType + "();\n"; |
---|
| | impl += "Iterator<" + compType + "> i1 = " + childrenImpl[0] + ".iterator();\n"; |
---|
| | impl += "Iterator<" + compType + "> i2 = " + childrenImpl[1] + ".iterator();\n"; |
---|
| | impl += compType + " t1 = null;\n"; |
---|
| | impl += compType + " t2 = null;\n"; |
---|
| | impl += "while (i1.hasNext() || i2.hasNext()) {\n"; |
---|
| | impl += "\tif (t1 == null && i1.hasNext()) {\n"; |
---|
| | impl += "\t\tt1 = i1.next();\n"; |
---|
| | impl += "\t}\n"; |
---|
| | impl += "\tif (t2 == null && i2.hasNext()) {\n"; |
---|
| | impl += "\t\tt2 = i2.next();\n"; |
---|
| | impl += "\t}\n"; |
---|
| | impl += "\tif (t1 == null || (t2 != null && t1" + idxGetter + " > t2" + idxGetter + ")) {\n"; |
---|
| | impl += "\t\ttemp_l" + count[0] +".add(t2);\n"; |
---|
| | impl += "\t\tt2 = null;\n"; |
---|
| | impl += "\t} else {\n"; |
---|
| | impl += "\t\ttemp_l" + count[0] + ".add(t1);\n"; |
---|
| | impl += "\t\tt1 = null;\n"; |
---|
| | impl += "\t}\n"; |
---|
| | impl += "}\n"; |
---|
| | sideEffects[0] = sideEffects[0] + impl; |
---|
| | return "temp_l" + count[0]; |
---|
| | } |
---|
| | }); |
---|
| | merge.setImplOperatorType(Symbol.Type.GENERATIVE); |
---|
| | merge.setSignature(new Type[] {DataConstraintModel.typeList, DataConstraintModel.typeList, DataConstraintModel.typeList}); |
---|
| | } |
---|
| | |
---|
| | // Create a map from type names (lower case) to their types. |
---|
| | Map<String, TypeDeclaration> typeMap = new HashMap<>(); |
---|
| | for (CompilationUnit code: codes) { |
---|
| | for (TypeDeclaration type: code.types()) { |
---|
| |
---|
| | } catch (ParameterizedIdentifierIsFutureWork | ResolvingMultipleDefinitionIsFutureWork |
---|
| | | InvalidMessage | UnificationFailed | ValueUndefined e1) { |
---|
| | e1.printStackTrace(); |
---|
| | } |
---|
| | |
---|
| | if (floor != null) floor.setMemento(floorMem); |
---|
| | if (sum != null) sum.setMemento(sumMem); |
---|
| | if (merge != null) merge.setMemento(mergeMem); |
---|
| | return codes; |
---|
| | } |
---|
| | |
---|
| | private static MethodDeclaration getUpdateMethod(TypeDeclaration type, TypeDeclaration from) { |
---|
| |
---|
| | |