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

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


The following commit(s) were added to refs/heads/main by this push:
     new 275baf9cf4d Fix accumulate for non-reversible functions (#6796)
275baf9cf4d is described below

commit 275baf9cf4da6a167de22f8036ed89224d508c9e
Author: Mario Fusco <[email protected]>
AuthorDate: Wed Jul 8 15:06:36 2026 +0200

    Fix accumulate for non-reversible functions (#6796)
---
 .../drools/core/phreak/PhreakAccumulateNode.java   |  4 +-
 .../compiler/integrationtests/AccumulateTest.java  | 49 ++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git 
a/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java 
b/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java
index ffb4abf744d..4c10f846417 100644
--- a/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java
+++ b/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java
@@ -379,7 +379,7 @@ public class PhreakAccumulateNode {
                         TupleImpl temp = match.getHandleNext();
                         match.reAddRight();
                         match = temp;
-                        isDirty = accumulate.hasRequiredDeclarations();
+                        isDirty |= accumulate.hasRequiredDeclarations();
                     }
                 } else if (match != null && match.getRightParent() == 
rightTuple) {
                     TupleImpl temp = match.getHandleNext();
@@ -391,7 +391,7 @@ public class PhreakAccumulateNode {
                     // and the accumulate does not support the reverse 
operation, then the whole
                     // result is dirty (since removeMatch above is not 
recalculating the total)
                     // and we need to do this later
-                    isDirty = !reversed;
+                    isDirty |= !reversed;
                 }
                 // else do nothing, was false before and false now.
             }
diff --git 
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/AccumulateTest.java
 
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/AccumulateTest.java
index efa6835adfb..011cb1753e1 100644
--- 
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/AccumulateTest.java
+++ 
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/AccumulateTest.java
@@ -64,6 +64,7 @@ import org.kie.api.runtime.KieContainer;
 import org.kie.api.runtime.KieSession;
 import org.kie.api.runtime.KieSessionConfiguration;
 import org.kie.api.runtime.rule.AccumulateFunction;
+import org.kie.api.definition.type.FactType;
 import org.kie.api.runtime.rule.FactHandle;
 import org.kie.api.runtime.rule.Match;
 import org.kie.api.runtime.rule.QueryResults;
@@ -4240,4 +4241,52 @@ public class AccumulateTest {
             ksession.dispose();
         }
     }
+
+    @ParameterizedTest(name = "KieBase type={0}")
+    @MethodSource("parameters")
+    @Timeout(10000)
+    public void 
testAccumulateMinStaleAfterLeftTupleUpdate(KieBaseTestConfiguration 
kieBaseTestConfiguration) throws Exception {
+        final String drl =
+                "package repro;\n" +
+                "declare P b : long end\n" +
+                "declare S v : long end\n" +
+                "declare G g : long end\n" +
+                "global java.util.List results;\n" +
+                "rule R_acc when\n" +
+                "    $p : P($b : b)\n" +
+                "    accumulate( S(v >= $b, $s : v); $m : min($s) )\n" +
+                "then\n" +
+                "    results.add($m);\n" +
+                "end\n" +
+                "rule R_low salience -5 when $g : G() $p : P()\n" +
+                "then modify($p){ setB(5) } end\n";
+
+        final KieBase kbase = 
KieBaseUtil.getKieBaseFromKieModuleFromDrl("accumulate-test", 
kieBaseTestConfiguration, drl);
+        final KieSession ksession = kbase.newKieSession();
+        try {
+            final List<Number> results = new ArrayList<>();
+            ksession.setGlobal("results", results);
+
+            final FactType P = kbase.getFactType("repro", "P");
+            final FactType S = kbase.getFactType("repro", "S");
+            final FactType G = kbase.getFactType("repro", "G");
+
+            final Object p = P.newInstance(); P.set(p, "b", -10L);
+            final Object s12 = S.newInstance(); S.set(s12, "v", 12L);
+            final Object sm2 = S.newInstance(); S.set(sm2, "v", -2L);
+            final Object g = G.newInstance(); G.set(g, "g", 1L);
+
+            ksession.insert(p);
+            ksession.insert(s12);
+            ksession.insert(sm2);
+            ksession.insert(g);
+            ksession.fireAllRules();
+
+            assertThat(results).hasSize(2);
+            assertThat(results.get(0).longValue()).isEqualTo(-2L);
+            assertThat(results.get(1).longValue()).isEqualTo(12L);
+        } finally {
+            ksession.dispose();
+        }
+    }
 }


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

Reply via email to