2019-02-20 10:57:35 +00:00
|
|
|
|
// This should be the new way to implement mecanisms
|
|
|
|
|
// In a specific file
|
|
|
|
|
// TODO import them automatically
|
|
|
|
|
// TODO convert the legacy functions to new files
|
2020-05-08 10:04:00 +00:00
|
|
|
|
import mecanismRound, { unchainRoundMecanism } from './mecanisms/arrondi'
|
|
|
|
|
import barème from './mecanisms/barème'
|
|
|
|
|
import durée from './mecanisms/durée'
|
|
|
|
|
import encadrement from './mecanisms/encadrement'
|
|
|
|
|
import grille from './mecanisms/grille'
|
|
|
|
|
import operation from './mecanisms/operation'
|
|
|
|
|
import régularisation from './mecanisms/régularisation'
|
|
|
|
|
import tauxProgressif from './mecanisms/tauxProgressif'
|
|
|
|
|
import variableTemporelle from './mecanisms/variableTemporelle'
|
|
|
|
|
import variations from './mecanisms/variations'
|
2019-09-10 16:24:05 +00:00
|
|
|
|
import { Grammar, Parser } from 'nearley'
|
2019-04-03 15:40:51 +00:00
|
|
|
|
import {
|
|
|
|
|
add,
|
2020-04-14 14:09:31 +00:00
|
|
|
|
difference,
|
2019-04-03 15:40:51 +00:00
|
|
|
|
divide,
|
|
|
|
|
equals,
|
2019-09-10 16:24:05 +00:00
|
|
|
|
fromPairs,
|
2019-04-03 15:40:51 +00:00
|
|
|
|
gt,
|
|
|
|
|
gte,
|
|
|
|
|
lt,
|
|
|
|
|
lte,
|
|
|
|
|
multiply,
|
2020-02-19 16:47:21 +00:00
|
|
|
|
subtract
|
2019-04-03 15:40:51 +00:00
|
|
|
|
} from 'ramda'
|
|
|
|
|
import React from 'react'
|
2020-02-16 18:56:07 +00:00
|
|
|
|
import { EngineError, syntaxError } from './error'
|
2020-04-23 07:30:03 +00:00
|
|
|
|
import { formatValue } from './format'
|
2019-09-10 16:24:05 +00:00
|
|
|
|
import grammar from './grammar.ne'
|
2019-04-03 15:40:51 +00:00
|
|
|
|
import {
|
|
|
|
|
mecanismAllOf,
|
|
|
|
|
mecanismInversion,
|
|
|
|
|
mecanismMax,
|
|
|
|
|
mecanismMin,
|
|
|
|
|
mecanismOneOf,
|
2019-09-10 16:24:05 +00:00
|
|
|
|
mecanismOnePossibility,
|
2019-04-03 15:40:51 +00:00
|
|
|
|
mecanismProduct,
|
2020-01-02 14:39:31 +00:00
|
|
|
|
mecanismRecalcul,
|
2019-04-03 15:40:51 +00:00
|
|
|
|
mecanismReduction,
|
|
|
|
|
mecanismSum,
|
2019-09-10 16:24:05 +00:00
|
|
|
|
mecanismSynchronisation
|
2019-04-03 15:40:51 +00:00
|
|
|
|
} from './mecanisms'
|
2019-07-11 16:25:08 +00:00
|
|
|
|
import { parseReferenceTransforms } from './parseReference'
|
2020-04-20 09:46:13 +00:00
|
|
|
|
import { EvaluatedRule } from './types'
|
2019-06-13 16:01:49 +00:00
|
|
|
|
|
2020-02-19 16:47:21 +00:00
|
|
|
|
export const parse = (rules, rule, parsedRules) => rawNode => {
|
|
|
|
|
if (rawNode == null) {
|
|
|
|
|
syntaxError(
|
|
|
|
|
rule.dottedName,
|
|
|
|
|
`
|
|
|
|
|
Une des valeurs de la formule est vide.
|
|
|
|
|
Vérifiez que tous les champs à droite des deux points sont remplis`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if (typeof rawNode === 'boolean') {
|
|
|
|
|
syntaxError(
|
|
|
|
|
rule.dottedName,
|
|
|
|
|
`
|
|
|
|
|
Les valeure booléenes true / false ne sont acceptée.
|
|
|
|
|
Utilisez leur contrepartie française : 'oui' / 'non'`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
const node =
|
|
|
|
|
typeof rawNode === 'object' ? rawNode : parseExpression(rule, '' + rawNode)
|
2019-06-13 16:01:49 +00:00
|
|
|
|
|
2020-02-19 16:47:21 +00:00
|
|
|
|
const parsedNode = parseMecanism(rules, rule, parsedRules)(node)
|
|
|
|
|
parsedNode.evaluate = parsedNode.evaluate ?? ((_, __, ___, node) => node)
|
|
|
|
|
return parsedNode
|
2019-06-13 16:01:49 +00:00
|
|
|
|
}
|
2019-02-20 10:57:35 +00:00
|
|
|
|
|
2019-09-10 16:24:05 +00:00
|
|
|
|
const compiledGrammar = Grammar.fromCompiled(grammar)
|
2018-06-29 09:13:05 +00:00
|
|
|
|
|
2020-04-23 07:30:03 +00:00
|
|
|
|
const parseExpression = (rule, rawNode) => {
|
2019-05-14 17:40:47 +00:00
|
|
|
|
/* Strings correspond to infix expressions.
|
|
|
|
|
* Indeed, a subset of expressions like simple arithmetic operations `3 + (quantity * 2)` or like `salary [month]` are more explicit that their prefixed counterparts.
|
|
|
|
|
* This function makes them prefixed operations. */
|
2019-11-26 18:14:57 +00:00
|
|
|
|
try {
|
2020-04-30 15:13:45 +00:00
|
|
|
|
const [parseResult] = new Parser(compiledGrammar).feed(rawNode).results
|
2020-02-19 16:47:21 +00:00
|
|
|
|
return parseResult
|
2019-11-26 18:14:57 +00:00
|
|
|
|
} catch (e) {
|
2019-11-28 11:03:23 +00:00
|
|
|
|
syntaxError(
|
|
|
|
|
rule.dottedName,
|
2020-02-19 16:47:21 +00:00
|
|
|
|
`\`${rawNode}\` n'est pas une expression valide`,
|
2019-11-28 11:03:23 +00:00
|
|
|
|
e
|
|
|
|
|
)
|
2019-11-26 18:14:57 +00:00
|
|
|
|
}
|
2018-06-29 09:13:05 +00:00
|
|
|
|
}
|
2020-02-19 16:47:21 +00:00
|
|
|
|
const parseMecanism = (rules, rule, parsedRules) => rawNode => {
|
2020-02-16 18:56:07 +00:00
|
|
|
|
if (Array.isArray(rawNode)) {
|
|
|
|
|
syntaxError(
|
|
|
|
|
rule.dottedName,
|
|
|
|
|
`
|
|
|
|
|
Il manque le nom du mécanisme pour le tableau : [${rawNode
|
|
|
|
|
.map(x => `'${x}'`)
|
|
|
|
|
.join(', ')}]
|
|
|
|
|
Les mécanisme possibles sont : 'somme', 'le maximum de', 'le minimum de', 'toutes ces conditions', 'une de ces conditions'.
|
|
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-04-14 14:09:31 +00:00
|
|
|
|
const keys = Object.keys(rawNode)
|
|
|
|
|
const unchainableMecanisms = difference(keys, chainableMecanisms)
|
|
|
|
|
if (keys.length > 1) {
|
|
|
|
|
if (unchainableMecanisms.length > 1) {
|
|
|
|
|
syntaxError(
|
|
|
|
|
rule.dottedName,
|
|
|
|
|
`
|
|
|
|
|
Les mécanismes suivants se situent au même niveau : ${unchainableMecanisms
|
|
|
|
|
.map(x => `'${x}'`)
|
|
|
|
|
.join(', ')}
|
2020-02-19 16:47:21 +00:00
|
|
|
|
Cela vient probablement d'une erreur dans l'indentation
|
|
|
|
|
`
|
2020-04-14 14:09:31 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parseChainedMecanisms(rules, rule, parsedRules, rawNode)
|
2019-09-10 16:24:05 +00:00
|
|
|
|
}
|
2020-02-19 16:47:21 +00:00
|
|
|
|
const mecanismName = Object.keys(rawNode)[0]
|
|
|
|
|
const values = rawNode[mecanismName]
|
2019-05-15 08:52:20 +00:00
|
|
|
|
|
2020-02-19 16:47:21 +00:00
|
|
|
|
const parseFunctions = {
|
|
|
|
|
...statelessParseFunction,
|
|
|
|
|
'une possibilité': mecanismOnePossibility(rule.dottedName),
|
|
|
|
|
'inversion numérique': mecanismInversion(rule.dottedName),
|
2020-01-02 14:39:31 +00:00
|
|
|
|
recalcul: mecanismRecalcul(rule.dottedName),
|
2020-02-19 16:47:21 +00:00
|
|
|
|
filter: () =>
|
|
|
|
|
parseReferenceTransforms(
|
|
|
|
|
rules,
|
|
|
|
|
rule,
|
|
|
|
|
parsedRules
|
|
|
|
|
)({
|
|
|
|
|
filter: values.filter,
|
|
|
|
|
variable: values.explanation
|
|
|
|
|
}),
|
|
|
|
|
variable: () =>
|
|
|
|
|
parseReferenceTransforms(rules, rule, parsedRules)({ variable: values }),
|
|
|
|
|
unitConversion: () =>
|
|
|
|
|
parseReferenceTransforms(
|
|
|
|
|
rules,
|
|
|
|
|
rule,
|
|
|
|
|
parsedRules
|
|
|
|
|
)({
|
|
|
|
|
variable: values.explanation,
|
|
|
|
|
unit: values.unit
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-06-29 09:13:05 +00:00
|
|
|
|
|
2020-02-19 16:47:21 +00:00
|
|
|
|
const parseFn = parseFunctions[mecanismName]
|
|
|
|
|
if (!parseFn) {
|
|
|
|
|
syntaxError(
|
|
|
|
|
rule.dottedName,
|
|
|
|
|
`
|
|
|
|
|
Le mécanisme ${mecanismName} est inconnu.
|
|
|
|
|
Vérifiez qu'il n'y ait pas d'erreur dans l'orthographe du nom.`
|
2019-05-15 08:52:20 +00:00
|
|
|
|
)
|
2020-02-19 16:47:21 +00:00
|
|
|
|
}
|
2020-02-16 18:56:07 +00:00
|
|
|
|
try {
|
|
|
|
|
return parseFn(parse(rules, rule, parsedRules), mecanismName, values)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e instanceof EngineError) {
|
|
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
syntaxError(rule.dottedName, e.message)
|
|
|
|
|
}
|
2020-02-19 16:47:21 +00:00
|
|
|
|
}
|
2019-05-15 08:52:20 +00:00
|
|
|
|
|
2020-04-14 14:09:31 +00:00
|
|
|
|
const chainableMecanisms = ['arrondi', 'plancher', 'plafond']
|
|
|
|
|
|
|
|
|
|
function parseChainedMecanisms(rules, rule, parsedRules, rawNode) {
|
|
|
|
|
const keys = Object.keys(rawNode)
|
|
|
|
|
const recurse = parseMecanism(rules, rule, parsedRules)
|
|
|
|
|
if (keys.includes('arrondi')) {
|
|
|
|
|
return recurse(
|
|
|
|
|
unchainRoundMecanism(parse(rules, rule, parsedRules), rawNode)
|
|
|
|
|
)
|
|
|
|
|
} else if (keys.includes('plancher')) {
|
|
|
|
|
const { plancher, ...valeur } = rawNode
|
|
|
|
|
return recurse({
|
|
|
|
|
encadrement: {
|
|
|
|
|
valeur,
|
|
|
|
|
plancher
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else if (keys.includes('plafond')) {
|
|
|
|
|
const { plafond, ...valeur } = rawNode
|
|
|
|
|
return recurse({
|
|
|
|
|
encadrement: {
|
|
|
|
|
valeur,
|
|
|
|
|
plafond
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 16:47:21 +00:00
|
|
|
|
const knownOperations = {
|
|
|
|
|
'*': [multiply, '×'],
|
|
|
|
|
'/': [divide, '∕'],
|
|
|
|
|
'+': [add],
|
|
|
|
|
'-': [subtract, '−'],
|
|
|
|
|
'<': [lt],
|
|
|
|
|
'<=': [lte, '≤'],
|
|
|
|
|
'>': [gt],
|
|
|
|
|
'>=': [gte, '≥'],
|
|
|
|
|
'=': [equals],
|
|
|
|
|
'!=': [(a, b) => !equals(a, b), '≠']
|
|
|
|
|
}
|
2018-06-29 09:13:05 +00:00
|
|
|
|
|
2020-02-19 16:47:21 +00:00
|
|
|
|
const operationDispatch = fromPairs(
|
|
|
|
|
Object.entries(knownOperations).map(([k, [f, symbol]]) => [
|
|
|
|
|
k,
|
|
|
|
|
operation(k, f, symbol)
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const statelessParseFunction = {
|
|
|
|
|
...operationDispatch,
|
|
|
|
|
'une de ces conditions': mecanismOneOf,
|
|
|
|
|
'toutes ces conditions': mecanismAllOf,
|
|
|
|
|
somme: mecanismSum,
|
2020-02-24 17:34:38 +00:00
|
|
|
|
régularisation,
|
2020-02-19 16:47:21 +00:00
|
|
|
|
multiplication: mecanismProduct,
|
2020-03-11 13:08:41 +00:00
|
|
|
|
produit: mecanismProduct,
|
2020-02-04 17:33:03 +00:00
|
|
|
|
temporalValue: variableTemporelle,
|
2020-02-19 16:47:21 +00:00
|
|
|
|
arrondi: mecanismRound,
|
|
|
|
|
barème,
|
|
|
|
|
grille,
|
|
|
|
|
'taux progressif': tauxProgressif,
|
|
|
|
|
encadrement,
|
|
|
|
|
durée,
|
|
|
|
|
'le maximum de': mecanismMax,
|
|
|
|
|
'le minimum de': mecanismMin,
|
|
|
|
|
allègement: mecanismReduction,
|
|
|
|
|
variations,
|
|
|
|
|
synchronisation: mecanismSynchronisation,
|
2020-04-14 14:09:31 +00:00
|
|
|
|
valeur: (recurse, __, v) => recurse(v),
|
2020-02-19 16:47:21 +00:00
|
|
|
|
constant: (_, __, v) => ({
|
|
|
|
|
type: v.type,
|
2020-04-23 07:30:03 +00:00
|
|
|
|
constant: true,
|
2020-02-19 16:47:21 +00:00
|
|
|
|
nodeValue: v.nodeValue,
|
|
|
|
|
unit: v.unit,
|
|
|
|
|
// eslint-disable-next-line
|
2020-05-18 14:43:29 +00:00
|
|
|
|
jsx: (node: EvaluatedRule) => (
|
2020-02-19 16:47:21 +00:00
|
|
|
|
<span className={v.type}>
|
2020-05-18 14:43:29 +00:00
|
|
|
|
{formatValue(node, {
|
2020-04-23 07:30:03 +00:00
|
|
|
|
// We want to display constants with full precision,
|
|
|
|
|
// espacilly for percentages like APEC 0,036 %
|
|
|
|
|
precision: 5
|
|
|
|
|
})}
|
2020-02-19 16:47:21 +00:00
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
})
|
2018-06-29 09:13:05 +00:00
|
|
|
|
}
|