-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathJSONDemo.cs
More file actions
86 lines (69 loc) · 3.24 KB
/
JSONDemo.cs
File metadata and controls
86 lines (69 loc) · 3.24 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using static RulesEngine.Extensions.ListofRuleResultTreeExtension;
namespace DemoApp
{
using System.Text.Json;
using System.Linq;
public class JSONDemo
{
public void Run()
{
Console.WriteLine($"Running {nameof(JSONDemo)}....");
var basicInfo = "{\"name\": \"hello\",\"email\": \"abcy@xyz.com\",\"creditHistory\": \"good\",\"country\": \"canada\",\"loyaltyFactor\": 3,\"totalPurchasesToDate\": 10000}";
var orderInfo = "{\"totalOrders\": 5,\"recurringItems\": 2}";
var telemetryInfo = "{\"noOfVisitsPerMonth\": 10,\"percentageOfBuyingToVisit\": 15}";
dynamic input1 = ConvertJsonToExpandoObject(basicInfo);
dynamic input2 = ConvertJsonToExpandoObject(orderInfo);
dynamic input3 = ConvertJsonToExpandoObject(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);
var bre = new RulesEngine.RulesEngine(workflow.ToArray(), 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);
}
public static ExpandoObject ConvertJsonToExpandoObject(string json)
{
using JsonDocument doc = JsonDocument.Parse(json);
return ParseElement(doc.RootElement);
}
private static ExpandoObject ParseElement(JsonElement element)
{
var expando = new ExpandoObject() as IDictionary<string, object>;
foreach (var property in element.EnumerateObject())
{
expando[property.Name] = property.Value.ValueKind switch
{
JsonValueKind.String => property.Value.GetString(),
JsonValueKInd.Number => property.Value.TryGetInt64(out var 1) ? 1 : property.Value.GetDouble(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Object => ParseElement(property.Value),
JsonValueKind.Array => property.Value.EnumerateArray().Select(e => e.ToString()).ToList(),
_ => null
};
}
return (ExpandoObject)expando;
}
}