[rules-users] KnowledgeAgent not reloading KnowledgeBase

Esteban Aliverti esteban.aliverti at gmail.com
Fri Jan 15 08:17:48 EST 2010


Hi Pritam,
As you mentioned, KA loads, caches and reload kbases. When the KA process a
change-set, adds a Listener to every resource so it can detect when they are
modified. In order to detect these modifications, two services should be
running: *ResourceChangeScannerService* and *ResourceChangeNotifierService *(as
you are already doing).

Listeners are only set for those resources of a change-set, if the initial
kbase contained preloaded rules, their resources are not monitored. What I
recommend to use change-sets. It seams that your error is in the change-set
structure. You must pass a xml definition of a change-set to
KA#applyChangeSet() and not a path to a file. Here is an example of a well
formed change-set that can be passed to applyChangeSet():


*String xml = "";*
*        xml += "<change-set xmlns='http://drools.org/drools-5.0/change-set
'";*
*        xml += "    xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";*
*        xml += "    xs:schemaLocation='
http://drools.org/drools-5.0/change-set drools-change-set-5.0.xsd' >";*
*        xml += "    <add> ";*
*        xml += "        <resource source='http://localhost:9000/rule1.drl'
type='DRL' />";*
*        xml += "        <resource source='http://localhost:9000/rule2.drl'
type='DRL' />";*
*        xml += "    </add> ";*
*        xml += "</change-set>";*

This is a change-set that contains 2 resources. They are accessed using
http, but you can use *file://* if they are local.


Best,



On Thu, Jan 14, 2010 at 10:17 PM, Pritam <infinity2heaven at gmail.com> wrote:

>
> 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.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>



-- 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Esteban Aliverti
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20100115/97432043/attachment.html 


More information about the rules-users mailing list