Skip to content

Commit

Permalink
altered Readme with a nice Basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
asulwer committed Jun 24, 2024
1 parent e7acd34 commit c86f982
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 82 deletions.
6 changes: 3 additions & 3 deletions DemoApp/Demos/Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rule> {
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 })
Expand Down
121 changes: 42 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
<details>
<summary>1. Create the Workflow and add a Rule to it</summary>

```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<RuleResultTree> 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)._
</details>

<details>
<summary>2. Create instance of RulesEngine, parameter Workflows (optional ReSettings)</summary>

<summary>Basic</summary>

A simple example via code only is as follows:

```c#
List<Rule> rules = new List<Rule>();

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>();

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);
```
</details>

<details>
<summary>3. Create RuleParameters to pass as parameter to instance of RulesEngine</summary>

<summary>Entity Framework</summary>
```
var ruleParameters = new RuleParameter[] {
new RuleParameter("input1", new { count = 1 })
};
```
</details>

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.
<details>
<summary>4. Excute all Workflows and associated Rules (parameters RuleParameters and CancellationToken)</summary>

```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(() => {
});
}
```
</details>

[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
Expand Down

0 comments on commit c86f982

Please sign in to comment.