diff --git a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java index 470ecb1..1f68945 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/CodeGeneratorFromDataFlowGraph.java @@ -177,7 +177,9 @@ } } for (Map.Entry>> entry: initStatementsAndUpdateUpdates.getValue().entrySet()) { - updateStatements.put(entry.getKey(), entry.getValue()); + Map.Entry> updateInfo = entry.getValue(); + updateStatements.put(entry.getKey(), updateInfo); +// extractConstructorParameterFromUpdateExpression(updateInfo, constructorParams, langSpec); } // Declare the fields to refer to other resources for push/pull transfer in the parent/this component, and the state field in the parent component. @@ -209,7 +211,9 @@ } } for (Map.Entry>> entry: initStatementsAndInputUpdates.getValue().entrySet()) { - updateStatements.put(entry.getKey(), entry.getValue()); + Map.Entry> updateInfo = entry.getValue(); + updateStatements.put(entry.getKey(), updateInfo); +// extractConstructorParameterFromUpdateExpression(updateInfo, constructorParams, langSpec); } } @@ -236,6 +240,39 @@ } } + private void extractConstructorParameterFromUpdateExpression( + Map.Entry> updateInfo, + Map> constructorParams, ILanguageSpecific langSpec) { + Expression updateExp = updateInfo.getKey(); + ResourceHierarchy newResource = updateInfo.getValue().getValue(); + if (updateExp instanceof Term) { + Map subTerms = ((Term) updateExp).getSubTerms(Term.class); + for (Term term: subTerms.values()) { + if (term.getType() != null) { + if (term.getType().equals(newResource.getResourceStateType())) { + if (term instanceof JsonTerm) { + JsonTerm jsonTerm = (JsonTerm) term; + for (String key: jsonTerm.keySet()) { + Map params = constructorParams.getOrDefault(newResource, new HashMap<>()); + Expression member = jsonTerm.get(key); + if (member != null && member instanceof Term) { + if (!params.containsKey(key) && ((Term) member).getType() != null) { + params.put(key, new VariableDeclaration(((Term) member).getType(), key)); + } + } else if (member != null && member instanceof Variable) { + if (!params.containsKey(key) && ((Variable) member).getType() != null) { + params.put(key, langSpec.newVariableDeclaration(((Variable) member).getType(), key)); + } + } + constructorParams.put(newResource, params); + } + } + } + } + } + } + } + private static List addConstructorParameters(ResourceHierarchy resource, Map resourceComponents, Map resourceConstructors, Map> constructorParams, ILanguageSpecific langSpec) { List params = new ArrayList<>(); @@ -273,6 +310,15 @@ if (!existsParam) { constructor.addParameter(param); constructor.getBody().addStatement(langSpec.getFieldAccessor(langSpec.toVariableName(param.getName())) + langSpec.getAssignment() + langSpec.toVariableName(param.getName()) + langSpec.getStatementDelimiter()); + boolean existsField = false; + for (FieldDeclaration field: resourceComponents.get(resource).getFields()) { + if (field.getName().equals(param.getName())) { + existsField = true; + } + } + if (!existsField) { + resourceComponents.get(resource).addField(langSpec.newFieldDeclaration(param.getType(), param.getName())); + } } } } @@ -1893,6 +1939,10 @@ String parentResName = langSpec.toVariableName(getComponentName(insideResPath.getResourceHierarchy(), langSpec)); String parentResPath = insideResPath.toResourcePath().replaceAll(":.*\\}","\\}").replaceAll("\\{", "\"+").replaceAll("\\}", "+\""); generatePullDataTransfer(update, parentResName, parentResPath, parentResType, true, platformSpec, langSpec); + if (component != null && !((RestApiSpecific) platformSpec).hasHttpClientFieldDeclaration(component)) { + // Declare a client field to connect to the destination resource of push transfer. + ((RestApiSpecific) platformSpec).addHttpClientFieldDeclaration(component); + } } } } else if (selExp instanceof Term) { @@ -1927,6 +1977,10 @@ pathParams.add("\" + " + pathExp.toImplementation(sideEffects) + " + \""); } generatePullDataTransfer(update, refVarName, ref.getResourceHierarchy().toResourcePath(pathParams), refResourceType, false, platformSpec, langSpec); + if (component != null && !((RestApiSpecific) platformSpec).hasHttpClientFieldDeclaration(component)) { + // Declare a client field to connect to the destination resource of push transfer. + ((RestApiSpecific) platformSpec).addHttpClientFieldDeclaration(component); + } } else { Expression refGetter = getPullAccessor(platformSpec).getDirectStateAccessorFor(ref, srcRes); String refExp = refGetter.toImplementation(sideEffects);