[rules-users] KnowledgeAgent not reloading KnowledgeBase

Pritam infinity2heaven at gmail.com
Thu Jan 14 20:17:33 EST 2010


Before I ask the problem, here's the design I currently have:

Our "site" (webapp) has 100 pages. While rendering each page, the rule
engine is called for filtering the page contents. We have 100 drl files
(corresponding to each page). Since creating of a KnowledgeBase is
expensive, I'm using a KnowledgeAgent.getKnowledeBase() since the javadocs
mentions it as "it loads, caches, reloads knowledgeBase." 

However after using the sample code given, a change in the rule file is not
reloading the knowledgeBase automatically. Note that I cache the
knowledgeAgent since I need one agent per knowledge base (is there a better
way to manage multiple drl files in one knowledge base and agent?)

Is something wrong with the below code?

// ruleFile is a complete file path on server
public static KnowledgeBase createKnowledgeBaseFromAgent(String ruleFile)
            throws DroolsParserException, IOException {

        ResourceChangeScannerConfiguration sconf = ResourceFactory
                .getResourceChangeScannerService()
                .newResourceChangeScannerConfiguration();
 
        // static map of knowledgeAgents
        if (knowledgeAgents.containsKey(ruleFile)) {
            return
((KnowledgeAgent)knowledgeAgents.get(ruleFile)).getKnowledgeBase();
        }
        
        sconf.setProperty("drools.resource.scanner.interval", "5"); 

        ResourceFactory.getResourceChangeScannerService().configure(sconf);

        KnowledgeBase knowledgeBase = createKnowledgeBase(ruleFile);

        ResourceFactory.getResourceChangeScannerService().start();
        ResourceFactory.getResourceChangeNotifierService().start();

        KnowledgeAgentConfiguration conf =
KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
        conf.setProperty("drools.agent.scanDirectories", "true");

        final KnowledgeAgent agent =
KnowledgeAgentFactory.newKnowledgeAgent(
                "my agent", knowledgeBase, conf);
       
       // agent.applyChangeSet(ResourceFactory.newFileResource(ruleFile));
        
        setKnowledgeAgent(ruleFile, agent);
               
        return agent.getKnowledgeBase();
    }


public static KnowledgeBase createKnowledgeBase(
        KnowledgeBaseConfiguration config,
        KnowledgeBuilderConfiguration knowledgeBuilderConfig,
        String ruleFile) throws DroolsParserException, IOException {        

    // else, create a knowledgeBase
    KnowledgeBuilder knowledgeBuilder =  
        KnowledgeBuilderFactory.newKnowledgeBuilder(knowledgeBuilderConfig);
        knowledgeBuilder.add(ResourceFactory.newFileResource(ruleFile),
ResourceType.DRL);

    if (knowledgeBuilder.hasErrors()) {
        throw new RuntimeException(knowledgeBuilder.getErrors().toString());
    }

    KnowledgeBase knowledgeBase =
KnowledgeBaseFactory.newKnowledgeBase(config);
   
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());

    return knowledgeBase;
}

I commented out 
agent.applyChangeSet(ResourceFactory.newFileResource(ruleFile));

since I get a NPE
java.lang.NullPointerException
	at
org.drools.agent.impl.KnowledgeAgentImpl.processChangeSet(KnowledgeAgentImpl.java:135)

Pl suggest
-- 
View this message in context: http://n3.nabble.com/KnowledgeAgent-not-reloading-KnowledgeBase-tp121401p121401.html
Sent from the Drools - User mailing list archive at Nabble.com.



More information about the rules-users mailing list