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)
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

