Drools 规则没有得到评估,以前的规则工作正常

Drools rule does not get evaluated, previous rules work fine

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

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

当我将规则更改为在计数器为 0 时求值为真时,它求值为真。

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

我的 Java 对象看起来像那样(简化):

public class CTDSIRSNotification {

    private double temperature;
    private double heartRate;
    private double respRate;
    private double paCo2;
    private double wbCellCount;
    private double immatureBand;

    private double counter;


    public CTDSIRSNotification(double temperature, double heartRate, double respRate, double paCo2, double wbCellCount, double immatureBand) {
        this.temperature = temperature;
        this.heartRate = heartRate;
        this.respRate = respRate;
        this.paCo2 = paCo2;
        this.wbCellCount = wbCellCount;
        this.immatureBand = immatureBand;
    }
//getters and setters
}

这些是我的规则:

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

并且输出显示计数器正在递增:

1.0, temperature
2.0, respRateAndPaCo2
3.0, wbCellCountAndimmatureBand

当我更改最后一条规则以检查 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