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 39c2596
Show file tree
Hide file tree
Showing 5 changed files with 263 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" xmlns:biodi="http://bpmn.io/schema/dmn/biodi/2.0" id="definitions_1neduxd" name="definitions" namespace="http://camunda.org/schema/1.0/dmn" exporter="dmn-js (https://demo.bpmn.io/dmn)" exporterVersion="17.0.2">
<decision id="decision_1lf69hr" name="">
<decisionTable id="decisionTable_0dfuj5m">
<input id="input1" label="marks" biodi:width="192">
<inputExpression id="inputExpression1" typeRef="number">
<text></text>
</inputExpression>
</input>
<output id="output1" label="" name="result" typeRef="string" />
<rule id="DecisionRule_0srjd12">
<inputEntry id="UnaryTests_12ec02a">
<text>&lt; 45</text>
</inputEntry>
<outputEntry id="LiteralExpression_0ubdara">
<text>"failed"</text>
</outputEntry>
</rule>
<rule id="DecisionRule_0jnrhht">
<inputEntry id="UnaryTests_0rxvw2t">
<text>&gt;= 45</text>
</inputEntry>
<outputEntry id="LiteralExpression_136w9tn">
<text>"passed"</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" xmlns:biodi="http://bpmn.io/schema/dmn/biodi/2.0" id="definitions_1neduxd" name="definitions" namespace="http://camunda.org/schema/1.0/dmn" exporter="dmn-js (https://demo.bpmn.io/dmn)" exporterVersion="17.0.2">
<decision id="decision_1lf69hr" name="">
<decisionTable id="decisionTable_0dfuj5m">
<input id="input1" label="marks" biodi:width="192">
<inputExpression id="inputExpression1" typeRef="number">
<text></text>
</inputExpression>
</input>
<output id="output1" label="" name="result" typeRef="string" />
<rule id="DecisionRule_0srjd12">
<inputEntry id="UnaryTests_12ec02a">
<text>&lt; 40</text>
</inputEntry>
<outputEntry id="LiteralExpression_0ubdara">
<text>"failed"</text>
</outputEntry>
</rule>
<rule id="DecisionRule_0jnrhht">
<inputEntry id="UnaryTests_0rxvw2t">
<text>&gt;= 40</text>
</inputEntry>
<outputEntry id="LiteralExpression_136w9tn">
<text>"passed"</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
</definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:bpsim="http://www.bpsim.org/schemas/1.0" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:drools="http://www.jboss.org/drools" id="_hi-_MJ_1ED2CT4MzKBb52A" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd http://www.jboss.org/drools drools.xsd http://www.bpsim.org/schemas/1.0 bpsim.xsd http://www.omg.org/spec/DD/20100524/DC DC.xsd http://www.omg.org/spec/DD/20100524/DI DI.xsd " exporter="jBPM Process Modeler" exporterVersion="2.0" targetNamespace="http://www.omg.org/bpmn20">
<bpmn2:itemDefinition id="_marksItem" structureRef="Integer"/>
<bpmn2:itemDefinition id="_resultItem" structureRef="String"/>
<bpmn2:itemDefinition id="_universityItem" structureRef="String"/>
<bpmn2:itemDefinition id="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_fileNameInputXItem" structureRef="java.lang.String"/>
<bpmn2:itemDefinition id="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputXItem" structureRef="java.lang.String"/>
<bpmn2:itemDefinition id="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputXItem" structureRef="java.lang.String"/>
<bpmn2:itemDefinition id="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_decisionInputXItem" structureRef="java.lang.String"/>
<bpmn2:itemDefinition id="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_marksInputXItem" structureRef="Integer"/>
<bpmn2:itemDefinition id="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_resultOutputXItem" structureRef="String"/>
<bpmn2:collaboration id="_A34F0437-B798-4AEE-BDE6-EF9CFCA8F336" name="Default Collaboration">
<bpmn2:participant id="_60CF6AAE-8C75-4C03-A663-737FE209A0AC" name="Pool Participant" processRef="DynamicDmnProcess"/>
</bpmn2:collaboration>
<bpmn2:process id="DynamicDmnProcess" drools:packageName="decision" drools:version="1.0" drools:adHoc="false" name="DynamicDmnProcess" isExecutable="true" processType="Public">
<bpmn2:property id="marks" itemSubjectRef="_marksItem" name="marks"/>
<bpmn2:property id="result" itemSubjectRef="_resultItem" name="result"/>
<bpmn2:property id="university" itemSubjectRef="_universityItem" name="university"/>
<bpmn2:sequenceFlow id="_14DB8038-D485-4BCE-BFD9-7A98B9DC3361" sourceRef="_3EDB5055-D95B-43D0-A87A-A827EE7229A7" targetRef="_C61E8BCD-A87D-4976-B519-FF3DB53CE306">
<bpmn2:extensionElements>
<drools:metaData name="isAutoConnection.source">
<drools:metaValue><![CDATA[true]]></drools:metaValue>
</drools:metaData>
<drools:metaData name="isAutoConnection.target">
<drools:metaValue><![CDATA[true]]></drools:metaValue>
</drools:metaData>
</bpmn2:extensionElements>
</bpmn2:sequenceFlow>
<bpmn2:sequenceFlow id="_BB6D2A53-9775-4654-AC1B-31E35737510A" sourceRef="_BF61E48E-A824-4D10-8198-A17D3F081B70" targetRef="_3EDB5055-D95B-43D0-A87A-A827EE7229A7">
<bpmn2:extensionElements>
<drools:metaData name="isAutoConnection.source">
<drools:metaValue><![CDATA[true]]></drools:metaValue>
</drools:metaData>
<drools:metaData name="isAutoConnection.target">
<drools:metaValue><![CDATA[true]]></drools:metaValue>
</drools:metaData>
</bpmn2:extensionElements>
</bpmn2:sequenceFlow>
<bpmn2:endEvent id="_C61E8BCD-A87D-4976-B519-FF3DB53CE306">
<bpmn2:incoming>_14DB8038-D485-4BCE-BFD9-7A98B9DC3361</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:businessRuleTask id="_3EDB5055-D95B-43D0-A87A-A827EE7229A7" name="Exam" implementation="http://www.jboss.org/drools/dmn">
<bpmn2:extensionElements>
<drools:metaData name="elementname">
<drools:metaValue><![CDATA[Exam]]></drools:metaValue>
</drools:metaData>
</bpmn2:extensionElements>
<bpmn2:incoming>_BB6D2A53-9775-4654-AC1B-31E35737510A</bpmn2:incoming>
<bpmn2:outgoing>_14DB8038-D485-4BCE-BFD9-7A98B9DC3361</bpmn2:outgoing>
<bpmn2:ioSpecification>
<bpmn2:dataInput id="_3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputX" drools:dtype="java.lang.String" itemSubjectRef="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputXItem" name="namespace"/>
<bpmn2:dataInput id="_3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputX" drools:dtype="java.lang.String" itemSubjectRef="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputXItem" name="model"/>
<bpmn2:dataInput id="_3EDB5055-D95B-43D0-A87A-A827EE7229A7_marksInputX" drools:dtype="Integer" itemSubjectRef="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_marksInputXItem" name="marks"/>
<bpmn2:dataOutput id="_3EDB5055-D95B-43D0-A87A-A827EE7229A7_resultOutputX" drools:dtype="String" itemSubjectRef="__3EDB5055-D95B-43D0-A87A-A827EE7229A7_resultOutputXItem" name="result"/>
<bpmn2:inputSet>
<bpmn2:dataInputRefs>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputX</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputX</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_marksInputX</bpmn2:dataInputRefs>
</bpmn2:inputSet>
<bpmn2:outputSet>
<bpmn2:dataOutputRefs>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_resultOutputX</bpmn2:dataOutputRefs>
</bpmn2:outputSet>
</bpmn2:ioSpecification>
<bpmn2:dataInputAssociation>
<bpmn2:targetRef>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputX</bpmn2:targetRef>
<bpmn2:assignment>
<bpmn2:from xsi:type="bpmn2:tFormalExpression"><![CDATA[decision]]></bpmn2:from>
<bpmn2:to xsi:type="bpmn2:tFormalExpression"><![CDATA[_3EDB5055-D95B-43D0-A87A-A827EE7229A7_namespaceInputX]]></bpmn2:to>
</bpmn2:assignment>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation>
<bpmn2:targetRef>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputX</bpmn2:targetRef>
<bpmn2:assignment>
<bpmn2:from xsi:type="bpmn2:tFormalExpression"><![CDATA[#{university}]]></bpmn2:from>
<bpmn2:to xsi:type="bpmn2:tFormalExpression"><![CDATA[_3EDB5055-D95B-43D0-A87A-A827EE7229A7_modelInputX]]></bpmn2:to>
</bpmn2:assignment>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation>
<bpmn2:sourceRef>marks</bpmn2:sourceRef>
<bpmn2:targetRef>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_marksInputX</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataOutputAssociation>
<bpmn2:sourceRef>_3EDB5055-D95B-43D0-A87A-A827EE7229A7_resultOutputX</bpmn2:sourceRef>
<bpmn2:targetRef>result</bpmn2:targetRef>
</bpmn2:dataOutputAssociation>
</bpmn2:businessRuleTask>
<bpmn2:startEvent id="_BF61E48E-A824-4D10-8198-A17D3F081B70">
<bpmn2:outgoing>_BB6D2A53-9775-4654-AC1B-31E35737510A</bpmn2:outgoing>
</bpmn2:startEvent>
</bpmn2:process>
<bpmndi:BPMNDiagram>
<bpmndi:BPMNPlane bpmnElement="DynamicDmnProcess">
<bpmndi:BPMNShape id="shape__BF61E48E-A824-4D10-8198-A17D3F081B70" bpmnElement="_BF61E48E-A824-4D10-8198-A17D3F081B70">
<dc:Bounds height="56" width="56" x="392" y="428"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape__3EDB5055-D95B-43D0-A87A-A827EE7229A7" bpmnElement="_3EDB5055-D95B-43D0-A87A-A827EE7229A7">
<dc:Bounds height="102" width="154" x="528" y="405"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape__C61E8BCD-A87D-4976-B519-FF3DB53CE306" bpmnElement="_C61E8BCD-A87D-4976-B519-FF3DB53CE306">
<dc:Bounds height="56" width="56" x="762" y="428"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge_shape__BF61E48E-A824-4D10-8198-A17D3F081B70_to_shape__3EDB5055-D95B-43D0-A87A-A827EE7229A7" bpmnElement="_BB6D2A53-9775-4654-AC1B-31E35737510A">
<di:waypoint x="448" y="456"/>
<di:waypoint x="528" y="456"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge_shape__3EDB5055-D95B-43D0-A87A-A827EE7229A7_to_shape__C61E8BCD-A87D-4976-B519-FF3DB53CE306" bpmnElement="_14DB8038-D485-4BCE-BFD9-7A98B9DC3361">
<di:waypoint x="682" y="456"/>
<di:waypoint x="762" y="456"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
<bpmn2:relationship type="BPSimData">
<bpmn2:extensionElements>
<bpsim:BPSimData>
<bpsim:Scenario id="default" name="Simulationscenario">
<bpsim:ScenarioParameters/>
<bpsim:ElementParameters elementRef="_BF61E48E-A824-4D10-8198-A17D3F081B70">
<bpsim:TimeParameters>
<bpsim:ProcessingTime>
<bpsim:NormalDistribution mean="0" standardDeviation="0"/>
</bpsim:ProcessingTime>
</bpsim:TimeParameters>
</bpsim:ElementParameters>
<bpsim:ElementParameters elementRef="_3EDB5055-D95B-43D0-A87A-A827EE7229A7">
<bpsim:TimeParameters>
<bpsim:ProcessingTime>
<bpsim:NormalDistribution mean="0" standardDeviation="0"/>
</bpsim:ProcessingTime>
</bpsim:TimeParameters>
<bpsim:ResourceParameters>
<bpsim:Availability>
<bpsim:FloatingParameter value="0"/>
</bpsim:Availability>
<bpsim:Quantity>
<bpsim:FloatingParameter value="0"/>
</bpsim:Quantity>
</bpsim:ResourceParameters>
<bpsim:CostParameters>
<bpsim:UnitCost>
<bpsim:FloatingParameter value="0"/>
</bpsim:UnitCost>
</bpsim:CostParameters>
</bpsim:ElementParameters>
</bpsim:Scenario>
</bpsim:BPSimData>
</bpmn2:extensionElements>
<bpmn2:source>_hi-_MJ_1ED2CT4MzKBb52A</bpmn2:source>
<bpmn2:target>_hi-_MJ_1ED2CT4MzKBb52A</bpmn2:target>
</bpmn2:relationship>
</bpmn2:definitions>

0 comments on commit 39c2596

Please sign in to comment.