-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathcondition.js
More file actions
275 lines (260 loc) · 9.2 KB
/
condition.js
File metadata and controls
275 lines (260 loc) · 9.2 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use strict'
import debug from './debug'
export default class Condition {
constructor (properties) {
if (!properties) throw new Error('Condition: constructor options required')
const booleanOperator = Condition.booleanOperator(properties)
Object.assign(this, properties)
if (booleanOperator) {
const subConditions = properties[booleanOperator]
const subConditionsIsArray = Array.isArray(subConditions)
if (booleanOperator !== 'not' && !subConditionsIsArray) { throw new Error(`"${booleanOperator}" must be an array`) }
if (booleanOperator === 'not' && subConditionsIsArray) { throw new Error(`"${booleanOperator}" cannot be an array`) }
this.operator = booleanOperator
// boolean conditions always have a priority; default 1
this.priority = parseInt(properties.priority, 10) || 1
if (subConditionsIsArray) {
this[booleanOperator] = subConditions.map((c) => new Condition(c))
} else {
this[booleanOperator] = new Condition(subConditions)
}
} else if (!Object.prototype.hasOwnProperty.call(properties, 'condition')) {
const hasFact = Object.prototype.hasOwnProperty.call(properties, 'fact')
const hasOperator = Object.prototype.hasOwnProperty.call(properties, 'operator')
const hasValue = Object.prototype.hasOwnProperty.call(properties, 'value')
// Check if this is a nested condition (operator: 'some' with conditions property)
if (this.isNestedCondition()) {
// Nested array conditions require fact (to get the array)
if (!hasFact) {
throw new Error('Condition: constructor "fact" property required for nested conditions')
}
// Parse the nested conditions tree
this.conditions = new Condition(properties.conditions)
} else if (this.isScopedCondition()) {
// Scoped conditions (path only, no fact) - used inside nested conditions
// where the "fact" is implicitly the current array item
if (!hasOperator) {
throw new Error('Condition: constructor "operator" property required')
}
if (!hasValue) {
throw new Error('Condition: constructor "value" property required')
}
// path is already validated by isScopedCondition()
} else {
// Regular fact-based condition
if (!hasFact) {
throw new Error('Condition: constructor "fact" property required')
}
if (!hasOperator) {
throw new Error('Condition: constructor "operator" property required')
}
if (!hasValue) {
throw new Error('Condition: constructor "value" property required')
}
}
// a non-boolean condition does not have a priority by default. this allows
// priority to be dictated by the fact definition
if (Object.prototype.hasOwnProperty.call(properties, 'priority')) {
properties.priority = parseInt(properties.priority, 10)
}
}
}
/**
* Converts the condition into a json-friendly structure
* @param {Boolean} stringify - whether to return as a json string
* @returns {string,object} json string or json-friendly object
*/
toJSON (stringify = true) {
const props = {}
if (this.priority) {
props.priority = this.priority
}
if (this.name) {
props.name = this.name
}
const oper = Condition.booleanOperator(this)
if (oper) {
if (Array.isArray(this[oper])) {
props[oper] = this[oper].map((c) => c.toJSON(false))
} else {
props[oper] = this[oper].toJSON(false)
}
} else if (this.isConditionReference()) {
props.condition = this.condition
} else if (this.isNestedCondition()) {
props.operator = this.operator
props.fact = this.fact
props.conditions = this.conditions.toJSON(false)
if (this.factResult !== undefined) {
props.factResult = this.factResult
}
if (this.result !== undefined) {
props.result = this.result
}
if (this.params) {
props.params = this.params
}
if (this.path) {
props.path = this.path
}
} else if (this.isScopedCondition()) {
// Scoped condition (path only, no fact)
props.operator = this.operator
props.value = this.value
props.path = this.path
if (this.factResult !== undefined) {
props.factResult = this.factResult
}
if (this.valueResult !== undefined) {
props.valueResult = this.valueResult
}
if (this.result !== undefined) {
props.result = this.result
}
if (this.params) {
props.params = this.params
}
} else {
props.operator = this.operator
props.value = this.value
props.fact = this.fact
if (this.factResult !== undefined) {
props.factResult = this.factResult
}
if (this.valueResult !== undefined) {
props.valueResult = this.valueResult
}
if (this.result !== undefined) {
props.result = this.result
}
if (this.params) {
props.params = this.params
}
if (this.path) {
props.path = this.path
}
}
if (stringify) {
return JSON.stringify(props)
}
return props
}
/**
* Takes the fact result and compares it to the condition 'value', using the operator
* LHS OPER RHS
* <fact + params + path> <operator> <value>
*
* @param {Almanac} almanac
* @param {Map} operatorMap - map of available operators, keyed by operator name
* @returns {Boolean} - evaluation result
*/
evaluate (almanac, operatorMap) {
if (!almanac) return Promise.reject(new Error('almanac required'))
if (!operatorMap) return Promise.reject(new Error('operatorMap required'))
if (this.isBooleanOperator()) { return Promise.reject(new Error('Cannot evaluate() a boolean condition')) }
const op = operatorMap.get(this.operator)
if (!op) { return Promise.reject(new Error(`Unknown operator: ${this.operator}`)) }
// Handle scoped conditions (path only, no fact)
// These are used inside nested conditions where the almanac is a ScopedAlmanac
if (this.isScopedCondition()) {
return Promise.all([
almanac.getValue(this.value),
almanac.resolvePath(this.path)
]).then(([rightHandSideValue, leftHandSideValue]) => {
const result = op.evaluate(leftHandSideValue, rightHandSideValue)
debug(
'condition::evaluate (scoped)', {
path: this.path,
leftHandSideValue,
operator: this.operator,
rightHandSideValue,
result
}
)
return {
result,
leftHandSideValue,
rightHandSideValue,
operator: this.operator
}
})
}
// Regular fact-based condition
return Promise.all([
almanac.getValue(this.value),
almanac.factValue(this.fact, this.params, this.path)
]).then(([rightHandSideValue, leftHandSideValue]) => {
const result = op.evaluate(leftHandSideValue, rightHandSideValue)
debug(
'condition::evaluate', {
leftHandSideValue,
operator: this.operator,
rightHandSideValue,
result
}
)
return {
result,
leftHandSideValue,
rightHandSideValue,
operator: this.operator
}
})
}
/**
* Returns the boolean operator for the condition
* If the condition is not a boolean condition, the result will be 'undefined'
* @return {string 'all', 'any', or 'not'}
*/
static booleanOperator (condition) {
if (Object.prototype.hasOwnProperty.call(condition, 'any')) {
return 'any'
} else if (Object.prototype.hasOwnProperty.call(condition, 'all')) {
return 'all'
} else if (Object.prototype.hasOwnProperty.call(condition, 'not')) {
return 'not'
}
}
/**
* Returns the condition's boolean operator
* Instance version of Condition.isBooleanOperator
* @returns {string,undefined} - 'any', 'all', 'not' or undefined (if not a boolean condition)
*/
booleanOperator () {
return Condition.booleanOperator(this)
}
/**
* Whether the operator is boolean ('all', 'any', 'not')
* @returns {Boolean}
*/
isBooleanOperator () {
return Condition.booleanOperator(this) !== undefined
}
/**
* Whether the condition represents a reference to a condition
* @returns {Boolean}
*/
isConditionReference () {
return Object.prototype.hasOwnProperty.call(this, 'condition')
}
/**
* Whether the condition is a nested condition (operator: 'some' with 'conditions' property)
* @returns {Boolean}
*/
isNestedCondition () {
return this.operator === 'some' &&
Object.prototype.hasOwnProperty.call(this, 'conditions')
}
/**
* Whether this is a scoped condition (path only, no fact)
* Used inside nested conditions where the "fact" is the current array item
* The path is resolved directly against the scoped item
* @returns {Boolean}
*/
isScopedCondition () {
return !Object.prototype.hasOwnProperty.call(this, 'fact') &&
Object.prototype.hasOwnProperty.call(this, 'path') &&
Object.prototype.hasOwnProperty.call(this, 'operator') &&
!this.isBooleanOperator()
}
}