Package: sauron-rule-engine
Sauron Rule engine - One engine to rule them allA 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 actionsHeavily inspired on FastAPI. We use type annotations in our engine so that we can export data to other systems or frontends to convey what conditions and actions are possible using that engineInstallpipinstallsauron-rule-engineConceptsSauron rule engine is based on custom functions that can be called by a rule.ConditionCondition to be satisfied in order for the actions to run, they can take some or no parameters at all
Multiple conditions can be chained in order to create more complex ones, currently all chained conditions must be satisfiedActionAn Action is the intented result. Usually they are there to mutate state or trigger/schedule other kinds of actions in your system. Actions can also be chained and will run in order.RuleA Rule is a dict or json string containing the conditions and actions and the arguments they should be run with. Usually those rules will be built by a frontend to match complex and adaptable business rules from your customerUse itA simple example of the usagefromsauron.rule_engineimportRuleEngineengine=RuleEngine()@engine.condition("First Condition")deffirst_condition(session,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(session):"""Takes no argument and always returns True"""returnTrue@engine.action("The Action")defprint_the_equation(session,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)Choices FieldsChoices fields are supported through python's built-in Enum type. Example:fromsauron.rule_engineimportRuleEnginefromenumimportEnumclassColor(str,Enum):red="R"green="G"blue="B"@engine.condition("is it red?")defis_red(session,color:Color)->bool:"""Checks if the color is red"""returncolor==color.redExport Conditions and ActionsYou can use the function export_metadata to export your data in a dict or as a json string (just passjson=True). Here is an Example and the output: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)Results in the following json to be served to your frontend:{"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?"}}}SessionsResults are stored in a result stack inside the session, so that jobs can share data with each other.More Features coming to townSupport pydantic typesSupport for complex types with hints to the frontend (like a range for an int type)ContributeWe need all the help we can get. Please readCONTRIBUTE.mdfor instructionsAuthor👤João Ricardo Lhullier LugãoTwitter:@joaovoceGithub:@jlugaoShow your supportGive a ⭐️ if this project helped you!This README was generated with ❤️ byreadme-md-generator