RulesFest 2011 — Michael Walsh: A DSL and Rules Engine for Python

on October 26, 2011
Python-Based Rules Engine

At RulesFest 2011, Michael Walsh presented his work creating a business rules engine based on Python. This should be an interesting subject as there are few rules solutions based on dynamic languages.

There’s No Good Python-Based Open Source Rules Engine Available

Michael reviewed his choices as he was trying to find a solution for an open source rules-based policy engine tightly integrated to the Python world:

  • Use an existing rules solution: Pyke, Pychinko, Fuxi (based on Pychinko). The options come from worlds that are wider than pure rules for policies. Therefore, they carry a fair amount of complexity and weight.
  • Integrate with a non-Python solution: Drools with Python through IPC. This option makes Python integration brittle and cumbersome.

If You Can’t Find It, Build It

Given these choices, Michael created his own solution. This was surprising given that the Python community does not enthusiastically endorse creating your own rules solution. This community is very much re-use oriented and proud of the productivity, expressiveness, and applicability that can be achieved with that language.

  • See for example this Stack Overflow discussion: http://stackoverflow.com/questions/467738/implementing-a-rules-engine-in-python.
  • An even more confrontational point of view can be found here: http://catherinedevlin.blogspot.com/2009/06/dont-need-no-stinking-rules-engine.html

Michael shared his approach:

  • ANother Tool for Language Recognition (ANTLR) provided the key support to create and maintain languages
  • Domain-Specific Language (DSL) easily supported by ANTLR. (For more on DSLs, I recommend checking out Martin Fowler’s book, Domain-Specific Languages)
ANTLR is an excellent product so his approach seemed sensible to me. As far as I’m aware, almost all DSL tooling leverages ANTLR. Michael used it to support a DSL syntax that catered better to the rules writers than straight Python code. The engine itself is an interpreter essentially working on a tree built from the parsing. It navigates the tree, evaluating the premises and executing the actions. There is no RETE type algorithm in it.

You can find the details of his implementation on Github at https://github.com/nemonik/Intellect

Example Rules

Below is an example rules base (obtained from Github). The complete implementation amounted to approximately 16,000 lines of code. Jacob Feldman shared his experience with a Python-based constraint engine that may be able to complement this solution.

import logging
first_sum = 0
second_sum = 0
rule "set both first_sum and second_sum to 1":
        agenda-group "test_d"
        then:
                attribute (first_sum, second_sum) = (1,1)
                log("first_sum is {0}".format(first_sum), "example", logging.DEBUG)
                log("second_sum is {0}".format(second_sum), "example", logging.DEBUG)
rule "add 2":
        agenda-group "test_d"
        then:
                attribute first_sum += 2
                attribute second_sum += 2
                log("first_sum is {0}".format(first_sum), "example", logging.DEBUG)
                log("second_sum is {0}".format(second_sum), "example", logging.DEBUG)
rule "add 3":
        agenda-group "test_d"
        then:
                attribute first_sum += 3
                attribute second_sum += 3
                log("first_sum is {0}".format(first_sum), "example", logging.DEBUG)
                log("second_sum is {0}".format(second_sum), "example", logging.DEBUG)
rule "add 4":
        agenda-group "test_d"
        then:
                attribute first_sum += 4
                attribute second_sum += 4
                log("first_sum is {0}".format(first_sum), "example", logging.DEBUG)
                log("second_sum is {0}".format(second_sum), "example", logging.DEBUG)
                halt
rule "should never get here":
        agenda-group "test_d"
        then:
                log("Then how did I get here?", "example", logging.DEBUG)

Next, see our recap on Nate Derbinsky’s talk.

Learn more about Decision Management, Sparkling Logic’s SMARTS™ Data-Powered Decision Manager, and how we’re helping organizations operationalize Python 

Search Posts by Category

ABOUT US

Sparkling Logic Inc. is a Silicon Valley-based company dedicated to helping organizations automate and optimize key decisions in daily business operations and customer interactions in a low-code, no-code environment. Our core product, SMARTS™ Data-Powered Decision Manager, is an all-in-one decision management platform designed for business analysts to quickly automate and continuously optimize complex operational decisions. Learn more by requesting a live demo or free trial today.