Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-2646

KieContainer#updateToVersion removing rules: logically inserted objects not removed correctly

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 7.7.0.Final
    • Fix Version/s: None
    • Component/s: core engine
    • Labels:
      None
    • Sprint:
      2018 Week 48-50
    • Story Points:
      5
    • Steps to Reproduce:
      Hide

      Consider the rule file: SimpleRuleAB.drl

      declare MyIntA
      val : Integer @key
      end

      declare MyIntB
      val : Integer @key
      end

      rule "Simple rule A"
      when
      String(this == "I'm active")
      $i : Integer(intValue > 5)
      then
      insertLogical(new MyIntA($i))
      end

      rule "Simple rule B"
      when
      String(this == "I'm active")
      $i : Integer(intValue <= 5)
      then
      insertLogical(new MyIntB($i))
      end

      The test which reproduces the bug:

      @Test
      public void ruleRemovalTest() {

      /**

      • Create the kie container based on a simple rule from SimpleRule.drl
        */
        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newFileResource("src/test/java/rulesengine/SimpleRuleAB.drl"));
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        KieModule kieModule = kieBuilder.getKieModule();
        KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
        KieSession kieSession = kieContainer.newKieSession();

      /**

      • Insert three Integer objects: 3, 7, 9
        */
        kieSession.insert("I'm active");
        kieSession.insert(new Integer(3));
        kieSession.insert(new Integer(7));
        kieSession.insert(new Integer(9));
        Assert.assertEquals(4, kieSession.getObjects().size());

      /**

      • Fire all rules, verify that two objects of type MyIntA and one object of type
      • MyIntB are created
        */
        kieSession.fireAllRules();
        System.out.println("***************************************************");
        System.out.println("Objects in the working memory after all rules fired");
        for (Object o : kieSession.getObjects()) { System.out.println(o); }
        System.out.println("***************************************************");
        Assert.assertEquals(7, kieSession.getObjects().size());

        /**
        * Update the kie base using the kie module which no longer contains
        * SimpleRule.drl
        */
        kieFileSystem = kieServices.newKieFileSystem();
        kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        kieModule = kieBuilder.getKieModule();
        kieContainer.updateToVersion(kieModule.getReleaseId());

        /**
        * Fire all rules. Logically inserted objects of types MyIntA and MyIntB should be
        * automatically removed from the working memory
        */
        kieSession.fireAllRules();
        System.out.println("***************************************************");
        System.out.println("Objects in the working memory after the SimpleRule.drl is removed");
        for (Object o : kieSession.getObjects()) { System.out.println(o); }

        System.out.println("***************************************************");
        Assert.assertEquals(4, kieSession.getObjects().size());
        }

      Display results:

      ***************************************************
      Objects in the working memory after all rules fired
      I'm active
      7
      3
      9
      MyIntA( val=7 )
      MyIntA( val=9 )
      MyIntB( val=3 )
      ***************************************************

      ***************************************************
      Objects in the working memory after the SimpleRule.drl is removed
      I'm active
      7
      3
      9
      MyIntB( val=3 )
      ***************************************************

      Show
      Consider the rule file: SimpleRuleAB.drl declare MyIntA val : Integer @key end declare MyIntB val : Integer @key end rule "Simple rule A" when String(this == "I'm active") $i : Integer(intValue > 5) then insertLogical(new MyIntA($i)) end rule "Simple rule B" when String(this == "I'm active") $i : Integer(intValue <= 5) then insertLogical(new MyIntB($i)) end The test which reproduces the bug: @Test public void ruleRemovalTest() { /** Create the kie container based on a simple rule from SimpleRule.drl */ KieServices kieServices = KieServices.Factory.get(); KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); kieFileSystem.write(ResourceFactory.newFileResource("src/test/java/rulesengine/SimpleRuleAB.drl")); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem); kieBuilder.buildAll(); KieModule kieModule = kieBuilder.getKieModule(); KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId()); KieSession kieSession = kieContainer.newKieSession(); /** Insert three Integer objects: 3, 7, 9 */ kieSession.insert("I'm active"); kieSession.insert(new Integer(3)); kieSession.insert(new Integer(7)); kieSession.insert(new Integer(9)); Assert.assertEquals(4, kieSession.getObjects().size()); /** Fire all rules, verify that two objects of type MyIntA and one object of type MyIntB are created */ kieSession.fireAllRules(); System.out.println("***************************************************"); System.out.println("Objects in the working memory after all rules fired"); for (Object o : kieSession.getObjects()) { System.out.println(o); } System.out.println("***************************************************"); Assert.assertEquals(7, kieSession.getObjects().size()); /** * Update the kie base using the kie module which no longer contains * SimpleRule.drl */ kieFileSystem = kieServices.newKieFileSystem(); kieBuilder = kieServices.newKieBuilder(kieFileSystem); kieBuilder.buildAll(); kieModule = kieBuilder.getKieModule(); kieContainer.updateToVersion(kieModule.getReleaseId()); /** * Fire all rules. Logically inserted objects of types MyIntA and MyIntB should be * automatically removed from the working memory */ kieSession.fireAllRules(); System.out.println("***************************************************"); System.out.println("Objects in the working memory after the SimpleRule.drl is removed"); for (Object o : kieSession.getObjects()) { System.out.println(o); } System.out.println("***************************************************"); Assert.assertEquals(4, kieSession.getObjects().size()); } Display results: *************************************************** Objects in the working memory after all rules fired I'm active 7 3 9 MyIntA( val=7 ) MyIntA( val=9 ) MyIntB( val=3 ) *************************************************** *************************************************** Objects in the working memory after the SimpleRule.drl is removed I'm active 7 3 9 MyIntB( val=3 ) ***************************************************
    • Docs QE Status:
      NEW
    • QE Status:
      NEW

      Description

      Consider two rules which have one of the patterns in common, each of which logically inserts an object in the RHS. Build the KieModule from the KieFileSystem including these rules, create the KieContainer and the KieSession. Insert the facts. Everything works fine.
      Create a new KieModule excluding these rules from the KieFileSystem, update the KieContainer. Expect the logically inserted facts to be retracted, as the rules which trigged their creation is no longer present. Turns out that is true for the facts inserted by the first rule, but not for the second one: the facts remain in the working memory.

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                mfusco Mario Fusco
                Reporter:
                olga13 Olga Rodionova
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: