我需要在java web-app中访问运行drools引擎的运行时信息
我需要知道的事情:
任何时刻运行时的活动规则是什么?
到目前为止,会话中插入了多少对象?
是否有一些课程可以让你访问drools运行时的信息?
提前致谢
答案 0 :(得分:4)
您只需要检查KnowledgeBase和StatefulKnowledgeSession类。以下方法演示了如何掌握知识库中的所有规则以及工作记忆中的所有事实。
/**
* 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();
}
为了清楚起见编辑 - 上面的objectDetails(Object)方法是使用Apache Commons BeanUtils将任何旧Java bean渲染为String的方法。它看起来像这样:
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();
}
答案 1 :(得分:1)
出于调试目的,您还可以将这些侦听器添加到 KnowledgeSession
Drools有一个事件模型,揭示了许多正在发生的事情 在内部,提供了两个默认的调试侦听器 DebugAgendaEventListener和DebugWorkingMemoryEventListener 将调试事件信息打印到err控制台,添加监听器 会议很简单,如下所示。 WorkingMemoryFileLogger 提供执行审计,可以在图形查看器中查看; 它实际上是建立在议程上的专门实施 工作内存监听器,当引擎完成执行时 必须调用logger.writeToDisk()。
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" );