|
3 | 3 | The Engine stores and executes rules, emits events, and maintains state. |
4 | 4 |
|
5 | 5 | * [Methods](#methods) |
6 | | - * [constructor([Array rules], Object [options])](#constructorarray-rules-object-options) |
7 | | - * [Options](#options) |
8 | | - * [engine.addFact(String id, Function [definitionFunc], Object [options])](#engineaddfactstring-id-function-definitionfunc-object-options) |
9 | | - * [engine.removeFact(String id)](#engineremovefactstring-id) |
10 | | - * [engine.addRule(Rule instance|Object options)](#engineaddrulerule-instanceobject-options) |
11 | | - * [engine.removeRule(Rule instance)](#engineremoverulerule-instance) |
12 | | - * [engine.addOperator(String operatorName, Function evaluateFunc(factValue, jsonValue))](#engineaddoperatorstring-operatorname-function-evaluatefuncfactvalue-jsonvalue) |
13 | | - * [engine.removeOperator(String operatorName)](#engineremoveoperatorstring-operatorname) |
14 | | - * [engine.run([Object facts], [Object options]) -> Promise ({ events: Events, almanac: Almanac})](#enginerunobject-facts-object-options---promise--events-events-almanac-almanac) |
15 | | - * [engine.stop() -> Engine](#enginestop---engine) |
16 | | - * [engine.on('success', Function(Object event, Almanac almanac, RuleResult ruleResult))](#engineonsuccess-functionobject-event-almanac-almanac-ruleresult-ruleresult) |
17 | | - * [engine.on('failure', Function(Object event, Almanac almanac, RuleResult ruleResult))](#engineonfailure-functionobject-event-almanac-almanac-ruleresult-ruleresult) |
| 6 | + * [constructor([Array rules], Object [options])](#constructorarray-rules-object-options) |
| 7 | + * [Options](#options) |
| 8 | + * [engine.addFact(String id, Function [definitionFunc], Object [options])](#engineaddfactstring-id-function-definitionfunc-object-options) |
| 9 | + * [engine.removeFact(String id)](#engineremovefactstring-id) |
| 10 | + * [engine.addRule(Rule instance|Object options)](#engineaddrulerule-instanceobject-options) |
| 11 | + * [engine.removeRule(Rule instance)](#engineremoverulerule-instance) |
| 12 | + * [engine.addOperator(String operatorName, Function evaluateFunc(factValue, jsonValue))](#engineaddoperatorstring-operatorname-function-evaluatefuncfactvalue-jsonvalue) |
| 13 | + * [engine.removeOperator(String operatorName)](#engineremoveoperatorstring-operatorname) |
| 14 | + * [engine.run([Object facts], [Object options]) -> Promise ({ events: [], failureEvents: [], almanac: Almanac, results: [], failureResults: []})](#enginerunobject-facts-object-options---promise--events--failureevents--almanac-almanac-results--failureresults-) |
| 15 | + * [engine.stop() -> Engine](#enginestop---engine) |
| 16 | + * [engine.on('success', Function(Object event, Almanac almanac, RuleResult ruleResult))](#engineonsuccess-functionobject-event-almanac-almanac-ruleresult-ruleresult) |
| 17 | + * [engine.on('failure', Function(Object event, Almanac almanac, RuleResult ruleResult))](#engineonfailure-functionobject-event-almanac-almanac-ruleresult-ruleresult) |
18 | 18 |
|
19 | 19 | ## Methods |
20 | 20 |
|
@@ -104,9 +104,6 @@ engine.addRule(rule) |
104 | 104 | engine.removeRule(rule) |
105 | 105 | ``` |
106 | 106 |
|
107 | | - |
108 | | - |
109 | | - |
110 | 107 | ### engine.addOperator(String operatorName, Function evaluateFunc(factValue, jsonValue)) |
111 | 108 |
|
112 | 109 | Adds a custom operator to the engine. For situations that require going beyond the generic, built-in operators (`equal`, `greaterThan`, etc). |
@@ -156,24 +153,24 @@ engine.removeOperator('startsWithLetter'); |
156 | 153 |
|
157 | 154 |
|
158 | 155 |
|
159 | | -### engine.run([Object facts], [Object options]) -> Promise ({ events: Events, almanac: Almanac}) |
| 156 | +### engine.run([Object facts], [Object options]) -> Promise ({ events: [], failureEvents: [], almanac: Almanac, results: [], failureResults: []}) |
160 | 157 |
|
161 | 158 | Runs the rules engine. Returns a promise which resolves when all rules have been run. |
162 | 159 |
|
163 | 160 | ```js |
164 | 161 | // run the engine |
165 | | -engine.run() |
| 162 | +await engine.run() |
166 | 163 |
|
167 | 164 | // with constant facts |
168 | | -engine.run({ userId: 1 }) |
169 | | - |
170 | | -// returns rule events that were triggered |
171 | | -engine |
172 | | - .run({ userId: 1 }) |
173 | | - .then(function(results) { |
174 | | - console.log(results.events) |
175 | | - // almanac available via results.almanac to interact with as defined in Almanac docs |
176 | | - }) |
| 165 | +await engine.run({ userId: 1 }) |
| 166 | + |
| 167 | +const { |
| 168 | + results, // rule results for successful rules |
| 169 | + failureResults, // rule results for failed rules |
| 170 | + events, // array of successful rule events |
| 171 | + failureEvents, // array of failed rule events |
| 172 | + almanac // Almanac instance representing the run |
| 173 | +} = await engine.run({ userId: 1 }) |
177 | 174 | ``` |
178 | 175 | Link to the [Almanac documentation](./almanac.md) |
179 | 176 |
|
|
0 commit comments