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

Calling addPackage causes rules that use "collect" to incorrectly fire.

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Do
    • Icon: Major Major
    • None
    • 4.0.7
    • None
    • Compatibility/Configuration
    • High

      Steps to reproduce:
      1. Create a rulebase containing at least one Package. The package should contain at least one rule that uses the "collect" statement (ie, with a condition on how many elements are collected) and no additional conditional elements in the LHS of the rule.
      2. Request a new StatefulSession using myRuleBase.newStatefulSession() or myRuleBase.newStatefulSession(true).
      3. Add an altered package to the RuleBase using myRuleBase.addPackage;

      Expected:
      nothing

      Result: the call to addPackage causes a "fireAllRules". During this process, rules that depend on a "collect" statement are executed whether or not the conditioned number of facts collected was met.

      I am sorry not having the time to further investigate different combinations and/or versions of the software myself.

      Below is a testcase that demonstrates the behaviour. Note: myRuleBase.newStatefulSession(false) does not cause this behaviour.

      public class TestAddPkg extends TestCase {
      private static boolean alarmRaised;

      public static void raiseAlarm()

      { alarmRaised = true; }

      public void testKeepReferenceFalse() throws Exception

      { alarmRaised = false; RuleBase rules = null; rules = addSourceToRules(createRule1(), rules); rules.newStatefulSession(false); rules = addSourceToRules(createRule2(), rules); // We never inserted any alarms, so... assertFalse(alarmRaised); }

      public void testKeepReferenceTrue() throws Exception

      { alarmRaised = false; RuleBase rules = null; rules = addSourceToRules(createRule1(), rules); rules.newStatefulSession(true); rules = addSourceToRules(createRule2(), rules); // We never inserted any alarms, so... assertFalse(alarmRaised); }

      private RuleBase addSourceToRules(String source, RuleBase rules) throws DroolsParserException, IOException,
      Exception {
      System.out.println(source);
      StringReader reader = new StringReader(source);
      PackageBuilder builder = new PackageBuilder();
      builder.addPackageFromDrl(reader);

      Package pkg = builder.getPackage();
      if (rules == null)

      { rules = RuleBaseFactory.newRuleBase(); rules.addPackage(pkg); }

      else {
      Package[] packages = rules.getPackages();
      for (Package pack : packages) {
      if (pack.getName().equals(pkg.getName())) {
      Vector<Rule> rulesVector = new Vector<Rule>();
      Rule[] packageRules = pack.getRules();
      {
      for (Rule rule : packageRules)

      { rulesVector.add(rule); }

      }
      rules.removePackage(pack.getName());
      for (Rule rule : rulesVector)

      { pkg.addRule(rule); }

      }
      }
      rules.addPackage(pkg);
      }
      return rules;
      }

      public class Alarm {

      private String level;

      public Alarm(String level)

      { this.level = level; }

      public void setLevel(String level) { this.level = level; }

      public String getLevel()

      { return level; }

      }

      private String createRule2()

      { StringBuilder sb = new StringBuilder(); sb.append("package alarms; \n"); sb.append("import java.util.ArrayList; \n"); sb.append("TestAddPkg; \n"); sb.append("TestAddPkg.Alarm; \n"); sb.append("rule highLvlAlarm \n"); sb.append("when \n"); sb.append(" $a : ArrayList(size > 0) from collect (Alarm( level != \"low\") )\n"); sb.append("then \n"); sb.append(" // callTheCops(); \n"); sb.append(" TestAddPkg.raiseAlarm(); \n"); sb.append(" System.out.println(\">>>Police alerted.<<<\"); \n"); sb.append("end \n"); return sb.toString(); }

      private String createRule1()

      { StringBuilder sb = new StringBuilder(); sb.append("package alarms; \n"); sb.append("import java.util.ArrayList; \n"); sb.append("import TestAddPkg; \n"); sb.append("import TestAddPkg.Alarm; \n"); sb.append("rule lowLvlAlarm \n"); sb.append("when \n"); sb.append(" $a : ArrayList(size > 0) from collect (Alarm( level == \"low\")) \n"); sb.append("then \n"); sb.append(" // quietlyInvestigateLowLvlAlarms(); \n"); sb.append(" System.out.println(\"All is good.\"); \n"); sb.append("end \n"); return sb.toString(); }

      }

              etirelli@redhat.com Edson Tirelli
              jannemanrobinson Janneman Robinson (Inactive)
              Archiver:
              rhn-support-ceverson Clark Everson

                Created:
                Updated:
                Resolved:
                Archived:

                  Estimated:
                  Original Estimate - 1 hour
                  1h
                  Remaining:
                  Remaining Estimate - 1 hour
                  1h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified