A Json based Rules Engine with extensive Dynamic expression support
 
 
Go to file
Abbas Cyclewala f3ac4316df
Abbasc52/null rule param fix (#119)
* Fixed scoped parameter throwing exception on compilation error
* Fixed null RuleParameter throwing exception
* Replaced thrown Exceptions with RuleException
2021-04-19 11:21:33 +05:30
.config Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
.github/workflows Update dotnetcore-build.yml (#100) 2021-02-02 11:09:20 +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 Abbasc52/nested fix (#96) 2021-02-02 10:29:21 +05:30
demo/DemoApp * fixed namespace issue (#82) 2020-12-23 11:04:10 +05:30
docs Abbasc52/docs checkin (#104) 2021-02-10 14:34:28 +05:30
schema Update workflowRules-schema.json (#21) 2020-05-20 13:55:29 +05:30
src/RulesEngine Abbasc52/null rule param fix (#119) 2021-04-19 11:21:33 +05:30
test/RulesEngine.UnitTest Abbasc52/null rule param fix (#119) 2021-04-19 11:21:33 +05:30
.editorconfig * fixed namespace issue (#82) 2020-12-23 11:04:10 +05:30
.gitignore Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
CHANGELOG.md Fixed scopedParams runtime error not logging as exception message (#108) 2021-03-08 15:40:04 +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 Abbasc52/nested fix (#96) 2021-02-02 10:29:21 +05:30
RulesEngine.sln * fixed namespace issue (#82) 2020-12-23 11:04:10 +05:30
global.json Merge develop to master (#68) 2020-11-02 09:25:43 +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 the system. This works in a very simple way by giving you an ability to put your rules in a store outside the core logic of the system thus ensuring that any change in rules doesn't affect the core system.

Installation

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

How to use it

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, SQL Servers, file systems etc. The expressions are supposed to be a lambda expressions.

An example rule could be -

[
  {
    "WorkflowName": "Discount",
    "Rules": [
      {
        "RuleName": "GiveDiscount10",
        "SuccessEvent": "10",
        "ErrorMessage": "One or more adjust rules failed.",
        "ErrorType": "Error",
        "RuleExpressionType": "LambdaExpression",
        "Expression": "input1.country == \"india\" AND input1.loyalityFactor <= 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.loyalityFactor >= 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(workflowRules, logger);

Here, workflowRules is a list of deserialized object based out of the schema explained above and logger is a custom logger instance made out of an ILogger instance.

Once done, the Rules Engine needs to execute the rules for a given input. It can be done by calling the method ExecuteAllRulesAsync as shown below -

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.

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.

How it works

The rules can be stored in any store and be fed to the system in a structure which follows a proper schema of WorkFlow model.

The 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. Also, the wrapper then needs to handle 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.

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.