我有drools规则,我将来自java对象的数值与规则中的数字进行比较,如果规则为true,则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,它的计算结果为真:
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