Skip to main content

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 300or300 or 300+)

Step 1: Create a New Excel Spreadsheet

  1. Open Microsoft Excel® or your preferred spreadsheet application
  2. Create a new blank workbook
  3. Save it as ShippingCharges.drl.xlsx in your project’s src/main/resources/rules directory.
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.
  1. In cell A1, type a descriptive comment like Shipping Charges Decision Table
  2. In cell B1, type RuleSet
  3. In cell C1, type com.example.shipping (this will be your rule package name)
  4. Move to the next row (row 2)
  5. In cell B2, type Import
  6. In cell C2, type com.example.model.Order, com.example.model.Charge
Your worksheet should now look like this:

Step 3: Define the First RuleTable

Now let’s create the rule table for orders under $300:
  1. Skip a row and in cell B4, type RuleTable followed by a name: RuleTable Shipping Under 300
  2. In row 5, define the condition and action headers:
  3. Cell B5: CONDITION
  4. Cell C5: CONDITION
  5. Cell D5: ACTION
  6. In row 6, define the object types:
  7. Cell B6: $order : Order
  8. Cell C6: Order
  9. Cell D6: (leave empty)
  10. In row 7, define the constraints and actions:
  11. Cell B7: total < 300
  12. Cell C7: itemsCount == $1, deliverInDays == $2
  13. Cell D7: insert(new Charge($param));
  14. In row 8, add descriptive column headers:
  15. Cell B8: Under $300
  16. Cell C8: Items & Delivery
  17. Cell D8: Shipping Charge
Now add your rule data rows (rules 9-14): | 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:
  1. Skip a row after the previous table
  2. In cell B16, type RuleTable Shipping Over 300
  3. Repeat the same headers and descriptive rows as with the first table
  4. Change B19 (the constraint) to total >= 300
  5. Use the same format for the other condition and action cells
Add the data rows for orders of $300 or more:

Step 5: Create the Data Models

Now, let’s create the Java classes that represent our data model:

Order.java

Charge.java

Complete Example

Your completed decision table should look like this: In the next section, we’ll show you how to run this decision table with a simple Java application.