Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"if then else "with rule engines

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
like image 710
Ricky Bobby Avatar asked Nov 03 '11 10:11

Ricky Bobby


People also ask

When would you use a rule engine?

A rules engine is a good fit for logic that changes often and is highly complex, involving numerous levels of logic. Rules engines are typically part of a Business Rules Management System (BRMS) that provide extensive capabilities to manage the complexity.

What is rule based engine?

A rules engine is a flexible piece of software that manages business rules. Think of business rules as “if-then” statements. So, a basic example of a rule would be, “If A, then B, else if X, then do Y.”

How do you make a rule based engine?

You can build a simple rules engine yourself. All you need is to create a bunch of objects with conditions and actions, store them in a collection, and run through them to evaluate the conditions and execute the actions.


1 Answers

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
like image 135
Perception Avatar answered Sep 20 '22 13:09

Perception