Search code examples
drools

Drools actions with conditional logic, how to express


I have a rule like this from a Decision Table

rule "Tips 2_525"
    salience 65011
    when
        ub:User(ub.getDrinksCount() > 0, ub.getMissedDrink() >= 2)
    then
        ub.getTip("Attract waiter in "+ub.getAvgTime()+ " minutes")==null?ub.putTip("Attract waiter in "+ub.getAvgTime()+ " minutes",20):ub.putTip("Attract waiter in "+ub.getAvgTime()+ " minutes",(ub.getTip("Attract waiter in "+ub.getAvgTime()+ " minutes").getValue()+2));
end

The "==" causes a problem here with "The left-hand side of an assignment must be a variable" and "Syntax error on token "==", invalid AssignmentOperator"

What I am trying to do is check that this particular tip has an entry in the Map before I increment the minute count, if it does not then I put the initial value in the Map. Is there a way of doing this that satisfies the syntactic constraints of drools?


Solution

  • A conditional expression (!) is not a statement in Java and therefore not valid on a Drools RHS. Rewrite:

    ub.putTip("Attract waiter in "+ub.getAvgTime()+ " minutes",
              ub.getTip("Attract waiter in "+ub.getAvgTime()+ " minutes")==null
              ? 20
              : ub.getTip("Attract waiter in "+ub.getAvgTime()+ " minutes").getValue()+2));