diff --git a/source/engine/rules.js b/source/engine/rules.js index 9fb295980..41057a578 100644 --- a/source/engine/rules.js +++ b/source/engine/rules.js @@ -152,6 +152,25 @@ export let collectMissingVariables = (groupMethod='groupByMissingVariable') => a let isVariant = R.path(['formule', 'une possibilité']) +export let findVariantsAndRecords2 = (allRules, names) => { + let tag = name => { + let parent = parentName(name), + gramps = parentName(parent), + findV = name => isVariant(findRuleByDottedName(allRules,name)) + + return findV(gramps) ? {type: "variantGroups", [gramps]:[name]} + : findV(parent) ? {type: "variantGroups", [parent]:[name]} + : {type: "recordGroups", [parent]:[name]} + } + + let classify = R.map(tag), + groupByType = R.groupBy(R.prop("type")), + stripTypes = R.map(R.map(R.omit("type"))), + mergeLists = R.map(R.reduce(R.mergeWith(R.concat),{})) + + return R.pipe(classify,groupByType,stripTypes,mergeLists)(names) +} + export let findVariantsAndRecords = (allRules, {variantGroups, recordGroups}, dottedName, childDottedName) => { let child = findRuleByDottedName(allRules, dottedName), diff --git a/test/rules.test.js b/test/rules.test.js index f01b94e18..d4f42c33f 100644 --- a/test/rules.test.js +++ b/test/rules.test.js @@ -1,5 +1,6 @@ +import R from 'ramda' import {expect} from 'chai' -import {enrichRule, collectMissingVariables, findVariantsAndRecords, getObjectives} from '../source/engine/rules' +import {rules, enrichRule, collectMissingVariables, findVariantsAndRecords2, findVariantsAndRecords, getObjectives} from '../source/engine/rules' import {analyseSituation} from '../source/engine/traverse' let stateSelector = (state, name) => null @@ -63,7 +64,7 @@ describe('findVariantsAndRecords', function() { {nom: "cinq", espace: "top", question:"?"}], rules = rawRules.map(enrichRule), situation = analyseSituation(rules,"startHere")(stateSelector), - result = findVariantsAndRecords(rules, {variantGroups: {}, recordGroups: {}}, 'top . cinq', null) + result = findVariantsAndRecords2(rules, ['top . cinq']) expect(result).to.have.deep.property('recordGroups', {top: ['top . cinq']}) }); @@ -76,9 +77,32 @@ describe('findVariantsAndRecords', function() { {nom: "ko", espace: "top . sum . evt"}], rules = rawRules.map(enrichRule), situation = analyseSituation(rules,"sum")(stateSelector), - result = findVariantsAndRecords(rules, {variantGroups: {}, recordGroups: {}}, 'top . sum . evt . ko') + result = findVariantsAndRecords2(rules, ['top . sum . evt . ko']) expect(result).to.have.deep.property('variantGroups', {"top . sum . evt": ['top . sum . evt . ko']}) }); + it('should find variants', function() { + let rawRules = [ + {nom: "sum", formule: {somme: [2, "deux"]}, espace: "top"}, + {nom: "deux", formule: 2, "non applicable si" : "top . sum . evt . ko", espace: "top"}, + {nom: "evt", espace: "top . sum", formule: {"une possibilité":["ko"]}, titre: "Truc", question:"?"}, + {nom: "ko", espace: "top . sum . evt"}], + rules = rawRules.map(enrichRule), + situation = analyseSituation(rules,"sum")(stateSelector), + result = findVariantsAndRecords2(rules, ['top . sum . evt . ko']) + + expect(result['variantGroups']).to.deep.equal({"top . sum . evt": ['top . sum . evt . ko']}) + }); + + it('should provide equivalent function to findVAR with findVariants', function() { + let situation = analyseSituation(rules,"surcoût CDD")(stateSelector), + findVAR = (memo,name) => findVariantsAndRecords(rules, memo, name, null), + missing = collectMissingVariables()(situation), + newResult = R.pipe(R.keys,R.reduce(findVAR, {variantGroups: {}, recordGroups: {}}))(missing), + oldResult = findVariantsAndRecords2(rules, R.keys(missing)) + + expect(newResult).to.deep.equal(oldResult) + }); + });