A Json based Rules Engine with extensive Dynamic expression support
 
 
Go to file
dependabot[bot] dc52989895
Bump github/codeql-action from 2 to 3 (#572)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 2 to 3.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/v2...v3)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-17 02:27:49 +05:30
.config Bump dotnet-reportgenerator-globaltool from 5.1.10 to 5.1.23 (#498) 2023-07-12 11:06:52 +05:30
.devcontainer * Updated dependecies (#394) 2022-10-01 10:23:01 +05:30
.github Bump github/codeql-action from 2 to 3 (#572) 2024-01-17 02:27:49 +05:30
.vscode Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
assets Initial Commit 2019-08-13 15:36:57 +05:30
benchmark/RulesEngineBenchmark Users/abbasc52/dotnet8 (#571) 2024-01-13 15:20:51 +05:30
demo Users/abbasc52/dotnet8 (#571) 2024-01-13 15:20:51 +05:30
deployment Added strongname signing for RulesEngine (#231) 2021-09-09 16:03:52 +05:30
docs Users/abbasc52/dotnet8 (#571) 2024-01-13 15:20:51 +05:30
schema Abbasc52/combined fixes (#341) 2022-04-12 19:24:16 +05:30
scripts * Updated dependecies (#394) 2022-10-01 10:23:01 +05:30
signing Added strongname signing for RulesEngine (#231) 2021-09-09 16:03:52 +05:30
src/RulesEngine Users/abbasc52/dotnet8 (#571) 2024-01-13 15:20:51 +05:30
test/RulesEngine.UnitTest Users/abbasc52/dotnet8 (#571) 2024-01-13 15:20:51 +05:30
.editorconfig * fixed namespace issue (#82) 2020-12-23 11:04:10 +05:30
.gitignore Added strongname signing for RulesEngine (#231) 2021-09-09 16:03:52 +05:30
CHANGELOG.md Abbasc52/optional fast compile (#570) 2024-01-12 18:32:53 +05:30
CODE_OF_CONDUCT.md Initial Commit 2019-08-13 15:36:57 +05:30
LICENSE Initial Commit 2019-08-13 15:36:57 +05:30
README.md Update README.md (#536) 2023-10-21 12:42:15 +05:30
RulesEngine.sln Abbasc52/combined fixes (#341) 2022-04-12 19:24:16 +05:30
SECURITY.md Microsoft mandatory file (#356) 2022-05-13 12:56:29 +05:30
global.json Users/abbasc52/dotnet8 (#571) 2024-01-13 15:20:51 +05:30

README.md

Rules Engine

build Coverage Status Nuget download

Overview

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.

Installation

To install this library, download the latest version of NuGet Package from nuget.org and refer it into your project.

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 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 RuleExpressionType LambdaExpression, the rule is written as a lambda 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 -

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:

List<RuleResultTree> response = await rulesEngine.ExecuteAllRulesAsync(workflowName, input);

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 which gives information if a particular rule passed or failed.

Note: A detailed example showcasing how to use Rules Engine is explained in Getting Started page of Rules Engine Wiki.

A demo app for the is available at this location.

Basic

A simple example via code only is as follows:

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

Entity Framework

Consuming Entity Framework and populating the Rules Engine is shown in the EFDemo class with Workflow rules populating the array and passed to the Rules Engine, The Demo App includes an example RulesEngineDemoContext using SQLite and could be swapped out for another provider.

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.

How it works

The rules can be stored in any store and be fed to the system in a structure which adheres to the schema 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 visit How it works section in Rules Engine Wiki.

3rd Party Tools

RulesEngine Editor

There is an editor library with it's own NuGet Package written in Blazor, more information is in it's repo https://github.com/alexreich/RulesEngineEditor.

Live Demo

https://alexreich.github.io/RulesEngineEditor

This can also be installed as a standalone PWA and used offline.

With Sample Data

https://alexreich.github.io/RulesEngineEditor/demo

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.


For more details please check out Rules Engine Wiki.