* 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 defaul
(factId, params = {}, path = '')
| 135 | * @return {Promise} a promise which will resolve with the fact computation. |
| 136 | */ |
| 137 | factValue (factId, params = {}, path = '') { |
| 138 | let factValuePromise |
| 139 | const fact = this._getFact(factId) |
| 140 | if (fact === undefined) { |
| 141 | if (this.allowUndefinedFacts) { |
| 142 | return Promise.resolve(undefined) |
| 143 | } else { |
| 144 | return Promise.reject(new UndefinedFactError(`Undefined fact: ${factId}`)) |
| 145 | } |
| 146 | } |
| 147 | if (fact.isConstant()) { |
| 148 | factValuePromise = Promise.resolve(fact.calculate(params, this)) |
| 149 | } else { |
| 150 | const cacheKey = fact.getCacheKey(params) |
| 151 | const cacheVal = cacheKey && this.factResultsCache.get(cacheKey) |
| 152 | if (cacheVal) { |
| 153 | factValuePromise = Promise.resolve(cacheVal) |
| 154 | debug(`almanac::factValue cache hit for fact:${factId}`) |
| 155 | } else { |
| 156 | debug(`almanac::factValue cache miss for fact:${factId}; calculating`) |
| 157 | factValuePromise = this._setFactValue(fact, params, fact.calculate(params, this)) |
| 158 | } |
| 159 | } |
| 160 | if (path) { |
| 161 | debug(`condition::evaluate extracting object property ${path}`) |
| 162 | return factValuePromise |
| 163 | .then(factValue => { |
| 164 | if (isObjectLike(factValue)) { |
| 165 | const pathValue = this.pathResolver(factValue, path) |
| 166 | debug(`condition::evaluate extracting object property ${path}, received: ${JSON.stringify(pathValue)}`) |
| 167 | return pathValue |
| 168 | } else { |
| 169 | debug(`condition::evaluate could not compute object path(${path}) of non-object: ${factValue} <${typeof factValue}>; continuing with ${factValue}`) |
| 170 | return factValue |
| 171 | } |
| 172 | }) |
| 173 | } |
| 174 | |
| 175 | return factValuePromise |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Interprets value as either a primitive, or if a fact, retrieves the fact value |
no test coverage detected