Drools Rule Language (DRL) Structure¶
Rule Structure Comparison¶
A Drools rule consists of three main parts:
- Rule Declaration: Defines the rule name and optional attributes
- Conditions (LHS): The 'when' section that defines what conditions must be true for the rule to fire
- Actions (RHS): The 'then' section that defines what actions to take when the rule fires
rule "Rule Name"
// Optional attributes (salience, no-loop, agenda-group, etc.)
salience 10 // (1)
no-loop true // (2)
when
// Conditions (Left-Hand Side)
$customer: Customer(age > 18, status == "active")
$order: Order(customer == $customer, total > 100) // (3)
then
// Actions (Right-Hand Side)
$order.applyDiscount(10); // (4)
update($order);
System.out.println("Discount applied to order " + $order.getId());
end
- Has a priority of 10 (higher than default)
- Will not re-trigger even if its actions modify objects (no-loop true)
- Fires when finding an active customer over 18 with an order over $100
- Applies a 10% discount to the matching order
The rule above:
- Has a priority of 10 (higher than default)
- Will not re-trigger even if its actions modify objects (no-loop true)
- Fires when finding an active customer over 18 with an order over $100
- Applies a 10% discount to the matching order
Notice how variables are bound with the $ prefix for clarity.
Writing Your First Rule¶
Let's create a simple rule for a discount system:
rule "First-time Customer Discount"
when
$customer: Customer(orderCount == 0)
$order: Order(customer == $customer)
then
$order.applyDiscount(15);
System.out.println("Applied 15% first-time customer discount");
update($order);
end
This rule matches when:
- We have a customer with no previous orders (first-time customer)
- We have an order associated with this customer
When these conditions are met, it applies a 15% discount to the order.
Additional Resources¶
Was this page helpful?