Package: business-rule-engine
business-rule-engineAs 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.Usage1. Define Your set of variablesVariables 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 functionsdeforder_more(items_to_order):print("you ordered{}new items".format(items_to_order))returnitems_to_order3. Write the rulesrules="""rule "order new items"whenproducts_in_stock < 20thenorder_more(50)end"""3. Create the parser and parse the rulefrombusiness_rule_engineimportRuleParserparser=RuleParser()parser.register_function(order_more)parser.parsestr(rules)parser.execute(params)Supported functionsBusiness rule engine uses Excel like functions (thanks toformulas. So it is possible to use most of them in rules.Multiple conditions and multiple actionsYou can make multiple checks on the same params, and call multiple actions as needed:rules="""rule "order new items"whenAND(products_in_stock < 20,products_in_stock >= 5)thenorder_more(50)endrule "order new items urgent"whenproducts_in_stock < 5,thenAND(order_more(10, true),order_more(50))end"""Custom functionsYou can also write your own functions to validate conditions and use other libraries functions as actions:frombusiness_rule_engineimportRuleParserdefis_even(num):if(num%2)==0:returnTruereturnFalseparams={'number':10}rules="""rule "check even number"whenis_even(number) = Truethenprint("is even")end"""parser=RuleParser()parser.register_function(is_even)parser.register_function(print)parser.parsestr(rules)parser.execute(params)Handle missing rule parametersIf 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 parameterset_defaule_arg. The default argument will have the ValueNone. To provide another value you can usedefault_arg.params={}rules="""rule "order new items"whenproducts_in_stock < 20thenorder_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 RulePraserif 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.frombusiness_rule_engineimportRuleParserfrombusiness_rule_engine.exceptionsimportMissingArgumentErrordeforder_more(items_to_order):return"you ordered{}new items".format(items_to_order)rules="""rule "order new items"whenproducts_in_stock < 20thenorder_more(50)end"""params={'products_in_stock':10}parser=RuleParser()parser.register_function(order_more)parser.parsestr(rules)forruleinparser:try:rvalue_condition,rvalue_action=rule.execute(params)ifrule.status:print(rvalue_action)breakexceptMissingArgumentError:passError HandlingMost of the errors are caused by missing parameters, you can handle the errors and interpret the results handling ValueError:frombusiness_rule_engineimportRuleParser# proposital typoparams={'produtcs_in_stock':30}rules="""rule "order new items"whenproducts_in_stock < 20thenorder_more(50)end"""parser=RuleParser()parser.register_function(order_more)parser.parsestr(rules)try:ret=parser.execute(params)ifretisFalse:print("No conditions matched")exceptValueErrorase:print(e)DebugTo 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)