* 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
(ruleArray, almanac)
| 235 | * @return {Promise} resolves when all rules in the array have been evaluated |
| 236 | */ |
| 237 | evaluateRules (ruleArray, almanac) { |
| 238 | return Promise.all(ruleArray.map((rule) => { |
| 239 | if (this.status !== RUNNING) { |
| 240 | debug(`engine::run status:${this.status}; skipping remaining rules`) |
| 241 | return Promise.resolve() |
| 242 | } |
| 243 | return rule.evaluate(almanac).then((ruleResult) => { |
| 244 | debug(`engine::run ruleResult:${ruleResult.result}`) |
| 245 | almanac.addResult(ruleResult) |
| 246 | if (ruleResult.result) { |
| 247 | almanac.addEvent(ruleResult.event, 'success') |
| 248 | return this.emitAsync('success', ruleResult.event, almanac, ruleResult) |
| 249 | .then(() => this.emitAsync(ruleResult.event.type, ruleResult.event.params, almanac, ruleResult)) |
| 250 | } else { |
| 251 | almanac.addEvent(ruleResult.event, 'failure') |
| 252 | return this.emitAsync('failure', ruleResult.event, almanac, ruleResult) |
| 253 | } |
| 254 | }) |
| 255 | })) |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Runs the rules engine |