From bac3bd3ef62dc555f706230e71d67e75310586dd Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Tue, 11 Jul 2017 16:39:05 +0200 Subject: [PATCH] =?UTF-8?q?:gear:=20Refactoring=20de=20l'=C3=A9valuation?= =?UTF-8?q?=20des=20m=C3=A9canismes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/mecanisms.js | 108 +++++++++------------------------ test/generateQuestions.test.js | 13 +++- 2 files changed, 41 insertions(+), 80 deletions(-) diff --git a/source/engine/mecanisms.js b/source/engine/mecanisms.js index 2462add4c..e5bfe0ac9 100644 --- a/source/engine/mecanisms.js +++ b/source/engine/mecanisms.js @@ -9,12 +9,32 @@ let transformPercentage = s => +s.replace('%', '') / 100 : +s +let evaluateArray = (reducer, start) => (situationGate, parsedRules, node) => { + let evaluateOne = child => evaluateNode(situationGate, parsedRules, child), + explanation = R.map(evaluateOne, node.explanation), + values = R.pluck("nodeValue",explanation), + nodeValue = R.any(R.equals(null),values) ? null : R.reduce(reducer, start, values) + + let collectMissing = node => R.chain(collectNodeMissing,node.explanation) + + return { + ...node, + nodeValue, + collectMissing, + explanation, + jsx: { + ...node.jsx, + value: nodeValue + } + } +} + export let decompose = (recurse, k, v) => { let subProps = R.dissoc('composantes')(v), filter = val(recurse("sys . filter")), isRelevant = c => !filter || !c.attributs || c.attributs['dû par'] == filter, - composantes = v.composantes.filter(isRelevant).map(c => + explanation = v.composantes.filter(isRelevant).map(c => ({ ... recurse( R.objOf(k, @@ -25,23 +45,20 @@ export let decompose = (recurse, k, v) => { ), composante: c.nom ? {nom: c.nom} : c.attributs }) - ), - nodeValue = anyNull(composantes) ? null - : R.reduce(R.add, 0, composantes.map(val)) + ) return { - nodeValue, + evaluate: evaluateArray(R.add,0), category: 'mecanism', name: 'composantes', type: 'numeric', - explanation: composantes, + explanation, jsx: - { composantes.map((c, i) => + { explanation.map((c, i) => [
    • {R.toPairs(c.composante).map(([k,v]) => @@ -55,7 +72,7 @@ export let decompose = (recurse, k, v) => { {c.jsx} , - i < (composantes.length - 1) &&
    • + i < (explanation.length - 1) &&
    • ] ) } @@ -68,25 +85,10 @@ export let decompose = (recurse, k, v) => { export let mecanismOneOf = (recurse, k, v) => { if (!R.is(Array,v)) throw 'should be array' - let evaluate = (situationGate, parsedRules, node) => { - let evaluateOne = child => evaluateNode(situationGate, parsedRules, child).nodeValue, - values = R.map(evaluateOne, node.explanation), - nodeValue = R.any(R.equals(null),values) ? null : R.reduce(R.or,false,values) - - return { - ...node, - nodeValue, - jsx: { - ...node.jsx, - value: nodeValue - } - } - } - let explanation = R.map(recurse, v) return { - evaluate, + evaluate: evaluateArray(R.or,false), explanation, category: 'mecanism', name: 'une de ces conditions', @@ -106,25 +108,10 @@ export let mecanismOneOf = (recurse, k, v) => { export let mecanismAllOf = (recurse, k,v) => { if (!R.is(Array,v)) throw 'should be array' - let evaluate = (situationGate, parsedRules, node) => { - let evaluateOne = child => evaluateNode(situationGate, parsedRules, child).nodeValue, - values = R.map(evaluateOne, node.explanation), - nodeValue = R.any(R.equals(null),values) ? null : R.reduce(R.and,true,values) - - return { - ...node, - nodeValue, - jsx: { - ...node.jsx, - value: nodeValue - } - } - } - let explanation = R.map(recurse, v) return { - evaluate, + evaluate: evaluateArray(R.and,true), explanation, category: 'mecanism', name: 'toutes ces conditions', @@ -234,23 +221,6 @@ export let mecanismPercentage = (recurse,k,v) => { {v} } - // Si c'est une liste historisée de pourcentages - // TODO revoir le test avant le bug de l'an 2100 - else if ( R.is(Array)(v) && R.all(R.test(/(19|20)\d\d(-\d\d)?(-\d\d)?/))(R.keys(v)) ) { - //TODO sélectionner la date de la simulation en cours - let lazySelection = R.first(R.values(v)) - return { - category: 'percentage', - type: 'numeric', - percentage: lazySelection, - nodeValue: transformPercentage(lazySelection), - explanation: null, - jsx: - - {lazySelection} - - } - } else { let node = recurse(v) return { @@ -265,30 +235,10 @@ export let mecanismPercentage = (recurse,k,v) => { } export let mecanismSum = (recurse,k,v) => { - let evaluate = (situationGate, parsedRules, node) => { - let evaluateOne = child => evaluateNode(situationGate, parsedRules, child), - explanation = R.map(evaluateOne, node.explanation), - values = R.pluck("nodeValue",explanation), - nodeValue = R.any(R.equals(null),values) ? null : R.reduce(R.add,0,values) - - let collectMissing = node => R.chain(collectNodeMissing,node.explanation) - - return { - ...node, - nodeValue, - collectMissing, - explanation, - jsx: { - ...node.jsx, - value: nodeValue - } - } - } - let explanation = v.map(recurse) return { - evaluate, + evaluate: evaluateArray(R.add,0), explanation, category: 'mecanism', name: 'somme', diff --git a/test/generateQuestions.test.js b/test/generateQuestions.test.js index 3adbcd191..76c87588f 100644 --- a/test/generateQuestions.test.js +++ b/test/generateQuestions.test.js @@ -1,6 +1,6 @@ import R from 'ramda' import {expect} from 'chai' -import {rules, enrichRule} from '../source/engine/rules' +import {rules as realRules, enrichRule} from '../source/engine/rules' import {analyseSituation, analyseTopDown} from '../source/engine/traverse' import {buildNextSteps, collectMissingVariables, getObjectives} from '../source/engine/generateQuestions' @@ -71,4 +71,15 @@ describe('buildNextSteps', function() { expect(R.path(["question","props","label"])(result[0])).to.equal("?") }); + it('should generate questions from the real rules', function() { + let rules = realRules.map(enrichRule), + situation = analyseTopDown(rules,"surcoût CDD")(stateSelector), + objectives = getObjectives(stateSelector, situation.root, situation.parsedRules), + result = buildNextSteps(stateSelector, rules, situation) + + expect(objectives).to.have.lengthOf(4) + expect(result).to.have.lengthOf(1) + expect(R.path(["question","props","label"])(result[0])).to.equal("?") + }); + });