Call to Action

Webinar: Take a tour of Sparkling Logic's SMARTS Decision Manager Register Now
Home » RulesFest 2011- Michael Walsh: A DSL and Rules Engine for Python

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


Written by: Carlos SerranoPublished on: Oct 26, 20111 comment

Michael presents his work creating a rules engine based on Python. This should be an interesting subject – there are few rules solutions based on dynamic languages.

Michael Walsh
Micheal Walsh at RulesFest 2011

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

  • use an existing rules solutionPyke
    Pychinko
    Fuxi (based on Pychinko)
    these come from worlds that are wider than pure rules for policies, and carry a fair amount of complexity and weight
  • integrate with a non-Python solutionDrools with Python through IPC, solutions that make the Python integration brittle and cumbersome

Which basically pushed Michael into creating his own solution. Creating your own rules solution tends to be something that the Python community – very much re-use oriented and very proud of the productivity that can be achieved with that language, and its expressive power and applicability – does not enthusiastically endorse. See for example this Stackoverflow discussion. An even more confrontational point of view can be found here

Michael shared his key choices

  • ANTLR provides the key support to create and maintain languages
  • The DSL approach easily supported by ANTLR(on DSLs, check Fowler’s book)
The choice of ANTLR is not surprising – at this point, almost all DSL tooling I know leverages that excellent product. 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.

The details of his implementation can be found on Github at https://github.com/nemonik/Intellect

 

An example rules base (obtained from Github):

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)

 

The complete implementation amounted to approximately 16000 lines of code.

Jacob Feldman shared his experience with a Python-based constraint engine that may be able to complement this solution.

 


One thought on “RulesFest 2011- Michael Walsh: A DSL and Rules Engine for Python

Please share your thoughts on this post:

Your email address will not be published. Required fields are marked *

 2019 SparklingLogic. All Rights Reserved.