-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy path02-nested-boolean-logic.mts
86 lines (80 loc) · 1.92 KB
/
02-nested-boolean-logic.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
/*
* This example demonstates nested boolean logic - e.g. (x OR y) AND (a OR b).
*
* Usage:
* node ./examples/02-nested-boolean-logic.js
*
* For detailed output:
* DEBUG=json-rules-engine node ./examples/02-nested-boolean-logic.js
*/
import "colors";
import { Engine } from "json-rules-engine";
async function start() {
/**
* Setup a new engine
*/
const engine = new Engine();
// define a rule for detecting the player has exceeded foul limits. Foul out any player who:
// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
engine.addRule({
conditions: {
any: [
{
all: [
{
fact: "gameDuration",
operator: "equal",
value: 40,
},
{
fact: "personalFoulCount",
operator: "greaterThanInclusive",
value: 5,
},
],
name: "short foul limit",
},
{
all: [
{
fact: "gameDuration",
operator: "equal",
value: 48,
},
{
not: {
fact: "personalFoulCount",
operator: "lessThan",
value: 6,
},
},
],
name: "long foul limit",
},
],
},
event: {
// define the event to fire when the conditions evaluate truthy
type: "fouledOut",
params: {
message: "Player has fouled out!",
},
},
});
/**
* define the facts
* note: facts may be loaded asynchronously at runtime; see the advanced example below
*/
const facts = {
personalFoulCount: 6,
gameDuration: 40,
};
const { events } = await engine.run(facts);
events.map((event) => console.log(event.params!.message.red));
}
export default start();
/*
* OUTPUT:
*
* Player has fouled out!
*/