5

I'm new to drools and given a condition (Condition) and a boolean variable "a" , I would like to create the following rule with drools :

if (Condition)
   { 
    a = true;
   }
else
   {
    a = false;
   }

What is the best way to do it ?

For the time being I have 2 options :

1.Create 2 rules with Condition and not contidition (If ... then ... , If not ... then ...)

rule "test"
where
  $o: Object( Condition)
then 
  $o.a = true;
end


rule "test2"
where
  $o: Object( not Condition)
then 
  $o.a = false
end

2.Set the variable a to false by default and then fire the rule

rule "test"
no loop
salience 100
where 
  $o: Object()
then 
  $o.a = false;
end


rule "test"
where
  $o: Object( not Condition)
then 
  $o.a = true;
end
Ricky Bobby
  • 7,490
  • 7
  • 46
  • 63
  • http://rule.codeeffects.com solves this for .NET web world. I'm sure, if you Google you can find the same type of UI for Java. – Kizz Nov 03 '11 at 23:34

2 Answers2

6

By nature the Rete engine looks for positive matches, so yes you will need multiple rules, one for each condition check in your if-then-else block. Your first example is cleaner and more intuitive, I would go with that.

As an alternative, if you are processing a simple logic negation (if-else) where your variables value matches the conditional, then you can use just one rule:

rule "test"
where 
  $o: Object()
then 
  $o.a = (! Condition);
end
Perception
  • 79,279
  • 19
  • 185
  • 195
2

Remember that a Rule engine is not a IF/ELSE container only. You are changing from the imperative approach of if(condition){} else {} to a declarative approach where you are letting the engine to decide and evaluate the rules based on the context (facts) that you have in your working memory.

Having two rules is fine, but what exactly are you trying to achieve? depending the Object structure and the business logic that you want to implement there are several ways to solve different problems. Can you give us more details about what do you want to achieve? Cheers

salaboy
  • 4,123
  • 1
  • 14
  • 15