首页 > 解决方案 > 如何验证 drools 规则中的哪个条件失败?

问题描述

我正在使用 drools 7.44 并且有一个包含 20 多个规则的包。目前我面临的问题是有时我希望触发的某些规则没有触发。在then我可以添加日志语句,但不能在when中。有没有办法调试或记录规则的哪些条件失败?

如果客户年龄=21,性别=男性,则使用以下规则。我如何在运行时确定性别是导致规则不触发的标准。

rule "some rule"
    when
        $c : Customer(age>18 , gender == "female" )
    then
........
        do something here
        log.debug("something to log") 
end

这是一个简单的例子来说明问题。理想情况下,我们有多个条件。

标签: droolsrules

解决方案


Use the DebugAgendaEventListener to trace the rules evaluation. See the manual

This will cover only the case when the match succeeds. If not, nothing will be evaluated. There's a JIRA to track such failed evaluation

Extract from the manual

The Drools engine also supports the following agenda and working memory event listeners for debug logging:

DebugAgendaEventListener DebugRuleRuntimeEventListener

These event listeners implement the same supported event-listener methods and include a debug print statement by default. You can add a specific supported event to be monitored and documented, or monitor all agenda or working memory activity.

For example, the following code uses the DebugRuleRuntimeEventListener event listener to monitor and print all working memory events:

Example code to monitor and print all working memory events

ksession.addEventListener( new DebugRuleRuntimeEventListener() );

推荐阅读