From bfd1f2a3af6180a28f80ba5c339b7a61bf16f169 Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Wed, 28 Jun 2017 09:06:52 +0200 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20Tester=20le=20m=C3=A9c?= =?UTF-8?q?anisme=20de=20maximum,=20restructurer=20l'aiguillage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/traverse.test.js | 7 ++++ source/engine/traverse.js | 84 ++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/__tests__/traverse.test.js b/__tests__/traverse.test.js index 540a2a3c7..b718fd712 100644 --- a/__tests__/traverse.test.js +++ b/__tests__/traverse.test.js @@ -129,4 +129,11 @@ describe('analyseSituation with mecanisms', function() { expect(analyseSituation(rules,"startHere")(stateSelector)).to.have.property('nodeValue',100+1200+80) }); + it('should handle max', function() { + let rawRules = [ + {nom: "startHere", formule: {"le maximum de": [3200, 60, 9]}}], + rules = rawRules.map(enrichRule) + expect(analyseSituation(rules,"startHere")(stateSelector)).to.have.property('nodeValue',3200) + }); + }); diff --git a/source/engine/traverse.js b/source/engine/traverse.js index d1b82d318..bdb64dd0a 100644 --- a/source/engine/traverse.js +++ b/source/engine/traverse.js @@ -654,6 +654,39 @@ let treat = (situationGate, rules, rule) => rawNode => { /> } }, + mecanismMax = (k,v) => { + let contenders = v.map(reTreat), + contenderValues = R.pluck('nodeValue')(contenders), + stopEverything = R.contains(null, contenderValues), + maxValue = R.max(...contenderValues), + nodeValue = stopEverything ? null : maxValue + + return { + type: 'numeric', + category: 'mecanism', + name: 'le maximum de', + nodeValue, + explanation: contenders, + jsx: + {contenders.map((item, i) => +
  • +
    {v[i].description}
    + {item.jsx} +
  • + )} + + } + /> + } + }, + mecanismError = (k,v) => { + throw "Le mécanisme est inconnu !" + }, treatObject = rawNode => { let mecanisms = R.intersection(R.keys(rawNode), R.keys(knownMecanisms)) @@ -665,46 +698,19 @@ let treat = (situationGate, rules, rule) => rawNode => { let k = R.head(mecanisms), v = rawNode[k] - if (k === 'une de ces conditions') return mecanismOneOf(k,v) - if (k === 'toutes ces conditions') return mecanismAllOf(k,v) - if (k === 'logique numérique') return mecanismNumericalLogic(k,v) - if (k === 'taux') return mecanismPercentage(k,v) - if (k === 'somme') return mecanismSum(k,v) - if (k === 'multiplication') return mecanismProduct(k,v) - if (k === 'barème') return mecanismScale(k,v) + let dispatch = { + 'une de ces conditions': mecanismOneOf, + 'toutes ces conditions': mecanismAllOf, + 'logique numérique': mecanismNumericalLogic, + 'taux': mecanismPercentage, + 'somme': mecanismSum, + 'multiplication': mecanismProduct, + 'barème': mecanismScale, + 'le maximum de': mecanismMax, + }, + action = R.pathOr(mecanismError,[k],dispatch) - if (k === 'le maximum de') { - let contenders = v.map(treat(situationGate, rules, rule)), - contenderValues = R.pluck('nodeValue')(contenders), - stopEverything = R.contains(null, contenderValues), - maxValue = R.max(...contenderValues), - nodeValue = stopEverything ? null : maxValue - - return { - type: 'numeric', - category: 'mecanism', - name: 'le maximum de', - nodeValue, - explanation: contenders, - jsx: - {contenders.map((item, i) => -
  • -
    {v[i].description}
    - {item.jsx} -
  • - )} - - } - /> - } - } - - throw "Le mécanisme est inconnu !" + return action(k,v) } let onNodeType = R.cond([