Skip to content

Commit

Permalink
dynamic variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhitocode committed Dec 19, 2024
1 parent 626c85e commit e9570f9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,21 @@ protected Node handleNode(final Node node, final Element element, final String u
}
}

String namespace = parameters.get(NAMESPACE_PROP);
String model = parameters.get(MODEL_PROP);
String decision = parameters.get(DECISION_PROP);
String namespace = parameters.getOrDefault(NAMESPACE_PROP, "");
String model = parameters.getOrDefault(MODEL_PROP, "");//we have to actually get #{university}
//but we were not getting that. we were just getting null as label for this expression was empty
String decision = parameters.getOrDefault(DECISION_PROP, "");
String variableRegex = "#\\{[^}]+}";
if (namespace.matches(variableRegex)) {
namespace = resolveProcessVariable(namespace, parser, element);
}
if (model.matches(variableRegex)) {
model = resolveProcessVariable(model, parser, element);
}
if (decision.matches(variableRegex)) {
decision = resolveProcessVariable(decision, parser, element);
}

ruleSetNode.setRuleType(RuleType.decision(
namespace,
model,
Expand All @@ -94,7 +106,6 @@ public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
RuleType ruleType = ruleSetNode.getRuleType();
if (ruleType != null) {
xmlDump.append("g:ruleFlowGroup=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(ruleType.getName()) + "\" " + EOL);
// else DMN
}

xmlDump.append(" implementation=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(ruleSetNode.getLanguage()) + "\" >" + EOL);
Expand All @@ -104,4 +115,13 @@ public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
endNode("businessRuleTask", xmlDump);
}

private String resolveProcessVariable(String expression, Parser parser, Element element) {

String varName = expression.substring(expression.indexOf("#{") + 2, expression.indexOf("}"));

String variableValue = element.getAttribute(varName);//as we understand that the variable is university. But how to get the value since the variables are not in the attribute list?

return expression.replace("#{" + varName + "}", variableValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ public void testDecision() throws Exception {
}
}

@Test
public void testDynamicDecision() throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("decision/dynamic/DynamicDmnProcess.bpmn2"));
resourcesTypeMap.put(TYPE.DECISION, Collections.singletonList("decision/dynamic/HarvardUniversity/HarvardUniversity.dmn"));
Application app = generateCode(resourcesTypeMap);
assertThat(app).isNotNull();
Process<? extends Model> p =
app.get(Processes.class)
.processById("DynamicDmnProcess");

// first run 16, 1 and expected days is 27
{
Model m = p.createModel();
HashMap<String, Object> vars = new HashMap<>();
vars.put("marks", 43);
vars.put("university", "HarvardUniversity");
m.fromMap(vars);

ProcessInstance<? extends Model> processInstance = p.createInstance(m);
processInstance.start();

assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = processInstance.variables();

assertThat(result.toMap().get("result"))
.isNotNull()
.isEqualTo("failed");
}
}

@Test
public void testBusinessRuleTaskWithIOExpression() throws Exception {
Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
Expand Down

0 comments on commit e9570f9

Please sign in to comment.