Due to my recent suspicion that there is a bug in Drools I needed to monitor its guts in detail. To be more specific, I needed to see what is in the working memory after execution of all rules.
Debbuger was not a good option since there were hundruds of invocations of the problematic part and I needed to get overview of quite a large amount of data. If I inly could store all the data for each invocation in a file and then grep through it.
But wait! Of course I can. Byteman will help me! The identified point in source code was execution of fireAllRules() in org.drools.common.AbstractWorkingMemory. Ideally, just before return from the method. So here is the corresponding rule:
RULE Debug fireAllRules HELPER org.drools.planner.examples.tournaments.helper.BytemanHelper CLASS ^org.drools.common.AbstractWorkingMemory METHOD fireAllRules AT EXIT IF TRUE DO printWorkingMemory($0.getKnowledgeRuntime()); ENDRULE
Just a few notes to it:
- ^ means to apply to child classes as well
- $0 refers to this
As you can see there is a custom helper class to print the working memory content since Byteman rules are little bit limited in the supported Java syntax.
import org.drools.impl.StatefulKnowledgeSessionImpl; import org.jboss.byteman.rule.Rule; import org.jboss.byteman.rule.helper.Helper; public class BytemanHelper extends Helper { protected BytemanHelper(Rule rule) { super(rule); } public void printWorkingMemory(Object obj) { StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) obj; debug("vvvvvvvvvvvvvvvvvvvvvvvvvvv"); debug(session.toString()); for (Object o: session.getObjects()) { debug(o.toString()); } debug("^^^^^^^^^^^^^^^^^^^^^^^^^^^"); } }
And now I was able to get some nice output of individual working memories! So the resulting bug is JBRULES-3337.