Orderly execution of multiple rule defined in one DRL file - jboss

I defined multiples rules in one DRL file, how to set order, want to execute one after another (top to bottom).

Rules are fired automatically when the conditions are met when the inserted facts(objects) are updated. But if in case you want to run it from top to bottom, you can set a property called salience in the rule. The value it takes is an integer. The rule with the highest salience is executed first.
rule "First name mandatory"
salience 10
when
(Person(firstName=="" || firstName==null))
then
...
end

If you use salience you will be killing the rule engine, because you will be forcing the rule execution order instead of letting the engine decide.
Cheers

Setting priority to rules is the best form.
Use Salience to determine the priority of each rule, where a higher number denotes a higher priority.
The default Salience for rules is 0, and you can give negative Salience, for example,if you want a rule to be fired last.

Related

Determining the cause of a drools rule not firing

With drools, is there a way to tell which part of a rule's "when" clause has failed that prevented the rule from firing? Or, barring that, to tell whether a rule did not fire because its conditions were not satisfied, or if it simply had a lower salience than a rule that halted the context?

How to extend the rules with ability to override existing ones

I need to design a solution where a set of rules can be extended with another set of rules without having to modify the original set.
Problem:
There are possible conflicts when simply combining rules from different rule sets since multiple rules can be matched. The system should trigger only the extended rule when in conflict with the base rule.
Example:
An time tracking system for determining overtime has a threshold to decide when the extra time counts for overtime.
A base rule counts overtime as time exceeding min. 30 minutes.
An extended rule counts overtime as time exceeding min. 60 minutes.
So extended rule is more strict because you have to work for at least
60 minutes to be counted as extra time (otherwise you get 0) but the
original rules counts anything above 30 minutes.
So it both rules as present and you spent +45 minutes at work the base
rule will fire and count it. But we don't want that because the extended rule has higher threshold.
You could put each rule in a different agenda-group. When you want to use your 30 minute rule focus on the agenda-group that contains it. When you want to use your 60 minute rule focus on the agenda-group that contains that.
Alternatively focus both agenda-groups. The rules we be matched in the most recently focused agenda-group first.

Drools - multiple activation group

I have written a Drool rule as shown below:
rule "first and second"
activation-group "first"
activation-group "second"
when
// conditions
then
// actions
end
However, when compiling, I get this error:
Duplicate attribute definition: activation-group in rule first and second
I want to know if it is possible to include multiple activation-groups in a rule. If this is possible, how can you do it?
Thanks in advance.
It's not possible to use multiple activation groups in a single rule. If you feel the need to do this then you should restructure your rules to be a little smarter with your conditions so you can get it to a state that would work with single activation groups (per rule).

Exit from executing the remaining rules in Drools Decision Table

We have a scenario to be implemented in Decision Table to exit from executing the remaining rules if certain rule successfully executes the action part of the rule. Suppose I have 50 rules and 5th rule is something which says insurance claim is invalid then we set claim as invalid to the object, then there is no need to execute remaining rules. How could this can be achieved. Please suggest
You can
retract the fact under evaluation, after setting invalid to true, on that rule's RHS,
throw an exception (ugly, ugly),
run the session using fireUntilHalt and call method halt on the session on that rule's RHS - here you'll need a very low salience rule (added in a .drl file) to call halt in case the fact passes all decision table rules.

How to execute a drools rule at last?

I need to execute some rule as the last rule. Any one knows how to do this ?
Also can a rule change behavior of other rules ? For example can a rule be used to execute certain rule package ?
Thanks in Advance.
yes you can use the rule attribute salience to give your rules priority.
You can insert a fact in the Right Hand Side of the rule to make another rules match.
You can also read the documentation for Agenda Groups to select different groups of rules that will be executed at different times.
Hope it helps!