用python实现的简单规则引擎

sauron-rule-engine的Python项目详细描述


Sauron Rule Engine-一个引擎将它们全部规则化

Coverage StatusGitHubTwitter: joaovoce

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
与JDBC(h2数据库)相比,使用hibernate的java长SQL查询   java高亮显示JComboBox中的文本,然后在用户输入内容时将其删除   java ifstream从字符串中读取字符时出错   java如何使用icu4j字符集实现和字符集解码?   javax:将json数组映射到对象列表   java无法执行目标组织。科德豪斯。mojo:execmavenplugin:1.2.1:exec(defaultcli)   java单击活动中的一个按钮将同时打开两个活动   java我想使用Jsoup从整个在线词典中获取定义,我必须做些什么来防止我的IP被阻塞?   如何防止Java图形中的闪烁?   将字符串转换为浮点的数组会创建意外的返回。JAVA   java JAVAFX:在BorderPane中的窗格之间切换   JavaSpringWebFlux网络客户端:延迟执行   java根据其他菜单中的选项显示子菜单?   ProcessBuilder的java设置工作目录不工作   java lucene:使StandardTokenizer适应Twitter数据