1. Home
  2. Modeling decision table

Modeling decision table

Print Friendly, PDF & Email

◷ Reading Time: 12 minutes

The Result Decision Table

In this example of modeling a Decision Table, we picked the table from a previous article.

DestinationAgeIsWeekDayMoreThan6Discount
Group1100
IndiaGroup2Y40
IndiaGroup2N50
Group3YN0
Group3YY10
IndiaGroup3NY30
IndiaGroup3NN20
AsiaGroup2YY50
AsiaGroup2YN40
AsiaGroup2NY75
AsiaGroup2NN65
AsiaGroup3NY35
AsiaGroup3NN25

With a slight change (adding a row) to this table, the model is ready.

Decision Table in a Spreadsheet

So far the prepared table has the following:

  1. All the conditions required for the decision (Check Conditions)
  2. An action for each rule (Check Actions)
  3. Column names

In order to execute the decision, there are some additional rows that need to be added to the table in order to make it executable:

Question: What is the input of this Decision Table?

Answer: The data type we prepared for the decision that encapsulates all of the columns of the table (excluding the action columns) as properties of the type. Let’s call it model

Additional row: Input model


Question: What is the goal of this Decision Table? What does it try to achieve?

Answer: The discount percentage for different scenarios.

Additional row: Output discount


Question: How does the data in the table relate to the model?

Answer: The attributes of the model are the properties shown in each column. The actions (goals of the decision) are located in the last column.

Additional rows:

DestinationAgeIsWeekDayMoreThan6Discount
ConditionConditionConditionConditionAction
model.DestinationAgeIsWeekDayMoreThan6discount

Note that in the last new row we added, we connected the data to the Input and Output of the Decision Table. You can use $value as a replacement for the cell values from your table.

In the condition column, you can use expressions to establish the relationship between the values and the condition. As we are simply going to set the discount value in the Action column, we use assignment expression to assign a value to discount. Now you can understand where those rows originate.

Modeling Using Excel

In a spreadsheet (e.g., MS Excel-based Decision Tables), the properties of a Decision Table can be defined as shown below:

  1. Title
  2. Header
    1. Input
    2. Output (optional)
    3. Import (optional)
  3. Column Type
  4. Expression (formula)
  5. Column Name
  6. Empty row
  7. Data rows (values/expressions)

You can have all of the rows above defined in an Excel-based Decision Table

Title

The Decision Table must start with a Decision keyword and then a name in the first row.

Format: Decision [decision name]

To finalize the signature part of the Decision Table, after the row containing the column name, there must be an empty row and then your data in the table may begin. All done. The decision table is modeled.

Input

Allows adding input parameters with series of comma-separated variable names

Format: Input v1,v2,v3...

Output

Allows defining output [parameter] with adding a series or comma-separated variable names

Format: Output v1, v2, v3...

Import

Allows the addition of a type parameter and usage of the Type’s static members

Format: Import fullTypeName

For example, Import System.DateTime will enable you to use DateTime.Now in your rules expression

Building Sample

You can build an Excel spreadsheet that has the following structure:

Decision discount percentage
Input model
Output discount
ConditionConditionConditionConditionAction
model.Destinationmodel.Agemodel.IsWeekDaymodel.MoreThan6discount
DestinationAgeIsWeekDayMoreThan6Discount
     
Group1100
IndiaGroup2Y40
IndiaGroup2N50
Group3YN0
Group3YY10
IndiaGroup3NY30
IndiaGroup3NN20
AsiaGroup2YY50
AsiaGroup2YN40
AsiaGroup2NY75
AsiaGroup2NN65
AsiaGroup3NY35
AsiaGroup3NN25

Default Compare Operator

As you can see in the condition columns, we just compare the properties value of the object to the cell values. Where is the compare operator? By default, in condition columns, the compare operation is the equal operator or ==, which can be eliminated. So for example in the first column (condition for Destination), instead of writing model.Destination==$value we can just write model.Destination.

This default operator behaviour can simplify the Decision Tables a lot.

Cell Values

Cells can simply be values or expressions: The Decision Table can have many different expressions:

  1. Simple values
  2. Partial conditions
  3. Range expressions
  4. … and more, which you can learn about at Decision Table Expressions.

Authoring Decision Table

Using Excel

Simply copy and paste the table into MS Excel or Google Documents, for instance. Save the Excel spreadsheet to something like DecisionTable.xlsx.

Use MS Excel for Authoring

If you don’t have MS Excel, you can use Google Documents and create a spreadsheet to author the rule.

Use Google Document for Authoring

Using Visual Designer

Using Decision Table Editor over a generic Spreadsheet document has its own advantages.

  1. You can simply validate the document and find any overlaps by pressing the validate button on the FlexRule Designer. Please have a look at Model Validation to see how it works.
  2. Allows expression builder to help you build the values and expressions
  3. Has information and context about the whole rule project and relations of the logic and how it fits into scenarios (e.g., IN DMN for Flow scenario).
  4. You can link back and forth to the Decision Table and trace and track its usages

A picture of the Decision Table Editor is shown below:

Using XML

In XML language, you need to fill the following structure.

<DecisionTable name="discount percentage">
  <Declaration>
    <!-- Input, Output and Type definitions come here -->
  </Declaration>
  <Columns>
    <!-- Table signature comes here -->
  </Columns>
  <Data>
    <!-- Table data comes here between series of 'Row' and 'Value' elements 
         in which each row contains multiple value entries as a text element
         The data section represents the condition's expressions. 
         These can be as simple values (data) or as complex expressions.
     -->
  </Data>
</DecisionTable>

For this example, the following Xml is the completed model:

<DecisionTable name="discount percentage">
  <Declaration>
    <Define name="model" direction="in"/>
    <Define name="discount" direction="out"/>
  </Declaration>
  <Columns>
    <Condition name="Destination" expression="model.Destination"/>
    <Condition name="Age" expression="model.Age"/>
    <Condition name="IsWeekDay" expression="model.IsWeekDay"/>
    <Condition name="MoreThan6" expression="model.MoreThan6"/>
 
    <Action name="Discount" expression="discount"/>
  </Columns>
  <Data>
    <Row><Value></Value><Value>Group1</Value><Value></Value><Value></Value><Value>100</Value></Row>
    <Row><Value>India</Value><Value>Group2</Value><Value></Value><Value>Y</Value><Value>40</Value></Row>
    <Row><Value>India</Value><Value>Group2</Value><Value></Value><Value>N</Value><Value>50</Value></Row>
    <Row><Value></Value><Value>Group3</Value><Value>Y</Value><Value>N</Value><Value>0</Value></Row>
    <Row><Value></Value><Value>Group3</Value><Value>Y</Value><Value>Y</Value><Value>10</Value></Row>
    <Row><Value>India</Value><Value>Group3</Value><Value>N</Value><Value>Y</Value><Value>30</Value></Row>
    <Row><Value>India</Value><Value>Group3</Value><Value>N</Value><Value>N</Value><Value>20</Value></Row>
    <Row><Value>Asia</Value><Value>Group2</Value><Value>Y</Value><Value>Y</Value><Value>50</Value></Row>
    <Row><Value>Asia</Value><Value>Group2</Value><Value>Y</Value><Value>N</Value><Value>40</Value></Row>
    <Row><Value>Asia</Value><Value>Group2</Value><Value>N</Value><Value>Y</Value><Value>75</Value></Row>
    <Row><Value>Asia</Value><Value>Group2</Value><Value>N</Value><Value>N</Value><Value>65</Value></Row>
    <Row><Value>Asia</Value><Value>Group3</Value><Value>N</Value><Value>Y</Value><Value>35</Value></Row>
    <Row><Value>Asia</Value><Value>Group3</Value><Value>N</Value><Value>N</Value><Value>25</Value></Row>
  </Data>
</DecisionTable>

Now you are able to read the model by using XmlTableReader

private static IIterable<IElementModel> LoadDecisionTableModel()
{
    // Load the excel document
    var reader = new XmlTableReader(File.ReadAllBytes(@"DecisionTable.xml"));
    reader.Entries.Add("Y", "true", true);
    reader.Entries.Add("N", "false", true);
 
    // Reads all the tables in a specific sheet
    IIterable<ITable> tables = reader.GetTables();
 
    // Create parser for table
    ITableParser tableParser = TableParser.CreateParser();
 
    // Parse the tables to model
    return tableParser.Parse(tables);
}

Executing a Decision Table Model

By defining the ageing group as an Enum type

enum AgeGroup
{
    Group1,
    Group2,
    Group3
}

Executing the Decision Table is simple. What you need to do is create an execution plan and engine, then execute and retrieve the discount value.

// Load an engine instance and configure custom entries (when required)
var engine = RuntimeEngine.FromSpreadSheet(File.OpenRead(@"YOUR_EXCEL_FILE.xlsx"), "YOUR_SHEET_IN_EXCEL");
engine.OnRunning = (ae) => ae.Context.VariableContainer.RegisterFunction(typeof(AgeExtensions));
 
// Note you can load the custom entries from config file as well.
engine.Entries.Add("Y", "true", true);
engine.Entries.Add("N", "false", true);
 
// Creating the input model 
var sampleData = new 
    {
        Age = AgeGroup.Group3,
        Destination = "India",
        MoreThan6 = false,
        IsWeekDay = false,
    };
 
// Call run method of engine and pass your input object
var result = engine.Run(new object[] { model });
 
// Reading the discount value from the result that is returned by the Run method
var discount = result.VariableContainer["discount"];

Logic Model Reflection

And you are ready to read the table as a model in your application by using ExcelTableReader.

private IIterable<IElementModel> LoadDecisionTableModel()
{
    // Load the excel document
    var reader = new ExcelTableReader(@"DecisionTable.xlsx", "Sheet1");
    reader.Entries.Add("Y", "true", true);
    reader.Entries.Add("N", "false", true);
 
    // Reads all the tables in a specific sheet
    IIterable<ITable> tables = reader.GetTables();
 
    // Create parser for table
    ITableParser tableParser = TableParser.CreateParser();
 
    // Parse the tables to model
    return tableParser.Parse(tables);
}

Please note: The application that is executing the code DOES NOT need to have an Excel application or third-party component installed.

What’s next?

  1. Introduction to decision tables
  2. Preparing a decision table
  3. Modeling decision table
  4. Decision Model and Notation – decision table
  5. Check overlaps
  6. Decision Table final logic
  7. Multilingual decision table
  8. Decision Table 101

Tutorials

  1. Decision Table Hello World
Updated on July 19, 2019

Was this article helpful?