1. Home
  2. Sample Projects
  3. Delivery Cost Calculator

Delivery Cost Calculator

◷ Reading Time: 9 minutes

Introduction

The sample will show how to build a project that calculates the delivery cost.

Scenario

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

  1. If the parcel weight is between 0 and 22kg (>0 and <=22), then the delivery cost is $30
  2. If the parcel weight is between 22 and 50kg (>22 and <=50), then the delivery cost is $55
  3. If the parcel weight is between 50 and 110kg (>50 and <=110), then the delivery cost is $120
  4. If the parcel weight is heavier than 110kg (>110), then the delivery cost is $200

Video Description

How to build from scratch:

Process Steps

These are the process steps of the project.

New Project

Let’s create a new project by clicking on the button, Create a New Project on the Home screen:

Add new decision table

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

…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:

Entering Business Rules

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

In this tutorial, we need:

  1. two conditions
  2. one action

Once you add the conditions and actions, then start setting the properties of the 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:

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

Run Preparation

Parameters

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

  1. Input: parcel weight
  2. Output: delivery cost

Select ‘Properties’ on the toolbar of the Decision Table:

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

or click on the Variable Parameters button.

And then add the parameters below:

  1. Input: parcel weight
  2. Output: delivery cost
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:

parcel weight > $value

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

Select the column Minimum Weight and on the properties window, you can see the column properties.
Select Expression and click on the (…) button and the expression editor will pop up.
Use Control+Space on the keyboard and a list box will appear. Then use your arrow key on the keyboard or mouse to select from the list.

Minimum Weight Build the expression below for the Minimum Weight column:

parcel weight > $value

Maximum Weight Build the expression below for the Maximum Weight column:

parcel weight <= $value

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:

Or you can click on the validation icon.

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 and Running the Rules

When a model requires Input data then these can be prepared and passed into a model for testing and execution purposes. JSON Composer is a convenient way of providing values directly to the model without using the Data Feed Provider.

Click on Debug with JSON composer.

Load one of the sample JSON files. Samples are in the folder Sample JSONs in your project folder.

Click OK

Click Next Step to go step by step.

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

You can use the 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 56 conditions Total Weight is between 50 and 110.

The yellow highlight is the current rule that is about to execute.

A 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:

Download the project

The project can be downloaded using the attachment at the end of the page.

Updated on November 24, 2022

Article Attachments

Was this article helpful?

Related Articles