views:

196

answers:

3

Hi I want to bring a little change in my site. So i have written a set of rules but I dont know how to fire them in the place that i want them to work i.e. the original code of my site.Can anyone plz help me on it?

A: 

Hi

A little information is missing (like which Drools version, the environment you're running in, etc.), so I'll try to answer in a general way, and if you need something more specific let me know:

In general in Drools you 'assert' objects into the working memory (using methods like, well, assertObject..), this allows the rule engine to be aware of them, and later, when you execute 'fireAllRules' on that working memory, all the rules are executed (so, you explicitly call the fireAllRules method on your working memory when you want to - I think this is what you were asking).

For more detailed information, in case you haven't looked already, you can check this: http://legacy.drools.codehaus.org/Working+Memory .

thanks Gadi

Gadi
A: 

Hi Thanks for your answer Gadi...I actually hav already an object defined(lets say like this : Abc abc = new Abc() ) in my java environment and i want to bring abt changes on 1 or two methods of this abc by using drools. so How to Insert that coz session.insert(abc) is giving errors

A: 

Hi, when you have your abc object, this will be the fact that you insert into the Working Memory. For example:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newFileSystemResource( fileName ), ResourceType.DRL );
if (kbuilder.hasErrors() ) {
    System.out.println( kbuilder.getErrors() );
} else {
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
    StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
    ksession.execute( abc );
}

Then you will have your modified abc object when the execute finishes. Take a look at here

You probably dont't want the rules being build everytime (this is really time consumer), so you can use or KnowledgeAgent or have an static KnowledgeBase and recreate it whenever your rules files change.

diega

related questions