Reputation: 3812
I need to access run-time information of running drools engine in my java web-app.
Things i need to know:
what are the active rules in run-time at any instant?
How much objects are inserted into session till now?
Are there some classes that let you access the information of drools run-time?
Thanks in advance
Upvotes: 1
Views: 1298
Reputation: 767
For debug purposes you can also add these listeners to the KnowledgeSession
Drools has an event model that exposes much of whats happening internally, two default debug listeners are supplied DebugAgendaEventListener and DebugWorkingMemoryEventListener which print out debug event information to the err console, adding listeners to a session is trivial and shown below. The WorkingMemoryFileLogger provides execution auditing which can be viewed in a graphical viewer; it's actually a specialised implementation built on the agenda and working memory listeners, when the engine has finished executing logger.writeToDisk() must be called.
ksession.addEventListener(new DebugAgendaEventListener()); // add 2 debug event listeners
ksession.addEventListener(new DebugWorkingMemoryEventListener());
// setup the audit logging
WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger( session );
logger.setFileName( "log/helloworld" );
Upvotes: 1
Reputation: 9480
You just need to examine the KnowledgeBase and StatefulKnowledgeSession classes. The following methods demonstrate how to get hold of all rules in your knowledge base, and all facts in working memory.
/**
* Get a String showing all packages and rules in a knowledge base.
*/
public String kbString(KnowledgeBase kbase) {
StringBuilder sb = new StringBuilder();
for (KnowledgePackage p : kbase.getKnowledgePackages()) {
sb.append("\n Package : " + p.getName());
for (Rule r : p.getRules()) {
sb.append("\n Rule: " + r.getName());
}
}
return "Knowledge base built with packages: " + sb.toString();
}
/**
* Get a String showing the facts currently in working memory,
* and their details.
*
* @param session The session to search for facts.
*/
public String sessionFactsString(StatefulKnowledgeSession session) {
StringBuilder sb = new StringBuilder();
sb.append("\nThe following facts are currently in the system...");
for (Object fact : session.getObjects()) {
sb.append("\n\nFact: " + DroolsUtil.objectDetails(fact));
}
return sb.toString();
}
Edit for clarity - The objectDetails(Object) method above is a method for rendering any old Java bean as a String, using Apache Commons BeanUtils. It looks like this:
public static String objectDetails(Object o) {
StringBuilder sb = new StringBuilder(o.getClass().getSimpleName());
try {
@SuppressWarnings("unchecked")
Map<String, Object> objectProperties = BeanUtils.describe(o);
for (String k : objectProperties.keySet()) {
sb.append(", " + k + "=\"" + objectProperties.get(k) + "\"");
}
} catch (IllegalAccessException e) {
return "IllegalAccessException attempting to parse object.";
} catch (InvocationTargetException e) {
return "InvocationTargetException attempting to parse object.";
} catch (NoSuchMethodException e) {
return "NoSuchMethodException attempting to parse object.";
}
return sb.toString();
}
Upvotes: 4