From 69aa933bf1a8eec8119406428730ed3f90bf2189 Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Sat, 23 Sep 2017 12:54:09 +0200 Subject: [PATCH] =?UTF-8?q?:bug:=20Corrige=20la=20mise=20en=20cache=20des?= =?UTF-8?q?=20variables=20filtr=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/traverse.js | 6 ++++-- test/traverse.test.js | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/source/engine/traverse.js b/source/engine/traverse.js index e65c0b337..61e8c0c22 100644 --- a/source/engine/traverse.js +++ b/source/engine/traverse.js @@ -74,7 +74,9 @@ let fillVariableNode = (rules, rule) => (parseResult) => { let dottedName = node.dottedName, // On va vérifier dans le cache courant, dict, si la variable n'a pas été déjà évaluée // En effet, l'évaluation dans le cas d'une variable qui a une formule, est coûteuse ! - cached = dict[dottedName], + filter = situation("sys.filter"), + cacheName = dottedName + (filter ? "." + filter: ""), + cached = dict[cacheName], // make parsedRules a dict object, that also serves as a cache of evaluation ? variable = cached ? cached : findRuleByDottedName(parsedRules, dottedName), variableIsCalculable = variable.formule != null, @@ -101,7 +103,7 @@ let fillVariableNode = (rules, rule) => (parseResult) => { ...rewriteNode(node,nodeValue,explanation,collectMissing), missingVariables, } - dict[dottedName] = result + dict[cacheName] = result return result } diff --git a/test/traverse.test.js b/test/traverse.test.js index 97e5009ef..85024531f 100644 --- a/test/traverse.test.js +++ b/test/traverse.test.js @@ -1,6 +1,6 @@ import {expect} from 'chai' -import {enrichRule} from '../source/engine/rules' -import {analyseSituation} from '../source/engine/traverse' +import {rules as realRules, enrichRule} from '../source/engine/rules' +import {analyseSituation, analyseTopDown} from '../source/engine/traverse' let stateSelector = (state, name) => null @@ -219,4 +219,21 @@ describe('analyseSituation with mecanisms', function() { expect(analyseSituation(rules,"startHere")(stateSelector)).to.have.property('nodeValue',50+400+40) }); + // TODO - make this a smaller test case + it('should compute consistent values', function() { + let stateSelector = (name) => ({ + "contrat salarié . CDD . événement . poursuite du CDD en CDI":"oui", + "contrat salarié . salaire brut":2300, + "contrat salarié . statut cadre":"non", + "entreprise . effectif":20 + })[name] + + let rules = realRules.map(enrichRule), + part = analyseTopDown(rules,"coût du travail")(stateSelector), + whole = analyseTopDown(rules,"Salaire")(stateSelector) + + expect(part.root.nodeValue).to.be.closeTo(2971.44,0.01) + expect(whole.root.formule.explanation.explanation[1].nodeValue).to.be.closeTo(2971.44,0.01) + }); + });