2020-09-08 15:32:10 +00:00
|
|
|
/* eslint-disable @typescript-eslint/ban-types */
|
2020-10-04 23:39:11 +00:00
|
|
|
import { map } from 'ramda'
|
2020-04-23 07:30:03 +00:00
|
|
|
import { evaluationError, warning } from './error'
|
2020-11-04 15:47:12 +00:00
|
|
|
import { evaluationFunctions } from './evaluation'
|
2020-05-08 10:04:00 +00:00
|
|
|
import { convertNodeToUnit, simplifyNodeUnit } from './nodeUnits'
|
|
|
|
import { parse } from './parse'
|
2020-03-26 15:03:19 +00:00
|
|
|
import parseRules from './parseRules'
|
2020-11-04 15:47:12 +00:00
|
|
|
import * as utils from './ruleUtils'
|
2020-05-08 10:04:00 +00:00
|
|
|
import { EvaluatedNode, EvaluatedRule, ParsedRules, Rules } from './types'
|
|
|
|
import { parseUnit } from './units'
|
2020-03-10 10:52:53 +00:00
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
const emptyCache = () => ({
|
|
|
|
_meta: { contextRule: [] }
|
|
|
|
})
|
2020-03-10 10:52:53 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
type Cache = {
|
|
|
|
_meta: {
|
|
|
|
contextRule: Array<string>
|
2020-11-04 15:47:12 +00:00
|
|
|
inversionFail?:
|
|
|
|
| {
|
|
|
|
given: string
|
|
|
|
estimated: string
|
|
|
|
}
|
|
|
|
| true
|
|
|
|
inRecalcul?: boolean
|
|
|
|
filter?: string
|
2020-03-26 15:03:19 +00:00
|
|
|
}
|
2020-03-10 10:52:53 +00:00
|
|
|
}
|
2020-05-08 10:04:00 +00:00
|
|
|
|
2020-10-04 23:39:11 +00:00
|
|
|
type ParsedSituation<Names extends string> = Partial<ParsedRules<Names>>
|
2020-03-10 10:52:53 +00:00
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
export type EvaluationOptions = Partial<{
|
|
|
|
unit: string
|
|
|
|
}>
|
|
|
|
|
2020-05-08 10:04:00 +00:00
|
|
|
export * from './components'
|
2020-11-04 15:47:12 +00:00
|
|
|
export { default as cyclesLib } from './cyclesLib/index'
|
2020-06-14 14:12:06 +00:00
|
|
|
export { formatValue, serializeValue } from './format'
|
2020-03-30 16:24:18 +00:00
|
|
|
export { default as translateRules } from './translateRules'
|
2020-05-08 10:04:00 +00:00
|
|
|
export * from './types'
|
2020-03-26 15:03:19 +00:00
|
|
|
export { parseRules }
|
2020-09-01 23:47:30 +00:00
|
|
|
export { utils }
|
2020-11-04 15:47:12 +00:00
|
|
|
export type evaluationFunction = (
|
|
|
|
this: Engine<string>,
|
|
|
|
node: EvaluatedNode
|
|
|
|
) => EvaluatedNode
|
2020-05-08 10:04:00 +00:00
|
|
|
|
2020-03-30 17:14:03 +00:00
|
|
|
export default class Engine<Names extends string> {
|
|
|
|
parsedRules: ParsedRules<Names>
|
2020-10-04 23:39:11 +00:00
|
|
|
parsedSituation: ParsedSituation<Names> = {}
|
2020-11-04 15:47:12 +00:00
|
|
|
cache: Cache
|
2020-05-06 20:08:49 +00:00
|
|
|
private warnings: Array<string> = []
|
2020-03-10 10:52:53 +00:00
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
constructor(rules: string | Rules<Names> | ParsedRules<Names>) {
|
|
|
|
this.cache = emptyCache()
|
2020-11-04 15:47:12 +00:00
|
|
|
this.resetCache()
|
2020-03-26 15:03:19 +00:00
|
|
|
this.parsedRules =
|
2020-03-30 17:14:03 +00:00
|
|
|
typeof rules === 'string' || !(Object.values(rules)[0] as any)?.dottedName
|
|
|
|
? parseRules(rules)
|
|
|
|
: (rules as ParsedRules<Names>)
|
2020-03-10 10:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private resetCache() {
|
2020-04-23 07:30:03 +00:00
|
|
|
this.cache = emptyCache()
|
2020-03-10 10:52:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
private evaluateExpression(
|
|
|
|
expression: string,
|
2020-05-06 20:08:49 +00:00
|
|
|
context: string
|
2020-09-27 09:57:28 +00:00
|
|
|
): EvaluatedRule<Names> {
|
2020-05-26 14:12:16 +00:00
|
|
|
// EN ATTENDANT d'AVOIR une meilleure gestion d'erreur, on va mocker
|
|
|
|
// console.warn
|
|
|
|
const originalWarn = console.warn
|
|
|
|
console.warn = (warning: string) => {
|
|
|
|
this.warnings.push(warning)
|
|
|
|
originalWarn(warning)
|
|
|
|
}
|
2020-04-23 07:30:03 +00:00
|
|
|
const result = simplifyNodeUnit(
|
2020-11-04 15:47:12 +00:00
|
|
|
this.evaluateNode(
|
2020-04-23 07:30:03 +00:00
|
|
|
parse(
|
|
|
|
this.parsedRules,
|
|
|
|
{ dottedName: context },
|
|
|
|
this.parsedRules
|
|
|
|
)(expression)
|
|
|
|
)
|
|
|
|
)
|
2020-05-26 14:12:16 +00:00
|
|
|
console.warn = originalWarn
|
2020-04-23 07:30:03 +00:00
|
|
|
|
|
|
|
if (Object.keys(result.defaultValue?.missingVariable ?? {}).length) {
|
2020-04-29 14:19:20 +00:00
|
|
|
throw evaluationError(
|
2020-04-23 07:30:03 +00:00
|
|
|
context,
|
|
|
|
"Impossible d'évaluer l'expression car celle ci fait appel à des variables manquantes"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return result
|
2020-04-02 16:54:41 +00:00
|
|
|
}
|
2020-03-10 10:52:53 +00:00
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
setSituation(
|
|
|
|
situation: Partial<Record<Names, string | number | object>> = {}
|
|
|
|
) {
|
|
|
|
this.resetCache()
|
2020-10-04 23:39:11 +00:00
|
|
|
this.parsedSituation = map(
|
|
|
|
value =>
|
|
|
|
typeof value === 'object'
|
|
|
|
? value
|
|
|
|
: parse(
|
|
|
|
this.parsedRules,
|
|
|
|
{ dottedName: '' },
|
|
|
|
this.parsedRules
|
|
|
|
)(value),
|
2020-04-23 07:30:03 +00:00
|
|
|
situation
|
2020-10-04 23:39:11 +00:00
|
|
|
)
|
2020-04-23 07:30:03 +00:00
|
|
|
return this
|
|
|
|
}
|
2020-04-21 13:49:48 +00:00
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
evaluate(expression: Names, options?: EvaluationOptions): EvaluatedRule<Names>
|
|
|
|
evaluate(
|
|
|
|
expression: string,
|
|
|
|
options?: EvaluationOptions
|
2020-05-08 10:04:00 +00:00
|
|
|
): EvaluatedNode<Names> | EvaluatedRule<Names>
|
|
|
|
evaluate(expression: string, options?: EvaluationOptions) {
|
2020-04-23 07:30:03 +00:00
|
|
|
let result = this.evaluateExpression(
|
|
|
|
expression,
|
2020-05-06 20:08:49 +00:00
|
|
|
`[evaluation] ${expression}`
|
2020-03-26 15:03:19 +00:00
|
|
|
)
|
2020-04-23 07:30:03 +00:00
|
|
|
if (result.category === 'reference' && result.explanation) {
|
2020-04-23 09:55:51 +00:00
|
|
|
result = {
|
2020-09-27 09:57:28 +00:00
|
|
|
...result.explanation,
|
2020-04-23 09:55:51 +00:00
|
|
|
nodeValue: result.nodeValue,
|
2020-05-06 20:08:49 +00:00
|
|
|
missingVariables: result.missingVariables,
|
2020-05-08 10:04:00 +00:00
|
|
|
...('unit' in result && { unit: result.unit }),
|
2020-04-23 09:55:51 +00:00
|
|
|
...('temporalValue' in result && {
|
|
|
|
temporalValue: result.temporalValue
|
|
|
|
}),
|
2020-09-27 09:57:28 +00:00
|
|
|
dottedName: result.dottedName
|
2020-05-08 10:04:00 +00:00
|
|
|
} as EvaluatedRule<Names>
|
2020-04-23 07:30:03 +00:00
|
|
|
}
|
|
|
|
if (options?.unit) {
|
|
|
|
try {
|
|
|
|
return convertNodeToUnit(
|
|
|
|
parseUnit(options.unit),
|
|
|
|
result as EvaluatedNode<Names, number>
|
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
warning(
|
|
|
|
`[evaluation] ${expression}`,
|
|
|
|
"L'unité demandée est incompatible avec l'expression évaluée"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
2020-03-10 10:52:53 +00:00
|
|
|
}
|
2020-05-06 20:08:49 +00:00
|
|
|
|
2020-05-26 14:12:16 +00:00
|
|
|
getWarnings() {
|
|
|
|
return this.warnings
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
inversionFail(): boolean {
|
|
|
|
return !!this.cache._meta.inversionFail
|
|
|
|
}
|
|
|
|
|
|
|
|
getParsedRules(): ParsedRules<Names> {
|
|
|
|
return this.parsedRules
|
|
|
|
}
|
2020-11-04 15:47:12 +00:00
|
|
|
|
|
|
|
evaluateNode(node) {
|
|
|
|
if (!node.nodeKind) {
|
|
|
|
throw Error('The provided node must have a "nodeKind" attribute')
|
|
|
|
} else if (!evaluationFunctions[node.nodeKind]) {
|
|
|
|
throw Error(`Unknown "nodeKind": ${node.nodeKind}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return evaluationFunctions[node.nodeKind].call(this, node)
|
|
|
|
}
|
2020-03-10 10:52:53 +00:00
|
|
|
}
|