[rules-users] Programatically adding rules to KnowledgeBase/StatefulKnowledgeSession

Michael Anstis michael.anstis at gmail.com
Thu Nov 18 11:55:14 EST 2010


Hi,

Have a look at KnowledgeBuilder's use of ResourceFactory.

There's a newReaderResource() static method which could be passed a
StringReader rather than writing to an intermediate file.

AFAIK kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()) is
effectively the "addRule" you desire.

With kind regards,

Mike

2010/11/18 Pierre de Leusse <pdl at agh.edu.pl>

> Hello all,
>
>
>
> I’m trying to add rules through my java code to a KnowledgeBase and I
> cannot find any easy way to do so. So far, the only solution I could think
> of, is to write the rule as DRL in a file that is “observed” by a
> KnowledgeAgent (same with a RuleAgent I suppose). Is it possible to create a
> new KnowledgePackage with the same name as the existing one and load it into
> the KnowledgeBuilder?
>
>
>
> I manage to remove rules using the following code:
> statefulKnowledgeSession.getKnowledgeBase().removeRule(knowledgeSetName,
> ruleName);
>
>
>
> I thought that there would be a addRule method somewhere but not luck so
> far.
>
>
>
> The code I’m using to create my KnowledgeBase and StatefulKnowledgeSession
> is shown below.
>
>
>
> All the best,
>
> Pierre
>
>
>
> StatefulKnowledgeSession creation code:
>
>
>
> StatefulKnowledgeSession ksession;
>
> KnowledgeBuilder kbuilder;
>
> KnowledgeBase kbase;
>
>
>
> kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
>
>
>
> kbuilder.add(ResourceFactory.newFileResource(Demo.class.getResource(Demo.drlFile).getPath()),
> ResourceType.DRL);
>
>
>
> if (kbuilder.hasErrors()) {
>
>                 System.out.println(kbuilder.getErrors());
>
> }
>
> kbase = KnowledgeBaseFactory.newKnowledgeBase();
>
> kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
>
> ksession = kbase.newStatefulKnowledgeSession();
>
> ksession.insert(…);
>
>>
> ksession.fireAllRules();
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20101118/8919927a/attachment.html 


More information about the rules-users mailing list