From d5a1e08c728dc38dd3d38ca91d985c81382f6169 Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Fri, 28 Jul 2017 11:42:57 +0200 Subject: [PATCH] =?UTF-8?q?:bug:=20R=C3=A9pare=20la=20collecte=20des=20var?= =?UTF-8?q?iables=20manquantes=20des=20logiques=20num=C3=A9riques?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/mecanisms.js | 14 +++++++--- test/generateQuestions.test.js | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/source/engine/mecanisms.js b/source/engine/mecanisms.js index 73d1e3bea..012658b62 100644 --- a/source/engine/mecanisms.js +++ b/source/engine/mecanisms.js @@ -137,8 +137,12 @@ export let mecanismNumericalLogic = (recurse, k,v) => { consequenceNode = mecanismNumericalLogic(recurse, condition, consequence) let evaluate = (situationGate, parsedRules, node) => { - let collectMissing = node => - R.concat(collectNodeMissing(conditionNode),collectNodeMissing(consequenceNode)) + let collectMissing = node => { + let leftMissing = collectNodeMissing(node.explanation.condition), + investigate = node.explanation.condition.nodeValue == true, + rightMissing = investigate ? collectNodeMissing(node.explanation.consequence) : [] + return R.concat(leftMissing, rightMissing) + } let explanation = R.evolve({ condition: R.curry(evaluateNode)(situationGate, parsedRules), @@ -179,7 +183,11 @@ export let mecanismNumericalLogic = (recurse, k,v) => { choice = R.find(node => node.condValue, explanation), nodeValue = choice ? choice.nodeValue : null - let collectMissing = node => R.chain(collectNodeMissing,node.explanation) + let collectMissing = node => { + let choice = R.find(node => node.condValue, node.explanation) + return choice ? collectNodeMissing(choice) : R.chain(collectNodeMissing,node.explanation) + } + return rewriteNode(node,nodeValue,explanation,collectMissing) } diff --git a/test/generateQuestions.test.js b/test/generateQuestions.test.js index cc5a0abb4..d335fe6b9 100644 --- a/test/generateQuestions.test.js +++ b/test/generateQuestions.test.js @@ -77,6 +77,54 @@ describe('collectMissingVariables', function() { expect(result).to.be.empty }); + it('should report missing variables in switch statements', function() { + let rawRules = [ + { nom: "startHere", formule: {somme: ["logic"]}, espace: "top"}, + { nom: "logic", formule: {"logique numérique": { + "11 > dix":"1000%", + "3 > dix":"1100%", + "1 > dix":"1200%" + }}, espace: "top"}, + {nom: "dix", espace: "top"}], + rules = rawRules.map(enrichRule), + situation = analyseTopDown(rules,"startHere")(stateSelector), + result = collectMissingVariables()(stateSelector,situation) + + expect(result).to.have.property('top . dix') + }); + + it('should not report missing variables in switch for consequences of false conditions', function() { + let rawRules = [ + { nom: "startHere", formule: {somme: ["logic"]}, espace: "top"}, + { nom: "logic", formule: {"logique numérique": { + "11 > 10":"1000%", + "3 > 1":"1100%", + "1 > 2":"dix" + }}, espace: "top"}, + {nom: "dix", espace: "top"}], + rules = rawRules.map(enrichRule), + situation = analyseTopDown(rules,"startHere")(stateSelector), + result = collectMissingVariables()(stateSelector,situation) + + expect(result).to.be.empty + }); + + it('should not report missing variables when a switch short-circuits', function() { + let rawRules = [ + { nom: "startHere", formule: {somme: ["logic"]}, espace: "top"}, + { nom: "logic", formule: {"logique numérique": { + "11 > 10":"1000%", + "3 > dix":"1100%", + "1 > dix":"1200%" + }}, espace: "top"}, + {nom: "dix", espace: "top"}], + rules = rawRules.map(enrichRule), + situation = analyseTopDown(rules,"startHere")(stateSelector), + result = collectMissingVariables()(stateSelector,situation) + + expect(result).to.be.empty + }); + }); describe('buildNextSteps', function() {