[rules-users] Question on Rules, Packages, and adding/Removing from RuleBase

James Brannan jamesbrannan at earthlink.net
Tue May 20 11:53:24 EDT 2008


Sorry if this is covered, I looked up in documentation and searched  
here before posting.

env: Drools 4.0.7 running in Eclipse 3.3.2 on Mac OSX 10.5

Here's the confusion......

create 2 rules in agenda A in package A
create 2 rules in agenda B in package A

setFocus(A)
setFocus(B)

load some objects to evaluate in stateless session, call fireallrules  
and works as expected.

now, say I wish to remove the rules in Agenda A from the RuleBase, I  
tried three different ways, including removing the rules directly from  
the rulebase!

But then, when I load in some new objects in a new session, set the  
focus to Agenda A (which I thought I removed all the rules for  
it)...instead of getting nothing or an exception, the rules in agenda  
A still fire, even though I removed them (or I thought I did).

1) how do I remove rules if these three ways dont work (at least they  
dont appear to
- or much more likely -
1) what the heck am I doing wrong....

2) my needs are two add and subtract agendas on the fly, as I will  
have multiple customers with multiple sets of rules with different  
agendas (is there a better way then this - i.e. should I have separate  
rulebases?)

Thanks,
James A. Brannan


package com.sample;

import java.io.*;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.StatefulSession;
import org.drools.rule.Package;
import org.drools.rule.Rule;

/**
  * This is a sample file to launch a rule package from a rule source  
file.
  */
public class DroolsTestTwo {

     public static void main(String[] args) {
         try
         {

         	File dir = new File("/Users/brannanj/Documents/workspace/ 
BankingTutorial/src/main/rules");
         	
         	File[] files = dir.listFiles();
         	
         	
         	PackageBuilder builder = new PackageBuilder();
         	
         	

         	for(int i = 0; i < files.length; i++)
         	{
         		if(files[i].getName().indexOf(".xml") > 0)        				
         			continue;
         		
         		//Reader reader = ;
         		builder.addPackageFromXml(new FileReader(files[i]));
         	}
         	
         	
         	
         	    	
         	Package pkg = builder.getPackage();

         	RuleBase ruleBase = RuleBaseFactory.newRuleBase();
         	ruleBase.addPackage( pkg );
         	ruleBase.addPackage(builder.getPackage());
         		
         	final StatefulSession session =  
ruleBase.newStatefulSession(false);
         		
         	 User user = new User();
              user.addRole("admin");
              user.addRole("tester");
         	

         	
             FooBarInfoTwo y = new FooBarInfoTwo();
             y.setProperty("type");
             y.setValue("cheddar");

             FooBarInfoTwo z = new FooBarInfoTwo();
             z.setProperty("age");
             z.setValue(new Integer(32));

             FooBarInfoTwo a = new FooBarInfoTwo();
             z.setProperty("weight");
             z.setValue(new Double(22.222));

     		FooBarInfo x = new FooBarInfo();
     			
     		x.setValue("color", "red");
     		x.setValue("count", new Integer(23));
     		
     		
     		FooBarInfoTwo b = new FooBarInfoTwo();
             b.setProperty("smelly");
             b.setValue("true");
     		
             FooBarInfoTwo c = new FooBarInfoTwo();
             b.setProperty("birthday");
             b.setValue(new SimpleDate("12/01/2006"));
     		

     		
     		session.insert(x);
     		session.insert(y);
     		session.insert(z);
     		session.insert(a);
     		session.insert(b);
     		session.insert(user);
     		
     		session.setFocus("GroupOne");
     		session.setFocus("GlobalsGroup");
     		session.setFocus("GroupTwo");
             session.fireAllRules();
     		

             //remove GroupOne rules from package

             pkg = builder.getPackage();
             Rule[] rules = pkg.getRules();

             for(int i =0;i<rules.length;i++)
             {
             	System.out.println("Agendagroup in package: " +  
rules[i].getAgendaGroup());
             	if(rules[i].getAgendaGroup().equals("GroupOne"))
             	{
             		pkg.removeRule(rules[i]);
             	}
             }

             System.out.println("GET THE RULES AGAIN TO SEE IF BY  
REFERENCE......");

             pkg = builder.getPackage();
             rules = pkg.getRules();

             for(int i =0;i<rules.length;i++)
             {
             	System.out.println("Agendagroup in package: " +  
rules[i].getAgendaGroup());
             	
             }

             System.out.println("NOW EVALUATE THE RULEBASE  
PACKAGE......");

             pkg = ruleBase.getPackages()[0];


             rules = pkg.getRules();

             for(int i =0;i<rules.length;i++)
             {
             	System.out.println("Agendagroup in package from  
rulebase: " + rules[i].getAgendaGroup());
             	
             }

             session.dispose();


         	final StatefulSession session2 =  
ruleBase.newStatefulSession(false);
     		
        	 	User user2 = new User();
             user2.addRole("admin");
             user2.addRole("tester");
        	

        	

    		FooBarInfo x2 = new FooBarInfo();
    			
    		x2.setValue("color", "red");
    		x2.setValue("count", new Integer(23));
    		x2.setValue("description", "a foobarinfo");

    		System.out.println("SECOND TEST**************************");

    		
    		session2.insert(x2);
    		session2.insert(user2);
    		
    		session2.setFocus("GroupOne");

         session2.fireAllRules();
    		session2.dispose();

         System.out.println("RULES STILL THERE EVEN THOUGH SEEMS TO BE  
BY REFERENCE!");



         rules = pkg.getRules();

         for(int i =0;i<rules.length;i++)
         {
         	System.out.println("Agendagroup in package from rulebase: "  
+ rules[i].getAgendaGroup());
         	if(rules[i].getAgendaGroup().equals("GroupOne"))
         	{
         		ruleBase.removeRule(rules[i].getAgendaGroup(),  
rules[i].getName());
         		System.out.println("removed rule: " + rules[i].getName() +  
" directly from ruleBase");
         	}
         }

         System.out.println("THIRD TEST WITH RULES REMOVED FROM  
RULEBASE ************************");

     	final StatefulSession session3 =  
ruleBase.newStatefulSession(false);
		
    	 	User user3 = new User();
         user3.addRole("admin");
         user3.addRole("tester");
    	

    	

		FooBarInfo x3 = new FooBarInfo();
			
		x3.setValue("color", "red");
		x3.setValue("count", new Integer(23));
		x3.setValue("description", "a foobarinfo");

		
		session3.insert(x3);
		session3.insert(user3);
		
		session3.setFocus("GroupOne");

        session3.fireAllRules();
		
            System.out.println("Mystifying I still get the rules  
fired....");

         } catch (Throwable t) {
             t.printStackTrace();
         }
     }


}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080520/2a9e0fc8/attachment.html 


More information about the rules-users mailing list