gpt4 book ai didi

java - Drools 规则未得到评估,以前的规则工作正常

转载 作者:行者123 更新时间:2023-11-30 01:51:31 25 4
gpt4 key购买 nike

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

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

当我更改规则以在计数器为 0 时评估为 true 时,它​​评估为 true。

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

我的 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,它的计算结果也为 true:

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

关于java - Drools 规则未得到评估,以前的规则工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55865269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com