tkobayas commented on PR #7116:
URL: https://github.com/apache/incubator-kie/pull/7116#issuecomment-5806749225

   @Rikkola  Thank you for the fix.
   
   One more point, when a rule is removed, we also need to remove the 
registered eager prototype in `mergeEagerProtos()`.
   
   Test case:
   ```
   @ParameterizedTest(name = "KieBase type={0}")
   @MethodSource("parameters")
   public void testRemoveSiblingRule_afterTopLevelNegationSegmentSplit(
           KieBaseTestConfiguration kieBaseTestConfiguration) {
   
       final String drlSubject =
               "package repro.b;\n" +
               "import java.util.ArrayList;\n" +
               "import java.util.LinkedList;\n" +
               "rule \"subject\"\n" +
               "    when\n" +
               "        String()\n" +
               "        ArrayList()\n" +
               "        not LinkedList()\n" +
               "    then\n" +
               "end\n";
   
       final String drlSibling =
               "package repro.c;\n" +
               "import java.util.ArrayList;\n" +
               "import java.util.HashMap;\n" +
               "rule \"sibling\"\n" +
               "    when\n" +
               "        String()\n" +
               "        ArrayList()\n" +
               "        HashMap()\n" +
               "    then\n" +
               "end\n";
   
       final InternalKnowledgeBase kbase = (InternalKnowledgeBase) 
KieBaseUtil.getKieBaseFromKieModuleFromDrl(
               "remove-sibling-top-level-not-test", kieBaseTestConfiguration, 
drlSubject);
       final KieBase siblingBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl(
               "remove-sibling-top-level-not-sibling", 
kieBaseTestConfiguration, drlSibling);
   
       // Add the sibling after the subject's segment exists, forcing a split 
that makes the NotNode segment eager.
       kbase.addPackages(siblingBase.getKiePackages());
       // Merging the segments again must remove the obsolete eager prototype 
before a new session is created.
       kbase.removeRule("repro.c", "sibling");
   
       final KieSession session = kbase.newKieSession();
       try {
           session.insert("start");
           session.insert(new java.util.ArrayList<>());
           assertThat(session.fireAllRules())
                   .as("After removing sibling and merging segments, subject 
must still fire")
                   .isEqualTo(1);
       } finally {
           session.dispose();
       }
   }
   ```
   
   Possible fix:
   ```
   diff --git 
a/drools-core/src/main/java/org/drools/core/reteoo/SegmentMemory.java 
b/drools-core/src/main/java/org/drools/core/reteoo/SegmentMemory.java
   index b2953361bff..1d7c727dbb6 100644
   --- a/drools-core/src/main/java/org/drools/core/reteoo/SegmentMemory.java
   +++ b/drools-core/src/main/java/org/drools/core/reteoo/SegmentMemory.java
   @@ -922,8 +922,8 @@ public class SegmentMemory extends 
LinkedList<SegmentMemory>
                }
    
                SegmentPrototype[] eager = endNode.getEagerSegmentPrototypes();
   -            if (requiresEager() && proto2.requiresEager()) {
   -                // keep proto1 and remove proto2
   +            if ((!requiresEager() && proto2WasEager) || (requiresEager() && 
proto2.requiresEager())) {
   +                // remove proto2 from the eager array
                    SegmentPrototype[] newEager = new 
SegmentPrototype[eager.length - 1];
                    copyWithRemoval(eager, newEager, proto2);
                    endNode.setEagerSegmentPrototypes(newEager);
   ```
   
   Without the fix, the test case fails with an 
`ArrayIndexOutOfBoundsException`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to