Rule Engine Documentation

This project provides a library for creating general purpose “Rule” objects from a logical expression which can then be applied to arbitrary objects to evaluate whether or not they match.

The source code is available on the GitHub homepage.

Symbol Resolution

Symbols / variables in rules can be resolved from objects using an arbitrary function. Two are builtin, one for resolving symbols as keys on objects (such as dictionaries) and one for resolving symbols as attributes on objects.

Type Hinting

Symbol type information can be provided to the Rule through a Context instance and will be used for compatibility testing. With type information, the engine will raise an EvaluationError when an incompatible operation is detected such as a regex match (=~) using an integer on either side. This makes it possible to detect errors in a rule’s syntax prior to it being applied to an object. When symbol type information is specified, the value resolved from a symbol and object must either match the specified type or be NULL otherwise a SymbolTypeError will be raised when the symbol is resolved.

Alternatively, a function can be specified that simply returns UNDEFINED for valid symbols. In both cases, the engine will raise a SymbolResolutionError when an invalid symbol is specified in a rule.

Usage Example

import rule_engine
# match a literal first name and applying a regex to the email
rule = rule_engine.Rule(
    'first_name == "Luke" and email =~ ".*@rebels.org$"'
) # => <Rule text='first_name == "Luke" and email =~ ".*@rebels.org$"' >
rule.matches({
    'first_name': 'Luke', 'last_name': 'Skywalker', 'email': 'luke@rebels.org'
}) # => True
rule.matches({
   'first_name': 'Darth', 'last_name': 'Vader', 'email': 'dvader@empire.net'
}) # => False

See the examples folder for more.

Indices and tables