Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools - rule hierarchy and conditional execution

I was wondering if there is a way to define hierarchy (not just order of execution) between rules and control the rule execution - i.e. if the parent rule fired then the ones below should not be evaluated etc...

Information in this thread is an option but it is essentially IF/THEN/ELSE

Is there a different option?

thanks

like image 860
shikarishambu Avatar asked May 30 '12 18:05

shikarishambu


People also ask

How do you call one rule from another rule in drools?

Well, the answer is we can't call a Rule from another Rule. Drools matches the Rules with incoming data/facts, and if the data satisfies the Rule condition, it stores the data in an Agenda. It might be possible for the same data or facts to be matched by different rules, so it stores matching facts in an Agenda.

What is DRL file in drools?

DRL (Drools Rule Language) rules are business rules that you define directly in . drl text files. These DRL files are the source in which all other rule assets in Business Central are ultimately rendered.

How do I set priority in drools?

Setting a PriorityThe number determines the salience priority. A higher number denotes a higher priority, so rules with a higher salience will be executed first by the Drools engine. So, if Rule 1 has a salience of 10, Rule 2 has a 1, and Rule 3 has a 5, the order should be: Rule 1>Rule 3>Rule 2.


1 Answers

I am not sure if I understand your question, but using a combination of Activation Groups and the traditional conflict resolution strategies might achieve what you need. For instance, lets say you have 3 rules, A, B and C. You want to use, lets say, salience to give priority of execution to them in that order, and once one is executed, no other rule in that group should execute. You can define them like this:

rule A
    salience 30
    activation-group "x"
...

rule B
    salience 20
    activation-group "x"
...

rule C
    salience 10
    activation-group "x"
...

The salience guarantees that if A activates, it will fire first, followed by B, followed by C if they were activated. The Activation Group guarantees that once one of the rules fire, all other activated rules in that group will be cancelled. So, lets say that in your session, rules B and C activate, but not A, then B will fire and C will be cancelled.

Please note that activation groups do not prevent rules from later being re-activated. It just cancels any activations currently in the agenda at the time one of the rules in the group fire.

like image 77
Edson Tirelli Avatar answered Sep 22 '22 09:09

Edson Tirelli