A Json based Rules Engine with extensive Dynamic expression support
 
 
Go to file
Abbas Cyclewala b68861dfd5
Merge develop to master (#68)
* Users/abbasc52/add-actions (#56)

* Removed ruleinput

* Simplified compiled params code

* clean up and renaming

* Fixed caching and made test cases more specific

* updated settings

* updated nuget and fix warnings

* Added test case for invalid input name

* changed input from object to dictionary

* Added action related fields and added expression output action

* Added evaluateRuleAction and added basic tests for actions

* made action names and context case insensitive

* Added exception handling

* fixed exception message for parsing error

* improved constructor for action factory

* Added build trigger for pr to develop

* Added more testcases for actions

* Shared same instance of parser for RulesEngine and OutputExpression action

* fixed review comments

* Added null check for actions not mentioned in json

* pull fixes from master (#61)

* Renamed ExecuteRule to ExecuteAllRulesAsync and added action support (#63)

* Added support for actions
* Renamed ExecuteRule to ExecuteAllRulesAsync

* added github nuget publish
2020-11-02 09:25:43 +05:30
.config Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
.github/workflows Merge develop to master (#68) 2020-11-02 09:25:43 +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
demo/DemoApp Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
schema Update workflowRules-schema.json (#21) 2020-05-20 13:55:29 +05:30
src/RulesEngine Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
test/RulesEngine.UnitTest Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
.gitignore Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
CHANGELOG.md Merge develop to master (#68) 2020-11-02 09:25:43 +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 Merge develop to master (#68) 2020-11-02 09:25:43 +05:30
RulesEngine.sln Merge develop to master (#68) 2020-11-02 09:25:43 +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.