[rules-users] Questions about using Knowledge Agent

Esteban Aliverti esteban.aliverti at gmail.com
Tue May 11 12:25:11 EDT 2010


Hi John,
Knowledge Agent is indeed the replacement of RuleAgent. It maintains a kbase
and update it using change sets. A change set is an xml that list all the
resources that the agent should take care of. In conjunction with
ResouceChangeNotifier and ResourceChangeScanner you can use a knowledge
agent to use packages exposed using guvnor.
Basically, the scanner will scan periodically a resource and find
modifications. Whenever the resource changed, the scanner creates a change
set and uses the Notifier to let the kagent know about the change.
The thing is that you need to setup an initial change set, so the agent can
start working. Here is one way to do that:

                //Scanner and notifier work as services. They must be
started though
                ResourceFactory.getResourceChangeNotifierService().start();
                ResourceFactory.getResourceChangeScannerService().start();

                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-setdrools-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>";
 File fxml = fileManager.newFile("changeset.xml");
output = new BufferedWriter(new FileWriter(fxml));
 output.write(xml);
output.close();

KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

ResourceChangeScannerConfiguration sconf = ResourceFactory
.getResourceChangeScannerService()
 .newResourceChangeScannerConfiguration();
sconf.setProperty("drools.resource.scanner.interval", "2");
 ResourceFactory.getResourceChangeScannerService().configure(sconf);

KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory
 .newKnowledgeAgentConfiguration();
aconf.setProperty("drools.agent.scanDirectories", "true");
 aconf.setProperty("drools.agent.scanResources", "true");
aconf.setProperty("drools.agent.newInstance", "true");
 KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent(
"test agent", kbase, aconf);

assertEquals("test agent", kagent.getName());

kagent.applyChangeSet(ResourceFactory.newUrlResource(fxml.toURI()
 .toURL()));

StatefulKnowledgeSession ksession = kagent.getKnowledgeBase()
 .newStatefulKnowledgeSession();


I took this code from kagent's test cases.
You can also create a new ChangeSetImpl by hand instead of use an xml file.

I hope you find this helpful.
If you have any other question, or you have doubts about this, feel free to
ask!

Best,

2010/5/11 John Peterson <john.peterson.gv3k at statefarm.com>

>  Hi all,
>
> I’m trying to get the sample code in the Drools 5.0.1 User Manual to work
> locally for a Knowledge Agent, but I’m missing some info (from my
> perspective) to get it to run.
>
> Question #1:
>
> From the User Manual (3.2.6):
>
> KnowledgeAgent kagent =
>
>           KnowledgeAgentFactory.newKnowledgeAgent( "MyAgent" );
>
> kagent.applyChangeSet( ResourceFactory.newUrlResource( url ) );
>
> KnowledgeBase kbase = kagent.getKnowledgeBase();
>
> In the third line, what is “url” and how is it defined in the java code?
> I don’t know what to set up here to make it work.  I’ve found this same code
> on multiple sites, but no answer to the question.
>
> Question #2:
>
> How do I set up a Change Set XML file?  I see the XML, but I don’t know
> where to put the file and how to link it into the code.
>
> Question #3:
>
> Does KnowledgeAgent replace RuleAgent for invoking Guvnor-deployed packages
> in 5.0.1?  If so, how do you specify the url of the deployed package from
> Guvnor?
>
> Sorry if my questions are ignorant.  I’ve been trying to find these
> answers for several days and hit the point where I needed to get some
> help.
>
> Thanks,
>
> John
>
> _______________________________________________
> 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/20100511/7cd55575/attachment.html 


More information about the rules-users mailing list