Skip to main content

business-rule-engine

CodeFactor Github version PyPI version Supported Python versions PyPI downloads GitHub

As a software system grows in complexity and usage, it can become burdensome if every change to the logic/behavior of the system also requires you to write and deploy new code. The goal of this business rules engine is to provide a simple interface allowing anyone to capture new rules and logic defining the behavior of a system, and a way to then process those rules on the backend.

You might, for example, find this is a useful way for analysts to define marketing logic around when certain customers or items are eligible for a discount or to automate emails after users enter a certain state or go through a particular sequence of events.

Usage

1. Define Your set of variables

Variables represent values in your system, usually the value of some particular object. You create rules by setting threshold conditions such that when a variable is computed that triggers the condition some action is taken.

params = {
    'products_in_stock': 10
}

2. Define custom functions

def order_more(items_to_order):
    print("you ordered {} new items".format(items_to_order))
    return items_to_order

3. Write the rules

rules = """
rule "order new items"
when
    products_in_stock < 20
then
    order_more(50)
end
"""

3. Create the parser and parse the rule

from business_rule_engine import RuleParser

parser = RuleParser()
parser.register_function(order_more)
parser.parsestr(rules)
parser.execute(params)

Supported functions

Business rule engine uses Excel like functions (thanks to formulas. So it is possible to use most of them in rules.

Multiple conditions and multiple actions

You can make multiple checks on the same params, and call multiple actions as needed:

rules = """
rule "order new items"
when
    AND(products_in_stock < 20,
    products_in_stock >= 5)
then
    order_more(50)
end

rule "order new items urgent"
when
    products_in_stock < 5,
then
    AND(order_more(10, true),
    order_more(50))
end
"""

Custom functions

You can also write your own functions to validate conditions and use other libraries functions as actions:

from business_rule_engine import RuleParser

def is_even(num):
   if (num % 2) == 0:
      return True
   return False

params = {
    'number': 10
}

rules = """
rule "check even number"
when
    is_even(number) = True
then
    print("is even")
end
"""

parser = RuleParser()
parser.register_function(is_even)
parser.register_function(print)
parser.parsestr(rules)
parser.execute(params)

Handle missing rule parameters

If some argruments are missing, the rule engine will raise a ValueError.

There are some use cases, when you have to work with incomplete data. In such cases, you can define default arguments.

You enable default rule arguments with the parameter set_defaule_arg. The default argument will have the Value None. To provide another value you can use default_arg.

params = {}

rules = """
rule "order new items"
when
    products_in_stock < 20
then
    order_more(50)
end
"""

parser = RuleParser()
parser.register_function(order_more)
parser.parsestr(rules)
parser.execute(params, set_default_arg=True, default_arg=0)

More control of the RulePraser

if you need more control, how the rule parser handles rules, you can iterate over the parser and execute each rule in your script.

This gives you more control on how to handle missing arguments, rules with errors and you have access to the return values of the conditions and the actions.

from business_rule_engine import RuleParser
from business_rule_engine.exceptions import MissingArgumentError

def order_more(items_to_order):
    return "you ordered {} new items".format(items_to_order)

rules = """
rule "order new items"
when
    products_in_stock < 20
then
    order_more(50)
end
"""

params = {
    'products_in_stock': 10
}

parser = RuleParser()
parser.register_function(order_more)
parser.parsestr(rules)
for rule in parser:
    try:
        rvalue_condition, rvalue_action = rule.execute(params)
        if rule.status:
            print(rvalue_action)
            break
    except MissingArgumentError:
        pass

Error Handling

Most of the errors are caused by missing parameters, you can handle the errors and interpret the results handling ValueError:

from business_rule_engine import RuleParser

# proposital typo
params = {
    'produtcs_in_stock': 30
}

rules = """
rule "order new items"
when
    products_in_stock < 20
then
    order_more(50)
end
"""

parser = RuleParser()
parser.register_function(order_more)
parser.parsestr(rules)
try:
    ret = parser.execute(params)
    if ret is False:
        print("No conditions matched")
except ValueError as e:
    print(e)

Debug

To debug the rules processing, use the logging lib.

You can insert in your Python script to log to stdout:

import logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

business-rule-engine-0.1.2.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

business_rule_engine-0.1.2-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file business-rule-engine-0.1.2.tar.gz.

File metadata

  • Download URL: business-rule-engine-0.1.2.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for business-rule-engine-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b10e120346a4b384785b45a9b49a1f4f8d25f8e4cb0faaae40a305b0ce69c5db
MD5 010c3d0009be2822d632ce5244801f1c
BLAKE2b-256 af9df65eaa00410bc88151ed5d32206325310f838dca9f5dc5a2f76b1f41f026

See more details on using hashes here.

File details

Details for the file business_rule_engine-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: business_rule_engine-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for business_rule_engine-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3544540bf86e23e5b75cc18394e81b699fe56ca84d97ea3444533199f6841c9f
MD5 d1da4c23a7b0b241f5a533c1fe282db6
BLAKE2b-256 2ec45dbc01c1e7752e7a69064cd73c94570a877ab22bdf42a45e9553b443e4d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page