Skip to content

Commit ab44bd1

Browse files
author
Cache Hamm
committed
add rule results to documentation
1 parent 85146b3 commit ab44bd1

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

docs/engine.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,22 @@ engine.stop()
130130

131131
There are two generic event emissions that trigger automatically:
132132

133-
#### ```engine.on('success', cb)```
133+
#### ```engine.on('success', Function(Object event, Almanac almanac, RuleResult ruleResult))```
134134

135-
Fires when a rule passes. In this case the callback will receive the entire event object.
135+
Fires when a rule passes. The callback will receive the event object, the current [Almanac](./almanac.md), and the [Rule Result](./rules.md#rule-results).
136136

137137
```js
138138
engine.on('success', function(event, almanac, ruleResult) {
139+
console.log(event) // { type: 'my-event', params: { id: 1 }
139140
})
140141
```
141142

142-
#### ```engine.on('failure', cb)```
143+
#### ```engine.on('failure', Function(Object event, Almanac almanac, RuleResult ruleResult))```
143144

144-
Companion to 'success', except fires when a rule fails.
145+
Companion to 'success', except fires when a rule fails. The callback will receive the event object, the current [Almanac](./almanac.md), and the [Rule Result](./rules.md#rule-results).
145146

146147
```js
147148
engine.on('failure', function(event, almanac, ruleResult) {
149+
console.log(event) // { type: 'my-event', params: { id: 1 }
148150
})
149151
```

docs/rules.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
12
# Rules
23

34
Rules contain a set of _conditions_ and a single _event_. When the engine is run, each rule condition is evaluated. If the results are truthy, the rule's _event_ is triggered.
45

6+
[Methods](#Methods)
7+
8+
[Conditions](#Conditions)
9+
10+
[Events](#Events)
11+
12+
[Operators](#Operators)
13+
14+
[Results](#Results)
15+
516
## Methods
617

718
### constructor([Object options|String json])
@@ -223,6 +234,8 @@ Listen for `success` and `failure` events emitted when rule is evaluated.
223234

224235
#### ```rule.on('success', Function(Object event, Almanac almanac, RuleResult ruleResult))```
225236

237+
The callback will receive the event object, the current [Almanac](./almanac.md), and the [Rule Result](./rules.md#rule-results).
238+
226239
```js
227240
// whenever rule is evaluated and the conditions pass, 'success' will trigger
228241
rule.on('success', function(event, almanac, ruleResult) {
@@ -232,7 +245,7 @@ rule.on('success', function(event, almanac, ruleResult) {
232245

233246
#### ```rule.on('failure', Function(Object event, Almanac almanac, RuleResult ruleResult))```
234247

235-
Companion to `success`, except fires when the rule fails.
248+
Companion to `success`, except fires when the rule fails. The callback will receive the event object, the current [Almanac](./almanac.md), and the [Rule Result](./rules.md#rule-results).
236249

237250
```js
238251
engine.on('failure', function(event, almanac, ruleResult) {
@@ -273,3 +286,34 @@ The ```operator``` compares the value returned by the ```fact``` to what is stor
273286
```contains``` - _fact_ (an array) must include _value_
274287

275288
```doesNotContain``` - _fact_ (an array) must not include _value_
289+
290+
## Rule Results
291+
292+
After a rule is evaluated, a `rule result` object is provided to the `success` and `failure` events. This argument is similar to a regular rule, and contains additional metadata about how the rule was evaluated. Rule results can be used to extract the results of individual conditions, computed fact values, and boolean logic results.
293+
294+
Rule results are structured similar to rules, with two additional pieces of metadata sprinkled throughout: `result` and `factResult`
295+
```js
296+
{
297+
result: false, // denotes whether rule computed truthy or falsey
298+
conditions: {
299+
all: [
300+
{
301+
fact: 'my-fact',
302+
operator: 'equal',
303+
value: 'some-value',
304+
result: false, // denotes whether condition computed truthy or falsey
305+
factResult: 'other-value' // denotes what 'my-fact' was computed to be
306+
}
307+
]
308+
},
309+
event: {
310+
type: 'my-event',
311+
params: {
312+
customProperty: 'customValue'
313+
}
314+
},
315+
priority: 1,
316+
}
317+
```
318+
319+
A demonstration can be found in the [rule-results](../examples/09-rule-results.js) example.

0 commit comments

Comments
 (0)