Skip to content

Commit 4b6d987

Browse files
author
Cache Hamm
committed
Document path resolver option; add table of contents to docs
1 parent 15f7033 commit 4b6d987

File tree

6 files changed

+90
-12
lines changed

6 files changed

+90
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* BREAKING CHANGES
33
* `path` support using `selectn` should use the `pathResolver` feature. Read more [here](). To continue using selectn, add the following to the engine constructor:
44
```js
5-
const pathResolver = (value, path) => {
6-
return selectn(path)(value)
5+
const pathResolver = (object, path) => {
6+
return selectn(path)(object)
77
}
88
const engine = new Engine(rules, { pathResolver })
99
```

docs/almanac.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Almanac
22

3+
* [Overview](#overview)
4+
* [Methods](#methods)
5+
* [almanac.factValue(Fact fact, Object params, String path) -> Promise](#almanacfactvaluefact-fact-object-params-string-path---promise)
6+
* [almanac.addRuntimeFact(String factId, Mixed value)](#almanacaddruntimefactstring-factid-mixed-value)
7+
* [Common Use Cases](#common-use-cases)
8+
* [Fact dependencies](#fact-dependencies)
9+
* [Retrieve fact values when handling events](#retrieve-fact-values-when-handling-events)
10+
* [Rule Chaining](#rule-chaining)
11+
312
## Overview
413

514
An almanac collects facts through an engine run cycle. As the engine computes fact values,

docs/engine.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
The Engine stores and executes rules, emits events, and maintains state.
44

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)
18+
519
## Methods
620

721
### constructor([Array rules], Object [options])
@@ -16,7 +30,8 @@ let engine = new Engine([Array rules])
1630

1731
// initialize with options
1832
let options = {
19-
allowUndefinedFacts: false
33+
allowUndefinedFacts: false,
34+
pathResolver: (object, path) => _.get(object, path)
2035
};
2136
let engine = new Engine([Array rules], options)
2237
```
@@ -27,6 +42,8 @@ let engine = new Engine([Array rules], options)
2742
an exception is thrown. Turning this option on will cause the engine to treat
2843
undefined facts as `undefined`. (default: false)
2944

45+
`pathResolver` - Allows a custom object path resolution library to be used. (default: `json-path` syntax). See [custom path resolver](./rules.md#condition-helpers-custom-path-resolver) docs.
46+
3047
### engine.addFact(String id, Function [definitionFunc], Object [options])
3148

3249
```js

docs/facts.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
Facts are methods or constants registered with the engine prior to runtime and referenced within rule conditions. Each fact method should be a pure function that may return a either computed value, or promise that resolves to a computed value.
44
As rule conditions are evaluated during runtime, they retrieve fact values dynamically and use the condition _operator_ to compare the fact result with the condition _value_.
55

6+
* [Methods](#methods)
7+
* [constructor(String id, Constant|Function(Object params, Almanac almanac), [Object options]) -> instance](#constructorstring-id-constantfunctionobject-params-almanac-almanac-object-options---instance)
8+
69
## Methods
710

811
### constructor(String id, Constant|Function(Object params, Almanac almanac), [Object options]) -> instance

docs/rules.md

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@
33

44
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.
55

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)
1530

1631
## Methods
1732

@@ -217,6 +232,34 @@ json-path support is provided by [jsonpath-plus](https://github.com/s3u/JSONPath
217232

218233
For an example, see [fact-dependency](../examples/04-fact-dependency.js)
219234

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+
220263
### Comparing facts
221264
222265
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.

docs/walkthrough.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Walkthrough
22

3+
* [Step 1: Create an Engine](#step-1-create-an-engine)
4+
* [Step 2: Add Rules](#step-2-add-rules)
5+
* [Step 3: Define Facts](#step-3-define-facts)
6+
* [Step 4: Handing Events](#step-4-handing-events)
7+
* [Step 5: Run the engine](#step-5-run-the-engine)
8+
39
## Step 1: Create an Engine
410

511
```js

0 commit comments

Comments
 (0)