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.
You define all the available variables for a certain kind of object in your code, and then later dynamically set the conditions and thresholds for those.
A rule is just a JSON object that gets interpreted by the business-rules engine.
Note that the JSON is expected to be auto-generated by a UI, which makes it simple for anyone to set and tweak business rules without knowing anything about the code. The javascript library used for generating these on the web can be found here.
An example of the resulting python lists/dicts is:
from business_rules import run_all
rules = _some_function_to_receive_from_client()
for product in Products.objects.all():
run_all(rule_list=rules,
defined_variables=ProductVariables(product),
defined_actions=ProductActions(product),
stop_on_first_trigger=True
)
API
Variable Types and Decorators:
The type represents the type of the value that will be returned for the variable and is necessary since there are different available comparison operators for different types, and the front-end that's generating the rules needs to know which operators are available.
All decorators can optionally take a label:
label - A human-readable label to show on the frontend. By default we just split the variable name on underscores and capitalize the words.
The available types and decorators are:
numeric - an integer, float, or python Decimal.
@numeric_rule_variable operators:
equal_to
greater_than
less_than
greater_than_or_equal_to
less_than_or_equal_to
Note: to compare floating point equality we just check that the difference is less than some small epsilon
string - a python bytestring or unicode string.
@string_rule_variable operators:
equal_to
starts_with
ends_with
contains
matches_regex
non_empty
boolean - a True or False value.
@boolean_rule_variable operators:
is_true
is_false
select - a set of values, where the threshold will be a single item.
@select_rule_variable operators:
contains
does_not_contain
select_multiple - a set of values, where the threshold will be a set of items.
@select_multiple_rule_variable operators:
contains_all
is_contained_by
shares_at_least_one_element_with
shares_exactly_one_element_with
shares_no_elements_with
Returning data to your client
Contributing
Open up a pull request, making sure to add tests for any new functionality. To set up the dev environment (assuming you're using virtualenvwrapper):