diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/CodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/CodeGenerator.java index bbf1201..5ba1720 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/CodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/CodeGenerator.java @@ -2,6 +2,7 @@ import java.util.ArrayList; +import code.ast.Block; import code.ast.FieldDeclaration; import code.ast.MethodDeclaration; import code.ast.TypeDeclaration; @@ -34,12 +35,39 @@ codes.add(new TypeDeclaration("Main")); for (ResourceNode rn : resources) { + boolean f = false; String name = rn.getIdentifierTemplate().getResourceName().substring(0, 1).toUpperCase() + rn.getIdentifierTemplate().getResourceName().substring(1); + String consstr = "new " + name + "("; + for (Edge e : rn.getOutEdges()) { + ResourceDependency re = (ResourceDependency) e; + String rename = ((ResourceNode) re.getDestination()).getIdentifierTemplate().getResourceName() + .substring(0, 1).toUpperCase() + + ((ResourceNode) re.getDestination()).getIdentifierTemplate().getResourceName().substring(1); + if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) == PushPullValue.PUSH) { + consstr += rename.toLowerCase() + ","; + f = true; + } + } + for (Edge e : rn.getInEdges()) { + ResourceDependency re = (ResourceDependency) e; + String rename = ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName() + .substring(0, 1).toUpperCase() + + ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName().substring(1); + if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) != PushPullValue.PUSH) { + consstr += rename.toLowerCase() + ","; + f = true; + } + } + if (f) + consstr = consstr.substring(0, consstr.length() - 1); + consstr += ")"; FieldDeclaration field = new FieldDeclaration(new Type(name, name), - rn.getIdentifierTemplate().getResourceName()); + rn.getIdentifierTemplate().getResourceName(), consstr); TypeDeclaration type = new TypeDeclaration(name); codes.get(0).addField(field); + MethodDeclaration cons = new MethodDeclaration(name, true); + Block block = new Block(); for (Edge e : rn.getOutEdges()) { ResourceDependency re = (ResourceDependency) e; String rename = ((ResourceNode) re.getDestination()).getIdentifierTemplate().getResourceName() @@ -48,10 +76,13 @@ if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) == PushPullValue.PUSH) { type.addField(new FieldDeclaration(new Type(rename, rename), ((ResourceNode) re.getDestination()).getIdentifierTemplate().getResourceName())); - type.addConstructors(new VariableDeclaration(new Type(rename, rename), + cons.addParameter(new VariableDeclaration(new Type(rename, rename), ((ResourceNode) re.getDestination()).getIdentifierTemplate().getResourceName())); + block.addStatement("this." + rename.toLowerCase() + " = " + rename.toLowerCase()); + cons.setBody(block); } } + block = new Block(); ArrayList vars = new ArrayList<>(); boolean flag = false; for (Edge e : rn.getInEdges()) { @@ -62,8 +93,10 @@ if (((PushPullAttribute) re.getAttribute()).getOptions().get(0) != PushPullValue.PUSH) { type.addField(new FieldDeclaration(new Type(rename, rename), ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName())); - type.addConstructors(new VariableDeclaration(new Type(rename, rename), + cons.addParameter(new VariableDeclaration(new Type(rename, rename), ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName())); + block.addStatement("this." + rename.toLowerCase() + " = " + rename.toLowerCase()); + cons.setBody(block); } else { vars.add(new VariableDeclaration( ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceStateType(), @@ -71,11 +104,15 @@ flag = true; } } + type.addMethod(cons); if (flag) type.addMethod(new MethodDeclaration("update", false, typeVoid, vars)); if (((StoreAttribute) rn.getAttribute()).isStored()) { + String str = "new " + rn.getIdentifierTemplate().getResourceStateType() + .getImplementationTypeName()+ "()"; + if(!rn.getIdentifierTemplate().getResourceStateType().getTypeName().contains("List")) str = null; type.addField(new FieldDeclaration(rn.getIdentifierTemplate().getResourceStateType(), - rn.getIdentifierTemplate().getResourceName())); + rn.getIdentifierTemplate().getResourceName(), str)); } type.addMethod(new MethodDeclaration("get" + type.getTypeName(), rn.getIdentifierTemplate().getResourceStateType()));