Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-1329

RuleBase.removeRule() prevents other rules from being applied

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 4.0.3
    • Fix Version/s: 4.0.5, 5.0.0.M1
    • Component/s: None
    • Labels:
      None
    • Environment:

      JDK 1.6.0

      Description

      After adding a package containing two rules and then removing one of the rules, the remaining rule is no longer activated when a matching fact is asserted. Following is a unit test displaying this behavior, along with its .drl file.

      import java.io.InputStreamReader;
      import java.io.Reader;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Properties;

      import junit.framework.TestCase;

      import org.drools.RuleBase;
      import org.drools.RuleBaseFactory;
      import org.drools.StatelessSession;
      import org.drools.WorkingMemory;
      import org.drools.compiler.PackageBuilder;
      import org.drools.compiler.PackageBuilderConfiguration;
      import org.drools.event.AfterActivationFiredEvent;
      import org.drools.event.DefaultAgendaEventListener;
      import org.drools.rule.Package;
      import org.drools.rule.Rule;

      public class DroolsRemoveRuleTest extends TestCase
      {
      /**

      • Create a new RuleBase from a package containing two rules, "Hello" and
      • "Goodbye". First verify both rules are applied as expected. Then remove
      • the "Hello" rule and verify the "Hello" rule is no longer applied, but
      • the "Goodbye" rule is applied. In Drools 4.0.3, the removal of the
      • "Hello" rule causes the "Goodbye" rule to no longer be applied.
        */
        public void testRemoveRule() throws Exception { Reader removeRuleSource = new InputStreamReader(getClass().getResourceAsStream("testRemoveRule.drl")); Properties props = new Properties(); props.setProperty("drools.dialect.java.compiler", "JANINO"); PackageBuilderConfiguration cfg = new PackageBuilderConfiguration(props); PackageBuilder packageBuilder = new PackageBuilder(cfg); packageBuilder.addPackageFromDrl(removeRuleSource); assertFalse(packageBuilder.hasErrors()); Package pkg = packageBuilder.getPackage(); RuleBase ruleBase = RuleBaseFactory.newRuleBase(); ruleBase.addPackage(pkg); Message hello = new Message(); hello.setName("Chuck"); hello.setType(Type.HELLO); Message goodbye = new Message(); goodbye.setName("Charles"); goodbye.setType(Type.GOODBYE); StatelessSession session = ruleBase.newStatelessSession(); FiredRulesListener listener = new FiredRulesListener(); session.addEventListener(listener); session.execute(hello); assertEquals(1, listener.getFiredRules().size()); assertEquals("Hello", listener.getFiredRules().get(0).getName()); session = ruleBase.newStatelessSession(); listener = new FiredRulesListener(); session.addEventListener(listener); session.execute(goodbye); assertEquals(1, listener.getFiredRules().size()); assertEquals("Goodbye", listener.getFiredRules().get(0).getName()); ruleBase.removeRule(pkg.getName(), "Hello"); session = ruleBase.newStatelessSession(); listener = new FiredRulesListener(); session.addEventListener(listener); session.execute(hello); assertEquals(0, listener.getFiredRules().size()); session = ruleBase.newStatelessSession(); listener = new FiredRulesListener(); session.addEventListener(listener); session.execute(goodbye); // The following assertion currently fails assertEquals(1, listener.getFiredRules().size()); assertEquals("Goodbye", listener.getFiredRules().get(0).getName()); }

      public static class FiredRulesListener extends DefaultAgendaEventListener
      {
      List<Rule> firedRules = new ArrayList<Rule>();

      public List<Rule> getFiredRules()

      { return this.firedRules; }

      @Override
      public void afterActivationFired(AfterActivationFiredEvent event, WorkingMemory workingMemory)

      { Rule rule = event.getActivation().getRule(); this.firedRules.add(rule); }

      }

      public static enum Type

      { HELLO, GOODBYE }

      ;

      public static class Message
      {
      private Type type = null;
      private String name = null;

      public Type getType()

      { return this.type; }

      public void setType(Type type)

      { this.type = type; }

      public String getName()

      { return this.name; }

      public void setName(String name)

      { this.name = name; }

      }
      }

      package test

      import DroolsRemoveRuleTest.Message
      import DroolsRemoveRuleTest.Type

      rule "Hello"
      dialect "java"
      when
      Message( type == Type.HELLO, name : name )
      then
      System.out.println("Hello " + name);
      end

      rule "Goodbye"
      dialect "java"
      when
      Message( type == Type.GOODBYE, name : name )
      then
      System.out.println("Goodbye " + name);
      end

        Gliffy Diagrams

          Attachments

          1. DroolsRemoveRuleTest.java
            4 kB
          2. Tester.java
            5 kB
          3. testRemoveRule.drl
            0.4 kB

            Issue Links

              Activity

                People

                • Assignee:
                  tirelli Edson Tirelli
                  Reporter:
                  jwsparrowubs James Sparrow
                • Votes:
                  2 Vote for this issue
                  Watchers:
                  2 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: