Skip to end of metadata
Go to start of metadata

Drools Decision Tables

Decision tables are a useful way to represent conditional logic in a compact format. This format is also readily readable and editable by non technical users (such as business analysts). Drools decision tables can utilise a spreadsheet (such as Excel) as the means to capture decision logic, in a user friendly way.

Developers can provide access to rule parameters, while hiding the code scripts that map the rule data to the underlying object model.

For a background on decision tables, including details on their structure, please read Business rules in decision tables explained

Overview


(click to enlarge)
You can see in the above example what one looks like, and how the rule scripts are embedded (and hidden) in the tables.

Capture "business" level rules in a "decision table" format !
Use you favourite spreadsheet software, such as OpenOffice or Microsoft Excel, or any Excel 97 format compatible spreadsheet software, or any software that can emit CSV data.

PURE java at runtime.

Commercial rules engines sometimes offer a UI that presents rules in a tabluar format called decision tables (which are really a very old fashioned but effective way of capturing conditional logic).

In Summary:

  • Works with Microsoft Excel (or Open Office !) spreadsheet input format
  • NEW: CSV Input format is now supported (**)
  • Pure java runtime.
  • Can emit DRL that is readable, and you can use Drools as normal if you need to.
  • Can use the cell number to set "salience" allowing top to bottom "ordering" or rules (easier for non RETE savvy people to understand).
  • Use excel features for input validation, hiding away code snippets, drop downs
  • Works by doing string interpolation to put values from the cells into the scripts

Getting it Working

This feature is part of Drools version 2.1. Decision tables currently use the java semantic module, and thus has all the same dependencies. The easiest thing to do is just use the "drools-all" jar, which has all the drools dependencies and the decision tables module. The decision table module is also compatible with version 2.0.

To use Drools decision tables, it is very similar to the regular drools API.

Syntax of decision tables

Decision tables are parsed top down, but only once (when you use the DecisionTableLoader utility class, and only one pass then). Drools then builds the rulebase as normal. You are free to cache the Rulebase (it is no different to a normal rule base).

Keyword

Description

required?

RuleSet

The cell to the right of this contains the ruleset name

exactly one

Sequential

The cell to the right of this can be true or false. If true, then salience is used to ensure that rules fire from the top down

optional

Import

The cell to the right contains a comma seperated list of java classes to import

optional

RuleTable

A cell starting with RuleTable indicates the start of a definition of a rule table. The actual rule table starts the next row down. The rule table is read left-to-right, and top-down, until there is one BLANK ROW.

at least one. if there are more, then they are all added to the one ruleset

CONDITION

Indicates that this column will be for rule conditions

At least one per rule table

ACTION

Indicates that this column will be for rule consequences

At least one per rule table

PRIORITY

Indicates that this columns values will set the 'salience' values for the rule row. Over-rides the 'Sequential' flag.

Optional

DURATION

Indicates that this columns values will set the duration values for the rule row.
The syntax for the values are HXX,MXX,SXX where 'XX' represents the appropriate unit of time.

Optional

NAME

Indicates that this columns values will set the name for the rule generated from that row

Optional

Worksheet

By default, the first worksheet is only looked at for decision tables.

Functions

The cell immediately to the right can contain functions which can be used in the rule snippets. Drools supports functions defined in the DRL, allowing logic to be embedded in the rule, and changed without hard coding, use with care.

optional, only once

Variables

The cell immediately to the right can contain 'application-data' which drools supports. This can be a type, followed by a variable name. (if multiple variables are needed, comma seperate them).

optional, only once

RuleTable syntax

Rule tables can appear anywhere (on the first worksheet) and you can have as many of them as you need. Each RuleTable will take in the same type and number of parameters, and it all gets added together as one big happy ruleset.

The Type can be a fully qualified java class, or just the class if it was imported. You must have at least on parameter.

RuleTable structure

The first row after RuleTable contains cell which will have either CONDITION or ACTION (or one of the other other optional column types mentioned above). The cell directly below RuleTable is where this starts.
Once there are no more cells this row which have CONDITION or ACTION, then this is not part of the rules.

The 2nd row contains the scripts. If they are an ACTION, then please remember to add a semi colon. You can have multiple actions, they all get added together as one drools consequence.

The 3rd row contains the plain english description (which you typically show). This is just text for display.

From there down, it is data for the scripts. If nothing appears in a cell, then the condition or action will not apply (thus you can create many columns with all possible conditions, and only apply them where you need them).

Script syntax

Script in the 2nd row, are as per normal drools. However, you use $param and $1, $2, $3... as place holders which will have the values from the cells below substituted in when the ruleset is built.

$param will be swapped for whatever value is in the cell (so don't forget quotes if it is a string).

You can use numbers if the cell contains a comma seperated list of values

$1 and $2 will be swapped for the values as per a list of comma seperated values in the cell

DRL

You can use DecisionTableLoader directly, and cache the RuleBase as per normal, or you may want to use SpreadsheetDRLConverter to generate classic DRL which you can then use/deploy. The choice is yours.

Its all pretty simple ! Which is half the idea.

Alternatives

For a high level "business" rule engine, OpenRules is an alternative that is all excel based, and more, under a GPL open source licence. NxBRE is an interesting inference engine for .Net that uses Visio documents as an input format (open source). Corticon is a vendor that produces as high-end decision table based rule engine, and of course ILog JRules supports decision tables as an input format (both are very much NOT open source or free !).

CSV Input format (**)

As well as excel 97 input format, CSV is now supported, so any tool that can emit CSV can be used (including every spreadsheet application under the sun). This is not part of the standard 2.1 build (didn't make it in time) but attached is a jar which is compatible with Drools from version 2.0 up (still requires Apache POI if you wish to use Excel input format as well): drools-decisiontables-2.1_with_CSV.jar

Further reading

Business rules in decision tables explained
TeamAllocationExample.xls --> example spreadsheet from Drools examples module.
This can also serve as a template for your decision tables.

Labels: