RulesEngine 5.0.3
dotnet add package RulesEngine --version 5.0.3
NuGet\Install-Package RulesEngine -Version 5.0.3
<PackageReference Include="RulesEngine" Version="5.0.3" />
paket add RulesEngine --version 5.0.3
#r "nuget: RulesEngine, 5.0.3"
// Install RulesEngine as a Cake Addin #addin nuget:?package=RulesEngine&version=5.0.3 // Install RulesEngine as a Cake Tool #tool nuget:?package=RulesEngine&version=5.0.3
Rules Engine
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- FastExpressionCompiler (>= 4.0.1)
- FluentValidation (>= 11.8.1)
- Microsoft.CSharp (>= 4.7.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Linq (>= 4.3.0)
- System.Linq.Dynamic.Core (>= 1.3.7)
- System.Threading.Tasks.Extensions (>= 4.5.4)
-
net6.0
- FastExpressionCompiler (>= 4.0.1)
- FluentValidation (>= 11.8.1)
- Microsoft.CSharp (>= 4.7.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Linq (>= 4.3.0)
- System.Linq.Dynamic.Core (>= 1.3.7)
- System.Threading.Tasks.Extensions (>= 4.5.4)
NuGet packages (13)
Showing the top 5 NuGet packages that depend on RulesEngine:
Package | Downloads |
---|---|
EachShow.Host
主机启动程序 |
|
RulesEngineEditor
Editor for Microsoft RulesEngine - Blazor UI library intended for integration in Web or Desktop |
|
Acubec.Solutions.Core
this product is a lebrery to enhance the domain driven .net development |
|
LINGYUN.Abp.Rules.RulesEngine
Package Description |
|
Masa.Contrib.RulesEngine.MicrosoftRulesEngine
Package Description |
GitHub repositories (7)
Showing the top 5 popular GitHub repositories that depend on RulesEngine:
Repository | Stars |
---|---|
fanliang11/surging
Surging is a micro-service engine that provides a lightweight, high-performance, modular RPC request pipeline. support Event-based Asynchronous Pattern and reactive programming ,The service engine supports http, TCP, WS,Grpc, Thrift,Mqtt, UDP, and DNS protocols. It uses ZooKeeper and Consul as a registry, and integrates it. Hash, random, polling, Fair Polling as a load balancing algorithm, built-in service governance to ensure reliable RPC communication, the engine contains Diagnostic, link tracking for protocol and middleware calls, and integration SkyWalking Distributed APM
|
|
IoTSharp/IoTSharp
IoTSharp is an open-source IoT platform for data collection, processing, visualization, and device management.
|
|
colinin/abp-next-admin
这是基于vue-vben-admin 模板适用于abp Vnext的前端管理项目
|
|
masastack/MASA.Framework
.NET next-generation microservice development framework, which provides cloud native best practices based on Dapr.
|
|
alexreich/RulesEngineEditor
Editor for Microsoft RulesEngine - Blazor UI library intended for integration in Web or Desktop
|
Version | Downloads | Last updated |
---|---|---|
5.0.3 | 838,252 | 1/12/2024 |
5.0.2 | 539,270 | 8/16/2023 |
5.0.1 | 170,337 | 7/12/2023 |
5.0.0 | 18,185 | 7/8/2023 |
4.0.0 | 1,413,210 | 10/2/2022 |
4.0.0-preview.1 | 11,678 | 4/25/2022 |
3.5.0 | 876,246 | 12/11/2021 |
3.4.0 | 89,340 | 10/1/2021 |
3.3.0 | 111,481 | 7/20/2021 |
3.2.0 | 101,838 | 6/8/2021 |
3.1.1 | 28,343 | 8/20/2021 |
3.1.0 | 57,145 | 4/28/2021 |
3.1.0-preview.4 | 221 | 4/19/2021 |
3.1.0-preview.3 | 5,158 | 3/8/2021 |
3.1.0-preview.2 | 198 | 3/2/2021 |
3.1.0-preview.1 | 3,926 | 2/2/2021 |
3.0.2 | 110,936 | 1/21/2021 |
3.0.1 | 13,197 | 12/23/2020 |
3.0.0 | 2,406 | 12/16/2020 |
3.0.0-preview.3 | 246 | 12/11/2020 |
3.0.0-preview.2 | 2,404 | 11/16/2020 |
3.0.0-preview.1 | 399 | 11/2/2020 |
2.1.5 | 67,682 | 11/2/2020 |
2.1.4 | 9,969 | 10/14/2020 |
2.1.3 | 4,122 | 10/12/2020 |
2.1.2 | 1,960 | 10/2/2020 |
2.1.1 | 27,025 | 9/1/2020 |
2.1.0 | 24,739 | 7/27/2020 |
2.0.0 | 117,357 | 5/18/2020 |
1.0.2 | 20,824 | 1/16/2020 |
1.0.1 | 2,224 | 9/24/2019 |
1.0.0 | 3,325 | 8/20/2019 |