From 39c2596aa986916e2ce8ea1cd6f3c1bb1474b3b0 Mon Sep 17 00:00:00 2001 From: Abhiram Gundala <164050036+Abhitocode@users.noreply.github.com> Date: Fri, 4 Oct 2024 09:40:18 -0400 Subject: [PATCH] dynamic variable --- .../bpmn2/xml/BusinessRuleTaskHandler.java | 28 +++- .../codegen/rules/BusinessRuleTaskIT.java | 31 ++++ .../HarvardUniversity/HarvardUniversity.dmn | 29 ++++ .../StanfordUniversity/StanfordUniversity.dmn | 29 ++++ .../decision/dynamic/dynamicDmnProcess.bpmn2 | 150 ++++++++++++++++++ 5 files changed, 263 insertions(+), 4 deletions(-) create mode 100644 kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/HarvardUniversity/HarvardUniversity.dmn create mode 100644 kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/StanfordUniversity/StanfordUniversity.dmn create mode 100644 kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/dynamicDmnProcess.bpmn2 diff --git a/jbpm/jbpm-bpmn2/src/main/java/org/jbpm/bpmn2/xml/BusinessRuleTaskHandler.java b/jbpm/jbpm-bpmn2/src/main/java/org/jbpm/bpmn2/xml/BusinessRuleTaskHandler.java index 6956bf551fc..1d69e68648a 100755 --- a/jbpm/jbpm-bpmn2/src/main/java/org/jbpm/bpmn2/xml/BusinessRuleTaskHandler.java +++ b/jbpm/jbpm-bpmn2/src/main/java/org/jbpm/bpmn2/xml/BusinessRuleTaskHandler.java @@ -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, @@ -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); @@ -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); + } + } diff --git a/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/rules/BusinessRuleTaskIT.java b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/rules/BusinessRuleTaskIT.java index 07aae3207c7..37b4b63cf0f 100644 --- a/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/rules/BusinessRuleTaskIT.java +++ b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/rules/BusinessRuleTaskIT.java @@ -208,6 +208,37 @@ public void testDecision() throws Exception { } } + @Test + public void testDynamicDecision() throws Exception { + Map> 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 p = + app.get(Processes.class) + .processById("DynamicDmnProcess"); + + // first run 16, 1 and expected days is 27 + { + Model m = p.createModel(); + HashMap vars = new HashMap<>(); + vars.put("marks", 43); + vars.put("university", "HarvardUniversity"); + m.fromMap(vars); + + ProcessInstance 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> resourcesTypeMap = new HashMap<>(); diff --git a/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/HarvardUniversity/HarvardUniversity.dmn b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/HarvardUniversity/HarvardUniversity.dmn new file mode 100644 index 00000000000..be62ea93da5 --- /dev/null +++ b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/HarvardUniversity/HarvardUniversity.dmn @@ -0,0 +1,29 @@ + + + + + + + + + + + + + < 45 + + + "failed" + + + + + >= 45 + + + "passed" + + + + + diff --git a/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/StanfordUniversity/StanfordUniversity.dmn b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/StanfordUniversity/StanfordUniversity.dmn new file mode 100644 index 00000000000..54cd0e76269 --- /dev/null +++ b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/StanfordUniversity/StanfordUniversity.dmn @@ -0,0 +1,29 @@ + + + + + + + + + + + + + < 40 + + + "failed" + + + + + >= 40 + + + "passed" + + + + + diff --git a/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/dynamicDmnProcess.bpmn2 b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/dynamicDmnProcess.bpmn2 new file mode 100644 index 00000000000..fd7dce0d4cd --- /dev/null +++ b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/resources/decision/dynamic/dynamicDmnProcess.bpmn2 @@ -0,0 +1,150 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _14DB8038-D485-4BCE-BFD9-7A98B9DC3361 + + + + + + + + _BB6D2A53-9775-4654-AC1B-31E35737510A + _14DB8038-D485-4BCE-BFD9-7A98B9DC3361 + + + + + + + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputX + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputX + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_marksInputX + + + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_resultOutputX + + + + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputX + + + + + + + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputX + + + + + + + marks + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_marksInputX + + + _3EDB5055-D95B-43D0-A87A-A827EE7229A7_resultOutputX + result + + + + _BB6D2A53-9775-4654-AC1B-31E35737510A + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _hi-_MJ_1ED2CT4MzKBb52A + _hi-_MJ_1ED2CT4MzKBb52A + + \ No newline at end of file