我有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,它的计算结果也为true:
1.0, temperature
2.0, respRateAndPaCo2
3.0, wbCellCountAndimmatureBand
3, Alert for SIRS
问题是我无法检查在规则执行运行期间更改的变量吗?
答案 0 :(得分:2)
您需要从规则中调用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