|
3 | 3 |
|
4 | 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. |
5 | 5 |
|
6 | | -[Methods](#methods) |
7 | | - |
8 | | -[Conditions](#conditions) |
9 | | - |
10 | | -[Events](#events) |
11 | | - |
12 | | -[Operators](#operators) |
13 | | - |
14 | | -[Rule Results](#rule-results) |
| 6 | +* [Methods](#methods) |
| 7 | + * [constructor([Object options|String json])](#constructorobject-optionsstring-json) |
| 8 | + * [setConditions(Array conditions)](#setconditionsarray-conditions) |
| 9 | + * [getConditions() -> Object](#getconditions---object) |
| 10 | + * [setEvent(Object event)](#seteventobject-event) |
| 11 | + * [getEvent() -> Object](#getevent---object) |
| 12 | + * [setPriority(Integer priority = 1)](#setpriorityinteger-priority--1) |
| 13 | + * [getPriority() -> Integer](#getpriority---integer) |
| 14 | + * [toJSON(Boolean stringify = true)](#tojsonboolean-stringify--true) |
| 15 | +* [Conditions](#conditions) |
| 16 | + * [Basic conditions](#basic-conditions) |
| 17 | + * [Boolean expressions: all and any](#boolean-expressions-all-and-any) |
| 18 | + * [Condition helpers: params](#condition-helpers-params) |
| 19 | + * [Condition helpers: path](#condition-helpers-path) |
| 20 | + * [Condition helpers: custom path resolver](#condition-helpers-custom-path-resolver) |
| 21 | + * [Comparing facts](#comparing-facts) |
| 22 | +* [Events](#events) |
| 23 | + * [rule.on('success', Function(Object event, Almanac almanac, RuleResult ruleResult))](#ruleonsuccess-functionobject-event-almanac-almanac-ruleresult-ruleresult) |
| 24 | + * [rule.on('failure', Function(Object event, Almanac almanac, RuleResult ruleResult))](#ruleonfailure-functionobject-event-almanac-almanac-ruleresult-ruleresult) |
| 25 | +* [Operators](#operators) |
| 26 | + * [String and Numeric operators:](#string-and-numeric-operators) |
| 27 | + * [Numeric operators:](#numeric-operators) |
| 28 | + * [Array operators:](#array-operators) |
| 29 | +* [Rule Results](#rule-results) |
15 | 30 |
|
16 | 31 | ## Methods |
17 | 32 |
|
@@ -217,6 +232,34 @@ json-path support is provided by [jsonpath-plus](https://github.com/s3u/JSONPath |
217 | 232 |
|
218 | 233 | For an example, see [fact-dependency](../examples/04-fact-dependency.js) |
219 | 234 |
|
| 235 | +### Condition helpers: custom `path` resolver |
| 236 | + |
| 237 | +To use a custom path resolver instead of the `json-path` default, a `pathResolver` callback option may be passed to the engine. The callback will be invoked during execution when a `path` property is encountered. |
| 238 | + |
| 239 | +```js |
| 240 | +const { get } = require('lodash') // to use the lodash path resolver, for example |
| 241 | + |
| 242 | +function pathResolver (object, path) { |
| 243 | + // when the rule below is evaluated: |
| 244 | + // "object" will be the 'fact1' value |
| 245 | + // "path" will be '.price[0]' |
| 246 | + return get(object, path) |
| 247 | +} |
| 248 | +const engine = new Engine(rules, { pathResolver }) |
| 249 | +engine.addRule(new Rule({ |
| 250 | + conditions: { |
| 251 | + all: [ |
| 252 | + { |
| 253 | + fact: 'fact1', |
| 254 | + path: '.price[0]', // uses lodash path syntax |
| 255 | + operator: 'equal', |
| 256 | + value: 1 |
| 257 | + } |
| 258 | + ] |
| 259 | + }) |
| 260 | +) |
| 261 | +``` |
| 262 | +
|
220 | 263 | ### Comparing facts |
221 | 264 |
|
222 | 265 | Sometimes it is necessary to compare facts against other facts. This can be accomplished by nesting the second fact within the `value` property. This second fact has access to the same `params` and `path` helpers as the primary fact. |
|
0 commit comments