diff --git a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java index ea45a55..e1bf0ae 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java @@ -675,18 +675,51 @@ break; } } - // Add statements to the input method. + // Add statements to the update method. String[] sideEffects = new String[] {""}; - String curState = updateExp.toImplementation(sideEffects); - String updateStatement; - if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { - updateStatement = sideEffects[0]; + String newState = updateExp.toImplementation(sideEffects); + if (generatesComponent(outRes)) { + String updateStatement; + if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { + updateStatement = sideEffects[0]; + } else { + updateStatement = sideEffects[0] + langSpec.getFieldAccessor(fieldOfResourceState) + langSpec.getAssignment() + newState + langSpec.getStatementDelimiter(); // this.value = ... + } + if (update.getBody() == null || !update.getBody().getStatements().contains(updateStatement)) { + update.addFirstStatement(updateStatement); + } } else { - updateStatement = sideEffects[0] + langSpec.getFieldAccessor(fieldOfResourceState) + langSpec.getAssignment() + curState + langSpec.getStatementDelimiter(); // this.value = ... + String updateStatement = null; + if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { + updateStatement = sideEffects[0]; + String resourceName = langSpec.toVariableName(getComponentName(outRes, langSpec)); + updateStatement = updateStatement.replace(langSpec.getFieldAccessor(fieldOfResourceState), langSpec.getFieldAccessor(resourceName)); + } else { + if (DataConstraintModel.typeList.isAncestorOf(resourceNode.getParent().getResourceStateType())) { + Term selector = new Term(DataConstraintModel.set); + selector.addChild(new Constant(langSpec.getFieldAccessor(fieldOfResourceState))); + selector.addChild(new Variable(update.getParameters().get(update.getParameters().size() - 2).getName())); + selector.addChild(new Constant(newState)); + String[] sideEffects2 = new String[] {""}; + String newList = selector.toImplementation(sideEffects2); + updateStatement = sideEffects[0] + sideEffects2[0]; + } else if (DataConstraintModel.typeMap.isAncestorOf(resourceNode.getParent().getResourceStateType())) { + Term selector = new Term(DataConstraintModel.insert); + selector.addChild(new Constant(langSpec.getFieldAccessor(fieldOfResourceState))); + selector.addChild(new Variable(update.getParameters().get(update.getParameters().size() - 2).getName())); + selector.addChild(new Constant(newState)); + String[] sideEffects2 = new String[] {""}; + String newMap = selector.toImplementation(sideEffects2); + updateStatement = sideEffects[0] + sideEffects2[0]; + } else { + String resourceName = langSpec.toVariableName(getComponentName(outRes, langSpec)); + updateStatement = sideEffects[0] + langSpec.getFieldAccessor(resourceName) + langSpec.getAssignment() + newState + langSpec.getStatementDelimiter(); + } + } + if (update.getBody() == null || !update.getBody().getStatements().contains(updateStatement)) { + update.addFirstStatement(updateStatement); + } } - if (update.getBody() == null || !update.getBody().getStatements().contains(updateStatement)) { - update.addFirstStatement(updateStatement); - } break; } } @@ -955,10 +988,23 @@ } else { // var has not come from a reference resource. resInputParams.add(langSpec.newVariableDeclaration(var.getType(), var.getName())); - mainInputParams.add(langSpec.newVariableDeclaration(var.getType(), var.getName())); + boolean bExists = false; + for (VariableDeclaration mainParam: mainInputParams) { + if (mainParam.getName().equals(var.getName()) ) { + bExists = true; + break; + } + } + if (!bExists) { + mainInputParams.add(langSpec.newVariableDeclaration(var.getType(), var.getName())); + } } } - input = langSpec.newMethodDeclaration(((Term) message).getSymbol().getImplName(), false, null, resInputParams); + String inputMethodName = ((Term) message).getSymbol().getImplName(); + if (((DataTransferChannel) ch).getOutputChannelMembers().size() > 1) { + inputMethodName += getComponentName(out.getResource().getResourceHierarchy(), langSpec); + } + input = langSpec.newMethodDeclaration(inputMethodName, false, null, resInputParams); if (component != null) { // A component is created for this resource. component.addMethod(input); @@ -968,10 +1014,10 @@ } // Declare the accessor in the main component to call the input method. - String str = ((Term) message).getSymbol().getImplName(); - inputAccessor = getMethod(mainComponent, str); + String messageSymbol = ((Term) message).getSymbol().getImplName(); + inputAccessor = getMethod(mainComponent, messageSymbol); if (inputAccessor == null) { - inputAccessor = langSpec.newMethodDeclaration(str, false, null, mainInputParams); + inputAccessor = langSpec.newMethodDeclaration(messageSymbol, false, null, mainInputParams); mainComponent.addMethod(inputAccessor); } else { // Add type to a parameter without type. @@ -1017,10 +1063,14 @@ v++; } } + String inputMethodName = ((Variable) message).getName(); + if (((DataTransferChannel) ch).getOutputChannelMembers().size() > 1) { + inputMethodName += getComponentName(out.getResource().getResourceHierarchy(), langSpec); + } if (resInputParams.size() == 0) { - input = langSpec.newMethodDeclaration(((Variable) message).getName(), null); + input = langSpec.newMethodDeclaration(inputMethodName, null); } else { - input = langSpec.newMethodDeclaration(((Variable) message).getName(), false, null, resInputParams); + input = langSpec.newMethodDeclaration(inputMethodName, false, null, resInputParams); } if (component != null) { // A component is created for this resource. @@ -1031,13 +1081,13 @@ } // Declare the accessor in the main component to call the input method. - String str = ((Variable) message).getName(); - inputAccessor = getMethod(mainComponent, str); + String messageSymbol = ((Variable) message).getName(); + inputAccessor = getMethod(mainComponent, messageSymbol); if (inputAccessor == null) { if (mainInputParams.size() == 0) { - inputAccessor = langSpec.newMethodDeclaration(str, null); + inputAccessor = langSpec.newMethodDeclaration(messageSymbol, null); } else { - inputAccessor = langSpec.newMethodDeclaration(str, false, null, mainInputParams); + inputAccessor = langSpec.newMethodDeclaration(messageSymbol, false, null, mainInputParams); } mainComponent.addMethod(inputAccessor); } @@ -1117,8 +1167,9 @@ } else { String updateStatement = null; if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { - // ToDo. updateStatement = sideEffects[0]; + String resourceName = langSpec.toVariableName(getComponentName(resource, langSpec)); + updateStatement = updateStatement.replace(langSpec.getFieldAccessor(fieldOfResourceState), langSpec.getFieldAccessor(resourceName)); } else { if (DataConstraintModel.typeList.isAncestorOf(resourceNode.getParent().getResourceStateType())) { Term selector = new Term(DataConstraintModel.set); @@ -1140,9 +1191,9 @@ String resourceName = langSpec.toVariableName(getComponentName(resource, langSpec)); updateStatement = sideEffects[0] + langSpec.getFieldAccessor(resourceName) + langSpec.getAssignment() + newState + langSpec.getStatementDelimiter(); } - if (updateStatement != null) { - input.addFirstStatement(updateStatement); - } + } + if (updateStatement != null) { + input.addFirstStatement(updateStatement); } } }