From c86f9826aac0dc9d3d6620203b0b6d0ff1a9ee09 Mon Sep 17 00:00:00 2001 From: Aaron Sulwer Date: Mon, 24 Jun 2024 15:44:28 -0700 Subject: [PATCH] altered Readme with a nice Basic example --- DemoApp/Demos/Basic.cs | 6 +- README.md | 121 ++++++++++++++--------------------------- 2 files changed, 45 insertions(+), 82 deletions(-) diff --git a/DemoApp/Demos/Basic.cs b/DemoApp/Demos/Basic.cs index 9c5a0f88..40128740 100644 --- a/DemoApp/Demos/Basic.cs +++ b/DemoApp/Demos/Basic.cs @@ -20,18 +20,18 @@ public async Task Run(CancellationToken ct = default) var workflows = new Workflow[] { new Workflow { WorkflowName = "Test Workflow Rule 1", - Rules = new List { + Rules = [ new Rule { RuleName = "Test Rule", SuccessMessage = "Count is within tolerance", ErrorMessage = "Over expected", Expression = "count < 3" } - } + ] } }; - var bre = new RulesEngine.RulesEngine(workflows, null); + var bre = new RulesEngine.RulesEngine(workflows); var rp = new RuleParameter[] { new RuleParameter("input1", new { count = 1 }) diff --git a/README.md b/README.md index 8a887bec..d3d72b23 100644 --- a/README.md +++ b/README.md @@ -6,102 +6,65 @@ Rules Engine is a library (not yet NuGet package) for abstracting business logic ## How to use it -There are several ways to populate workflows for the Rules Engine as listed below. - -You need to store the rules based on the [schema definition](https://github.com/asulwer/RulesEngine/blob/main/schema/workflow-schema.json) given and they can be stored in any store as deemed appropriate. For RuleExpressionType `LambdaExpression`, the rule is written as a [lambda expressions](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions). - -An example rule: - -```json -[ - { - "WorkflowName": "Discount", - "Rules": [ - { - "RuleName": "GiveDiscount10", - "SuccessMessage": "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", - "SuccessMessage": "20", - "ErrorMessage": "One or more adjust rules failed.", - "ErrorType": "Error", - "RuleExpressionType": "LambdaExpression", - "Expression": "input1.country == \"india\" AND input1.loyaltyFactor >= 3 AND input1.totalPurchasesToDate >= 10000" - } - ] - } -] -``` +### [Basic Exmample](https://github.com/asulwer/RulesEngine/blob/v6.0.2/DemoApp/Demos/Basic.cs) -You can inject the rules into the Rules Engine by initiating an instance by using the following code - +
+1. Create the Workflow and add a Rule to it -```c# -var rulesEngine = new RulesEngine(workflow); ``` -Here, *workflow* is a list of deserialized objects based on the schema explained above -Once initialised, the Rules Engine needs to execute the rules for a given input. This can be done by calling the method `ExecuteAllRulesAsync`: - -```c# -List response = await rulesEngine.ExecuteAllRulesAsync(workflowName, input); +var workflows = new Workflow[] { + new Workflow { + WorkflowName = "Test Workflow Rule 1", + Rules = [ + new Rule { + RuleName = "Test Rule", + SuccessMessage = "Count is within tolerance", + ErrorMessage = "Over expected", + Expression = "count < 3" + } + ] + } +}; ``` - -Here, *workflowName* is the name of the workflow, which is *Discount* in the above mentioned example. And *input* is the object which needs to be checked against the rules, which itself may consist of a list of class instances. - -The *response* will contain a list of [*RuleResultTree*](https://github.com/asulwer/RulesEngine/blob/main/src/RulesEngine/Models/RuleResultTree.cs) which gives information if a particular rule passed or failed. - -_A demo app for the is available at [this location](https://github.com/asulwer/RulesEngine/tree/main/demo)._ +
+2. Create instance of RulesEngine, parameter Workflows (optional ReSettings) -Basic - -A simple example via code only is as follows: - -```c# -List rules = new List(); - -Rule rule = new Rule(); -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); - -var workflows = new List(); - -Workflow exampleWorkflow = new Workflow(); -exampleWorkflow.WorkflowName = "Example Workflow"; -exampleWorkflow.Rules = rules; - -workflows.Add(exampleWorkflow); - -var bre = new RulesEngine.RulesEngine(workflows.ToArray()); ``` - -[Additional Examples](https://github.com/asulwer/RulesEngine/tree/main/demo/DemoApp/Demos) - +var rulesEngine = new RulesEngine.RulesEngine(workflows); +```
+3. Create RuleParameters to pass as parameter to instance of RulesEngine -Entity Framework +``` +var ruleParameters = new RuleParameter[] { + new RuleParameter("input1", new { count = 1 }) +}; +``` +
-Consuming Entity Framework and populating the Rules Engine is shown in the [EFDemo class](https://github.com/asulwer/RulesEngine/blob/main/demo/DemoApp/Demos/EF.cs) with Workflow rules populating the array and passed to the Rules Engine, The Demo App includes an example [RulesEngineDemoContext](https://github.com/asulwer/RulesEngine/blob/main/demo/DemoApp/Demos/RulesEngineContext.cs) using SQLite and could be swapped out for another provider. +
+4. Excute all Workflows and associated Rules (parameters RuleParameters and CancellationToken) -```c# -var wfr = db.Workflows.Include(i => i.Rules).ThenInclude(i => i.Rules).ToArray(); -var bre = new RulesEngine.RulesEngine(wfr, null); ``` - -*Note: For each level of nested rules expected, a ThenInclude query appended will be needed as shown above.* +await foreach (var async_rrt in rulesEngine.ExecuteAllWorkflows(ruleParameters, ct)) +{ + async_rrt.OnSuccess((eventName) => { + + }); + + async_rrt.OnFail(() => { + + }); +} +```
+[Additional Examples](https://github.com/asulwer/RulesEngine/tree/main/DemoApp) + ## Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a