From 768e7aee6bef5e306ea6669962799959930abe0e Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Tue, 10 Apr 2018 14:27:25 +0200 Subject: [PATCH] Simplifie le calcul des variables manquantes (5) --- source/engine/evaluation.js | 21 ++++++--------------- source/engine/generateQuestions.js | 6 +++--- source/engine/mecanisms.js | 15 +++++++-------- source/engine/traverse-common-functions.js | 2 -- 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/source/engine/evaluation.js b/source/engine/evaluation.js index efbf97388..e7248bdbe 100644 --- a/source/engine/evaluation.js +++ b/source/engine/evaluation.js @@ -43,13 +43,10 @@ export let evaluateArray = (reducer, start) => ( ? null : reduce(reducer, start, values), missingVariables = node.nodeValue == null - ? chain(collectNodeMissing, explanation) + ? map(collectNodeMissing, explanation) : [] - return { - ...rewriteNode(node, nodeValue, explanation), - missingVariables - } + return rewriteNode(node, nodeValue, explanation, missingVariables) } export let evaluateArrayWithFilter = (evaluationFilter, reducer, start) => ( @@ -69,13 +66,10 @@ export let evaluateArrayWithFilter = (evaluationFilter, reducer, start) => ( ? null : reduce(reducer, start, values), missingVariables = node.nodeValue == null - ? chain(collectNodeMissing, explanation) + ? map(collectNodeMissing, explanation) : [] - return { - ...rewriteNode(node, nodeValue, explanation), - missingVariables - } + return rewriteNode(node, nodeValue, explanation, missingVariables) } export let parseObject = (recurse, objectShape, value) => { @@ -99,10 +93,7 @@ export let evaluateObject = (objectShape, effect) => ( let transforms = map(k => [k, evaluateOne], keys(objectShape)), explanation = evolve(fromPairs(transforms))(node.explanation), nodeValue = effect(explanation), - missingVariables = chain(collectNodeMissing, values(explanation)) + missingVariables = map(collectNodeMissing, values(explanation)) - return { - ...rewriteNode(node, nodeValue, explanation), - missingVariables - } + return rewriteNode(node, nodeValue, explanation, missingVariables) } diff --git a/source/engine/generateQuestions.js b/source/engine/generateQuestions.js index 7af07df0f..4eb8038a1 100644 --- a/source/engine/generateQuestions.js +++ b/source/engine/generateQuestions.js @@ -1,5 +1,6 @@ import { - chain, + flatten, + pluck, groupBy, toPairs, sort, @@ -22,7 +23,6 @@ import SelectAtmp from 'Components/conversation/select/SelectTauxRisque' import formValueTypes from 'Components/conversation/formValueTypes' import { findRuleByDottedName, disambiguateRuleReference } from './rules' -import { collectNodeMissing } from './evaluation' /* COLLECTE DES VARIABLES MANQUANTES @@ -40,7 +40,7 @@ import { collectNodeMissing } from './evaluation' */ export let collectMissingVariables = targets => { - let missing = chain(collectNodeMissing, targets) + let missing = flatten(pluck('missingVariables', targets)) return groupBy(identity, missing) } diff --git a/source/engine/mecanisms.js b/source/engine/mecanisms.js index f0b44b033..e8aea53f9 100644 --- a/source/engine/mecanisms.js +++ b/source/engine/mecanisms.js @@ -150,11 +150,11 @@ let devariate = (recurse, k, v) => { let leftMissing = choice ? [] : uniq( - chain(collectNodeMissing, pluck('condition', explanation)) + map(collectNodeMissing, pluck('condition', explanation)) ), rightMissing = choice ? choice.missingVariables - : chain(collectNodeMissing, explanation), + : map(collectNodeMissing, explanation), missingVariables = concat(leftMissing, rightMissing || []) return rewriteNode(node, nodeValue, explanation, missingVariables) @@ -219,7 +219,7 @@ export let mecanismOneOf = (recurse, k, v) => { nodeValue = any(equals(true), values) ? true : any(equals(null), values) ? null : false, - missingVariables = nodeValue == null ? chain(collectNodeMissing, explanation) : [] + missingVariables = nodeValue == null ? map(collectNodeMissing, explanation) : [] return rewriteNode(node, nodeValue, explanation, missingVariables) } @@ -262,7 +262,7 @@ export let mecanismAllOf = (recurse, k, v) => { nodeValue = any(equals(false), values) ? false // court-circuit : any(equals(null), values) ? null : true, - missingVariables = nodeValue == null ? chain(collectNodeMissing, explanation) : [] + missingVariables = nodeValue == null ? map(collectNodeMissing, explanation) : [] return rewriteNode(node, nodeValue, explanation, missingVariables) } @@ -356,7 +356,7 @@ export let mecanismNumericalSwitch = (recurse, k, v) => { choice = find(node => node.condValue, explanation), missingVariables = choice ? choice.missingVariables - : chain(collectNodeMissing, explanation) + : map(collectNodeMissing, explanation) return rewriteNode(node, nodeValue, explanation, missingVariables) } @@ -444,14 +444,13 @@ let doInversion = (situationGate, parsedRules, v, dottedName) => { if (fx(1000) == null) return { nodeValue: null, - inversionMissingVariables: collectNodeMissing( + inversionMissingVariables: evaluateNode( {}, n => (dottedName === n ? 1000 : situationGate(n)), parsedRules, fixedObjectiveRule - ) - ) + ).missingVariables } let tolerancePercentage = 0.00001, diff --git a/source/engine/traverse-common-functions.js b/source/engine/traverse-common-functions.js index ab77c9047..e89d25549 100644 --- a/source/engine/traverse-common-functions.js +++ b/source/engine/traverse-common-functions.js @@ -5,5 +5,3 @@ export let val = node => node && node.nodeValue export let undefOrTrue = val => val == undefined || val == true export let anyNull = any(pipe(val, equals(null))) - -export let applyOrEmpty = func => v => (v ? func(v) : [])