用python实现的简单规则引擎
sauron-rule-engine的Python项目详细描述
Sauron Rule Engine-一个引擎将它们全部规则化
A simple rule engine to be used in python, it is based on simple rules and actions that can be chained with each other. The idea is to run the rule processor on events and have it mutate data or trigger actions
深受FastAPI启发。我们在引擎中使用类型注释,以便可以将数据导出到其他系统或前端,以传达使用该引擎可能的条件和操作
安装
pip install sauron-rule-engine
概念
sauron规则引擎基于可由规则调用的自定义函数。
条件
要满足条件才能运行操作,它们可以接受一些参数或根本不接受任何参数 可以链接多个条件以创建更复杂的条件,当前必须满足所有链接的条件
动作
行动是预期的结果。通常它们是为了改变系统中的状态或触发/调度其他类型的操作。操作也可以被链接并按顺序运行。
规则
规则是一个dict或json字符串,其中包含条件和操作以及它们应该使用的参数。通常,这些规则将由前端构建,以匹配来自客户的复杂且可调整的业务规则
使用它
用法的一个简单示例
fromsauron_rule_engine.rule_engineimportRuleEngineengine=RuleEngine()@engine.condition("First Condition")deffirst_condition(lower_number:int=10,greater_number:int=20)->bool:""" Checks if first number is lower than the first - lower_number: Number expected to be low - higher_number: Number expected to be high """returnlower_number<greater_number@engine.condition()defsecond_condition():""" Takes no argument and always returns True """returnTrue@engine.action("The Action")defprint_the_equation(lower_number:int=10,greater_number:int=20)->None:""" Prints a statement Asserting that the first number is lower than the second number - lower_number: Number expected to be low - higher_number: Number expected to be high """print(f"{lower_number} < {greater_number}")rule={"conditions":[{"name":"first_condition","args":{"lower_number":3,"greater_number":10},}],"actions":[{"name":"print_the_equation","args":{"lower_number":3,"greater_number":10},}],}engine.run(rule)
选项字段
选项字段通过python的内置枚举类型得到支持。示例:
fromsauron_rule_engine.rule_engineimportRuleEnginefromenumimportEnumclassColor(str,Enum):red="R"green="G"blue="B"@engine.condition("is it red?")defis_red(color:Color)->bool:""" Checks if the color is red """returncolor==color.red
导出条件和操作
您可以使用函数export_metadata以dict或json字符串的形式导出数据(只需传递json=True
)。下面是一个示例和输出:
fromsauron_rule_engine.rule_engineimportRuleEnginefromenumimportEnumengine=RuleEngine()@engine.condition("First Condition")deffirst_condition(lower_number:int=10,greater_number:int=20)->bool:""" Checks if first number is lower than the first - lower_number: Number expected to be low - higher_number: Number expected to be high """returnlower_number<greater_number@engine.condition()defsecond_condition():""" Takes no argument and always returns True """returnTrue@engine.action("The Action")defprint_the_equation(lower_number:int=10,greater_number:int=20)->None:""" Prints a statement Asserting that the first number is lower than the second number - lower_number: Number expected to be low - higher_number: Number expected to be high """print(f"{lower_number} < {greater_number}")classColor(str,Enum):red="R"green="G"blue="B"@engine.condition("is it red?")defis_red(color:Color)->bool:""" Checks if the color is red """returncolor==color.redmetadata=engine.export_metadata(json=True)print(metadata)
结果将以下JSON服务到您的前端:
{"actions":{"print_the_equation":{"args":{"lower_number":{"default":10,"type":"int","choices":null},"greater_number":{"default":20,"type":"int","choices":null}},"doc":"Prints a statement Asserting that the first number is lower than the second number\n- lower_number: Number expected to be low\n- higher_number: Number expected to be high","name":"The Action"}},"conditions":{"first_condition":{"args":{"lower_number":{"default":10,"type":"int","choices":null},"greater_number":{"default":20,"type":"int","choices":null}},"doc":"Checks if first number is lower than the first\n- lower_number: Number expected to be low\n- higher_number: Number expected to be high","name":"First Condition"},"second_condition":{"args":{},"doc":"Takes no argument and always returns True","name":"second_condition"},"is_red":{"args":{"color":{"default":null,"type":"Color","choices":["red","green","blue"]}},"doc":"Checks if the color is red","name":"is it red?"}}}
更多功能将进城
- 支持pydantic类型
- 支持具有前端提示的复杂类型(如int类型的范围
作者
jo_o ricardo lhullier lug_o
表示支持
如果这个项目对你有帮助,就给我一个答案!
此自述文件由readme-md-generator