diff --git a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java index 1378f07..e112a4a 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java @@ -62,7 +62,8 @@ public void generateCodeFromFlowGraph(DataTransferModel model, DataFlowGraph flowGraph, Collection components, ArrayList codes, Map> dependedRootComponentGraph, IPlatformSpecific platformSpec, ILanguageSpecific langSpec) { - Map resourceComponents = new HashMap<>(); + Map resNameToComponent = new HashMap<>(); + Map resHierarchyToComponent = new HashMap<>(); Map resourceConstructors = new HashMap<>(); Map> constructorParams = new HashMap<>(); List> constructorStatements = new ArrayList<>(); @@ -90,7 +91,7 @@ // A component will be generated for this resource. String resourceName = getComponentName(resourceNode.getResourceHierarchy(), langSpec); Type resStateType = getImplStateType(resourceNode.getResourceHierarchy(), langSpec); - component = resourceComponents.get(resourceNode.getResourceHierarchy()); + component = resNameToComponent.get(resourceName); List depends = new ArrayList<>(); if (component == null) { // Add compilation unit for this component. @@ -99,7 +100,7 @@ // For each root node, add component annotations. ((RestApiSpecific) platformSpec).addComponentAnnotations(component, resourceNode.getResourceName()); } - resourceComponents.put(resourceNode.getResourceHierarchy(), component); + resNameToComponent.put(resourceName, component); CompilationUnit cu = langSpec.newCompilationUnit(component); if (!platformSpec.isMonolithic() && resourceNode.getResourceHierarchy().getParent() == null) { // For each root node, add platform specific imports. @@ -143,6 +144,9 @@ } } if (component != null) { + if (resHierarchyToComponent.get(resourceNode.getResourceHierarchy()) == null) { + resHierarchyToComponent.put(resourceNode.getResourceHierarchy(), component); + } // (#1) Declare the getter methods in this resource to obtain the descendant resources. (complementary to #2) declareDescendantGetterMethods(resourceNode, component, descendantGetters, langSpec); } @@ -156,16 +160,17 @@ // Declare this resource. ResourceNode resourceNode = (ResourceNode) componentNode; Type resStateType = getImplStateType(resourceNode.getResourceHierarchy(), langSpec); + String resourceName = getComponentName(resourceNode.getResourceHierarchy(), langSpec); TypeDeclaration component = null; TypeDeclaration parentComponent = null; TypeDeclaration rootComponent = null; if (generatesComponent(resourceNode.getResourceHierarchy())) { - component = resourceComponents.get(resourceNode.getResourceHierarchy()); + component = resNameToComponent.get(resourceName); } if (resourceNode.getResourceHierarchy().getParent() != null) { - parentComponent = resourceComponents.get(resourceNode.getResourceHierarchy().getParent()); + parentComponent = resHierarchyToComponent.get(resourceNode.getResourceHierarchy().getParent()); } - rootComponent = resourceComponents.get(resourceNode.getResourceHierarchy().getRoot()); + rootComponent = resHierarchyToComponent.get(resourceNode.getResourceHierarchy().getRoot()); // Declare cache fields and update methods in this resource, and an update accessor method in the type of root resource. Map.Entry, Map>>> initStatementsAndUpdateUpdates @@ -187,7 +192,7 @@ // (#2) Declare the getter method to obtain the resource state in an ancestor resource. (complementary to #1) if (component == null) { - MethodDeclaration stateGetter = declareStateGetterMethodInAncestor(resourceNode, resourceComponents, resStateType, platformSpec, langSpec); + MethodDeclaration stateGetter = declareStateGetterMethodInAncestor(resourceNode, resHierarchyToComponent, resStateType, platformSpec, langSpec); if (stateGetter != null && platformSpec.hasMain()) { // Declare the accessor method in the main component to call the getter method. @@ -222,7 +227,7 @@ // Add constructor parameters to the ancestor components. for (ResourceNode root: flowGraph.getRootResourceNodes()) { - addConstructorParameters(root.getResourceHierarchy(), resourceComponents, resourceConstructors, constructorParams, langSpec); + addConstructorParameters(root.getResourceHierarchy(), resHierarchyToComponent, resourceConstructors, constructorParams, langSpec); } // Add constructor statements. @@ -235,7 +240,7 @@ Expression updateExp = updateStatements.get(method).getKey(); ResourceHierarchy resource = updateStatements.get(method).getValue().getKey(); ResourceHierarchy descendantRes = updateStatements.get(method).getValue().getValue(); - TypeDeclaration descendantComponent = resourceComponents.get(descendantRes); + TypeDeclaration descendantComponent = resHierarchyToComponent.get(descendantRes); addUpdateStatementWithConstructorInvocationToMethod(method, updateExp, resource, descendantRes, descendantComponent, langSpec); } }