Question

I've a simple rule like this:

rule "First Rule" //You do not talk about Fight Club
when
    MyInp(id=="1")
then
    insert(new MyOut(true));
end

What I want is, getting the created MyOut object from a Java class. Is there a way to do this or do I have to pass a global variable and update it inside the rule?

Was it helpful?

Solution

With a stateless session you can modify a fact which you insert, or register a global which you update.

If you have a stateful session then there are a few more options:

Fetching fact from stateful session of drools

If you're looking for a general means of examining what existed in working memory during your stateless session,

StatelessKnowledgeSession ksession = 
        kbase.newStatelessKnowledgeSession();
TrackingWorkingMemoryEventListener listener = 
        new TrackingWorkingMemoryEventListener();
ksession.addEventListener(listener);

List<Object> facts = new ArrayList<Object>();
facts.add(myRequestFact);

ksession.execute(facts);

List<ObjectInsertedEvent> insertions = listener.getInsertions();

It's handy for debugging and audit purposes, but I wouldn't recommend it as a means of getting the actual results out of a request. Example code (by me) for a tracking WorkingMemoryEventListener can be found here:

https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/java/com/sctrcd/drools/util/TrackingWorkingMemoryEventListener.java

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top