Hi,<br><br>Have a look at KnowledgeBuilder&#39;s use of ResourceFactory.<br><br>There&#39;s a newReaderResource() static method which could be passed a StringReader rather than writing to an intermediate file.<br><br>AFAIK kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()) is effectively the &quot;addRule&quot; you desire.<br>
<br>With kind regards,<br><br>Mike<br><br><div class="gmail_quote">2010/11/18 Pierre de Leusse <span dir="ltr">&lt;<a href="mailto:pdl@agh.edu.pl">pdl@agh.edu.pl</a>&gt;</span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div link="blue" vlink="purple" lang="EN-US"><div><div><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">Hello all,</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">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? </span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">I manage to remove rules using the following code: statefulKnowledgeSession.getKnowledgeBase().removeRule(knowledgeSetName, ruleName);</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">I thought that there would be a addRule method somewhere but not luck so far.</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">The code I’m using to create my KnowledgeBase and StatefulKnowledgeSession is shown below.</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">All the best,</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">Pierre</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">StatefulKnowledgeSession creation code:</span></p><p class="MsoNormal">
<span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">StatefulKnowledgeSession ksession;</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">KnowledgeBuilder kbuilder;</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">KnowledgeBase kbase;</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">                                </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">kbuilder.add(ResourceFactory.newFileResource(Demo.class.getResource(Demo.drlFile).getPath()), ResourceType.DRL);</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);"> </span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">if (kbuilder.hasErrors()) {</span></p><p class="MsoNormal">
<span style="font-size: 11pt; color: rgb(31, 73, 125);">                System.out.println(kbuilder.getErrors());</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">}</span></p><p class="MsoNormal">
<span style="font-size: 11pt; color: rgb(31, 73, 125);">kbase = KnowledgeBaseFactory.newKnowledgeBase();</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">ksession = kbase.newStatefulKnowledgeSession();</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">ksession.insert(…);</span></p>
<p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">…</span></p><p class="MsoNormal"><span style="font-size: 11pt; color: rgb(31, 73, 125);">ksession.fireAllRules();</span></p></div></div></div><br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>