easy-rules: is there an if-else rule?

I am trying to work with easy-rules.

Can we have an if-else rule executed using easy-rules?

I have checked on ActivationRuleGroup and ConditionalRuleGroup, but it seems it's only all if's.

Upvotes: 1

Views: 1643

Answers (1)

belwood
belwood

Reputation: 3779

All rules are if-then, there is no else; it's a different kind of thinking. Rules are not meant to replace just any logic you see in your average source code. They're best implemented for things akin to long switch-case-like logic or parameter-driven logic. Income tax forms are an example of a parameter-driven use-case.

If you need three actions based on the value of one fact, for example:

if age < 16
    action: discount = 15%
else if age >= 16 and age <= 65
    action: discount = 0%
else 
    action: discount = 20%

Then you simply write 3 rules:

when age < 16
    action: discount = 15%

when age >= 16 and age <= 65
    action: discount = 0%

when age > 65
    action: discount = 20%

The Composite rules are used for handling groups of rules.

ConditionalRuleGroup has one "primary" rule such that when it evaluates to true (triggered), then the remainder of the rules in the group are fired otherwise the group is skipped.

I use UnitRuleGroup for form-like validation. All of the data from a form is put into the facts and the validation rules are fired. If any one of the rules is false, then the whole group returns false and the form is deemed invalid.

Upvotes: 1

Related Questions