forked from CacheControl/json-rules-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-nested-boolean-logic.js
76 lines (69 loc) · 1.59 KB
/
02-nested-boolean-logic.js
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
'use strict'
/*
* 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
*/
require('colors')
let Engine = require('../dist').Engine
/**
* Setup a new engine
*/
let 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
}]
}, {
all: [{
fact: 'gameDuration',
operator: 'equal',
value: 48
}, {
fact: 'personalFoulCount',
operator: 'greaterThanInclusive',
value: 6
}]
}]
},
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
*/
let facts = {
personalFoulCount: 6,
gameDuration: 40
}
// run the engine
engine
.run(facts)
.then(events => { // run() return events with truthy conditions
events.map(event => console.log(event.params.message.red))
})
.catch(console.log)
/*
* OUTPUT:
*
* Player has fouled out!
*/