FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
json-rules-engine/src/engine.js at master · CacheControl/json-rules-engine · GitHub
CacheControl
/
json-rules-engine
Public
Notifications
You must be signed in to change notification settings
Fork
507
Star
3.1k
Code
Issues
61
Pull requests
7
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
json-rules-engine
/
src
/
engine.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
324 lines (298 loc) · 11.4 KB
Breadcrumbs
json-rules-engine
/
src
/
engine.js
Copy path
File metadata and controls
324 lines (298 loc) · 11.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
'use strict'
import
Fact
from
'./fact'
import
Rule
from
'./rule'
import
Almanac
from
'./almanac'
import
EventEmitter
from
'eventemitter2'
import
defaultOperators
from
'./engine-default-operators'
import
defaultDecorators
from
'./engine-default-operator-decorators'
import
debug
from
'./debug'
import
Condition
from
'./condition'
import
OperatorMap
from
'./operator-map'
export
const
READY
=
'READY'
export
const
RUNNING
=
'RUNNING'
export
const
FINISHED
=
'FINISHED'
class
Engine
extends
EventEmitter
{
/**
* Returns a new Engine instance
*
@param
{
Rule[]
} rules - array of rules to initialize with
*/
constructor
(
rules
=
[
]
,
options
=
{
}
)
{
super
(
)
this
.
rules
=
[
]
this
.
allowUndefinedFacts
=
options
.
allowUndefinedFacts
||
false
this
.
allowUndefinedConditions
=
options
.
allowUndefinedConditions
||
false
this
.
replaceFactsInEventParams
=
options
.
replaceFactsInEventParams
||
false
this
.
pathResolver
=
options
.
pathResolver
this
.
operators
=
new
OperatorMap
(
)
this
.
facts
=
new
Map
(
)
this
.
conditions
=
new
Map
(
)
this
.
status
=
READY
rules
.
map
(
r
=>
this
.
addRule
(
r
)
)
defaultOperators
.
map
(
o
=>
this
.
addOperator
(
o
)
)
defaultDecorators
.
map
(
d
=>
this
.
addOperatorDecorator
(
d
)
)
}
/**
* Add a rule definition to the engine
*
@param
{
object|Rule
} properties - rule definition. can be JSON representation, or instance of Rule
*
@param
{
integer
} properties.priority (>1) - higher runs sooner.
*
@param
{
Object
} properties.event - event to fire when rule evaluates as successful
*
@param
{
string
} properties.event.type - name of event to emit
*
@param
{
string
} properties.event.params - parameters to pass to the event listener
*
@param
{
Object
} properties.conditions - conditions to evaluate when processing this rule
*/
addRule
(
properties
)
{
if
(
!
properties
)
throw
new
Error
(
'Engine: addRule() requires options'
)
let
rule
if
(
properties
instanceof
Rule
)
{
rule
=
properties
}
else
{
if
(
!
Object
.
prototype
.
hasOwnProperty
.
call
(
properties
,
'event'
)
)
throw
new
Error
(
'Engine: addRule() argument requires "event" property'
)
if
(
!
Object
.
prototype
.
hasOwnProperty
.
call
(
properties
,
'conditions'
)
)
throw
new
Error
(
'Engine: addRule() argument requires "conditions" property'
)
rule
=
new
Rule
(
properties
)
}
rule
.
setEngine
(
this
)
this
.
rules
.
push
(
rule
)
this
.
prioritizedRules
=
null
return
this
}
/**
* update a rule in the engine
*
@param
{
object|Rule
} rule - rule definition. Must be a instance of Rule
*/
updateRule
(
rule
)
{
const
ruleIndex
=
this
.
rules
.
findIndex
(
ruleInEngine
=>
ruleInEngine
.
name
===
rule
.
name
)
if
(
ruleIndex
>
-
1
)
{
this
.
rules
.
splice
(
ruleIndex
,
1
)
this
.
addRule
(
rule
)
this
.
prioritizedRules
=
null
}
else
{
throw
new
Error
(
'Engine: updateRule() rule not found'
)
}
}
/**
* Remove a rule from the engine
*
@param
{
object|Rule|string
} rule - rule definition. Must be a instance of Rule
*/
removeRule
(
rule
)
{
let
ruleRemoved
=
false
if
(
!
(
rule
instanceof
Rule
)
)
{
const
filteredRules
=
this
.
rules
.
filter
(
ruleInEngine
=>
ruleInEngine
.
name
!==
rule
)
ruleRemoved
=
filteredRules
.
length
!==
this
.
rules
.
length
this
.
rules
=
filteredRules
}
else
{
const
index
=
this
.
rules
.
indexOf
(
rule
)
if
(
index
>
-
1
)
{
ruleRemoved
=
Boolean
(
this
.
rules
.
splice
(
index
,
1
)
.
length
)
}
}
if
(
ruleRemoved
)
{
this
.
prioritizedRules
=
null
}
return
ruleRemoved
}
/**
* sets a condition that can be referenced by the given name.
* If a condition with the given name has already been set this will replace it.
*
@param
{
string
} name - the name of the condition to be referenced by rules.
*
@param
{
object
} conditions - the conditions to use when the condition is referenced.
*/
setCondition
(
name
,
conditions
)
{
if
(
!
name
)
throw
new
Error
(
'Engine: setCondition() requires name'
)
if
(
!
conditions
)
throw
new
Error
(
'Engine: setCondition() requires conditions'
)
if
(
!
Object
.
prototype
.
hasOwnProperty
.
call
(
conditions
,
'all'
)
&&
!
Object
.
prototype
.
hasOwnProperty
.
call
(
conditions
,
'any'
)
&&
!
Object
.
prototype
.
hasOwnProperty
.
call
(
conditions
,
'not'
)
&&
!
Object
.
prototype
.
hasOwnProperty
.
call
(
conditions
,
'condition'
)
)
{
throw
new
Error
(
'"conditions" root must contain a single instance of "all", "any", "not", or "condition"'
)
}
this
.
conditions
.
set
(
name
,
new
Condition
(
conditions
)
)
return
this
}
/**
* Removes a condition that has previously been added to this engine
*
@param
{
string
} name - the name of the condition to remove.
*
@returns
true if the condition existed, otherwise false
*/
removeCondition
(
name
)
{
return
this
.
conditions
.
delete
(
name
)
}
/**
* Add a custom operator definition
*
@param
{
string
} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
*
@param
{
function(factValue, jsonValue)
} callback - the method to execute when the operator is encountered.
*/
addOperator
(
operatorOrName
,
cb
)
{
this
.
operators
.
addOperator
(
operatorOrName
,
cb
)
}
/**
* Remove a custom operator definition
*
@param
{
string
} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
*/
removeOperator
(
operatorOrName
)
{
return
this
.
operators
.
removeOperator
(
operatorOrName
)
}
/**
* Add a custom operator decorator
*
@param
{
string
} decoratorOrName - decorator identifier within the condition; i.e. instead of 'someFact', 'everyValue', etc
*
@param
{
function(factValue, jsonValue, next)
} callback - the method to execute when the decorator is encountered.
*/
addOperatorDecorator
(
decoratorOrName
,
cb
)
{
this
.
operators
.
addOperatorDecorator
(
decoratorOrName
,
cb
)
}
/**
* Remove a custom operator decorator
*
@param
{
string
} decoratorOrName - decorator identifier within the condition; i.e. instead of 'someFact', 'everyValue', etc
*/
removeOperatorDecorator
(
decoratorOrName
)
{
return
this
.
operators
.
removeOperatorDecorator
(
decoratorOrName
)
}
/**
* Add a fact definition to the engine. Facts are called by rules as they are evaluated.
*
@param
{
object|Fact
} id - fact identifier or instance of Fact
*
@param
{
function
} definitionFunc - function to be called when computing the fact value for a given rule
*
@param
{
Object
} options - options to initialize the fact with. used when "id" is not a Fact instance
*/
addFact
(
id
,
valueOrMethod
,
options
)
{
let
factId
=
id
let
fact
if
(
id
instanceof
Fact
)
{
factId
=
id
.
id
fact
=
id
}
else
{
fact
=
new
Fact
(
id
,
valueOrMethod
,
options
)
}
debug
(
'engine::addFact'
,
{
id
:
factId
}
)
this
.
facts
.
set
(
factId
,
fact
)
return
this
}
/**
* Remove a fact definition to the engine. Facts are called by rules as they are evaluated.
*
@param
{
object|Fact
} id - fact identifier or instance of Fact
*/
removeFact
(
factOrId
)
{
let
factId
if
(
!
(
factOrId
instanceof
Fact
)
)
{
factId
=
factOrId
}
else
{
factId
=
factOrId
.
id
}
return
this
.
facts
.
delete
(
factId
)
}
/**
* Iterates over the engine rules, organizing them by highest -> lowest priority
*
@return
{
Rule[][]
} two dimensional array of Rules.
* Each outer array element represents a single priority(integer). Inner array is
* all rules with that priority.
*/
prioritizeRules
(
)
{
if
(
!
this
.
prioritizedRules
)
{
const
ruleSets
=
this
.
rules
.
reduce
(
(
sets
,
rule
)
=>
{
const
priority
=
rule
.
priority
if
(
!
sets
[
priority
]
)
sets
[
priority
]
=
[
]
sets
[
priority
]
.
push
(
rule
)
return
sets
}
,
{
}
)
this
.
prioritizedRules
=
Object
.
keys
(
ruleSets
)
.
sort
(
(
a
,
b
)
=>
{
return
Number
(
a
)
>
Number
(
b
)
?
-
1
:
1
// order highest priority -> lowest
}
)
.
map
(
(
priority
)
=>
ruleSets
[
priority
]
)
}
return
this
.
prioritizedRules
}
/**
* Stops the rules engine from running the next priority set of Rules. All remaining rules will be resolved as undefined,
* and no further events emitted. Since rules of the same priority are evaluated in parallel(not series), other rules of
* the same priority may still emit events, even though the engine is in a "finished" state.
*
@return
{
Engine
}
*/
stop
(
)
{
this
.
status
=
FINISHED
return
this
}
/**
* Returns a fact by fact-id
*
@param
{
string
} factId - fact identifier
*
@return
{
Fact
} fact instance, or undefined if no such fact exists
*/
getFact
(
factId
)
{
return
this
.
facts
.
get
(
factId
)
}
/**
* Runs an array of rules
*
@param
{
Rule[]
} array of rules to be evaluated
*
@return
{
Promise
} resolves when all rules in the array have been evaluated
*/
evaluateRules
(
ruleArray
,
almanac
)
{
return
Promise
.
all
(
ruleArray
.
map
(
(
rule
)
=>
{
if
(
this
.
status
!==
RUNNING
)
{
debug
(
'engine::run, skipping remaining rules'
,
{
status
:
this
.
status
}
)
return
Promise
.
resolve
(
)
}
return
rule
.
evaluate
(
almanac
)
.
then
(
(
ruleResult
)
=>
{
debug
(
'engine::run'
,
{
ruleResult
:
ruleResult
.
result
}
)
almanac
.
addResult
(
ruleResult
)
if
(
ruleResult
.
result
)
{
almanac
.
addEvent
(
ruleResult
.
event
,
'success'
)
return
this
.
emitAsync
(
'success'
,
ruleResult
.
event
,
almanac
,
ruleResult
)
.
then
(
(
)
=>
this
.
emitAsync
(
ruleResult
.
event
.
type
,
ruleResult
.
event
.
params
,
almanac
,
ruleResult
)
)
}
else
{
almanac
.
addEvent
(
ruleResult
.
event
,
'failure'
)
return
this
.
emitAsync
(
'failure'
,
ruleResult
.
event
,
almanac
,
ruleResult
)
}
}
)
}
)
)
}
/**
* Runs the rules engine
*
@param
{
Object
} runtimeFacts - fact values known at runtime
*
@param
{
Object
} runOptions - run options
*
@return
{
Promise
} resolves when the engine has completed running
*/
run
(
runtimeFacts
=
{
}
,
runOptions
=
{
}
)
{
debug
(
'engine::run started'
)
this
.
status
=
RUNNING
const
almanac
=
runOptions
.
almanac
||
new
Almanac
(
{
allowUndefinedFacts
:
this
.
allowUndefinedFacts
,
pathResolver
:
this
.
pathResolver
}
)
this
.
facts
.
forEach
(
fact
=>
{
almanac
.
addFact
(
fact
)
}
)
for
(
const
factId
in
runtimeFacts
)
{
let
fact
if
(
runtimeFacts
[
factId
]
instanceof
Fact
)
{
fact
=
runtimeFacts
[
factId
]
}
else
{
fact
=
new
Fact
(
factId
,
runtimeFacts
[
factId
]
)
}
almanac
.
addFact
(
fact
)
debug
(
'engine::run initialized runtime fact'
,
{
id
:
fact
.
id
,
value
:
fact
.
value
,
type
:
typeof
fact
.
value
}
)
}
const
orderedSets
=
this
.
prioritizeRules
(
)
let
cursor
=
Promise
.
resolve
(
)
// for each rule set, evaluate in parallel,
// before proceeding to the next priority set.
return
new
Promise
(
(
resolve
,
reject
)
=>
{
orderedSets
.
map
(
(
set
)
=>
{
cursor
=
cursor
.
then
(
(
)
=>
{
return
this
.
evaluateRules
(
set
,
almanac
)
}
)
.
catch
(
reject
)
return
cursor
}
)
cursor
.
then
(
(
)
=>
{
this
.
status
=
FINISHED
debug
(
'engine::run completed'
)
const
ruleResults
=
almanac
.
getResults
(
)
const
{
results
,
failureResults
}
=
ruleResults
.
reduce
(
(
hash
,
ruleResult
)
=>
{
const
group
=
ruleResult
.
result
?
'results'
:
'failureResults'
hash
[
group
]
.
push
(
ruleResult
)
return
hash
}
,
{
results
:
[
]
,
failureResults
:
[
]
}
)
resolve
(
{
almanac
,
results
,
failureResults
,
events
:
almanac
.
getEvents
(
'success'
)
,
failureEvents
:
almanac
.
getEvents
(
'failure'
)
}
)
}
)
.
catch
(
reject
)
}
)
}
}
export
default
Engine
Back
|
FazBrowse Home
|
New Git URL