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; } diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java index 00d0b35..bc6363c 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java @@ -111,7 +111,12 @@ // For each source resource, a child resource is defined in the destination resource so that its state can be updated separately. update.addAnnotation(new Annotation("Path", "\"/" + srcName + "\"")); // Declare a field to cash the state of the source resource in the type of the destination resource. - type.addField(new FieldDeclaration(((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceStateType(), srcName)); + 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(cashType, srcName, cashInitializer)); } type.addMethod(update); } @@ -143,10 +148,10 @@ // Declare the field to store the state in the type of each resource. if (((StoreAttribute) rn.getAttribute()).isStored()) { String initializer = "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)) initializer = null; - type.addField(new FieldDeclaration(rn.getIdentifierTemplate().getResourceStateType(), - rn.getIdentifierTemplate().getResourceName(), initializer)); + type.addField(new FieldDeclaration(stateType, rn.getIdentifierTemplate().getResourceName(), initializer)); } // Declare the getter method to obtain the state in the type of each resource. @@ -176,7 +181,7 @@ String cons = "\t" + "private " + field.getType().getInterfaceTypeName() + " " + field.getName(); if (DataConstraintModel.isListType(field.getType())) - cons += " = new ArrayList<>()"; + cons += " = new " + field.getType().getImplementationTypeName() + "()"; cons += ";"; codes.add(cons); } else { @@ -243,47 +248,6 @@ return codes; } - static private ArrayList StoreResourceCheck(ResourceDependencyGraph graph) { - ArrayList resources = new ArrayList<>(); - for (Node n : graph.getNodes()) { - ResourceNode rn = (ResourceNode) n; - boolean flag = true; - for (Edge e : rn.getOutEdges()) { - ResourceDependency re = (ResourceDependency) e; - if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) == PushPullValue.PUSH) { - flag = false; - } - } - for (Edge e : rn.getInEdges()) { - ResourceDependency re = (ResourceDependency) e; - if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) != PushPullValue.PUSH) { - flag = false; - } - } - if (flag) - resources.add(rn); - } - trackNode(resources.get(0), resources); - return resources; - } - - static private void trackNode(ResourceNode current, ArrayList resources) { - if (!resources.contains(current)) - resources.add(current); - for (Edge e : current.getOutEdges()) { - ResourceDependency re = (ResourceDependency) e; - if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) != PushPullValue.PUSH) { - trackNode((ResourceNode) re.getDestination(), resources); - } - } - for (Edge e : current.getInEdges()) { - ResourceDependency re = (ResourceDependency) e; - if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) == PushPullValue.PUSH) { - trackNode((ResourceNode) re.getSource(), resources); - } - } - } - static public IResourceStateAccessor pushAccessor = new IResourceStateAccessor() { @Override public Expression getCurrentStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java index fccf7cf..ac931f1 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java @@ -23,6 +23,7 @@ import models.algebra.ValueUndefined; import models.dataConstraintModel.ChannelGenerator; import models.dataConstraintModel.ChannelMember; +import models.dataConstraintModel.DataConstraintModel; import models.dataFlowModel.DataFlowModel; import models.dataFlowModel.DataflowChannelGenerator; import models.dataFlowModel.PushPullAttribute; @@ -182,9 +183,14 @@ } private static String getHttpMethodParamsStatement(String callerResourceName, Type paramType, String paramName) { - if (paramType.getTypeName().equals("List")) { + if (DataConstraintModel.typeList.isAncestorOf(paramType)) { + String compType = "Integer"; + String interfaceType = paramType.getInterfaceTypeName(); + if (interfaceType.contains("<")) { + compType = interfaceType.substring(interfaceType.indexOf("<") + 1, interfaceType.lastIndexOf(">")); + } String statements = "Form form = new Form();\n"; - statements += "for (Integer i: " + paramName + ") {\n"; + statements += "for (" + compType + " i: " + paramName + ") {\n"; statements += "\tform.param(\"" + paramName + "\", i.toString());\n"; statements += "}\n"; statements += "Entity
entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);"; @@ -209,7 +215,7 @@ if (responseTypeName.contains("<")) { responseShortTypeName = responseTypeName.substring(0, responseTypeName.indexOf("<")); } - return responseTypeName + " " + resourceName + " = client.target(\"" + baseURL + "\").path(\"/" + resourceName + "\").request()." + httpMethod + "(" + responseShortTypeName + ".class);"; + return responseType.getInterfaceTypeName() + " " + resourceName + " = client.target(\"" + baseURL + "\").path(\"/" + resourceName + "\").request()." + httpMethod + "(" + responseShortTypeName + ".class);"; } private static MethodDeclaration getUpdateMethod(TypeDeclaration type, TypeDeclaration from) { diff --git a/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/actions/JerseyPrototypeGenerateAction.java b/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/actions/JerseyPrototypeGenerateAction.java index 649185a..0236d5d 100644 --- a/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/actions/JerseyPrototypeGenerateAction.java +++ b/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/actions/JerseyPrototypeGenerateAction.java @@ -28,7 +28,7 @@ private String lastDir = null; public JerseyPrototypeGenerateAction(Editor editor) { - super("Generate Jersey Prototype", editor); + super("Generate JAX-RS Prototype", editor); } @Override