diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java index 8b4c569..623cf06 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import code.ast.Block; @@ -58,15 +59,13 @@ static public ArrayList doGenerate(ResourceDependencyGraph graph, DataFlowModel model) { ArrayList codes = new ArrayList<>(); -// ArrayList resources = StoreResourceCheck(graph); - Set resources = graph.getNodes(); + ArrayList resources = determineResourceOrder(graph); TypeDeclaration mainType = new TypeDeclaration(mainTypeName); CompilationUnit mainCU = new CompilationUnit(mainType); mainCU.addImport(new ImportDeclaration("java.util.*")); codes.add(mainCU); - for (Node n : resources) { - ResourceNode rn = (ResourceNode) n; + for (ResourceNode rn: resources) { boolean f = false; String resourceName = rn.getIdentifierTemplate().getResourceName().substring(0, 1).toUpperCase() + rn.getIdentifierTemplate().getResourceName().substring(1); @@ -95,9 +94,13 @@ } else { if (rn.getIndegree() > 1) { // Declare a field to cash the state of the source resource in the type of the destination resource. + String cashInitializer = null; + Type cashType = ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceStateType(); + if (DataConstraintModel.typeList.isAncestorOf(cashType) || DataConstraintModel.typeTuple.isAncestorOf(cashType)) { + cashInitializer = "new " + cashType.getImplementationTypeName() + "()"; + } type.addField(new FieldDeclaration( - ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceStateType(), - ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName())); + cashType, ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName(), cashInitializer)); } } } @@ -192,10 +195,10 @@ if (((StoreAttribute) rn.getAttribute()).isStored()) { String str = "new " + rn.getIdentifierTemplate().getResourceStateType().getImplementationTypeName() + "()"; - if (!rn.getIdentifierTemplate().getResourceStateType().getTypeName().contains("List")) + Type stateType = rn.getIdentifierTemplate().getResourceStateType(); + if (!DataConstraintModel.typeList.isAncestorOf(stateType) && !DataConstraintModel.typeTuple.isAncestorOf(stateType)) str = null; - type.addField(new FieldDeclaration(rn.getIdentifierTemplate().getResourceStateType(), - rn.getIdentifierTemplate().getResourceName(), str)); + type.addField(new FieldDeclaration(stateType, rn.getIdentifierTemplate().getResourceName(), str)); } // Declare the getter method to obtain the state in the type of each resource. @@ -315,7 +318,7 @@ return codes; } - static private ArrayList StoreResourceCheck(ResourceDependencyGraph graph) { + static private ArrayList determineResourceOrder(ResourceDependencyGraph graph) { ArrayList resources = new ArrayList<>(); for (Node n : graph.getNodes()) { ResourceNode rn = (ResourceNode) n; @@ -335,7 +338,10 @@ if (flag) resources.add(rn); } - trackNode(resources.get(0), resources); + List initialResources = (List) resources.clone(); + for (ResourceNode r: initialResources) { + trackNode(r, resources); + } return resources; }