diff --git a/AlgebraicDataflowArchitectureModel/src/generators/CodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/generators/CodeGenerator.java index 9783715..6b1890c 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/CodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/CodeGenerator.java @@ -108,7 +108,7 @@ } // Update the main component for this component. - updateMainComponent(mainComponent, mainConstructor, componentNode, depends, langSpec); + updateMainComponent(model, mainComponent, mainConstructor, componentNode, depends, langSpec); if (componentNode instanceof ResourceNode || componentNode instanceof StatefulObjectNode) { // For this resource. @@ -207,13 +207,18 @@ orderedList.add(0, curNode); } - private static void updateMainComponent(TypeDeclaration mainType, MethodDeclaration mainConstructor, Node componentNode, List depends, ILanguageSpecific langSpec) { + private static void updateMainComponent(DataTransferModel model, TypeDeclaration mainType, MethodDeclaration mainConstructor, Node componentNode, List depends, ILanguageSpecific langSpec) { // Declare the field to refer to each object in the main type. + ResourceNode resNode = null; String nodeName = null; if (componentNode instanceof ResourceNode) { - nodeName = ((ResourceNode) componentNode).getIdentifierTemplate().getResourceName(); + resNode = (ResourceNode) componentNode; + nodeName = resNode.getIdentifierTemplate().getResourceName(); } else if (componentNode instanceof ObjectNode) { nodeName = ((ObjectNode) componentNode).getName(); + if (componentNode instanceof StatefulObjectNode) { + resNode = ((StatefulObjectNode) componentNode).getResource(); + } } String componentName = langSpec.toComponentName(nodeName); // Declare a field to refer each object. @@ -227,7 +232,23 @@ // For the callee objects (the destination resource of push transfer or the source resource of pull transfer). parameters.add(id.getResourceName()); } - // For the refs, ToDo. + // For the refs. + if (resNode != null) { + Set refs = new HashSet<>(); + for (ChannelGenerator cg : model.getChannelGenerators()) { + DataTransferChannelGenerator ch = (DataTransferChannelGenerator) cg; + if (ch.getInputIdentifierTemplates().contains(resNode.getIdentifierTemplate())) { + for (IdentifierTemplate id: ch.getReferenceIdentifierTemplates()) { + if (!refs.contains(id) && !depends.contains(id)) { + refs.add(id); + String refResName = id.getResourceName(); + parameters.add(refResName); + } + } + } + } + } + Block mainConstructorBody = mainConstructor.getBody(); if (mainConstructorBody == null) { mainConstructorBody = new Block(); @@ -341,10 +362,10 @@ ch = dIn.getChannelGenerator(); } } -// // for reference channel members, ToDo. -// for (ChannelMember c: d.getChannelGenerator().getReferenceChannelMembers()) { -// inputIdentifierToStateAccessor.put(c.getIdentifierTemplate(), langSpec.getPullAccessor()); // by pull data transfer -// } + // for reference channel members. + for (ChannelMember c: ch.getReferenceChannelMembers()) { + inputIdentifierToStateAccessor.put(c.getIdentifierTemplate(), langSpec.getPullAccessor()); // by pull data transfer + } // generate a return statement. try { @@ -413,14 +434,14 @@ // Declare an update method in the type of the destination resource. ArrayList vars = new ArrayList<>(); - vars.add(langSpec.newVariableDeclaration(srcRes.getResourceStateType(), srcRes.getResourceName())); - // For the refs, ToDo. -// DataTransferChannelGenerator c = (DataTransferChannelGenerator) re.getChannelGenerator(); -// for (IdentifierTemplate ref: c.getReferenceIdentifierTemplates()) { -// if (ref != rn.getIdentifierTemplate()) { -// vars.add(langSpec.newVariableDeclaration(ref.getResourceStateType(), ref.getResourceName())); -// } -// } + vars.add(langSpec.newVariableDeclaration(srcRes.getResourceStateType(), srcRes.getResourceName())); + // For the refs. + DataTransferChannelGenerator ch = (DataTransferChannelGenerator) re.getChannelGenerator(); + for (IdentifierTemplate ref: ch.getReferenceIdentifierTemplates()) { + if (ref != resourceNode.getIdentifierTemplate()) { + vars.add(langSpec.newVariableDeclaration(ref.getResourceStateType(), ref.getResourceName())); + } + } MethodDeclaration update = langSpec.newMethodDeclaration(updateMethodName + srcResComponentName, false, null, vars); component.addMethod(update); updateMethods.add(update);