As we already mentioned: Arta is a simple python rules engine.
But what do we mean by rules engine?
rule : a set of different conditions that can be True or False (i.e., we say verified or not verified) triggering an action (i.e., any python callable object).
engine : some code used for combining and evaluating different rules on some input data.
The Superhero School
Imagine the following use case:
Your are managing a superhero school and you want to use some school rules in your python app.
The rules (intentionally simple) are:
Rules
You can define above rules for Arta in one simple YAML file :
---rules:default_rule_set:admission:ADMITTED:simple_condition:input.power=="strength" or input.power=="fly"action:set_admissionaction_parameters:value:trueNOT_ADMITTED:simple_condition:nullaction:set_admissionaction_parameters:value:falsecourse:FRENCH:simple_condition:input.language=="french" and input.age!=Noneaction:set_courseaction_parameters:course_id:frenchSENIOR:simple_condition:input.age==Noneaction:set_courseaction_parameters:course_id:seniorINTERNATIONAL:simple_condition:input.language!="french"action:set_courseaction_parameters:course_id:internationalfavorite_meal:EMAIL:simple_condition:input.favorite_meal!=Noneaction:send_emailaction_parameters:mail_to:cook@super-heroes.testmail_content:"Thanksforpreparingonceamonththefollowingdish:"meal:input.favorite_mealactions_source_modules:-my_folder.actions
Simple Conditions
This configuration uses what we called simple conditions, you can find out more here.
Actions
An action is triggered when the conditions are verified (i.e., True).
Actions are defined by the following keys in the previous YAML file:
fromtypingimportAnydefset_admission(value:bool,**kwargs:Any)->dict[str,bool]:"""Return a dictionary containing the admission result."""return{"is_admitted":value}defset_course(course_id:str,**kwargs:Any)->dict[str,str]:"""Return the course id as a dictionary."""return{"course_id":course_id}defsend_email(mail_to:str,mail_content:str,meal:str,**kwargs:Any)->str|None:"""Send an email."""result:str|None=NoneifmealisnotNone:# API call hereresult="sent"returnresult
**kwargs
**kwargs is mandatory in action functions.
Engine
The rules engine is responsible for evaluating the configured rules against some data (usually named "input data").
In our use case, the input data could be a list of applicants: