[rules-users] trouble dynamically changing packages of a rulebase

Charles Binford Charles.Binford at Sun.COM
Mon Oct 6 18:29:19 EDT 2008


I'm using 4.07. 

Sorry for the long winded post but I was trying to give enough 
background and code snippets to be useful.  The way I'm running data 
through drools is in a batch mode.  As I've gained more experience I see 
that my app may be better suited for the stateless mode, but for now I'm 
still running stateful.  A "batch" consists of:
- loop on array of objects calling session.insert(a, true); // then I 
save off the factHandle
- session.fireAllRules();
- loop on array of objects calling session.retract(factHandle);

All has worked fine until I attempted to add the ability to dynamically 
update a rule package.  It worked both under Eclipse, and running just 
"java -jar myCompiledApp.jar". 

My application has rules split into two parts.  Part A (adm.rules) is 
static and contains the logic tied directly to the application.  Part B 
(adm.userrules) is the user half of the rules where the application can 
be customized to fit the user. 

After adding the logic to dynamically add the user rules all appears to 
work fine when run in eclipse.  BUT when I run outside of eclipse (i.e. 
java -jar ....) it doesn't work.  Only part of my static rules fire.

Here is the code snippet that dynamically loads the new package:

        pkg = pMgr.buildDslPackage(rule_str, dsl_def_file);
        if (userRulesLoaded) {
            Package pkgA[] = ruleBase.getPackages();
            for (Package p : pkgA) {
                System.out.println("LOADED: " + p.getName());
            }
            System.out.println("Removing: " + 
UserXmlRulesToDrls.getUserRulesPackageName());
            pMgr.removePackage(ruleBase,
                UserXmlRulesToDrls.getUserRulesPackageName());
        }
       
        System.out.println("Adding: " + pkg.getName());
        pMgr.addPackage(ruleBase, pkg);
        pMgr.createSession(ruleBase);   
        userRulesLoaded = true;
    }

pMgr.addPackage, createSession, etc. are small wrappers:
    public void createSession(RuleBase ruleBase) {
        if (session != null) {
            session.dispose();
        }
        session = ruleBase.newStatefulSession();
    }
    public void addPackage(RuleBase ruleBase, Package pkg) throws 
Exception {
        ruleBase.addPackage(pkg);
    }

    public void removePackage(RuleBase ruleBase, String pkgName) {
        ruleBase.removePackage(pkgName);
    }


Here are a couple of dummy rules I added in an attempt to debug this.  
These are from the static "adm.rules" package:
rule "testme"
    when
        $ar : ActionArchive();
    then
        System.out.println("Archive: " + $ar.getFile().getFullPath());
end

rule "testme2"
    when
        $f : File();
    then
        System.out.println("File: " + $f.getFullPath());
end

I always get the "archive" print (as expected), but never get the "File" 
print after the first dynamically set of user rules (the first load 
works, second and subsequent loading of user rules doesn't).  My user 
rules also have a "when" based on File() and it always works as expected.


Last, here is some sample output, running the exact same set of input 
through each time.  Each set does two things:
1. load a set of user rules
2. execute a "batch" of a single file

bash-3.2# /usr/jdk/instances/jdk1.6.0/bin/java -jar admDrools.jar
com.sun.evtl.pe.customerPolicy.adm.rules
Adding: com.sun.evtl.pe.customerPolicy.adm.userrules
ArchiveRdy: /zp1/fs2/23kd
Mask WFC: /zp1/fs2/23kd
File: /zp1/fs2/23kd
Archive: /zp1/fs2/23kd      <<< end of first run, all OK
LOADED: com.sun.evtl.pe.customerPolicy.adm.rules
LOADED: com.sun.evtl.pe.customerPolicy.adm.userrules
Removing: com.sun.evtl.pe.customerPolicy.adm.userrules
Adding: com.sun.evtl.pe.customerPolicy.adm.userrules
Archive: /zp1/fs2/23kd      <<< end of second run, missing "File", 
"Mask", and "ArchiveRdy" statements
LOADED: com.sun.evtl.pe.customerPolicy.adm.rules
LOADED: com.sun.evtl.pe.customerPolicy.adm.userrules
Removing: com.sun.evtl.pe.customerPolicy.adm.userrules
Adding: com.sun.evtl.pe.customerPolicy.adm.userrules
Archive: /zp1/fs2/23kd    <<< end of third run, again; missing "File", 
"Mask", and "ArchiveRdy" statements

Charles Binford



More information about the rules-users mailing list