Java Drools规则条件-检查属性是否为null

Java Drools规则条件-检查属性是否为null,java,drools,Java,Drools,我有一个相当简单的例子,我想在规则条件中检查属性是否为null rule "only do action if attribute is not null" when $fact : Fact(attribute!=null, $attribute : attribute) then rulesLogger.debug("Rule fires = " + $attribute); end 我在调试中跟踪了这一点。正在插入一个属性为null的事实,但该规则仍会激发。控制台输出如下

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

rule "only do action if attribute is not null"
when
    $fact : Fact(attribute!=null, $attribute : attribute)
then
    rulesLogger.debug("Rule fires = " + $attribute);
end
我在调试中跟踪了这一点。正在插入一个属性为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

drools版本是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

事实课只是一个无聊的POJO。它没有对象以外的超类,也没有实现接口。它不是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可能是一个没有代码中断的选项,但可以避免类似这样的故障。

请添加Drools版本和用于组装和插入事实对象的代码。您是否有特别的预感或只是为了完整性?实际的代码要比这个复杂得多,但是我逐渐地去掉了一些代码,直到我只剩下这个非常简单的例子,问题仍然存在。EDIT2工作似乎支持了我的担忧。我尝试了所有这些,发现B和D在使用getter方法修复EDIT2的同时也工作。因此有三种选择。我还尝试升级到5.5.0,但这并没有解决问题,尽管我确实注意到控制台中的日志记录非常有用。a和C不工作是一个错误,但你不会得到任何5.x的修复奇怪的是5.5。这并不好。我刚在谷歌上搜索到有一场5.6.0的决赛;对于6.0,我一定跳过了这个选项,所以如果你想使用更自然的A和C,这是最后一个选项。