You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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).
Copy file name to clipboardExpand all lines: docs/rules.md
+45-1Lines changed: 45 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,18 @@
1
+
1
2
# Rules
2
3
3
4
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.
4
5
6
+
[Methods](#Methods)
7
+
8
+
[Conditions](#Conditions)
9
+
10
+
[Events](#Events)
11
+
12
+
[Operators](#Operators)
13
+
14
+
[Results](#Results)
15
+
5
16
## Methods
6
17
7
18
### constructor([Object options|String json])
@@ -223,6 +234,8 @@ Listen for `success` and `failure` events emitted when rule is evaluated.
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).
@@ -273,3 +286,34 @@ The ```operator``` compares the value returned by the ```fact``` to what is stor
273
286
```contains``` - _fact_ (an array) must include _value_
274
287
275
288
```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