Wednesday, September 18, 2013

Business Rule Engine in Python

Problem:  This a typical business case – we have a lot of business rules to define a bill for certain Business Entity. These business rules were changing every now and then. Just to give a brief example, what are the kind of business rules we are talking about:
Sample rule set:
Charge $ X if quantity P is between A & B
Charge $ Z if time of day is between D & E
Charge $ Y if quantity Q is between B & C
Give DISCOUNT if R is less than F

These Rules will repeat in sets for various other business scenarios and Categories for example
Category 1
Rule 1

Rule 2
Category 2
Rule 1

Rule 2

Rule 3

Rule 4

One way to solve this issue is a lengthy business analysis cycle, where a Business Analyst sits for 2-3 month and write a complete set of business rules for each and every case. The problem with this approach was the Category and Rule keeps on changing, so by the time the Analysis is documented and given to development team the rule itself might have changed. Still let’s assume this will not happen so the code that will come out will be something like this:

Now its clear that the values a_multiplier_1 ,v_1, v_2 will be changed  for the sake of business rules frequently and code will have lot of if and else - Such code is error prone and hard to maintain , patchy stuff and don't forget the business problem of collecting these requirement


Well one way to solve this as above -With python we can solve this a bit differently by using eval(expression) built in function.  But first as defined in Python Docs

eval : The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace

>>> x = 1
>>> print eval('x+1')

Steps below uses the logic above to accomplish the solution for the problem

  • Lets hand over the power of writing & changing the business rules to Analysts team, let them update the values using forms - using Django its a trivial job.
  • The form will allow the Business Analysts to create variables and assign them values 
  • These variables will be of two type - Constants & Expression as shown below
  • Then allow user to write expression like  -
Variable_1 + Variable_2

  • Based on the order of the calculations  write the code that stores these variables and passes the variables of type expression ( Variable_3 & Variable_4) to the eval function to be processed as Python expression. 
  • Lets think of Logic to Calculate Variable_4 ,assuming the function parsing the variables has already stored Variable_1   , Variable_2 and Variable_3 , say in a dictionary with Key as variable name and value as calculated values                                        

  • Return all the values calculated as JSON , which will get consumed by any other function as Key Value pair.  
And with this we are good to go - writing code to write no code ! 

General: Many people will like to call it as Domain Specific Language where in Domain Related Parser can  be written in English Language as shown above and the code can parse it for domain specific purpose. It makes one think how will languages like Java and C# handle these dynamic expressions. That might be a  topic for some other day I guess. 


  1. This comment has been removed by the author.

  2. Wow! I am truly impressed by the way you detailed out virtually every oil information. It can be really going to help me an excellent deal.

    Free Nifty Future Tips