[rules-users] Cant seem to fire a dynamically added rule
samirsss
samirsavla at gmail.com
Mon Mar 3 13:43:12 EST 2008
Hi,
Here's the source code of the rule I am trying to add:
public void testDynamicRules()
{
//Create a rule dynamically and check if it fired from log statements
RuleBase ruleBase = wm.getRuleBase();
Package[] pkg = ruleBase.getPackages();
if(pkg[0] != null)
{
logger.warn("Package size before adding the rule :
"+pkg[0].getRules().length);
//Building a new package
final PackageDescr packageDescr = new PackageDescr(pkg[0].getName() );
//Building ruleDescriptor
RuleDescr ruleDescr = null;
String consequence = "mediator.addedNewRule();\n";
//String consequence = "logger.warn(\"New rule just got fired\");";
ruleDescr = new RuleDescr( "Added new Rule");
AndDescr lhs = new AndDescr();
ruleDescr.setLhs( lhs );
final PatternDescr pattern = new PatternDescr(
ApplicationEvent.class.getName(),
"newAppEvent");
lhs.addDescr( pattern );
final LiteralRestrictionDescr restrictionMessage = new
LiteralRestrictionDescr("==", "Test2", LiteralRestrictionDescr.TYPE_STRING);
final FieldConstraintDescr returnValue = new FieldConstraintDescr(
"messageId" );
returnValue.addRestriction(restrictionMessage);
pattern.addConstraint(returnValue);
ruleDescr.setConsequence( consequence );
List attributes = new ArrayList<AttributeDescr>();
attributes.add(new AttributeDescr("salience", "98"));
attributes.add(new AttributeDescr("enabled", "true"));
ruleDescr.addAttribute( new AttributeDescr("dialect", "java") );
ruleDescr.setAttributes(attributes);
Package defaultPackage = pkg[0];
PackageBuilderConfiguration conf = new PackageBuilderConfiguration();
PackageBuilder builder = new PackageBuilder( defaultPackage, conf );
//Adding rule to the package
packageDescr.addRule( ruleDescr );
builder.addPackage(packageDescr);
int errors = builder.getErrors().getErrors().length;
if(errors > 0)
{
//TODO remove the recently added rule or reload all the rules from
the DRL file
logger.warn("Error while adding a rule");
}
else
{
Rule newRule = builder.getPackage().getRule(ruleDescr.getName());
//String ruleFlowName = newRule.getRuleFlowGroup()
//newRule.get
//Check is the rule got added
logger.warn("Package size after adding the rule :
"+pkg[0].getRules().length);
}
}
else
{
logger.warn("Error getting default package for adding rules");
}
}
public void addedNewRule()
{
//Just testing addition of new rules.
logger.warn("New rule just got fired");
}
I cant seem to get the consequence to get fired. I have a similar rule in my
drl file where the messageID == "Test" and it works fine.
Any help is really helpful here cause I am stuck at this point and might
have to go to creating xml files for the rules and parsing that.
Thanks
Samir
--
View this message in context: http://www.nabble.com/Cant-seem-to-fire-a-dynamically-added-rule-tp15810172p15810172.html
Sent from the drools - user mailing list archive at Nabble.com.
More information about the rules-users
mailing list