import { mapObjIndexed } from 'ramda' import { evaluateControls } from './controls' import { evaluationError, warning } from './error' import { collectDefaults, evaluateNode } from './evaluation' import { convertNodeToUnit, simplifyNodeUnit } from './nodeUnits' import { parse } from './parse' import parseRules from './parseRules' import { EvaluatedNode, EvaluatedRule, ParsedRules, Rules } from './types' import { parseUnit } from './units' const emptyCache = () => ({ _meta: { contextRule: [] } }) type Cache = { _meta: { contextRule: Array inversionFail?: { given: string estimated: string } } } type Situation = Partial< Record > type EvaluatedSituation = Partial< Record> > export type EvaluationOptions = Partial<{ unit: string useDefaultValues: boolean }> export * from './components' export { formatValue } from './format' export { default as translateRules } from './translateRules' export * from './types' export { parseRules } export default class Engine { parsedRules: ParsedRules defaultValues: Situation situation: Situation = {} cache: Cache cacheWithoutDefault: Cache constructor(rules: string | Rules | ParsedRules) { this.cache = emptyCache() this.cacheWithoutDefault = emptyCache() this.parsedRules = typeof rules === 'string' || !(Object.values(rules)[0] as any)?.dottedName ? parseRules(rules) : (rules as ParsedRules) this.defaultValues = mapObjIndexed( (value, name) => typeof value === 'string' ? this.evaluateExpression(value, `[valeur par défaut] ${name}`, false) : value, collectDefaults(this.parsedRules) ) as EvaluatedSituation } private resetCache() { this.cache = emptyCache() this.cacheWithoutDefault = emptyCache() } private situationWithDefaultValues(useDefaultValues = true) { return { ...(useDefaultValues ? this.defaultValues : {}), ...this.situation } } private evaluateExpression( expression: string, context: string, useDefaultValues = true ): EvaluatedNode { const result = simplifyNodeUnit( evaluateNode( useDefaultValues ? this.cache : this.cacheWithoutDefault, this.situationWithDefaultValues(useDefaultValues), this.parsedRules, parse( this.parsedRules, { dottedName: context }, this.parsedRules )(expression) ) ) if (Object.keys(result.defaultValue?.missingVariable ?? {}).length) { throw evaluationError( context, "Impossible d'évaluer l'expression car celle ci fait appel à des variables manquantes" ) } return result } setSituation( situation: Partial> = {} ) { this.resetCache() this.situation = mapObjIndexed( (value, name) => typeof value === 'string' ? this.evaluateExpression(value, `[situation] ${name}`, true) : value, situation ) as EvaluatedSituation return this } evaluate(expression: Names, options?: EvaluationOptions): EvaluatedRule evaluate( expression: string, options?: EvaluationOptions ): EvaluatedNode | EvaluatedRule evaluate(expression: string, options?: EvaluationOptions) { let result = this.evaluateExpression( expression, `[evaluation] ${expression}`, options?.useDefaultValues ?? true ) if (result.category === 'reference' && result.explanation) { result = { nodeValue: result.nodeValue, ...('unit' in result && { unit: result.unit }), ...('temporalValue' in result && { temporalValue: result.temporalValue }), ...result.explanation } as EvaluatedRule } if (options?.unit) { try { return convertNodeToUnit( parseUnit(options.unit), result as EvaluatedNode ) } catch (e) { warning( `[evaluation] ${expression}`, "L'unité demandée est incompatible avec l'expression évaluée" ) } } return result } controls() { return evaluateControls( this.cache, this.situationWithDefaultValues(), this.parsedRules ) } inversionFail(): boolean { return !!this.cache._meta.inversionFail } getParsedRules(): ParsedRules { return this.parsedRules } }