Rule Language Reference - NeverGiveUp143/Drools GitHub Wiki
Drools Rule Language (DRL)
Drools Rule Language (DRL) is a notation established by the Drools open source business automation project for defining and describing business rules. You define DRL rules in .drl text files. A DRL file can contain one or more rules that define at a minimum the rule conditions (when) and actions (then).
DRL files consist of the following components:
Components in a DRL file
package
unit
import
declare // Optional
query // Optional
rule "rule name"
// Attributes
when
// Conditions
then
// Actions
end
rule "rule2 name"
// Attributes
when
// Conditions
then
// Actions
end
1. Rule Structure
A rule consists of:
- rule "Rule Name" โ Defines the rule name.
- when โ The conditions (LHS - Left Hand Side).
- then โ The actions (RHS - Right Hand Side).
- end โ Marks the end of a rule.
Example:
2. Rule Attributes
Rule attributes modify rule behavior.
Salience
In Drools, salience is used to determine the order in which rules are fired when multiple rules could potentially be triggered in response to the same set of facts. Salience is essentially a priority mechanism that helps control the firing sequence.
1.Rule Prioritization:
* Salience is particularly useful when you want certain rules to have higher priority than others. By assigning a salience value to each rule,
you can influence which rule is executed first when multiple rules match the same facts.
* Rules with a higher salience value will be fired first. If two rules have the same salience value, the order in which they are declared in the
rule file will decide the firing order (unless other conditions like activation-group or no-loop are involved).
2.Conflict Resolution:
* When multiple rules are eligible to fire and there is a conflict, Drools uses the salience value to resolve the conflict. Higher salience values
have priority over lower ones.
* For example, if you have multiple rules that handle different stages of an order process (e.g., "OrderReceived", "OrderProcessed"), you might
assign higher salience to the rule that should fire first in the order processing lifecycle.
3.Prevents Unnecessary Rule Firing:
* Sometimes, you may have a rule that should only fire if itโs the last rule to execute. In such cases, you can assign it a low salience (negative
or zero value) so it fires later than rules with higher salience.
* This can be useful in scenarios like updating a status or performing a final action when all other related actions have been completed.
Here in below example the rule with highest Salience value got executed first and the rule with the lowest Salience value got executed last
Summary: Salience is a useful in Drools for controlling the execution order of rules, ensuring that certain rules are given priority over others based on their salience values. It helps in managing complex rule engines where the firing sequence impacts the overall logic and system behavior.
no-loop
In Drools, the no-loop attribute is used to prevent a rule from firing repeatedly if it modifies the facts that trigger the rule in the first place. It is a way to avoid an infinite loop or redundant rule executions that might arise when a rule causes changes to the data, which then causes the same rule to be fired again.
Use Case of no-loop: When a rule fires and modifies a fact (for example, by updating a factโs properties), that fact could trigger the same rule again, leading to a loop or repeated execution. The no-loop attribute ensures that once a rule fires, it won't fire again due to the same fact being modified by that rule.
How it works: When the no-loop attribute is set to true for a rule, Drools will prevent that rule from firing again for the same fact, even if the fact is modified by the rule itself. Essentially, it ensures that a factโs update wonโt trigger the same rule.
Example: Here in below example we are updating the fact property which resulted in turn triggering the same rule infinitely.
Here, We used no-loop attribute to eliminate infinite execution of the same rule again and again
Agenda-group
The agenda-group feature in Drools allows you to group rules into logical sets and control their execution order. This is useful when you have multiple rules but want to ensure a specific order of execution.
How agenda-group Works
* Rules with the same agenda-group name are grouped together.
* When rules are inserted into the working memory, they will not fire automatically unless their agenda group is explicitly activated.
Example: In Below DRL file we have two agenda-group's
Here we are setting agenda-group to "Discount" in the below code.
when we set agenda-group to "No-Discount" below is the output.
lock-on-active
lock-on-active is a rule attribute in Drools that prevents a rule from reactivating while its agenda group is active, even if facts are updated.
When to Use lock-on-active?
* Prevents infinite loops when a rule modifies a fact and updates it.
* Ensures a rule fires only once per agenda-group activation, even if updates occur.
* Useful in decision-based rule executions, where rules should execute only once.
How lock-on-active Works?
* When a rule with lock-on-active is activated in an agenda-group, it fires only once while the agenda group is in focus.
* Even if the rule updates a fact (using update($fact)), it won't re-fire until the agenda group loses and regains focus.
Example:
Here in below example the rules are firing infinitely.
even after using no-loop attribute the rules are still continuing to fire infinitely because the no-loop attribute handles infinite execution of rules when no agenda group is focused but in below example we are setting agenda group as a result the no-loop attribute unable to handle the infinite loop execution of rule firing.
After using lock-on-active attribute the rules stopped re-firing again and again.
Why Use Both lock-on-active and no-loop?
activation-group
The activation-group attribute ensures that only one rule fires within a group of rules, even if multiple rules match the conditions.
๐ Summary
โ activation-group prevents multiple rules from firing if they belong to the same group.
โ Only the highest-priority rule (salience) fires.
โ Useful for exclusive conditions, like selecting only one rule.
โ Key Differences: activation-group vs agenda-group
Example: Without using activation-group attribute
After using activation-group attribute
date-effective and date-expires
3. Fact Insertion & Modification
Insert
Modify
Retract
4. Conditional Operators
5. Logical Operators
6.Accumulators
7. Global Variables
Global allow external objects (like services or logs) to be accessed in rules.
8. Queries
Queries extract information from the working memory.
9. Agenda Groups
Controls rule execution order by grouping rules.
10. Rule Flow Groups
Used in Drools Flow for process-driven rule execution.
reference Link: https://nheron.gitbooks.io/droolsonboarding/content/gettingStarted/lesson_4__ruleflow.html
11. Declaring Custom Types (Pojo Alternative)
12. Timers & Schedulers
13. Using Functions
14. Using Metadata Annotations
15. Using Decision Tables
Sample Drools Application
https://github.com/NeverGiveUp143/Drools/tree/main/Drools-Sample-Drool-Application/Order