Decision Table Hello World

From FlexRule Wiki
Jump to: navigation, search

Introduction

Bulbgraph.pngPlease ensure you have installed both FlexRule Runtime and FlexRule Designer on your machine. You can download the products from http://download.flexrule.com

The Decision Table is a tabular form to model business rules and consists of

  • Conditions (input)
  • Actions (output)

And some other columns (e.g., name, priority, etc.).

Info2.pngTo learn more about Decision Tables, please follow the Introduction to Decision table section.

Scenario

In this tutorial, we are going to create a simple Decision table that calculates the Delivery costs based on the Total Weight of a parcel. In the post office, this is usually based on couple of conditions and business rules, but for the sake of this tutorial, let's make it as simple as shown in the business rules below:

  1. If is Package between 0 and 22 kg then the delivery cost is 30$
  2. If is Package between 22 and 50 kg then the delivery cost is 33$
  3. If is Package between 50 and 110 kg then the delivery cost is 120$
  4. If is Package is heavier than 110 kg then the delivery cost is 200$

New Project

Let's create a new project by going to the Project->New menu and filling in the information below:

New-DeliveryCalc.png

When you are asked to link the Runtime

Update-Runtime-Settings.png

Press "Yes" - this will link the installed FlexRule Designer to the new project.

Bulbgraph.pngMake sure you have the FlexRule Runtime installed on your machine before creating a new project.

Add new Decision Table

Let's add a new Decision Table document by going to Document->New menu:

New-DT-Calc.png

...and change the name to Calculator.xml.

Once you create this new Decision Table, then find the file in your Project Explorer and double click to open it. Then you can see the screen below in your FlexRule Designer:

DT-New-Created.png

Setting Parameters

To communicate with a logic document (i.e., a Decision Table in this case), we need to define input and output parameters:

  1. Input: Total Weight
  2. Output: Delivery

Select 'Properties' on the toolbar of the Decision Table:

Delivery Calculator-toolbar-properties.png

Then in the properties window find the Parameters Declaration and click on the Variable Definition button:

Delivery Calculator-DT-properties.png

And then add the parameters below:

  1. Input: Total Weight
  2. Output: Delivery

Delivery Calc - Parameters.png

Info2.png Learn more about Parameters and how to use these.

Entering Business Rules

Using the toolbox, drag and drop required conditions and actions.

Delivery Calculator-building-rules.png

In this tutorial, we need:

  1. two conditions
  2. one action

Once you add the conditions and actions, then start setting the property of Column Name for each column in the Decision Table:

  1. Select the first column
  2. In the properties window, select Column Name
  3. Add the value for the column name
  4. Do the same for all of the columns
  5. Then add the business rules into your Decision Table

And build the Decision Table below:

Delivery-DT-with rules.png

Setting Expressions

Now that the modeling is done, you need to bind each column to runtime, which means you need to link each column to its Parameters. In the expressions below, $value refers to the value that is entered in each cell.

Minimum Weight

Build the expression below for the Minimum Weight column:

Total Weight > $value

Follow the instructions below to set the expressions for each column:

Minimum Weight

Build the expression below for the Minimum Weight column:

Total Weight > $value

Maximum Weight

Build the expression below for the Maximum Weight column:

Total Weight <= $value

Setting Hit Policy

When any of the rules meet the criteria and the delivery cost is decided, the execution must be finished. So we should set the Hit Policy of the Decision Table to SingleHit.

  1. Select the Properties button on the Decision Table toolbar
  2. In the Properties Window, find Process All Rows properties
  3. And set its value to false

As a result, in the Decision Table model you must see that the hit policy is set to SingleHit

DeliveryCalc-singleHit.png

Save and Validate

Make sure your document (i.e., this Decision Table) is saved (Project->Save menu). Then from the menu, select Document->Validate and you should see the image below as the result of validation:

DeliveryCalc-Validate2.png

This means your Decision Table is all good and no issue (i.e., error, overlap, missing, etc.) could be found with it.

Providing Input Values

In FlexRule Designer you can simply provide values to your logic and check the results. To do that you need to:

These steps allow you to build values and bind them to the parameters of your logic.

Info2.pngLearn more about Data composer and Debugging to understand these concepts.

Running the Rules

At this stage, after providing the input values, your Decision Table goes into the Debug mode and you will see the screen below:

DeliveryCalc-Debug.png

You can use menu Debug->Next Step to run the currently selected (Yellow highlighted rule). Then the debugger goes to the next one. Keep doing that until you reach rule #3, which meets the entered Total Weight of 60.60 condition Total Weight is between 50 and 110.

Bulbgraph.png Yellow highlight is the current rule that is about to execute.

DeliveryCalc-Done.png

Bulbgraph.pngA green highlight means the conditions of the rule are met and the action is executed.

Viewing Results

In your FlexRule Designer, switch the bottom pane to Parameter Window and you can see the parameter values:

DeliveryCalc-Parameters.png

Integration into Application

To integrate this into your application, please follow the Integration instruction.