Details
-
Bug
-
Resolution: Won't Do
-
Major
-
None
-
4.0.7
-
None
-
Compatibility/Configuration
-
High
Description
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)
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)
}
rules.removePackage(pack.getName());
for (Rule rule : rulesVector)
}
}
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(); }}
Attachments
Issue Links
- duplicates
-
JBRULES-1410 Rules with Collect / Accumulate CEs not working correctly when dinamically added to a rulebase
- Closed