A snake like rule engine...
Following on the java and squeak rule engine, a short python version. So short, the "engine" is little more than a wrapper around an array with a simple check method:
class RuleSet:
def __init__(self):
self.rules = []
def add(self, assertion):
self.rules.append(assertion)
def check(self, value):
for rule in self.rules:
if rule.check(value) == False:
return False
return True
and a small assertion class:
class NameIsMarkAssertion:
def check(self, value):
return value == "mark"
and finally creating the set of rules and assertions:
nameIsMarkRule = RuleSet()
nameIsMarkRule.add(NameIsMarkAssertion())
rules = {"nameIsMark":nameIsMarkRule}
We create the RuleSet and add one assertion, then store the RuleSet in a map for later use:
if rules["nameIsMark"].check("mark"):
print "Woohoo"
None of these three implementations come anywhere near a "rules engine", but they do all serve a simple purpose of extracting small logic blocks for reuse and allow the rule to be extended without altering the calling code. That being said, depending on what the codes actually doing or checking, there would be little need of a rule engine (even a so called simple one) over simple inline checks or extracted methods.