-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathcondition.js
162 lines (151 loc) · 5.55 KB
/
condition.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
'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')) {
if (!Object.prototype.hasOwnProperty.call(properties, 'fact')) { throw new Error('Condition: constructor "fact" property required') }
if (!Object.prototype.hasOwnProperty.call(properties, 'operator')) { throw new Error('Condition: constructor "operator" property required') }
if (!Object.prototype.hasOwnProperty.call(properties, 'value')) { 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 {
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}`)) }
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')
}
}