Filip Drools Decision Tables in Spreadsheets

From Training Material
Revision as of 09:46, 14 September 2018 by Fstachecki (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
title
Drools Decision Tables in Spreadsheets
author
Filip Stachecki (filip@nobleprog.pl)

Copyright ⌘

Based on: https://docs.jboss.org

Decision Tables ⌘

ClipCapIt-151203-215107.PNG

source: Decision Model and Notation specification

  • Decision tables are a to way to model complex rule sets and their corresponding actions.
  • Many businesses already use spreadsheets for managing data, calculations, etc.

Spreadsheet Decision Tables in Drools ⌘

  • Drools supports managing rules in a spreadsheet format.
  • Supported formats are Excel (XLS), and CSV
  • A variety of spreadsheet programs such as Microsoft Excel, OpenOffice.org Calc, ... can be used
  • In Drools decision tables are a way to generate rules driven from the data entered into a spreadsheet.

XLS Decision Tables ⌘

  • Require a specific structure to be executed.
  • Decision tables are first converted into DRL file.
  • Only the first sheet is examined for rules.
  • To make it work just import your XLS file into the resources folder.

XLS Decision Table example 1⌘

XLSDecisonTable1.png

XLS Decision Table result⌘

package myPackage;
//generated from Decision Table
import com.nobleprog.FactModel.*;
	no-loop true
// rule values at B11, header at B6
rule "Salary Example_11"
	when
		$e: Emp(sal > 0, sal <= 900)
	then
		System.out.println("Low salary" + ": " + $e.getName());
end

// rule values at B12, header at B6
rule "Salary Example_12"
	when
		$e: Emp(sal > 900, sal <= 1500)
	then
		System.out.println("Mid salary" + ": " + $e.getName());
end

// rule values at B13, header at B6
rule "Salary Example_13"
	when
		$e: Emp(sal > 1500)
	then
		System.out.println("High salary" + ": " + $e.getName());
end

XLS Decision Table example 2 MVEL⌘

XLSDecisonTable2MVEL.png

Problems :)⌘

  • In some locales, MS Office, LibreOffice and OpenOffice will encode a double quote " differently, which will cause a compilation error.
  • The difference is often hard to see. For example: “A” will fail, but "A" will work.
  • To disable auto correction in LibreOffice go to [Tools]->[Auto Correct Options] -> [Localized Options] and uncheck Double quotes Replace
  • XLS Decision tables are first converted to DRL files. Because of that dealing with errors in the XLS file can be hard.

XLS Decision Table example 3 Group⌘

XLSDecisonTable3Group.png

Result DRL analysis⌘

org.drools.io.Resource dt = 
        ResourceFactory.newClassPathResource("rules/Sheet1.xls", getClass());

DecisionTableProviderImpl decisionTableProvider = 
        new DecisionTableProviderImpl();

String drlFile = decisionTableProvider.loadFromResource(dt, null);

System.out.println(drlFile);

RuleSet entries

Keyword Value Usage

RuleSet

The package name for the generated DRL file. Optional, the default is rule_table.

Must be First entry.

Sequential

"true" or "false". If "true", then salience is used to ensure that rules fire from the top down.

Optional, at most once. If omitted, no firing order is imposed.

SequentialMaxPriority

Integer numeric value

Optional, at most once. In sequential mode, this option is used to set the start value of the salience. If omitted, the default value is 65535.

SequentialMinPriority

Integer numeric value

Optional, at most once. In sequential mode this option is used to check if this minimum salience value is not violated. If omitted the default value is 0.

EscapeQuotes

"true" or "false". If "true", then quotation marks are escaped so that they appear literally in the DRL.

Optional, at most once. If omitted, quotation marks are escaped.

NumericDisabled

"true" or "false". If "true", then a String representation is used for DRL instead of a double value from a Numeric cell

Optional, at most once. If omitted, a double value is used.

Import

A comma-separated list of Java classes to import.

Optional, may be used repeatedly.

Variables

Declarations of DRL globals, i.e., a type followed by a variable name. Multiple global definitions must be separated with a comma.

Optional, may be used repeatedly.

Functions

One or more function definitions, according to DRL syntax.

Optional, may be used repeatedly.

Queries

One or more query definitions, according to DRL syntax.

Optional, may be used repeatedly.

Declare

One or more declarative types, according to DRL syntax.

Optional, may be used repeatedly.

Rule attribute entries in the Rule Set area

Keyword Initial Value

PRIORITY

P

An integer defining the "salience" value for the rule. Overridden by the "Sequential" flag.

DURATION

D

A long integer value defining the "duration" value for the rule.

TIMER

T

A timer definition. See "Timers and Calendars".

ENABLED

B

A Boolean value. "true" enables the rule; "false" disables the rule.

CALENDARS

E

A calendars definition. See "Timers and Calendars".

NO-LOOP

U

A Boolean value. "true" inhibits looping of rules due to changes made by its consequence.

LOCK-ON-ACTIVE

L

A Boolean value. "true" inhibits additional activations of all rules with this flag set within the same ruleflow or agenda group.

AUTO-FOCUS

F

A Boolean value. "true" for a rule within an agenda group causes activations of the rule to automatically give the focus to the group.

ACTIVATION-GROUP

X

A string identifying an activation (or XOR) group. Only one rule within an activation group will fire, i.e., the first one to fire cancels any existing activations of other rules within the same group.

AGENDA-GROUP

G

A string identifying an agenda group, which has to be activated by giving it the "focus", which is one way of controlling the flow between groups of rules.

RULEFLOW-GROUP

R

A string identifying a rule-flow group.

Column Headers in the Rule Table

Keyword Initial Value Usage

NAME

N

Provides the name for the rule generated from that row. The default is constructed from the text following the RuleTable tag and the row number.

At most one column

DESCRIPTION

I

A text, resulting in a comment within the generated rule.

At most one column

CONDITION

C

Code snippet and interpolated values for constructing a constraint within a pattern in a condition.

At least one per rule table

ACTION

A

Code snippet and interpolated values for constructing an action for the consequence of the rule.

At least one per rule table

METADATA

@

Code snippet and interpolated values for constructing a metadata entry for the rule.

Optional, any number of columns