Tutorials/Database and Rules

From FlexRule Wiki
Jump to: navigation, search

This tutorial shows how to load a filtered data from database and Apply some rules on the data.

  1. How to connect to a Database
  2. Filter data on the database side
  3. Apply some rules on the data
Info2.pngIn this tutorial, we are going to calculate a discount for each individual car based on the year of the car


We want to apply discount business rules on some data that are in the database.

The idea is

  1. a flow loads the data from database by passing dateStart and dateEnd to database query
  2. Then iterates on the loaded data, and sets discount for each individual car based on below business rules
If Year is between 2012 and 2015 then Discount is 10%
If Year is between 2015 and 2017 then Discount is 4%
If Year is 2017 then Discount is 1%
Otherwise no Discount

Main Flow

The idea is

  1. A flow loads the relevant amount of data from database by passing dateStart and dateEnd input parameters. So not all the data is loaded.
  2. Using a Loop to iterate on the loaded data, and sets Discount for each individual car

New Generic Flow

Create a new Project and add a Flow as below:


Use Toolbox and Drag and Drop tools from it to surface of Flow to create the above diagram.


The above Flow below nodes:

  • a Generic Flow
    • Start
    • Database
    • Loop
      • Decision Table
    • End

Add Flow Parameters

Add the below parameters to flow

  1. cars (Out): List of the cars read from database
  2. car (Local): Individual car to set the discount
  3. dateStart (In): Start year to filter data
  4. dateEnd (In): End year to filter data


Database Access

You need to

  1. Set connection string
  2. Add query to load data

Connection String

Connect string can be static inside the flow model, or can be passed in by your application. In this example we use static method.

Bulbgraph.pngPlease note we connect to MsSql database here. You can choose other databases by changing Database Type property.

Add Query

In the database's Properties Window

And you finally have the query ready:


Note pinned.pngThe property Copy Value To of query should be set to Flow parameters cars. This will store the result of query into cars.


In this example, we use a Decision Table for the above rules. To model the above rules:

And you will have an empty Decision Table as below:


Our Decision Table is going to set a Discount on a Car

  1. Define and Input Parameter called car for this decision table
  2. And we will have
    1. One condition (year ranges)
    2. One action (discount assignment)

The final DT is like below:


Expressions settings are:

  1. Condition Year: car.Year
  2. Action Discount: car.Discount

Finalizing Flow

The last step is to finalize the flow to iterate over the cars and pass them to Decision Table. Now we are going to link the Loop node to flow parameters:

Do the same for Item Name and select car and you will have the below properties:



In the Flow, press F5 and the Data Deed Provider screen appears. Then use Data Composer and assign dateStart and dateEnd as below:

  • dateStart = 2014
  • dateEnd = 2017
Bulbgraph.pngData Composer allows you to provide values to the Input Parameters of the flow i.e. dateStart and dateEnd.


When the Flow is run, you can use the data viewer to explore your data. Before going to the Decision Table you can view the below values:


And after running the Decision Table containing the running business rules against the loaded data, you see below result:


Info2.pngAs you see in the cars the Decision Table assigned Discount to each car.

Setup Your Database

In this example, we use MsSql, Create a new database called Car-Insurance, and use below scripts to setup the table and data:

Setup Cars Table

Sample Project

You can download the sample project from here