From c0bd229cde3a2a065e46efc21507869f18ef9286 Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Mon, 4 Nov 2019 16:09:09 +0100 Subject: [PATCH] :gear: supporte plusieurs remplacement d'un coup --- source/engine/parseRule.js | 51 ++++++++++++++++++----------------- source/engine/traverse.js | 15 ++++++----- test/mécanismes/remplace.yaml | 18 ++++++++++--- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/source/engine/parseRule.js b/source/engine/parseRule.js index 6ebb5fe73..5d46b2154 100644 --- a/source/engine/parseRule.js +++ b/source/engine/parseRule.js @@ -71,30 +71,7 @@ export default (rules, rule, parsedRules) => { nonApplicableRules.map(referenceName => { return disambiguateRuleReference(rules, rule, referenceName) }), - remplace: reference => { - const referenceName = - typeof reference === 'string' ? reference : reference.règle - let replacementNode = reference.par - if (replacementNode) { - replacementNode = parse(rules, rule, parsedRules)(replacementNode) - } - let [whiteListedNames, blackListedNames] = [ - reference.dans, - reference['sauf dans'] - ] - .map(name => name && coerceArray(name)) - .map( - names => - names && - names.map(name => disambiguateRuleReference(rules, rule, name)) - ) - return { - referenceName: disambiguateRuleReference(rules, rule, referenceName), - replacementNode, - whiteListedNames, - blackListedNames - } - }, + remplace: evolveReplacement(rules, rule, parsedRules), formule: value => { let evaluate = (cache, situationGate, parsedRules, node) => { let explanation = evaluateNode( @@ -232,3 +209,29 @@ let evolveCond = (name, rule, rules, parsedRules) => value => { explanation: child } } + +let evolveReplacement = (rules, rule, parsedRules) => replacements => + coerceArray(replacements).map(reference => { + const referenceName = + typeof reference === 'string' ? reference : reference.règle + let replacementNode = reference.par + if (replacementNode != null) { + replacementNode = parse(rules, rule, parsedRules)(replacementNode) + } + let [whiteListedNames, blackListedNames] = [ + reference.dans, + reference['sauf dans'] + ] + .map(name => name && coerceArray(name)) + .map( + names => + names && + names.map(name => disambiguateRuleReference(rules, rule, name)) + ) + return { + referenceName: disambiguateRuleReference(rules, rule, referenceName), + replacementNode, + whiteListedNames, + blackListedNames + } + }) diff --git a/source/engine/traverse.js b/source/engine/traverse.js index 28206f737..3cf549c6f 100644 --- a/source/engine/traverse.js +++ b/source/engine/traverse.js @@ -60,12 +60,15 @@ export let parseAll = flatRules => { nonApplicableMapping[rule.dottedName] = parsed['rend non applicable'] } - const descriptor = parsed['remplace'] - if (descriptor) { - replacedByMapping[descriptor.referenceName] = [ - ...(replacedByMapping[descriptor.referenceName] ?? []), - { ...descriptor, referenceName: rule.dottedName } - ] + const replaceDescriptors = parsed['remplace'] + if (replaceDescriptors) { + replaceDescriptors.forEach( + descriptor => + (replacedByMapping[descriptor.referenceName] = [ + ...(replacedByMapping[descriptor.referenceName] ?? []), + { ...descriptor, referenceName: rule.dottedName } + ]) + ) } }) diff --git a/test/mécanismes/remplace.yaml b/test/mécanismes/remplace.yaml index 35f7f4d7d..6323c6994 100644 --- a/test/mécanismes/remplace.yaml +++ b/test/mécanismes/remplace.yaml @@ -136,14 +136,24 @@ valeur attendue: 40 applicable si: exemple4 +- test: remplacement de plusieurs variables d'un coup + formule: cotisations + remplace: + - règle: cotisations . chômage + par: 10 + - règle: cotisations . maladie + par: 0 + exemples: + - situation: + exemple5: oui + valeur attendue: 110 + applicable si: exemple5 + - nom: exemple1 - formule: non - nom: exemple2 - formule: non - nom: exemple3 - formule: non - nom: exemple4 - formule: non +- nom: exemple5 - nom: A formule: 1