java - Drools 规则条件 - 检查属性是否为空

标签 java drools

我有一个相当简单的案例,我想在我的规则条件中检查属性是否不为空。

rule "only do action if attribute is not null"
when
    $fact : Fact(attribute!=null, $attribute : attribute)
then
    rulesLogger.debug("Rule fires = " + $attribute);
end

我已经在调试中跟踪了这一点。正在插入一个 Fact,属性为 null,但规则仍会触发。控制台输出如下。
Rule fires = null

如果我将条件更改为 attribute==null那么规则不会触发。所以它似乎与我所期望的完全相反。

我们确实有一个使用函数的解决方法,但它有点难看,我无法弄清楚为什么它首先不起作用。
function Boolean attributeExists(Fact fact)
{
    if(fact.getAttribute() == null)
    {
        return Boolean.FALSE;
    }
    else
    {
        return Boolean.TRUE;
    }
}

rule "only do action if attribute is not null"
when
    $fact : Fact($attribute : attribute)
    Boolean(booleanValue == true) from attributeExists($fact)
then
    rulesLogger.debug("Rule fires = " + $attribute);
end

编辑 1

流口水版本是 5.3.0。

事实是通过另一个使用 from 的规则加载的。和一个服务方法调用。我看不出这个事实是无效的,因为它会按照我对控制台的预期打印,并且带有函数的手动解决方法也可以按预期工作。这是一个奇怪的。

编辑 2

我找到了更好的解决方法。如果我使用 getter 方法访问该属性,则该规则将按预期运行。这看起来比必须编写一个额外的函数要好得多,但是知道为什么在使用属性名称时它不起作用仍然会很好。
rule "only do action if attribute is not null"
when
    $fact : Fact(getAttribute()!=null, $attribute : attribute)
then
    rulesLogger.debug("Rule fires = " + $attribute);
end

Fact 类只是一个无聊的 POJO。它除了 Object 之外没有父类(super class),也没有实现任何接口(interface)。它不是 JPA 实体或任何可能存在代理或延迟加载的东西。

最佳答案

尝试其中一些。我不确定5.3是否可以使用D。

rule "only do action if attribute is not null"
when
  Fact($att: attribute != null)             /* A */
  Fact($att: attribute, eval($att != null)) /* B */
  Fact($att: attribute, attribute != null)  /* C */
  Fact($att: attribute, $att != null)       /* D */
then
  rulesLogger.debug("Rule fires = " + $attribute);
end

强烈建议升级。 5.5.0 可能是一种不会出现代码中断但可以避免此类故障的选项。

关于java - Drools 规则条件 - 检查属性是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48156325/

相关文章:

java - 如何使用 Java 将数据从 Cloud Storage 加载到 BigQuery

java - 无法让 Android 将数据 append 到 .txt 文件

java - 我是否需要为 Spring Data 中的可审计字段提供 getter/setter?

drools - 使用规则引擎背后的推理

java - Drools:如何设置多线程(multithreadEvaluation)7.34.0 -Java Maven Spring

Java 从 BigInteger 到 BitSet 再返回

java - richfaces + index.xhtml 有错误

Drools Controller URL 响应错误代码 405

drools - 如何通过 REST 公开 Drools 规则

drools - 使用 Drools 规则引擎对对象列表进行排序