1. Home
  2. Decision Table 101

Decision Table 101

Print Friendly, PDF & Email

◷ Reading Time: 5 minutes

Dealing with OR conditions

Make a complex rule into simpler rules. For converting a rule to a Decision Table, we must split the connected ORs rule into a few simpler rules. For example:

IF (var => 0 && var < 10) || (var >= 25))
   result = true

We can write this in two rules as follows:

IF (var => 0 && var < 10)
   result = true
IF (var >= 25)
   result = true

In this case, the Decision Table would be:

Dealing with IF-ELSE

We need to have more conditions when we have more variables in our original rule. For example, for the following rule:

If (var1== “x” && var2==“y”)
   result = 1
Else
   result = 2

We need two Conditions and an Action. Then its Decision Table would be:

Please note, when a cell in a condition is empty, that condition does not participate in the rule. And when all conditions are empty, the rule (row) does not have any conditions and consequently will be run automatically.

Programmatically

Use the builder to create a decision table model

var builder = new DecisionTableBuilder("DT name");
 builder
      .AddInput("va1", "var2")
      .AddOutput("result")
      .AddCondition("var1", "var1")
      .AddAction("Action", "result")
 
       // adding the rules
      .AddData("x", "y", "1")
      .AddData("", "", "2");
 
var table = builder.Build();

To create the Decision Table model from the builder, simply call Export method.

IElementModel dtModel = builder.Export().First();

Or, you can build an execution plan logic model from it using the TableParser

IElementModel plan = TableParser.CreateParser()
                                 .Parse(table) // parse the ITable model
                                 .First(); // There is only one table always. Get the first one.

If needed, you can create the XML equivalent of IElementModel:

string xml = dtModel.ToXml();

To create a RuntimeEngine check how you can create an engine from XML.

Hit Policy

In any scenario, if you need to process all of the Decision Table’s Rows, you must choose MultiHit (by default, the MultiHit is enabled for a new Decision Table). In some cases, you do not need to process All Rows, in which case “Process All Rows” on a Decision Table’s Properties can be set to SingleHit (for example, after finding a specific answer). As you can see in the screenshot below, the “Process All Rows” in a Decision Table Properties is ‘True’, which means it is a MultiHit Decision Table. If you change the “Process All Rows” to False, then it is a SingleHit Decision Table.

Validate Decision Table

When you have a Decision Table model you can validate the DT structure to ensure it is a valid Decision Table. There are two ways of validating:

  1. Create an instance of an engine using RuntimeEngine
  2. Build the execution plan of DT using the DecisionTable parser
  3. Check XML against the DecisionTable API. FlexRule Designer uses this approach to validate a Decision Table.

Using either of the above methods creates an execution plan for the Decision Table. If your table is not valid structurally, the execution plan cannot be built and an exception will be thrown.

Using RuntimeEngine

When you create an instance of a IRuntimeEngine for a Decision Table, the execution plan will not be created until the engine receives the first request. You can use EnsureLoaded method to force the engine to create the execution plan upfront.

// get the binary of your decision table model
var tableContent = Encoding.UTF8.GetBytes(rule);
 
// create an instance of engine for the DT
var engine = RuntimeEngine.FromXml(tableContent);
 
// Force engine to create the execution plan
engine.EnsureLoaded()

Using Decision Table Parser

Here is how you can create an execution plan for a Decision Table:

// get the binary of your Decision Table model
var tableContent = Encoding.UTF8.GetBytes(rule);
 
// create an instance of XmlTableReader that can read your DT model
var expectedTables = new XmlTableReader(tableContent).GetTables();
 
// Use table parser to build the execution plan for your DT
var dtExecutionPlan = TableParser.CreateParser().Parse(expectedTables).First();

Initializer

When there is a need to do an action initially or set initial values at the beginning of a Decision Table this pattern is useful. Just add a new rule at the beginning of the Decision Table with no condition and one or more actions. Use those actions to do the initial task i.e. setting the initial values, create an object, etc.

In the above example, the first row just sets the default value of output and the rest of the rows adds up that.

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 22, 2019

Was this article helpful?