diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/MethodBodyGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/MethodBodyGenerator.java index 3298912..1f86cc0 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/MethodBodyGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/MethodBodyGenerator.java @@ -15,8 +15,10 @@ import models.algebra.Term; import models.algebra.UnificationFailed; import models.algebra.ValueUndefined; +import models.dataConstraintModel.ChannelGenerator; import models.dataConstraintModel.ChannelMember; import models.dataFlowModel.DataFlowModel; +import models.dataFlowModel.DataflowChannelGenerator; import models.dataFlowModel.PushPullAttribute; import models.dataFlowModel.PushPullValue; import models.dataFlowModel.ResolvingMultipleDefinitionIsFutureWork; @@ -61,7 +63,7 @@ for (ChannelMember out: d.getChannelGenerator().getOutputChannelMembers()) { if (out.getIdentifierTemplate() == dst.getIdentifierTemplate()) { if (pushPull.getOptions().get(0) == PushPullValue.PUSH) { - // push + // for push data transfer MethodDeclaration update = getUpdateMethod(dstType); Expression updateExp = d.getChannelGenerator().deriveUpdateExpressionOf(out, CodeGenerator.pushAccessor); String curState = updateExp.toImplementation(); @@ -78,11 +80,13 @@ if (getter.getBody() == null || getter.getBody().getStatements().size() == 0) { getter.addStatement("return " + dstResourceName + ";"); } - // src side + // src side (for a chain of update method invocations) MethodDeclaration srcUpdate = getUpdateMethod(srcType); if (srcUpdate != null) srcUpdate.addStatement(dstResourceName + ".update(" + srcResourceName + ");"); + MethodDeclaration srcInput = getInputMethod(srcType, src, model); + if (srcInput != null) srcInput.addStatement(dstResourceName + ".update(" + srcResourceName + ");"); } else { - // pull or push/pull + // for pull (or push/pull) data transfer MethodDeclaration getter = getGetterMethod(dstType); if (getter.getBody() == null || getter.getBody().getStatements().size() == 0) { String curState = d.getChannelGenerator().deriveUpdateExpressionOf(out, CodeGenerator.pullAccessor).toImplementation(); @@ -92,13 +96,32 @@ } } } + // for source nodes + TypeDeclaration mainType = typeMap.get("main"); for (Node n: graph.getNodes()) { ResourceNode resource = (ResourceNode) n; - TypeDeclaration type = typeMap.get(resource.getIdentifierTemplate().getResourceName()); + String resourceName = resource.getIdentifierTemplate().getResourceName(); + TypeDeclaration type = typeMap.get(resourceName); + // getter method MethodDeclaration getter = getGetterMethod(type); if (getter.getBody() == null || getter.getBody().getStatements().size() == 0) { getter.addStatement("return " + resource.getIdentifierTemplate().getResourceName() + ";"); } + // methods for input events + MethodDeclaration input = getInputMethod(type, resource, model); + if (input != null) { + input.addFirstStatement("this." + resourceName + " = " + resourceName + ";"); + if (mainType != null) { + MethodDeclaration mainInput = getMethod(mainType, input.getName()); + if (mainInput == null) { + // to be removed later + mainInput = getMethod(mainType, "input"); + } + if (mainInput != null) { + mainInput.addStatement("this." + resourceName + "." + input.getName() + "(" + resourceName + ");"); + } + } + } } } catch (ParameterizedIdentifierIsFutureWork | ResolvingMultipleDefinitionIsFutureWork | InvalidMessage | UnificationFailed | ValueUndefined e1) { @@ -110,19 +133,42 @@ return types; } - private static MethodDeclaration getUpdateMethod(TypeDeclaration dstType) { - for (MethodDeclaration m: dstType.getMethods()) { + private static MethodDeclaration getUpdateMethod(TypeDeclaration type) { + for (MethodDeclaration m: type.getMethods()) { if (m.getName().startsWith("update")) return m; - if (m.getName().startsWith("set")) return m; } return null; } - private static MethodDeclaration getGetterMethod(TypeDeclaration dstType) { - for (MethodDeclaration m: dstType.getMethods()) { + private static MethodDeclaration getGetterMethod(TypeDeclaration type) { + for (MethodDeclaration m: type.getMethods()) { if (m.getName().startsWith("get")) return m; } return null; } + private static MethodDeclaration getInputMethod(TypeDeclaration type, ResourceNode resource, DataFlowModel model) { + for (ChannelGenerator c: model.getIOChannelGenerators()) { + DataflowChannelGenerator channel = (DataflowChannelGenerator) c; + // I/O channel + for (ChannelMember out: channel.getOutputChannelMembers()) { + if (out.getIdentifierTemplate().equals(resource.getIdentifierTemplate())) { + if (out.getStateTransition().getMessageExpression() instanceof Term) { + Term message = (Term) out.getStateTransition().getMessageExpression(); + MethodDeclaration input = getMethod(type, message.getSymbol().getName()); + return input; + } + } + } + } + return null; + } + + + private static MethodDeclaration getMethod(TypeDeclaration type, String methodName) { + for (MethodDeclaration m: type.getMethods()) { + if (m.getName().equals(methodName)) return m; + } + return null; + } }