-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathalmanac.js
186 lines (170 loc) · 5.69 KB
/
almanac.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict'
import Fact from './fact'
import { UndefinedFactError } from './errors'
import debug from './debug'
import { JSONPath } from 'jsonpath-plus'
function defaultPathResolver (value, path) {
return JSONPath({ path, json: value, wrap: false })
}
/**
* Fact results lookup
* Triggers fact computations and saves the results
* A new almanac is used for every engine run()
*/
export default class Almanac {
constructor (options = {}) {
this.factMap = new Map()
this.factResultsCache = new Map() // { cacheKey: Promise<factValu> }
this.allowUndefinedFacts = Boolean(options.allowUndefinedFacts)
this.pathResolver = options.pathResolver || defaultPathResolver
this.events = { success: [], failure: [] }
this.ruleResults = []
}
/**
* Adds a success event
* @param {Object} event
*/
addEvent (event, outcome) {
if (!outcome) throw new Error('outcome required: "success" | "failure"]')
this.events[outcome].push(event)
}
/**
* retrieve successful events
*/
getEvents (outcome = '') {
if (outcome) return this.events[outcome]
return this.events.success.concat(this.events.failure)
}
/**
* Adds a rule result
* @param {Object} event
*/
addResult (ruleResult) {
this.ruleResults.push(ruleResult)
}
/**
* retrieve successful events
*/
getResults () {
return this.ruleResults
}
/**
* Retrieve fact by id, raising an exception if it DNE
* @param {String} factId
* @return {Fact}
*/
_getFact (factId) {
return this.factMap.get(factId)
}
/**
* Registers fact with the almanac
* @param {[type]} fact [description]
*/
_addConstantFact (fact) {
this.factMap.set(fact.id, fact)
this._setFactValue(fact, {}, fact.value)
}
/**
* Sets the computed value of a fact
* @param {Fact} fact
* @param {Object} params - values for differentiating this fact value from others, used for cache key
* @param {Mixed} value - computed value
*/
_setFactValue (fact, params, value) {
const cacheKey = fact.getCacheKey(params)
const factValue = Promise.resolve(value)
if (cacheKey) {
this.factResultsCache.set(cacheKey, factValue)
}
return factValue
}
/**
* Add a fact definition to the engine. Facts are called by rules as they are evaluated.
* @param {object|Fact} id - fact identifier or instance of Fact
* @param {function} definitionFunc - function to be called when computing the fact value for a given rule
* @param {Object} options - options to initialize the fact with. used when "id" is not a Fact instance
*/
addFact (id, valueOrMethod, options) {
let factId = id
let fact
if (id instanceof Fact) {
factId = id.id
fact = id
} else {
fact = new Fact(id, valueOrMethod, options)
}
debug('almanac::addFact', { id: factId })
this.factMap.set(factId, fact)
if (fact.isConstant()) {
this._setFactValue(fact, {}, fact.value)
}
return this
}
/**
* Adds a constant fact during runtime. Can be used mid-run() to add additional information
* @deprecated use addFact
* @param {String} fact - fact identifier
* @param {Mixed} value - constant value of the fact
*/
addRuntimeFact (factId, value) {
debug('almanac::addRuntimeFact', { id: factId })
const fact = new Fact(factId, value)
return this._addConstantFact(fact)
}
/**
* Returns the value of a fact, based on the given parameters. Utilizes the 'almanac' maintained
* by the engine, which cache's fact computations based on parameters provided
* @param {string} factId - fact identifier
* @param {Object} params - parameters to feed into the fact. By default, these will also be used to compute the cache key
* @param {String} path - object
* @return {Promise} a promise which will resolve with the fact computation.
*/
factValue (factId, params = {}, path = '') {
let factValuePromise
const fact = this._getFact(factId)
if (fact === undefined) {
if (this.allowUndefinedFacts) {
return Promise.resolve(undefined)
} else {
return Promise.reject(new UndefinedFactError(`Undefined fact: ${factId}`))
}
}
if (fact.isConstant()) {
factValuePromise = Promise.resolve(fact.calculate(params, this))
} else {
const cacheKey = fact.getCacheKey(params)
const cacheVal = cacheKey && this.factResultsCache.get(cacheKey)
if (cacheVal) {
factValuePromise = Promise.resolve(cacheVal)
debug('almanac::factValue cache hit for fact', { id: factId })
} else {
debug('almanac::factValue cache miss, calculating', { id: factId })
factValuePromise = this._setFactValue(fact, params, fact.calculate(params, this))
}
}
if (path) {
debug('condition::evaluate extracting object', { property: path })
return factValuePromise
.then(factValue => {
if (factValue != null && typeof factValue === 'object') {
const pathValue = this.pathResolver(factValue, path)
debug('condition::evaluate extracting object', { property: path, received: pathValue })
return pathValue
} else {
debug('condition::evaluate could not compute object path of non-object', { path, factValue, type: typeof factValue })
return factValue
}
})
}
return factValuePromise
}
/**
* Interprets value as either a primitive, or if a fact, retrieves the fact value
*/
getValue (value) {
if (value != null && typeof value === 'object' && Object.prototype.hasOwnProperty.call(value, 'fact')) { // value = { fact: 'xyz' }
return this.factValue(value.fact, value.params, value.path)
}
return Promise.resolve(value)
}
}