jteichert
jteichert

Reputation: 617

Drools rule does not get evaluated, previous rules work fine

I have drools rules where I am comparing numerical values from a java object with a number in the rule and if the rule is true a counter that is in the java object get incremented. Finally if the counter exceeds a particular number another rule should be executed. This last rule never gets evaluated.

To check if the counter is high enough I printed the counter after it got incremented which showed that the counter variable should be high enough.

When I change the rule to evaluate to true when the counter is 0 it evaluates to true.

It seems to take the value that it was instantiated with.

My Java Object looks like that (simplified):

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
}

these are my rules:

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

and the output shows that the counter is being incremented:

1.0, temperature
2.0, respRateAndPaCo2
3.0, wbCellCountAndimmatureBand

when I change the last rule to check against 0:

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

it evaluates to true even though the counter if printed is actually 3:

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

Is the problem that i cant check variables that change during rule execution runtime?

Upvotes: 1

Views: 1024

Answers (1)

shazin
shazin

Reputation: 21883

You need to call the update method from your rules which increments the counter. That makes the rules engine aware that a fact has been modified. Therefore rules which depend upon that fact must be re-evaluated.

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

Upvotes: 3

Related Questions