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

Delivery Cost Calculator

◷ Reading Time: 12 minutes

Introduction

The Delivery Cost Calculator sample will show how to build a project that calculates delivery costs.

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. Or parcel weight is between 22 and 50kg (>22 and <=50), then the delivery cost is $55
  3. When parcel weight is between 50 and 110kg (>50 and <=110), then the delivery cost is $120
  4. Finally if the parcel weight is heavier than 110kg (>110), then the delivery cost is $200

Follow the process steps below to build the project from the beginning.

Process Steps

These are the process steps of the project to create a Delivery Cost Calculator.

New Project

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

Detail of the Create a New Project menu item in the FlexRule Designer Home modal
Create new Project modal showing Delivery Cost Calculator entered as the project name

Add new decision table

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

New document modal showing the list of logic documents available

Select the Decision Logic folder from the Document Types menu and select the Decision Table template. Change the name to Calculator.xml and select OK.

The file location is pre set however if you are storing FlexRule projects in a different location then navigate to your preferred folder.

Once you add this new Decision Table, navigate to the Project Explorer menu in Designer and double click to open it. You will see the new Decision table in the editor view with one condition column populated. See the screenshot below:

New decision table view

Entering Business Rules

In the Toolbox menu you will see the available options that can be added to the Table. For further detail on each option see our Resource Hub article on Decision Table commands.

Adding business rules to a decision table

In this tutorial, we need:

  1. two conditions
  2. one action

From the toolbox, drag and drop the required conditions and action into the table in the Editor view.

Once you add the conditions and action, you can start setting the Column Name for each column in the Decision Table:

  1. Select the first column
  2. From the properties menu, select Column Name
  3. Add the value for the column name
  4. Do the same for all of the columns

Now add the business rules rows and relevant data into your Decision Table.

For this example you will need four rows. As per the Scenario section at the start of this article, enter the following values to build the Decision Table.

Minimum WeightMaximum WeightDeliver Cost
02230
225055
50110120
110200
detail view of a decision table with two conditions and one rule defined. There are four rule rows populated with the required data.

Setting the Hit Policy

The Hit policy defines how the data in the table will be compared against the inputs at execution. By default the Hit Policy will be set to MultiHit.

In this example we want the delivery cost decided when the first set of rules meet the criteria of the input data, so ending the execution of the model.

To do this we will set the Hit Policy of the Decision Table to SingleHit.

  1. Select the Properties button on the Decision Table toolbar in the editor view.
  2. In the Properties menu find the Process All Rows properties
  3. Set the value to false

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

detail view of the decision table with the Single hit option displaying.

Run Preparation

Parameters

To execute a model, variables need to be defined to communicate with a logic document (i.e., a Decision Table in this case). Input and output parameters are required to be defined.

For this example the weight of the parcel defines the delivery cost. So you will need to add the following parameters:

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

To do this, select ‘Properties’ from the toolbar of the Decision Table in the editor view.

Detail of the Properties menu item

Then in the properties menu find the Parameters Declaration section and click on the ellipsis button at the right of the Variables Definition field:

Parameter declaration options in the Properties menu with the ellipsis button highlighted from the Variables Definition field

Alternately, select on the Variable Parameters button from the menu in the editor view.

Detail of the Variable Parameters menu item

This will open the Variables Parameters modal and from the modal menu select the ‘Add a New Empty Item’ option. This will add a new variable to the modal and display the settings options for the parameter in the right side of the modal.

  • Set the Direction to ‘In’.
  • The Name to parcel weight
  • Finally set the Type as decimal

Now add the same for a delivery cost parameter also with type as decimal but set the Direction to ‘Out’.

Variable definitions modal showing the Parcel weight parameter selected with the settings, Direction as In, Name as parcel weight and the type a decimal.
Setting Delivery Cost Calculator Expressions

Now that the modeling is done, you need to bind each column to Runtime, which means you need to link each column in the table to its relevant Parameter. You do this by adding an expression to the column header.

Select the column header cell Minimum Weight and in the Properties menu under the Value section, select the ellipsis button to the right of the Expression field.

Decision table detail view showing Minimum Weight condition header cell selected
Properties menu  options for the decision table condition
Select Expression and click on the (…) button and the .

This will open the expression editor where you can define the required expression. Expressions can be keyed in directly however there are some options to assist with entering values.

Using Control+Space on the keyboard will open a modal displaying all parameters within the document. Use your arrow key on the keyboard or mouse to select parameters from the list to include within your expression.

You can also use the expression Help that is found at the bottom of the modal input field to assist entering expression operators or use the list of expression format options for guidance on expression formats.

Expression definition modal showing the list modal of available parameters in the document.

For each column header cell add the following expressions. Note, $value refers to the value that is entered in each rule cell.

Add the expression below for the Minimum Weight column:

parcel weight > $value

Then the expression below for the Maximum Weight column:

parcel weight <= $value

Finally add the expression below for the Delivery Cost column:

delivery cost

Save and Validate

Make sure your document (i.e., this Decision Table) is saved ( Project --> Save menu). Then from the main menu, select Document --> Validate and you will see the result of the validation in the Message List view.

Document menu open with 'Validate the Current Document option selected

Alternately select the validation menu item from the main menu.

Detail of the Validate current document toolbar menu item

If everything has been entered correctly you should not see any error or conflict messages in the Message List view. If errors or conflicts are detected, follow the details in the Message list to fix any issues.

Message list modal showing four notification messages with zero conflicts, errors, or warnings

No errors or conflicts displaying in the Message List view it means your Decision Table is working as intended 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, this data can be prepared and passed into a model for testing and execution purposes. 

The JSON Composer option is a convenient way of providing values directly to the model without using the Data Feed Provider.

Select the Debug with JSON composer menu item from the main toolbar.

Detail of the Debug with JSON composer menu item

You can add the weight as an input or Load one of the sample JSON files from the folder Sample JSONs within the project zip file attached at the end of this article. Select OK to begin the test execution.

Detail of the Load JSON file menu item

Another way to enter the input is Logic Run Template, the Sample JSONs are saved in there as well. You can click on one of the sample inputs and debug the model.

The model will begin executing and will pause at each step, select Next Step from the debug options in the main toolbar to go step by step through the model.

Detail of the Debug Next Step menu item

After providing the JSON input values and executing the model, your Delivery Cost Calculator Decision Table goes into the Debug mode and you will see the relevant rule row highlighted as you step through the debug.

Decision Table in Debug mode showing yellow highlighted rule that is about to execute

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 Input value Total Weight of 56 conditions Total Weight is between 50 and 110.

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

Decision Table in Debug mode showing green highlighted rule that is met and executed

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

Viewing Delivery Cost Calculator 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 January 25, 2024

Article Attachments

Was this article helpful?

Related Articles