Tutorial: Drools Decision Tables in Excel for a Product Proposal
The Tutorial covers the following points
-
Defining a Product Sales Decision Table for a customer in Excel, covering a product proposal, which is
-
dependent of its life stage, accumulated assets as well as expressed needs
-
as well as calculating a discount which can be offered him
-
We will use a Drools Global Variable to communicate back the proposed initial offering
The official Drools site
http://www.drools.org/ provides the following definitions:
Drools is a Business Rules Management System (BRMS) solution. It provides a core Business Rules Engine (BRE), a web authoring and rules management application (Drools Workbench) and an Eclipse IDE plugin for core development.
-
This sounds quite huge and complex, but the current design and architecture of of Drools, allows a very lightweight and simple integration of the BRMS in Java applications, which require a flexible, “easy to manage” approach of Business Rules by using spreadsheet(s).
This nifty feature is called “Drools Decision Tables in Excel"
Very briefly speaking, in Drools decision tables are a way to generate rules driven from the data entered into a spreadsheet. All the usual features of a spreadsheet for data capture and manipulation can be taken advantage of. Decision tables are a “precise yet compact” (ref. Wikipedia) way of representing conditional logic, and are well suited to business level rules.
Two kind of objects plays a role in context of the Drools BRMS:
-
the Condition Object(s), which defines the input objects required in a decision rule
-
the Action Object(s), which is required in order to store the results of the calculated decision
Condition Object: Customer
So let’s start with our example:
We want to model the decision rule of a product proposal for a financial services digital journey which takes into consideration the questions answered by the customer on our web page. It includes the following questions
-
two personal data questions about its current life stage, as well as its current financial assets
-
a set of customer preferences in context of its financial needs
The data collected we will store in our Customer class
We define 3 enum’s in order to differentiate the customer’s question answers given
-
CustomerNeed, CustomerLifeStage, CustomerAssets
Action Object: Offer
The Offer object will store the result of our product offering rule calculation.
In our example our offering will consist of a
-
Product Package offered by the financial service
-
A list of individual Products
-
A discount of the overall price of the offering. We assume that the base price is not part of this calculation and provided as part of a Product Catalog service.
Also here we keep a enum for the Product Package, as well as the individual Product result.
Decision Table Excel
Having defined the Input and Output Object, it’s time to define the our product offering rule in a Decision Table Excel. Drools Decision Table Excel functionality is very powerful, you can find the full documentation
here.
Consider decision tables as a course of action if rules exist that can be expressed as rule templates and data: each row of a decision table provides data that is combined with a template to generate a rule.
As each row is a rule, the same principles apply. As the rule engine processes the facts, any rules that match may fire. (Some people are confused by this. It is possible to clear the agenda when a rule fires and simulate a very simple decision table where only the first match effects an action.) Also note that you can have multiple tables on one spreadsheet. This way, rules can be grouped where they share common templates, yet at the end of the day they are all combined into one rule package. Decision tables are essentially a tool to generate DRL rules automatically.
So it’s a lightweight and easy to understand approach
-
for a business analyst to model the rules on the ons side
-
as well as convenient way for a business user to verify and sign-off the rules
Having the possibility to distribute the rules in multiple tables or separate Excels, allows to handle governance and business ownership in an easy and straightforward way.
In a normal case the Excel’s are delivered as part of the resource bundle of the JAR file. But you could think of more flexible strategies by loading the Excels during startup from a local or remote location in order not being dependent from code build delivery.
The below screenshot provides an overall overview of a Drools Decision Table Excel
So let’s look at the different part of the Excel.
Rule Set Part
The Rule Set part consists amongst others (not all options used in the tutorial) of a
-
Import row, which imports the above defined decision and action classes.
-
Variables row, which defines global variables. In our example, we define the Offer class, which is used to store and transfer out the product proposal. As one can see in the later section, the offer variable, will be initialised by the caller and passed by reference to the rule execution.
-
Notes row, for a descriptive text.
Rule Table Part - Rule Definition
The screenshot below shows the Rule Definition Part.
The Name and Condition Column, define left-hand side (LHS) of the decision rule.
-
The blue cells, are defining the Code snippet and interpolated values for constructing a constraint within a pattern in a condition.
-
I.e. the first column condition will pass in the life stage of a Customer to the 9 rules defined, the second column the assets of a Customer and so on
-
Each row from 11-19 defines one specific rule. In our tutorial we define some rules
-
for a customer of the life stage *Career Focused”, i.e. which Product Package should be chosen (11),
-
as well which discount should be taken into consideration in context with its accumulated asset (12-15)
-
furthermore we will propose additional individual products dependent on his expressed needs (16-19)
The action we take for each row is defined in the ACTION columns
-
The first action column is used to identify optimal product package, so only relevant for the rule of line 11.
-
The second action column is used propose the single product and relevant for rule line 16-19
-
The third action column is used to calculate a discount and is relevant for rule line 12-15
Our tutorial covered some proposal rules mainly for the life stage Career Focused. In a real life example we would have rules for each life stage and multiple rules for any product combination. As already explained above, drools is comfortable to consumer multiple tables and spreadsheets which allows to decompose the offering for example along functional boundaries in order to manage the complexity within a single Excel table.
Instantiating a Rule Engine
Instantiating a Rule Engine is quite straightforward and can be achieved by establishing a
KieSession
an passing the Decision Table Excel.
Testing the Rules
In order to test our rules, we have build up a Customer object, which holds the answers to our questions from a dedicated customer
-
In our example a customer in life stage CAREERFOCUSED with an asset of 150k-300k and a need for LifeInsurance, Saving Accounts and Mortage
-
The customer will be inserted into the KieSession into which we loaded beforehand our Excel (which is part of our jar file delivery)
-
We initialize our action object offer, which will be added as a global variable under the name offer to the KieSession (remember the variable definition in the Excel)
-
It’s time to the calculate the offering by calling kSession.fireAllRules()
-
Having done that our offer Object will have the result of the calculation. In our example
-
An offer for the product package CAREERFOCUSED_PACKAGE with a discount of 10%, as well as to additional single products.
The test output will also spill out the compiled decision rules (DSR) from the Excel. So the Excel is just another rule definition concept which will be converted into the powerful DSR drool language and feeded into their Production Rule System with its inference engine.
Outlook - Future Proof
By building up a Product Proposal rule engine on top of Drools, you will gain
-
an easy to model and manage rule definition approach by just using Excel
-
the flexibility to load Excel either out of a JAR or any kind of other local or remote location
-
a future proof approach in case the complexity of the rules will grow. I.e. Drools is a hybrid reasoning system which supports forward-reasoning as well as backward-reasoning programming styles and comes with a Business Rules Engine (BRE), a web authoring and rules management application (Drools Workbench) and an Eclipse IDE plugin for core development (should your rule appetite grow)
This blog entry was fully produced within Evernote and published using the
Cloudburo Publishing Bot
.
comments powered by Disqus