Drools: how to use abbreviated condition notation together with further conditions?

65 Views Asked by At

Using Drools 6.5.0.Final

I want to use abbreviated combined relation condition (e.g. Person( age > 30 && < 40 )) in combination with additional conditions. I tried it, but the resulting rules are executed more than once.

I have a small example, where temperature deviation from a setpoint is checked and the allowed deviations depend on the setpoint. The allowed deviations are configured with Param facts, see below. If I execute the example (fire-all-rules):

  • rule 1 fires two times (bug?)
  • rule 2 fires once as expected (without abbreviated notation).

Is my usage of the abbreviated notation wrong or is this a bug?

Example rules:

declare Param
    from : float
    to : float
    low : float
    high : float
end

declare TemperatureEvent
@role( event )
    id : String
    setpoint : float
    t : float
end

rule "Init abbreviated conditions test"
when
then
    insert(new Param(0, 10, 1, 1));
    insert(new Param(10,20, 2, 3));
    insert(new TemperatureEvent("id1", 13.7f,11.5f));
    // rule 1 and rule 2 should fire exactly once
end

rule "rule 1"
when
    $p: Param()
    $x : TemperatureEvent($p.from <= setpoint && < $p.to, (t < setpoint+$p.low || t > setpoint+$p.high))
then
    System.out.println("rule 1: "+$x.getId()+" "+$x.getSetpoint()+" "+$x.getT());
end

rule "rule 2"
when
    $p: Param()
    $x : TemperatureEvent($p.from <= setpoint, setpoint < $p.to, (t < setpoint+$p.low || t > setpoint+$p.high))
then
    System.out.println("rule 2: "+$x.getId()+" "+$x.getSetpoint()+" "+$x.getT());
end
1

There are 1 best solutions below

0
On BEST ANSWER

The abbreviated restriction

 $p.from <= setpoint && < $p.to

is equivalent to

 $p.from <= setpoint && $p.from < $p.to

What you want is

 setpoint >= $p.from && < $p.to