-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy path05-optimizing-runtime-with-fact-priorities.mts
122 lines (112 loc) · 3.18 KB
/
05-optimizing-runtime-with-fact-priorities.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
114
115
116
117
118
119
120
121
122
/*
* This is an advanced example that demonstrates using fact priorities to optimize the rules engine.
*
* Usage:
* node ./examples/05-optimizing-runtime-with-fact-priorities.js
*
* For detailed output:
* DEBUG=json-rules-engine node ./examples/05-optimizing-runtime-with-fact-priorities.js
*/
import "colors";
import { Engine } from "json-rules-engine";
import accountClient from "./support/account-api-client.mjs";
async function start() {
/**
* Setup a new engine
*/
const engine = new Engine();
/**
* - Demonstrates setting high performance (cpu) facts higher than low performing (network call) facts.
*/
const microsoftRule = {
conditions: {
all: [
{
fact: "account-information",
operator: "equal",
value: true,
},
{
fact: "date",
operator: "lessThan",
value: 1467331200000, // unix ts for 2016-07-01; truthy when current date is prior to 2016-07-01
},
],
},
event: { type: "microsoft-employees" },
};
engine.addRule(microsoftRule);
/**
* Register listeners with the engine for rule success and failure
*/
const facts = { accountId: "washington" };
engine
.on("success", (event) => {
console.log(
facts.accountId +
" DID ".green +
"meet conditions for the " +
event.type.underline +
" rule.",
);
})
.on("failure", (event) => {
console.log(
facts.accountId +
" did " +
"NOT".red +
" meet conditions for the " +
event.type.underline +
" rule.",
);
});
/**
* Low and High Priorities.
* Facts that do not have a priority set default to 1
* @type {Integer} - Facts are run in priority from highest to lowest.
*/
const HIGH = 100;
const LOW = 1;
/**
* 'account-information' fact executes an api call - network calls are expensive, so
* we set this fact to be LOW priority; it will only be evaluated after all higher priority facts
* evaluate truthy
*/
engine.addFact(
"account-information",
async (_params, almanac) => {
// this fact will not be evaluated, because the "date" fact will fail first
console.log('Checking the "account-information" fact...'); // this message will not appear
const accountId = await almanac.factValue<string>("accountId");
return accountClient.getAccountInformation(accountId);
},
{ priority: LOW },
);
/**
* 'date' fact returns the current unix timestamp in ms.
* Because this is cheap to compute, we set it to "HIGH" priority
*/
engine.addFact(
"date",
() => {
console.log('Checking the "date" fact...');
return Date.now();
},
{ priority: HIGH },
);
// define fact(s) known at runtime
await engine.run();
}
export default start();
/*
* OUTPUT:
*
* Checking the "date" fact first..
* washington did NOT meet conditions for the microsoft-employees rule.
*/
/*
* NOTES:
*
* - Notice that the "account-information" fact was never evaluated, saving a network call and speeding up
* the engine by an order of magnitude(or more!). Swap the priorities of the facts to see both run.
*/