Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rafaelfgx/rulesengineservice
RulesEngineService.
https://github.com/rafaelfgx/rulesengineservice
rule-engine ruleengine rules-engine rulesengine
Last synced: about 1 month ago
JSON representation
RulesEngineService.
- Host: GitHub
- URL: https://github.com/rafaelfgx/rulesengineservice
- Owner: rafaelfgx
- License: mit
- Created: 2023-03-06T11:57:55.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-12T21:12:42.000Z (about 1 month ago)
- Last Synced: 2024-11-12T22:21:34.420Z (about 1 month ago)
- Topics: rule-engine, ruleengine, rules-engine, rulesengine
- Language: C#
- Homepage:
- Size: 4.88 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# RulesEngineService
https://microsoft.github.io/RulesEngine
## Run
Command Line
#### Prerequisites
* [.NET SDK](https://dotnet.microsoft.com/download)
#### Steps
1. Open directory **source\RulesEngineService** in command line and execute **dotnet run**.
3. Open .Visual Studio Code
#### Prerequisites
* [.NET SDK](https://dotnet.microsoft.com/download)
* [Visual Studio Code](https://code.visualstudio.com)
* [C# Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp)#### Steps
1. Open **source** directory in Visual Studio Code.
2. Press **F5**.Visual Studio
#### Prerequisites
* [Visual Studio](https://visualstudio.microsoft.com)
#### Steps
1. Open **source\RulesEngineService.sln** in Visual Studio.
2. Set **RulesEngineService** as startup project.
3. Press **F5**.## Example
### Rules
```json
[
{
"WorkflowName": "DiscountPercentage",
"GlobalParams": [
{
"Name": "CompareValue",
"Expression": "1000"
}
],
"Rules": [
{
"RuleName": "CreditCard",
"Expression": "PaymentType == \"CreditCard\"",
"SuccessEvent": "5"
},
{
"RuleName": "DebitCard",
"Expression": "PaymentType == \"DebitCard\"",
"SuccessEvent": "10"
},
{
"RuleName": "CashLessThan",
"Expression": "PaymentType == \"Cash\" && Value < CompareValue",
"SuccessEvent": "15"
},
{
"RuleName": "CashGreaterThanOrEqual",
"Expression": "PaymentType == \"Cash\" && Value >= CompareValue",
"SuccessEvent": "20"
}
]
}
]
```### Execute
```cs
var json = System.IO.File.ReadAllText("Rules.json");var workflows = JsonSerializer.Deserialize(json);
var rulesEngine = new RulesEngine.RulesEngine(workflows);
var parameters = new RuleParameter[]
{
new RuleParameter("PaymentType", "Cash"),
new RuleParameter("Value", 1000)
};var result = await rulesEngine.ExecuteAllRulesAsync(workflows.First().WorkflowName, parameters);
```### Result
```json
[
{
"rule": {
"ruleName": "CreditCard",
"expression": "PaymentType == \"CreditCard\"",
"successEvent": "5"
},
"isSuccess": false,
"inputs": {
"PaymentType": "Cash",
"Value": 1000,
"CompareValue": 1000
}
},
{
"rule": {
"ruleName": "DebitCard",
"expression": "PaymentType == \"DebitCard\"",
"successEvent": "10"
},
"isSuccess": false,
"inputs": {
"PaymentType": "Cash",
"Value": 1000,
"CompareValue": 1000
}
},
{
"rule": {
"ruleName": "CashLessThan",
"expression": "PaymentType == \"Cash\" && Value < CompareValue",
"successEvent": "15"
},
"isSuccess": false,
"inputs": {
"PaymentType": "Cash",
"Value": 1000,
"CompareValue": 1000
}
},
{
"rule": {
"ruleName": "CashGreaterThanOrEqual",
"expression": "PaymentType == \"Cash\" && Value >= CompareValue",
"successEvent": "20"
},
"isSuccess": true,
"inputs": {
"PaymentType": "Cash",
"Value": 1000,
"CompareValue": 1000
}
}
]
```### Value
```cs
var value = result.SingleOrDefault(rule => rule.IsSuccess)?.Rule.SuccessEvent; // 20
```