diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java index 5d55493..d061f00 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java @@ -16,6 +16,7 @@ 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; @@ -46,8 +47,80 @@ Symbol.Memento sumMem = null; if (sum != null) { sumMem = sum.createMemento(); - sum.setImplName("stream().mapToInt(x->x).sum"); - sum.setImplOperatorType(Symbol.Type.METHOD); + 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.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"; + 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("AbstractMap.SimpleEntry")) { + 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); } // Create a map from type names (lower case) to their types. @@ -77,12 +150,13 @@ if (((StoreAttribute) dst.getAttribute()).isStored()) { // update stored state of dst resource (when every incoming edge is in push style) Expression updateExp = d.getChannelGenerator().deriveUpdateExpressionOf(out, JavaCodeGenerator.pushAccessor); - String curState = updateExp.toImplementation(); + String[] sideEffects = new String[] {""}; + String curState = updateExp.toImplementation(sideEffects); String updateStatement; if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { - updateStatement = curState + ";"; + updateStatement = sideEffects[0]; } else { - updateStatement = dstResourceName + " = " + curState + ";"; + updateStatement = sideEffects[0] + dstResourceName + " = " + curState + ";"; } if (update.getBody() == null || !update.getBody().getStatements().contains(updateStatement)) { update.addFirstStatement(updateStatement); @@ -123,14 +197,15 @@ inputIdentifierToStateAccessor.put(((ResourceNode) dIn.getSource()).getIdentifierTemplate(), JavaCodeGenerator.pullAccessor); } } + String[] sideEffects = new String[] {""}; if (!isContainedPush) { // All incoming edges are in PULL style. - String curState = d.getChannelGenerator().deriveUpdateExpressionOf(out, JavaCodeGenerator.pullAccessor).toImplementation(); - getter.addStatement("return " + curState + ";"); + String curState = d.getChannelGenerator().deriveUpdateExpressionOf(out, JavaCodeGenerator.pullAccessor).toImplementation(sideEffects); + getter.addStatement(sideEffects[0] + "return " + curState + ";"); } else { // At least one incoming edge is in PUSH style. - String curState = d.getChannelGenerator().deriveUpdateExpressionOf(out, JavaCodeGenerator.pullAccessor, inputIdentifierToStateAccessor).toImplementation(); - getter.addStatement("return " + curState + ";"); + String curState = d.getChannelGenerator().deriveUpdateExpressionOf(out, JavaCodeGenerator.pullAccessor, inputIdentifierToStateAccessor).toImplementation(sideEffects); + getter.addStatement(sideEffects[0] + "return " + curState + ";"); } } } @@ -157,13 +232,14 @@ Term message = (Term) out.getStateTransition().getMessageExpression(); MethodDeclaration input = getMethod(type, message.getSymbol().getName()); if (input != null) { + String[] sideEffects = new String[] {""}; Expression updateExp = ioChannelAndMember.getKey().deriveUpdateExpressionOf(out, JavaCodeGenerator.pushAccessor); - String newState = updateExp.toImplementation(); + String newState = updateExp.toImplementation(sideEffects); String updateStatement; if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { - updateStatement = newState + ";"; + updateStatement = sideEffects[0]; } else { - updateStatement = "this." + resourceName + " = " + newState + ";"; + updateStatement = sideEffects[0] + "this." + resourceName + " = " + newState + ";"; } if (input.getBody() == null || !input.getBody().getStatements().contains(updateStatement)) { input.addFirstStatement(updateStatement); @@ -191,6 +267,7 @@ if (floor != null) floor.setMemento(floorMem); if (sum != null) sum.setMemento(sumMem); + if (merge != null) merge.setMemento(mergeMem); return codes; }