[jboss-jira] [JBoss JIRA] Created: (JBRULES-2011) Calling addPackage causes rules that use "collect" to incorrectly fire.

Janneman Robinson (JIRA) jira-events at lists.jboss.org
Tue Mar 17 09:07:22 EDT 2009


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

                 Key: JBRULES-2011
                 URL: https://jira.jboss.org/jira/browse/JBRULES-2011
             Project: JBoss Drools
          Issue Type: Bug
      Security Level: Public (Everyone can see)
    Affects Versions: 4.0.7
         Environment: Windows XP using Java version 1.6.
            Reporter: Janneman Robinson
            Assignee: Mark Proctor


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(x);

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.   

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("   QuickTest.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();
    }
}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the jboss-jira mailing list