- Notifications
You must be signed in to change notification settings - Fork579
A Json based Rules Engine with extensive Dynamic expression support
License
microsoft/RulesEngine
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Rules Engine is a library/NuGet package for abstracting business logic/rules/policies out of a system. It provides a simple way of giving you the ability to put your rules in a store outside the core logic of the system, thus ensuring that any change in rules don't affect the core system.
To install this library, download the latest version ofNuGet Package fromnuget.org and refer it into your project.
There are several ways to populate workflows for the Rules Engine as listed below.
You need to store the rules based on theschema definition given and they can be stored in any store as deemed appropriate like Azure Blob Storage, Cosmos DB, Azure App Configuration,Entity Framework, SQL Servers, file systems etc. For RuleExpressionTypeLambdaExpression
, the rule is written as alambda expressions.
An example rule:
[ {"WorkflowName":"Discount","Rules": [ {"RuleName":"GiveDiscount10","SuccessEvent":"10","ErrorMessage":"One or more adjust rules failed.","ErrorType":"Error","RuleExpressionType":"LambdaExpression","Expression":"input1.country ==\"india\" AND input1.loyaltyFactor <= 2 AND input1.totalPurchasesToDate >= 5000" }, {"RuleName":"GiveDiscount20","SuccessEvent":"20","ErrorMessage":"One or more adjust rules failed.","ErrorType":"Error","RuleExpressionType":"LambdaExpression","Expression":"input1.country ==\"india\" AND input1.loyaltyFactor >= 3 AND input1.totalPurchasesToDate >= 10000" } ] }]
You can inject the rules into the Rules Engine by initiating an instance by using the following code -
varrulesEngine=newRulesEngine(workflow);
Here,workflow is a list of deserialized objects based on the schema explained aboveOnce initialised, the Rules Engine needs to execute the rules for a given input. This can be done by calling the methodExecuteAllRulesAsync
:
List<RuleResultTree>response=awaitrulesEngine.ExecuteAllRulesAsync(workflowName,input);
Here,workflowName is the name of the workflow, which isDiscount in the above mentioned example. Andinput is the object which needs to be checked against the rules, which itself may consist of a list of class instances.
Theresponse will contain a list ofRuleResultTree which gives information if a particular rule passed or failed.
Note: A detailed example showcasing how to use Rules Engine is explained inGetting Started page ofRules Engine Wiki.
A demo app for the is available atthis location.
A simple example via code only is as follows:
List<Rule>rules=newList<Rule>();Rulerule=newRule();rule.RuleName="Test Rule";rule.SuccessEvent="Count is within tolerance.";rule.ErrorMessage="Over expected.";rule.Expression="count < 3";rule.RuleExpressionType=RuleExpressionType.LambdaExpression;rules.Add(rule);varworkflows=newList<Workflow>();WorkflowexampleWorkflow=newWorkflow();exampleWorkflow.WorkflowName="Example Workflow";exampleWorkflow.Rules=rules;workflows.Add(exampleWorkflow);varbre=newRulesEngine.RulesEngine(workflows.ToArray());
Consuming Entity Framework and populating the Rules Engine is shown in theEFDemo class with Workflow rules populating the array and passed to the Rules Engine, The Demo App includes an exampleRulesEngineDemoContext using SQLite and could be swapped out for another provider.
varwfr=db.Workflows.Include(i=>i.Rules).ThenInclude(i=>i.Rules).ToArray();varbre=newRulesEngine.RulesEngine(wfr,null);
Note: For each level of nested rules expected, a ThenInclude query appended will be needed as shown above.
The rules can be stored in any store and be fed to the system in a structure which adheres to theschema of WorkFlow model.
A wrapper needs to be created over the Rules Engine package, which will get the rules and input message(s) from any store that your system dictates and put it into the Engine. The wrapper then handles the output using appropriate means.
Note: To know in detail of the workings of Rules Engine, please visitHow it works section inRules Engine Wiki.
There is an editor library with it's ownNuGet Package written in Blazor, more information is in it's repohttps://github.com/alexreich/RulesEngineEditor.
https://alexreich.github.io/RulesEngineEditor
This can also be installed as a standalone PWA and used offline.
https://alexreich.github.io/RulesEngineEditor/demo
This project welcomes contributions and suggestions. Most contributions require you to agree to aContributor License Agreement (CLA) declaring that you have the right to, and actually do, grant usthe rights to use your contribution. For details, visithttps://cla.opensource.microsoft.com.
This project has adopted theMicrosoft Open Source Code of Conduct.For more information see theCode of Conduct FAQ orcontactopencode@microsoft.com with any additional questions or comments.
For more details please check outRules Engine Wiki.
About
A Json based Rules Engine with extensive Dynamic expression support