MCPcopy
hub / github.com/CacheControl/json-rules-engine / run

Method run

src/engine.js:264–317  ·  view source on GitHub ↗

* 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

(runtimeFacts = {}, runOptions = {})

Source from the content-addressed store, hash-verified

262 * @return {Promise} resolves when the engine has completed running
263 */
264 run (runtimeFacts = {}, runOptions = {}) {
265 debug('engine::run started')
266 this.status = RUNNING
267
268 const almanac = runOptions.almanac || new Almanac({
269 allowUndefinedFacts: this.allowUndefinedFacts,
270 pathResolver: this.pathResolver
271 })
272
273 this.facts.forEach(fact => {
274 almanac.addFact(fact)
275 })
276 for (const factId in runtimeFacts) {
277 let fact
278 if (runtimeFacts[factId] instanceof Fact) {
279 fact = runtimeFacts[factId]
280 } else {
281 fact = new Fact(factId, runtimeFacts[factId])
282 }
283
284 almanac.addFact(fact)
285 debug(`engine::run initialized runtime fact:${fact.id} with ${fact.value}<${typeof fact.value}>`)
286 }
287 const orderedSets = this.prioritizeRules()
288 let cursor = Promise.resolve()
289 // for each rule set, evaluate in parallel,
290 // before proceeding to the next priority set.
291 return new Promise((resolve, reject) => {
292 orderedSets.map((set) => {
293 cursor = cursor.then(() => {
294 return this.evaluateRules(set, almanac)
295 }).catch(reject)
296 return cursor
297 })
298 cursor.then(() => {
299 this.status = FINISHED
300 debug('engine::run completed')
301 const ruleResults = almanac.getResults()
302 const { results, failureResults } = ruleResults.reduce((hash, ruleResult) => {
303 const group = ruleResult.result ? 'results' : 'failureResults'
304 hash[group].push(ruleResult)
305 return hash
306 }, { results: [], failureResults: [] })
307
308 resolve({
309 almanac,
310 results,
311 failureResults,
312 events: almanac.getEvents('success'),
313 failureEvents: almanac.getEvents('failure')
314 })
315 }).catch(reject)
316 })
317 }
318}
319
320export default Engine

Callers 15

startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
startFunction · 0.95
index.test-d.tsFile · 0.80

Calls 6

prioritizeRulesMethod · 0.95
evaluateRulesMethod · 0.95
debugFunction · 0.85
getResultsMethod · 0.80
getEventsMethod · 0.80
addFactMethod · 0.45

Tested by

no test coverage detected