-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy path10-condition-sharing.js
139 lines (127 loc) · 3.19 KB
/
10-condition-sharing.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
/*
* This is an advanced example demonstrating rules that re-use a condition defined
* in the engine.
*
* Usage:
* node ./examples/10-condition-sharing.js
*
* For detailed output:
* DEBUG=json-rules-engine node ./examples/10-condition-sharing.js
*/
require('colors')
const { Engine } = require('json-rules-engine')
async function start () {
/**
* Setup a new engine
*/
const engine = new Engine()
/**
* Condition that will be used to determine if a user likes screwdrivers
*/
engine.setCondition('screwdriverAficionado', {
all: [
{
fact: 'drinksOrangeJuice',
operator: 'equal',
value: true
},
{
fact: 'enjoysVodka',
operator: 'equal',
value: true
}
]
})
/**
* Rule for identifying people who should be invited to a screwdriver social
* - Only invite people who enjoy screw drivers
* - Only invite people who are sociable
*/
const inviteRule = {
conditions: {
all: [
{
condition: 'screwdriverAficionado'
},
{
fact: 'isSociable',
operator: 'equal',
value: true
}
]
},
event: { type: 'invite-to-screwdriver-social' }
}
engine.addRule(inviteRule)
/**
* Rule for identifying people who should be invited to the other social
* - Only invite people who don't enjoy screw drivers
* - Only invite people who are sociable
*/
const otherInviteRule = {
conditions: {
all: [
{
not: {
condition: 'screwdriverAficionado'
}
},
{
fact: 'isSociable',
operator: 'equal',
value: true
}
]
},
event: { type: 'invite-to-other-social' }
}
engine.addRule(otherInviteRule)
/**
* Register listeners with the engine for rule success and failure
*/
engine
.on('success', async (event, almanac) => {
const accountId = await almanac.factValue('accountId')
console.log(
`${accountId}` +
'DID'.green +
` meet conditions for the ${event.type.underline} rule.`
)
})
.on('failure', async (event, almanac) => {
const accountId = await almanac.factValue('accountId')
console.log(
`${accountId} did ` +
'NOT'.red +
` meet conditions for the ${event.type.underline} rule.`
)
})
// define fact(s) known at runtime
let facts = {
accountId: 'washington',
drinksOrangeJuice: true,
enjoysVodka: true,
isSociable: true
}
// first run, using washington's facts
await engine.run(facts)
facts = {
accountId: 'jefferson',
drinksOrangeJuice: true,
enjoysVodka: false,
isSociable: true,
accountInfo: {}
}
// second run, using jefferson's facts; facts & evaluation are independent of the first run
await engine.run(facts)
}
start()
/*
* OUTPUT:
*
* washington DID meet conditions for the invite-to-screwdriver-social rule.
* washington did NOT meet conditions for the invite-to-other-social rule.
* jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.
* jefferson DID meet conditions for the invite-to-other-social rule.
*/