🐛 Corrige la mise en cache des variables filtrées

pull/58/head
Laurent Bossavit 2017-09-23 12:54:09 +02:00
parent 0382ac3095
commit 69aa933bf1
2 changed files with 23 additions and 4 deletions

View File

@ -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
}

View File

@ -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)
});
});