[rules-users] Cant seem to fire a dynamically added rule

Mark Proctor mproctor at codehaus.org
Mon Mar 3 15:02:58 EST 2008


check 4.0.x branch as we have lots of fixes there. If you continue to 
have problems, let me know.
http://anonsvn.labs.jboss.com/labs/jbossrules/branches/4.0.x/

samirsss wrote:
> 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
>   
>   




More information about the rules-users mailing list