This is an automated email from the ASF dual-hosted git repository.

martinweiler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie.git


The following commit(s) were added to refs/heads/main by this push:
     new 636f7f8667f [kie-issue#7297] Prevent premature ad hoc subprocess 
completion by the event listener (#7298)
636f7f8667f is described below

commit 636f7f8667f28fb7a1b5a4f6fc31dbba95cd4967
Author: Martin Weiler <[email protected]>
AuthorDate: Wed Nov 25 12:52:51 2026 -0600

    [kie-issue#7297] Prevent premature ad hoc subprocess completion by the 
event listener (#7298)
---
 .../instance/node/DynamicNodeInstance.java         |   4 +-
 ...MN2-AdHocSubProcessAutoCompleteEmptyStage.bpmn2 | 109 +++++++++++++++++++++
 .../org/jbpm/bpmn2/StandaloneBPMNProcessTest.java  |  26 +++++
 3 files changed, 138 insertions(+), 1 deletion(-)

diff --git 
a/kogito-jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/DynamicNodeInstance.java
 
b/kogito-jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/DynamicNodeInstance.java
index cad4ce63d0f..24d357d3452 100755
--- 
a/kogito-jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/DynamicNodeInstance.java
+++ 
b/kogito-jbpm/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/DynamicNodeInstance.java
@@ -50,7 +50,9 @@ public class DynamicNodeInstance extends 
CompositeContextNodeInstance {
             }
         });
         completeEventListener = ContextAwareEventListener.using(listener -> {
-            if (canComplete() && getState() == 
KogitoProcessInstance.STATE_ACTIVE) {
+            // autocomplete is only evaluated when a child node instance 
completes (see nodeInstanceCompleted),
+            // otherwise an empty ad hoc subprocess would be closed by any 
unrelated event, e.g. a variable change
+            if (!getDynamicNode().isAutoComplete() && canComplete() && 
getState() == KogitoProcessInstance.STATE_ACTIVE) {
                 triggerCompleted(CONNECTION_DEFAULT_TYPE);
             }
         });
diff --git 
a/kogito-jbpm/jbpm-tests/src/test/bpmn/org/jbpm/bpmn2/adhoc/BPMN2-AdHocSubProcessAutoCompleteEmptyStage.bpmn2
 
b/kogito-jbpm/jbpm-tests/src/test/bpmn/org/jbpm/bpmn2/adhoc/BPMN2-AdHocSubProcessAutoCompleteEmptyStage.bpmn2
new file mode 100644
index 00000000000..5b30689ed63
--- /dev/null
+++ 
b/kogito-jbpm/jbpm-tests/src/test/bpmn/org/jbpm/bpmn2/adhoc/BPMN2-AdHocSubProcessAutoCompleteEmptyStage.bpmn2
@@ -0,0 +1,109 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<definitions id="Definition"
+             targetNamespace="http://www.example.org/MinimalExample";
+             typeLanguage="http://www.java.com/javaTypes";
+             expressionLanguage="http://www.mvel.org/2.0";
+             xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL";
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+             xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL 
BPMN20.xsd"
+             xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI";
+             xmlns:dc="http://www.omg.org/spec/DD/20100524/DC";
+             xmlns:di="http://www.omg.org/spec/DD/20100524/DI";
+             xmlns:tns="http://www.jboss.org/drools";>
+
+  <itemDefinition id="_counterItem" structureRef="Integer" />
+  <process processType="Private" isExecutable="true" 
id="AdHocSubProcessAutoCompleteEmptyStage" name="AdHoc SubProcess" 
tns:packageName="org.jbpm.bpmn2.adhoc" >
+
+    <!-- process variables -->
+    <property id="counter" itemSubjectRef="_counterItem"/>
+    <!-- nodes -->
+    <startEvent id="_1" name="StartProcess" />
+    <adHocSubProcess id="_2" name="Hello"  ordering="Parallel" >
+      <extensionElements>
+        <tns:onExit-script scriptFormat="http://www.java.com/java";>
+          <tns:script>kcontext.setVariable("y", "Exit");</tns:script>
+        </tns:onExit-script>
+      </extensionElements>
+    <!-- nodes -->
+    <userTask id="_2-1" name="Hello1" >
+      <ioSpecification>
+        <inputSet>
+        </inputSet>
+        <outputSet>
+        </outputSet>
+      </ioSpecification>
+      <potentialOwner id="_2-3-1">
+        <resourceAssignmentExpression id="_2-3-1-1">
+          <formalExpression>john</formalExpression>
+        </resourceAssignmentExpression>
+      </potentialOwner>
+    </userTask>
+    <!-- connections -->
+    <completionCondition xsi:type="tFormalExpression" 
language="http://www.mvel.org/2.0";>autocomplete</completionCondition>
+    </adHocSubProcess>
+    <scriptTask id="_3" name="Goodbye" >
+      <script>System.out.println("Goodbye World");</script>
+    </scriptTask>
+    <endEvent id="_4" name="EndProcess" >
+        <terminateEventDefinition/>
+    </endEvent>
+
+    <!-- connections -->
+    <sequenceFlow id="_1-_2" sourceRef="_1" targetRef="_2" />
+    <sequenceFlow id="_2-_3" sourceRef="_2" targetRef="_3" />
+    <sequenceFlow id="_3-_4" sourceRef="_3" targetRef="_4" />
+
+  </process>
+
+  <bpmndi:BPMNDiagram>
+    <bpmndi:BPMNPlane bpmnElement="AdHocSubProcess" >
+      <bpmndi:BPMNShape bpmnElement="_1" >
+        <dc:Bounds x="16" y="67" width="48" height="48" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape bpmnElement="_2" >
+        <dc:Bounds x="94" y="39" width="161" height="99" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape bpmnElement="_2-1" >
+        <dc:Bounds x="124" y="70" width="100" height="48" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape bpmnElement="_3" >
+        <dc:Bounds x="292" y="66" width="97" height="48" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape bpmnElement="_4" >
+        <dc:Bounds x="424" y="65" width="48" height="48" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNEdge bpmnElement="_1-_2" >
+        <di:waypoint x="40" y="91" />
+        <di:waypoint x="174" y="88" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge bpmnElement="_2-_3" >
+        <di:waypoint x="174" y="88" />
+        <di:waypoint x="340" y="90" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge bpmnElement="_3-_4" >
+        <di:waypoint x="340" y="90" />
+        <di:waypoint x="448" y="89" />
+      </bpmndi:BPMNEdge>
+    </bpmndi:BPMNPlane>
+  </bpmndi:BPMNDiagram>
+
+</definitions>
diff --git 
a/kogito-jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/StandaloneBPMNProcessTest.java
 
b/kogito-jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/StandaloneBPMNProcessTest.java
index 9c434b7ea8c..d77dca556e0 100755
--- 
a/kogito-jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/StandaloneBPMNProcessTest.java
+++ 
b/kogito-jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/StandaloneBPMNProcessTest.java
@@ -33,6 +33,8 @@ import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.jbpm.bpmn2.activity.ScriptTaskModel;
 import org.jbpm.bpmn2.activity.ScriptTaskProcess;
+import org.jbpm.bpmn2.adhoc.AdHocSubProcessAutoCompleteEmptyStageModel;
+import org.jbpm.bpmn2.adhoc.AdHocSubProcessAutoCompleteEmptyStageProcess;
 import org.jbpm.bpmn2.adhoc.AdHocSubProcessAutoCompleteExpressionModel;
 import org.jbpm.bpmn2.adhoc.AdHocSubProcessAutoCompleteExpressionProcess;
 import org.jbpm.bpmn2.adhoc.AdHocSubProcessAutoCompleteModel;
@@ -620,6 +622,30 @@ public class StandaloneBPMNProcessTest extends 
JbpmBpmn2TestCase {
         
assertThat(instance.status()).isEqualTo(KogitoProcessInstance.STATE_COMPLETED);
     }
 
+    @Test
+    public void testAdHocSubProcessAutoCompleteStaysOpenWhileEmpty() throws 
Exception {
+        // an autocomplete ad hoc subprocess that has no active child must not 
complete just because
+        // some unrelated event (e.g. a variable change) happens; it completes 
only when a child finishes
+        Application app = ProcessTestHelper.newApplication();
+        ProcessTestHelper.registerHandler(app, "Human Task", new 
TestUserTaskWorkItemHandler());
+        
org.kie.kogito.process.Process<AdHocSubProcessAutoCompleteEmptyStageModel> 
definition = AdHocSubProcessAutoCompleteEmptyStageProcess.newProcess(app);
+
+        
org.kie.kogito.process.ProcessInstance<AdHocSubProcessAutoCompleteEmptyStageModel>
 instance = definition.createInstance(definition.createModel());
+        instance.start();
+        
assertThat(instance.status()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);
+
+        AdHocSubProcessAutoCompleteEmptyStageModel update = 
definition.createModel();
+        update.setCounter(1);
+        instance.updateVariablesPartially(update);
+        
assertThat(instance.status()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);
+
+        // the node can still be triggered later and completing it completes 
the subprocess and the process
+        instance.triggerNode("_2-1");
+        ProcessTestHelper.completeWorkItem(instance, Collections.emptyMap(), 
"john");
+
+        
assertThat(instance.status()).isEqualTo(KogitoProcessInstance.STATE_COMPLETED);
+    }
+
     @Test
     public void testAdHocSubProcessAutoCompleteExpression() throws Exception {
         // this autocomplete when we detect


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to