DT101

From FlexRule Wiki
Jump to: navigation, search

Dealing with OR conditions

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

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

We can write it in two rules as following:

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

In this case the Decision Table would be:

Sample1.jpg

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 2 Conditions and an Action. Then its Decision Table would be:

Example2.jpg

Please note, when a cell in 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 consequences will be run automatically.

Programmatically

Use builder to create a DecisionTable model

  1. var builder = new DecisionTableBuilder("DT name");
  2.  builder
  3.       .AddInput("va1", "var2")
  4.       .AddOutput("result")
  5.       .AddCondition("var1", "var1")
  6.       .AddCondition("var2", "var2")
  7.       .AddAction("Action", "result")
  8.  
  9.        // adding the rules
  10.       .AddData("x", "y", "1")
  11.       .AddData("", "", "2");
  12.  
  13. var table = builder.Build();

Once you have the DecisionTable model, you can build a logic model from it using the TableParser

IElementModel model = 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 to the logic model:

string xml = model.ToXml();

And to create an RuntimeEngine check how you can create an engine from XML.

Hit Policy

In any scenarios, if you need to process All Decision Table's Rows you must choose MultiHit(By default,the MultiHit is enabled for a new Decision Table). But in some cases, you do not need to process All Rows and, then "Process All Rows" on a Decision Table Properties can be set SingleHit(for example after finding an specific answer). As you can see in the below screenshot, the "Process All Rows" on 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.

DTMultiHit.png

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 to validating:

  1. Create an instance of an engine using RuntimeEngine
  2. Build the execution plan of DT using DecisionTable parser
  3. Check xml against the DecisionTable API. FlexRule Designer uses this approach to validate a Decision Table.
Info2.pngUsing either of 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 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();