Java Drools规则不会得到计算,以前的规则可以正常工作

Java Drools规则不会得到计算,以前的规则可以正常工作,java,drools,Java,Drools,我有drools规则,将java对象中的数值与规则中的数字进行比较,如果规则为真,则java对象中的计数器将递增。最后,如果计数器超过某个特定数字,则应执行另一条规则。最后一条规则永远不会得到评估 为了检查计数器是否足够高,我在计数器递增后打印了计数器,这表明计数器变量应该足够高 当计数器为0时,我将规则的计算结果更改为true时,它的计算结果为true 它似乎采用了实例化时使用的值 我的Java对象如下所示(简化): 以下是我的规则: rule "temperature" when

我有drools规则,将java对象中的数值与规则中的数字进行比较,如果规则为真,则java对象中的计数器将递增。最后,如果计数器超过某个特定数字,则应执行另一条规则。最后一条规则永远不会得到评估

为了检查计数器是否足够高,我在计数器递增后打印了计数器,这表明计数器变量应该足够高

当计数器为0时,我将规则的计算结果更改为true时,它的计算结果为true

它似乎采用了实例化时使用的值

我的Java对象如下所示(简化):

以下是我的规则:

rule "temperature"
    when
    $n1 : CTDSIRSNotification( temperature > 38 || temperature < 36 )
    then
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", temperature");
end

rule "respRateAndPaCo2"
    when
    $n1 : CTDSIRSNotification( respRate > 20 || paCo2 < 32 )
    then
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", respRateAndPaCo2");
end

rule "wbCellCountAndimmatureBand"
    when
    $n1 : CTDSIRSNotification( wbCellCount > 12000 || wbCellCount < 4000 || immatureBand > 10 )
    then 
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", wbCellCountAndimmatureBand");
end 

rule "sirsNotification"
    when
    $n1 : CTDSIRSNotification( counter >= 3 )
    then 
    System.out.println($n1.getCounter()+", Alert for SIRS");
end
当我更改最后一条规则以检查0时:

rule "sirsNotification"
    when
    $n1 : CTDSIRSNotification( counter >= 0 )
    then 
    System.out.println($n1.getCounter()+", Alert for SIRS");
end
即使计数器(如果打印)实际为3:

1.0, temperature
2.0, respRateAndPaCo2
3.0, wbCellCountAndimmatureBand
3, Alert for SIRS

问题是我无法检查在规则执行运行时更改的变量吗?

您需要从规则中调用
update
方法以增加计数器。这使规则引擎知道事实已被修改。因此,必须重新评估依赖于该事实的规则

rule "temperature"
    when
    $n1 : CTDSIRSNotification( temperature > 38 || temperature < 36 )
    then
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", temperature");
    update($n1);
end

rule "respRateAndPaCo2"
    when
    $n1 : CTDSIRSNotification( respRate > 20 || paCo2 < 32 )
    then
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", respRateAndPaCo2");
    update($n1);
end

rule "wbCellCountAndimmatureBand"
    when
    $n1 : CTDSIRSNotification( wbCellCount > 12000 || wbCellCount < 4000 || immatureBand > 10 )
    then 
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", wbCellCountAndimmatureBand");
    update($n1);
end 

rule "sirsNotification"
    when
    $n1 : CTDSIRSNotification( counter >= 3 )
    then 
    System.out.println($n1.getCounter()+", Alert for SIRS");
end
规则“温度”
什么时候
$n1:CTD密封(温度>38 | |温度<36)
然后
$n1.setCounter($n1.getCounter()+1);
System.out.println($n1.getCounter()+“,温度”);
更新(1美元);
结束
规则“respRateAndPaCo2”
什么时候
$n1:CTD消毒(呼吸率>20 | | paCo2<32)
然后
$n1.setCounter($n1.getCounter()+1);
System.out.println($n1.getCounter()+“,respRateAndPaCo2”);
更新(1美元);
结束
规则“WBCellCountandImageBand”
什么时候
$n1:CTD认证(wbCellCount>12000 | | wbCellCount<4000 | |未成熟带>10)
然后
$n1.setCounter($n1.getCounter()+1);
System.out.println($n1.getCounter()+“,wbCellCountAndimmatureBand”);
更新(1美元);
结束
规则“sirsNotification”
什么时候
$n1:CTD验证(计数器>=3)
然后
System.out.println($n1.getCounter()+“,SIRS警报”);
结束

我遇到的另一件怪事是,出于兴趣,我添加了一个十进制数,而不是一个整数
$n1.setCounter($n1.getCounter()+1.57)导致
3.570000000000003
。?知道是什么原因吗。57是我随机选择的第一个十进制数,其他十进制数也可以。这是
double
数据类型的一个众所周知的精度问题。如果您想要更高的精度,请使用
BigDecimal
作为计数器变量的数据类型。阅读此内容了解更多信息
1.0, temperature
2.0, respRateAndPaCo2
3.0, wbCellCountAndimmatureBand
3, Alert for SIRS
rule "temperature"
    when
    $n1 : CTDSIRSNotification( temperature > 38 || temperature < 36 )
    then
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", temperature");
    update($n1);
end

rule "respRateAndPaCo2"
    when
    $n1 : CTDSIRSNotification( respRate > 20 || paCo2 < 32 )
    then
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", respRateAndPaCo2");
    update($n1);
end

rule "wbCellCountAndimmatureBand"
    when
    $n1 : CTDSIRSNotification( wbCellCount > 12000 || wbCellCount < 4000 || immatureBand > 10 )
    then 
    $n1.setCounter($n1.getCounter()+1);
    System.out.println($n1.getCounter()+", wbCellCountAndimmatureBand");
    update($n1);
end 

rule "sirsNotification"
    when
    $n1 : CTDSIRSNotification( counter >= 3 )
    then 
    System.out.println($n1.getCounter()+", Alert for SIRS");
end