I tried using the jars from the 4.0.x branch after building them and that
still doesnt seem to change the outcome. I see that the package builder has
the rule added to it and the count of the rules from it is incremented.
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 = "\t\t mediator.addedNewRule();\n";
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 FieldConstraintDescr insertedValue = new FieldConstraintDescr(
"inserted" );
final LiteralRestrictionDescr insertedFieldRes = new
LiteralRestrictionDescr("==", "false",
LiteralRestrictionDescr.TYPE_BOOLEAN);
insertedValue.addRestriction(insertedFieldRes);
pattern.addConstraint(insertedValue);
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", "101"));
ruleDescr.addAttribute( new AttributeDescr("dialect", "java")
);
ruleDescr.setAttributes(attributes);
Package defaultPackage = pkg[0];
PackageBuilderConfiguration conf = new PackageBuilderConfiguration();
PackageBuilder builder = new PackageBuilder( defaultPackage, conf );
packageDescr.addRule( ruleDescr );
builder.addPackage(packageDescr);
DrlDumper drlDumper = new DrlDumper();
logger.warn("Dumping Package Descriptor for all rules: "+
drlDumper.dump(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());
logger.warn("Package after adding the rule : "+newRule.toString()+":
contains: "+pkg[0].getRules().length+" rules");
}
}
else
{
logger.warn("Error getting default package for adding rules");
}
}
I am trying to figure out if I am doing something wrong or its an issue with
Drools.
--
View this message in context:
http://www.nabble.com/Cant-seem-to-fire-a-dynamically-added-rule-tp158101...
Sent from the drools - user mailing list archive at
Nabble.com.