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

Edson Tirelli (JIRA) jira-events at lists.jboss.org
Tue Apr 7 19:31:22 EDT 2009


     [ https://jira.jboss.org/jira/browse/JBRULES-2011?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Edson Tirelli closed JBRULES-2011.
----------------------------------

    Resolution: Rejected


Although the Drools 4.0.x API is permissive and allow you to do what you are doing, it is completely wrong and may cause undeterministic behavior:

// THE FOLLOWING IS WRONG
                    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);
                    } 

The right way of doing is to simply get the new package with the updated rules and add it to the rulebase. The rulebase will replace an old rule by a new one if they have the same name, or merge the new rules into the rulebase if they have different names.

Drools 5 is exposing a much more restrictive and safer API, that will help users avoid such mistakes.

If I misunderstood your case, just let me know, with further explanation.

Thanks,
Edson




> 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: Edson Tirelli
>   Original Estimate: 1 hour
>  Remaining Estimate: 1 hour
>
> 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.   
> 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();
>     }
> }

-- 
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