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

mariofusco 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 f3179021b7 [KIE-1062] PhreakActivationIterator collects duplicated 
activations when serializing a KieSession (#5817)
f3179021b7 is described below

commit f3179021b7e97499e77a79ddceec483f5d568449
Author: Mario Fusco <[email protected]>
AuthorDate: Wed Apr 3 08:32:04 2024 +0200

    [KIE-1062] PhreakActivationIterator collects duplicated activations when 
serializing a KieSession (#5817)
---
 .../iterators/PhreakActivationIterator.java        |  26 ++++-
 .../compiler/common/InternalMatchIteratorTest.java | 110 ++++++++++++++++++++-
 2 files changed, 127 insertions(+), 9 deletions(-)

diff --git 
a/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/iterators/PhreakActivationIterator.java
 
b/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/iterators/PhreakActivationIterator.java
index a768f27e24..b12da2c012 100644
--- 
a/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/iterators/PhreakActivationIterator.java
+++ 
b/drools-serialization-protobuf/src/main/java/org/drools/serialization/protobuf/iterators/PhreakActivationIterator.java
@@ -18,6 +18,13 @@
  */
 package org.drools.serialization.protobuf.iterators;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
 import org.drools.base.reteoo.NodeTypeEnums;
 import org.drools.core.common.InternalFactHandle;
 import org.drools.core.common.InternalWorkingMemory;
@@ -26,14 +33,25 @@ import org.drools.core.common.ReteEvaluator;
 import org.drools.core.impl.InternalRuleBase;
 import org.drools.core.reteoo.AccumulateNode.AccumulateContext;
 import org.drools.core.reteoo.AccumulateNode.AccumulateMemory;
-import org.drools.core.reteoo.*;
+import org.drools.core.reteoo.BetaMemory;
+import org.drools.core.reteoo.BetaNode;
 import org.drools.core.reteoo.FromNode.FromMemory;
+import org.drools.core.reteoo.LeftInputAdapterNode;
+import org.drools.core.reteoo.LeftTuple;
+import org.drools.core.reteoo.LeftTupleSink;
+import org.drools.core.reteoo.LeftTupleSource;
+import org.drools.core.reteoo.ObjectSource;
+import org.drools.core.reteoo.ObjectTypeNode;
+import org.drools.core.reteoo.RightTuple;
+import org.drools.core.reteoo.RuleTerminalNode;
+import org.drools.core.reteoo.TerminalNode;
+import org.drools.core.reteoo.Tuple;
+import org.drools.core.reteoo.TupleImpl;
+import org.drools.core.reteoo.TupleMemory;
 import org.drools.core.rule.consequence.InternalMatch;
 import org.drools.core.util.FastIterator;
 import org.drools.core.util.Iterator;
 
-import java.util.*;
-
 public class PhreakActivationIterator
     implements
     Iterator<InternalMatch> {
@@ -208,7 +226,7 @@ public class PhreakActivationIterator
                 }
             } else if ( peer.getFirstChild() != null ) {
                 for (TupleImpl childLt = peer.getFirstChild(); childLt != 
null; childLt = childLt.getHandleNext()) {
-                    collectFromLeftInput(childLt, internalMatches, nodeSet, 
reteEvaluator);
+                    collectFromPeers(childLt, internalMatches, nodeSet, 
reteEvaluator);
                 }
             } else if (peer.getSink().getType() == 
NodeTypeEnums.RuleTerminalNode ) {
                 internalMatches.add((InternalMatch) peer);
diff --git 
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/compiler/common/InternalMatchIteratorTest.java
 
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/compiler/common/InternalMatchIteratorTest.java
index 06b52dce02..88d1338466 100644
--- 
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/compiler/common/InternalMatchIteratorTest.java
+++ 
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/compiler/common/InternalMatchIteratorTest.java
@@ -18,16 +18,17 @@
  */
 package org.drools.mvel.compiler.common;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
-import org.drools.core.rule.consequence.InternalMatch;
-import org.drools.serialization.protobuf.iterators.ActivationIterator;
 import org.drools.core.common.InternalAgenda;
 import org.drools.core.common.InternalWorkingMemory;
 import org.drools.core.impl.RuleBaseFactory;
+import org.drools.core.rule.consequence.InternalMatch;
 import org.drools.core.util.Iterator;
+import org.drools.serialization.protobuf.iterators.ActivationIterator;
 import org.drools.testcoverage.common.util.KieBaseTestConfiguration;
 import org.drools.testcoverage.common.util.KieBaseUtil;
 import org.drools.testcoverage.common.util.TestParametersUtil;
@@ -654,9 +655,9 @@ public class InternalMatchIteratorTest {
         KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", 
kieBaseTestConfiguration, str);
         KieSession ksession = kbase.newKieSession();
 
-        ksession.insert( new Integer( 1 ) );
-        ksession.insert( new Integer( 2 ) );
-        ksession.insert( new Integer( 3 ) );
+        ksession.insert( 1 );
+        ksession.insert( 2 );
+        ksession.insert( 3 );
 
         ksession.fireAllRules();
 
@@ -754,4 +755,103 @@ public class InternalMatchIteratorTest {
 
         assertThat(list.size()).isEqualTo(1);
     }
+
+    @Test
+    public void testCollectAndCountActivationsWithNodesSharing() {
+        // KIE-1062
+        String str =
+                "import " + CartLineDetails.class.getCanonicalName() + ";" +
+                        "\n" +
+                        "rule R1 when\n" +
+                        "        exists($cartLineDetails1 : 
CartLineDetails(cartLineProductId in (\"Product1\" ) &&\n" +
+                        "                    cartLineProductCategoryId == 
\"Category1\"))\n" +
+                        "        Number(doubleValue() > 1) from\n" +
+                        "            accumulate ( 
CartLineDetails(cartLineProductId in (\"Product1\" , \"NotUsedProduct\" ) &&\n" 
+
+                        "                            $qty : 
cartLineItemQuantity != null), sum($qty) )\n" +
+                        "        Number(doubleValue() > 1) from\n" +
+                        "            accumulate ( 
CartLineDetails(cartLineProductId in (\"Product2\" ) &&\n" +
+                        "                            $qty : 
cartLineItemQuantity != null), sum($qty) )\n" +
+                        "        Boolean(this == true)\n" +
+                        "        $cartLineDetails4 : 
CartLineDetails(cartLineProductCategoryId == \"Category1\")\n" +
+                        "    then\n" +
+                        "end\n" +
+                        "\n" +
+                        "rule R2 when\n" +
+                        "        exists($cartLineDetails1 : 
CartLineDetails(cartLineProductId in (\"Product1\" ) &&\n" +
+                        "                    cartLineProductCategoryId == 
\"Category1\"))\n" +
+                        "        Number(doubleValue() > 1) from\n" +
+                        "            accumulate ( 
CartLineDetails(cartLineProductId in (\"Product1\" , \"NotUsedProduct\" ) &&\n" 
+
+                        "                        $qty : cartLineItemQuantity 
!= null), sum($qty) )\n" +
+                        "        Number(doubleValue() > 1) from\n" +
+                        "            accumulate ( 
CartLineDetails(cartLineProductId in (\"Product2\" ) &&\n" +
+                        "                            $qty : 
cartLineItemQuantity != null), sum($qty) )\n" +
+                        "        Boolean(this == false)\n" +
+                        "        $cartLineDetails4 : 
CartLineDetails(cartLineProductCategoryId == \"Category1\")\n" +
+                        "    then\n" +
+                        "end";
+
+        KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", 
kieBaseTestConfiguration, str);
+        KieSession kieSession = kbase.newKieSession();
+
+        int totalMatchingLineItems = 4;
+        List<CartLineDetails> cartLineDetails = 
generateCartDetails(totalMatchingLineItems);
+
+        for (CartLineDetails lineItem : cartLineDetails) {
+            kieSession.insert(lineItem);
+        }
+        kieSession.insert(true);
+
+        kieSession.fireAllRules();
+
+        Iterator it = ActivationIterator.iterator( kieSession );
+        List<InternalMatch> matches = new ArrayList<>();
+        for (InternalMatch act = (InternalMatch) it.next(); act != null; act = 
(InternalMatch) it.next() ) {
+            matches.add( act );
+        }
+
+        assertThat(matches.size()).isEqualTo(totalMatchingLineItems);
+    }
+
+    private List<CartLineDetails> generateCartDetails(int 
totalMatchingLineItems) {
+        List<CartLineDetails> lineItemList = new ArrayList<>();
+        for(int i = 1 ; i <= totalMatchingLineItems; i++){
+            CartLineDetails lineItem = new CartLineDetails();
+            lineItem.setCartLineProductId("Product" + i);
+            lineItem.setCartLineProductCategoryId("Category" + "1");
+            lineItem.setCartLineItemQuantity(10.0);
+            lineItemList.add(lineItem);
+        }
+        return lineItemList;
+    }
+
+    public static class CartLineDetails implements Serializable {
+        private static final long serialVersionUID = 1L;
+        private String cartLineProductCategoryId;
+        private Double cartLineItemQuantity;
+        private String cartLineProductId;
+
+        public String getCartLineProductCategoryId(){
+            return cartLineProductCategoryId;
+        }
+
+        public void setCartLineProductCategoryId( String 
cartLineProductCategoryId ){
+            this.cartLineProductCategoryId = cartLineProductCategoryId;
+        }
+
+        public Double getCartLineItemQuantity(){
+            return cartLineItemQuantity;
+        }
+
+        public void setCartLineItemQuantity( Double cartLineItemQuantity ){
+            this.cartLineItemQuantity = cartLineItemQuantity;
+        }
+
+        public String getCartLineProductId(){
+            return cartLineProductId;
+        }
+
+        public void setCartLineProductId( String cartLineProductId ){
+            this.cartLineProductId = cartLineProductId;
+        }
+    }
 }


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

Reply via email to