]
James Sparrow updated JBRULES-1329:
-----------------------------------
Attachment: DroolsRemoveRuleTest.java
testRemoveRule.drl
Attached test case and .drl file included in the description
RuleBase.removeRule() prevents other rules from being applied
-------------------------------------------------------------
Key: JBRULES-1329
URL:
http://jira.jboss.com/jira/browse/JBRULES-1329
Project: JBoss Drools
Issue Type: Bug
Security Level: Public(Everyone can see)
Affects Versions: 4.0.3
Environment: JDK 1.6.0
Reporter: James Sparrow
Attachments: DroolsRemoveRuleTest.java, testRemoveRule.drl
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
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: