Skip to content

Instantly share code, notes, and snippets.

@Azaferany
Created June 1, 2022 13:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Azaferany/7a24c2e07442d7f061d05a991f52a91e to your computer and use it in GitHub Desktop.
Save Azaferany/7a24c2e07442d7f061d05a991f52a91e to your computer and use it in GitHub Desktop.
Microsoft RulesEngine Demo
using Newtonsoft.Json;
using RulesEngine.Models;
while (true)
{
string text = System.IO.File.ReadAllText(@"./workflow.json");
var workflowRules = JsonConvert.DeserializeObject<Workflow[]>(text);
var reSettings = new ReSettings()
{
CustomTypes = new[] { typeof(WageResult) }
};
var rulesEngine = new RulesEngine.RulesEngine(workflowRules, reSettings);
var user = new RuleParameter("User", new { Id = 64624, Wage = 0.2, MaxWage = 5000 , PanelType = 1});
var client = new RuleParameter("Client", new { Id = 4534534, Wage = 0.3, MaxWage = 2000});
var payment = new RuleParameter("Payment", new { Id = 4534534, Amount = 50000});
var results = await rulesEngine.ExecuteAllRulesAsync("Wage", user, client, payment);
var max= results.Select(x => x.ActionResult.Output as WageResult).Select(x => x?.Priority).Max();
var result = results.SingleOrDefault(x => (x.ActionResult.Output as WageResult)?.Priority == max);
Console.WriteLine(JsonConvert.SerializeObject(result.ActionResult.Output));
Console.ReadKey();
}
public class WageResult
{
public double Wage { get; set; }
public int Priority { get; set; }
}
[
{
"WorkflowName": "Wage",
"Rules": [
{
"RuleName": "10Per",
"Operator": "Or",
"Rules":[
{
"RuleName": "clienthave10per",
"Expression":"Client.Wage = 0.3" // more on https://dynamic-linq.net/expression-language
},
{
"RuleName": "userhave10per",
"Expression":"User.Wage = 0.1"
}
],
"Actions": {
"OnSuccess": {
"Name": "OutputExpression",
"Context": {
"Expression": "new WageResult (Payment.Amount * 0.1 as Wage, 5000 as Priority)" // more on https://dynamic-linq.net/expression-language
}
}
}
},
{
"RuleName": "20Per",
"Operator": "Or",
"Rules":[
{
"RuleName": "client have 20per ",
"Expression":"Client.Wage = 0.2"
},
{
"RuleName": "user have 20per",
"Expression":"User.Wage = 0.2"
}
],
"Actions": {
"OnSuccess": {
"Name": "OutputExpression",
"Context": {
"Expression": "new WageResult (iif(Payment.Amount * 0.2 > 5000 , 5000, Payment.Amount * 0.2) as Wage, 2222 as Priority)"
}
}
}
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment