Another Business Rule Engine: simple and free

Part A. Trees and tables.

sefedo
5 min readJul 16, 2023

When we think about business rule engines (BRE) we often if not always picture the process of decision making as walking down the decision tree. It is easy to explain, visualize and understand.
It is not however, easy to implement and maintain a BRE that operates directly with the trees.
In Part A of this series I will show how to migrate from decision tree to decision table. In subsequent parts I will outline a simple yet effective BRE based on such tables.

1. A simple example.
A small hotel offers “deals” to their customer (whatever the deal is, can be no cancellation or less towels in the room). For the deals the price would be $90 per night and for regular booking it is $100. The tree looks like this:

And a programmer may code it like this:

-- version 1 --
if [Offer Type] == deal
set {Room Price} to 90
else
set {Room Price} to 100

or like this:

-- version 2 --
if [Offer Type] == deal
set {Room Price} to 90
if [Offer Type] == regular
set {Room Price} to 100

We use square brackets [] for input parameters, where customers specify their requirements.
We use curly braces {} for output parameters, which contain the results of decision making.
(And no, this is not a real programming language that I use here.)

So the program works and the bookings flow in.

Now, encouraged by a success of the first implementation, hotel manager decides to add more flexibility to the booking:

And our programmer immediately codes that:

-- version 1 --
if [Offer Type] == deal
if [Room View] == sea
set {Room Price} to 95
else
set {Room Price} to 90
else
if [Room View] == sea
set {Room Price} to 105
else
set {Room Price} to 100

or

-- version 2 --
if [Offer Type] == deal and [Room View] == sea
set {Room Price} to 95
if [Offer Type] == deal and [Room View] == garden
set {Room Price} to 90
if [Offer Type] == regular and [Room View] == sea
set {Room Price} to 105
if [Offer Type] == regular and [Room View] == garden
set {Room Price} to 100

And now, our hotel manager goes wild, he adds another condition, re-arranges the tree and re-assigns the room prices:

What the programmer should do?

If he sticks with Version 1 style, the code will be completely new and hard to compare with previous version.
If he wants to preserve previous version as much as possible he has to rebuild the tree, that mean talking and arguing with the manager.
Version 2 seems a good option but it becomes overloaded and looks ugly overall.
Of course, there is an optimized version 3 (not shown here) but that would lead to even more maintenance as compared to Version 1.

-- version 2 --
if [Offer Type] == deal and [Room Floor] == top and [Room View] == sea
set {Room Price} to 100
if [Offer Type] == deal and [Room Floor] == top and [Room View] == garden
set {Room Price} to 90
if [Offer Type] == regular and [Room Floor] == top and [Room View] == sea
set {Room Price} to 105
if [Offer Type] == regular and [Room Floor] == top and [Room View] == garden
set {Room Price} to 100
if [Offer Type] == deal and [Room Floor] == ground and [Room View] == sea
set {Room Price} to 100
if [Offer Type] == deal and [Room Floor] == ground and [Room View] == garden
set {Room Price} to 90
if [Offer Type] == regular and [Room Floor] == ground and [Room View] == sea
set {Room Price} to 105
if [Offer Type] == regular and [Room Floor] == ground and [Room View] == garden
set {Room Price} to 100
if [Offer Type] == deal and [Room Floor] == other and [Room View] == sea
set {Room Price} to 100
if [Offer Type] == deal and [Room Floor] == other and [Room View] == garden
set {Room Price} to 95
if [Offer Type] == regular and [Room Floor] == other and [Room View] == sea
set {Room Price} to 105
if [Offer Type] == regular and [Room Floor] == other and [Room View] == garden
set {Room Price} to 100

Let me reformat Version 2 a bit (remember, this not a real programming language):

-- version 2 reformatted --
if [Offer Type] and [Room Floor] == top and [Room View] set {Room Price} to
== deal == top == sea 100

if [Offer Type] and [Room Floor] == top and [Room View] set {Room Price} to
== deal == top == garden 90

if [Offer Type] and [Room Floor] == top and [Room View] set {Room Price} to
== regular == top == sea 105
...
== regular == top == garden 100
== deal == ground == sea 100
== deal == ground == garden 90
== regular == ground == sea 105
== regular == ground == garden 100
== deal == other == sea 100
== deal == other == garden 95
== regular == other == sea 105
== regular == other == garden 100

Now it looks more like a table, is it not? After we complete the process we have what is known as “decision table”.
It has “conditions” section, “actions” section and a set of rules that analyze conditions and select appropriate action(s).

Will review this in more details in Part B.

--

--