-
Notifications
You must be signed in to change notification settings - Fork 558
/
EFDemo.cs
71 lines (56 loc) · 2.5 KB
/
EFDemo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using DemoApp.EFDataExample;
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using static RulesEngine.Extensions.ListofRuleResultTreeExtension;
using Microsoft.EntityFrameworkCore;
namespace DemoApp
{
using System.Text.Json;
public class EFDemo
{
public void Run()
{
Console.WriteLine($"Running {nameof(EFDemo)}....");
var basicInfo = "{\"name\": \"hello\",\"email\": \"[email protected]\",\"creditHistory\": \"good\",\"country\": \"canada\",\"loyaltyFactor\": 3,\"totalPurchasesToDate\": 10000}";
var orderInfo = "{\"totalOrders\": 5,\"recurringItems\": 2}";
var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}";
dynamic input1 = JsonSerializer.Deserialize<ExpandoObject>(basicInfo);
dynamic input2 = JsonSerializer.Deserialize<ExpandoObject>(orderInfo);
dynamic input3 = JsonSerializer.Deserialize<ExpandoObject>(telemetryInfo);
var inputs = new dynamic[]
{
input1,
input2,
input3
};
var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "Discount.json", SearchOption.AllDirectories);
if (files == null || files.Length == 0)
throw new Exception("Rules not found.");
var fileData = File.ReadAllText(files[0]);
var workflow = JsonSerializer.Deserialize<List<Workflow>>(fileData);
RulesEngineDemoContext db = new RulesEngineDemoContext();
if (db.Database.EnsureCreated())
{
db.Workflows.AddRange(workflow);
db.SaveChanges();
}
var wfr = db.Workflows.Include(i => i.Rules).ThenInclude(i => i.Rules).ToArray();
var bre = new RulesEngine.RulesEngine(wfr, null);
string discountOffered = "No discount offered.";
List<RuleResultTree> resultList = bre.ExecuteAllRulesAsync("Discount", inputs).Result;
resultList.OnSuccess((eventName) => {
discountOffered = $"Discount offered is {eventName} % over MRP.";
});
resultList.OnFail(() => {
discountOffered = "The user is not eligible for any discount.";
});
Console.WriteLine(discountOffered);
}
}
}