-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy path09-rule-results.mts
113 lines (107 loc) · 3.3 KB
/
09-rule-results.mts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* This is a basic example demonstrating how to leverage the metadata supplied by rule results
*
* Usage:
* node ./examples/09-rule-results.js
*
* For detailed output:
* DEBUG=json-rules-engine node ./examples/09-rule-results.js
*/
import "colors";
import {
Engine,
NestedCondition,
NestedConditionResult,
RuleResult,
} from "json-rules-engine";
async function start() {
/**
* Setup a new engine
*/
const engine = new Engine();
// rule for determining honor role student athletes (student has GPA >= 3.5 AND is an athlete)
engine.addRule({
conditions: {
all: [
{
fact: "athlete",
operator: "equal",
value: true,
},
{
fact: "GPA",
operator: "greaterThanInclusive",
value: 3.5,
},
],
},
event: {
// define the event to fire when the conditions evaluate truthy
type: "honor-roll",
params: {
message: "Student made the athletics honor-roll",
},
},
name: "Athlete GPA Rule",
});
function render(message: string, ruleResult: RuleResult) {
// if rule succeeded, render success message
if (ruleResult.result) {
return console.log(`${message}`.green);
}
// if rule failed, iterate over each failed condition to determine why the student didn't qualify for athletics honor roll
const detail = (
ruleResult.conditions as { all: NestedConditionResult[] }
).all
.filter(({ result }) => !result)
.map((condition) => {
switch (condition.operator) {
case "equal":
return `was not an ${condition.fact}`;
case "greaterThanInclusive":
return `${condition.fact} of ${condition.factResult} was too low`;
default:
return "";
}
})
.join(" and ");
console.log(`${message} ${detail}`.red);
}
/**
* On success, retrieve the student's username and print rule name for display purposes, and render
*/
engine.on("success", (event, almanac, ruleResult) => {
almanac.factValue<string>("username").then((username) => {
render(
`${username.bold} succeeded ${ruleResult.name}! ${event.params!.message}`,
ruleResult,
);
});
});
/**
* On failure, retrieve the student's username and print rule name for display purposes, and render
*/
engine.on("failure", (_event, almanac, ruleResult) => {
almanac.factValue<string>("username").then((username) => {
render(`${username.bold} failed ${ruleResult.name} - `, ruleResult);
});
});
// Run the engine for 5 different students
await Promise.all([
engine.run({ athlete: false, GPA: 3.9, username: "joe" }),
engine.run({ athlete: true, GPA: 3.5, username: "larry" }),
engine.run({ athlete: false, GPA: 3.1, username: "jane" }),
engine.run({ athlete: true, GPA: 4.0, username: "janet" }),
engine.run({ athlete: true, GPA: 1.1, username: "sarah" }),
]);
}
start();
/*
* OUTPUT:
*
* joe failed Athlete GPA Rule - was not an athlete
* larry succeeded Athlete GPA Rule! Student made the athletics honor-roll
* jane failed Athlete GPA Rule - was not an athlete and GPA of 3.1 was too low
* janet succeeded Athlete GPA Rule! Student made the athletics honor-roll
* sarah failed Athlete GPA Rule - GPA of 1.1 was too low
*/