Creating Your First Decision Table
This guide walks you through creating a decision table for a simple shipping charges calculation system. You’ll build the same shipping charges example mentioned in the documentation, but with a step-by-step approach.Example Use Case: Shipping Charges
Our online shopping platform has the following shipping pricing rules:- Free shipping when:
- The order has 4+ items AND the total is $300+
- Standard shipping is selected (4-5 business days)
- Otherwise, shipping charges vary based on:
- Number of items
- Delivery speed (Next day, 2nd day, or Standard)
- Order total (less than 300+)
Step 1: Create a New Excel Spreadsheet
- Open Microsoft Excel® or your preferred spreadsheet application
- Create a new blank workbook
- Save it as
ShippingCharges.drl.xlsxin your project’ssrc/main/resources/rulesdirectory.
Important: Starting with Drools v8, file extension must end in either.drl.xls,.drl.xlsx, or.drl.csv
Step 2: Define the RuleSet Area
The RuleSet area defines global elements for all rules in the package.- In cell A1, type a descriptive comment like
Shipping Charges Decision Table - In cell B1, type
RuleSet - In cell C1, type
com.example.shipping(this will be your rule package name) - Move to the next row (row 2)
- In cell B2, type
Import - In cell C2, type
com.example.model.Order, com.example.model.Charge
Step 3: Define the First RuleTable
Now let’s create the rule table for orders under $300:- Skip a row and in cell B4, type
RuleTablefollowed by a name:RuleTable Shipping Under 300 - In row 5, define the condition and action headers:
- Cell B5:
CONDITION - Cell C5:
CONDITION - Cell D5:
ACTION - In row 6, define the object types:
- Cell B6:
$order : Order - Cell C6:
Order - Cell D6: (leave empty)
- In row 7, define the constraints and actions:
- Cell B7:
total < 300 - Cell C7:
itemsCount == $1, deliverInDays == $2 - Cell D7:
insert(new Charge($param)); - In row 8, add descriptive column headers:
- Cell B8:
Under $300 - Cell C8:
Items & Delivery - Cell D8:
Shipping Charge
| Shipping Charges Decision Table | RuleSet | ai.aletyx.shipping |
| | Unit | ShippingUnit |
| | Import | ai.aletyx.model.Order, ai.aletyx.model.Charge |
| | Queries | query FindShippingCharge(Order order, Charge charge) order := /orders charge := /charges end |
| | | |
Step 4: Create the Second RuleTable
Now let’s add rules for orders of $300 or more:- Skip a row after the previous table
- In cell B16, type
RuleTable Shipping Over 300 - Repeat the same headers and descriptive rows as with the first table
- Change B19 (the constraint) to
total >= 300 - Use the same format for the other condition and action cells