[rules-users] ODP: Programatically adding rules toKnowledgeBase/StatefulKnowledgeSession

Pierre de Leusse pdl at agh.edu.pl
Fri Nov 19 05:15:53 EST 2010


Thanks Nathan and Mike! It did the trick. I wish there were easier solution
code wise though.

 

Od: rules-users-bounces at lists.jboss.org
[mailto:rules-users-bounces at lists.jboss.org] W imieniu Nathan Bell
Wysłano: Thursday, November 18, 2010 9:22 PM
Do: Rules Users List
Temat: Re: [rules-users] Programatically adding rules
toKnowledgeBase/StatefulKnowledgeSession

 

Here is a method cut & pasted from working code in a validation class used
by some of my unit tests. I think it does what you are asking.

 

                private ValidationResult addDrlToKnowledgeBase(String
drlAsString ) 

                {

                                ValidationResult retval = null;

                                try

                                {

                                                KnowledgeBuilder kBuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();

                                                Reader rdr = new
StringReader(drlAsString);

                                                Resource resource =
ResourceFactory.newReaderResource(rdr);

                                                kBuilder.add(resource,
ResourceType.DRL);

                                                if (kBuilder.hasErrors()) 

                                                {

                                                                retval =
ValidationResult.newInstance("DRL errors: " +
kBuilder.getErrors().toString(), null, null);

                                                } 

                                                else 

                                                {

 
knowledgeSession.getKnowledgeBase().addKnowledgePackages(kBuilder.getKnowled
gePackages());

                                                                retval =
ValidationResult.newInstance();

                                                }

                                }

                                catch(Exception ex)

                                {

                                                retval =
ValidationResult.newInstance(ex.getMessage(), null, ex);

                                }

                                return retval;

                }

 

 

The type of the instance variable "knowledgeSession" is
"StatefulKnowledgeSession". Ignore the class "ValidationResult", it is a
custom class used for returning results from this validation helper class.
Hope this helps.

 

-Nathan Bell

 

 

From: rules-users-bounces at lists.jboss.org
[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Pierre de Leusse
Sent: Thursday, November 18, 2010 8:14 AM
To: 'Rules Users List'
Subject: [rules-users] Programatically adding rules
toKnowledgeBase/StatefulKnowledgeSession

 

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.drl
File).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();

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20101119/f88e085d/attachment.html 


More information about the rules-users mailing list