diff --git a/source/engine/mecanisms.js b/source/engine/mecanisms.js index 9d31079ab..3cec78b91 100644 --- a/source/engine/mecanisms.js +++ b/source/engine/mecanisms.js @@ -3,6 +3,7 @@ import React from 'react' import {anyNull, val} from './traverse-common-functions' import {Node, Leaf} from './traverse-common-jsx' import {makeJsx, evaluateNode, rewriteNode, evaluateArray, evaluateArrayWithFilter, evaluateObject, parseObject, collectNodeMissing} from './evaluation' +import {findRuleByName} from './rules' let constantNode = constant => ({nodeValue: constant, jsx: nodeValue => {nodeValue}}) @@ -609,6 +610,49 @@ export let mecanismComplement = (recurse,k,v) => { } } +export let mecanismSelection = (recurse,k,v) => { + if (v.composantes) { //mécanisme de composantes. Voir known-mecanisms.md/composantes + return decompose(recurse,k,v) + } + + let dataSourceName = v['données'] + let dataSearchField = v['dans'] + let dataTargetName = v['renvoie'] + let explanation = recurse(v['cherche']) + + let evaluate = (situationGate, parsedRules, node) => { + let collectMissing = node => collectNodeMissing(node.explanation), + explanation = evaluateNode(situationGate, parsedRules, node.explanation), + dataSource = findRuleByName(parsedRules, dataSourceName), + data = dataSource ? dataSource['data'] : null, + dataKey = explanation.nodeValue, + dataItems = (data && dataKey && dataSearchField) ? R.filter(item => item[dataSearchField] == dataKey, data) : null, + dataItemValues = dataItems ? R.values(dataItems) : null, + // TODO - over-specific! transform the JSON instead + dataItemSubValues = dataItemValues && dataItemValues[0][dataTargetName] ? dataItemValues[0][dataTargetName]["taux"] : null, + sortedSubValues = dataItemSubValues ? R.sortBy(pair => pair[0], R.toPairs(dataItemSubValues)) : null, + nodeValue = sortedSubValues ? Number.parseFloat(R.last(sortedSubValues)[1]) : null + return rewriteNode(node,nodeValue,explanation,collectMissing) + } + + let jsx = (nodeValue, explanation) => + {makeJsx(explanation)} + : makeJsx(explanation) + } + /> + + return { + evaluate, + explanation, + jsx + } +} + export let mecanismError = (recurse,k,v) => { throw "Le mécanisme '"+k+"' est inconnu !"+v } diff --git a/source/engine/rules.js b/source/engine/rules.js index a3bfdb8e3..04858bf36 100644 --- a/source/engine/rules.js +++ b/source/engine/rules.js @@ -68,7 +68,7 @@ export let disambiguateRuleReference = (allRules, {ns, name}, partialName) => { } // On enrichit la base de règles avec des propriétés dérivées de celles du YAML -export let rules = rawRules.map(enrichRule, {taux_versement_transport}) +export let rules = rawRules.map(rule => enrichRule(rule, {taux_versement_transport})) /**************************************** diff --git a/source/engine/traverse.js b/source/engine/traverse.js index ec595e73a..88762e651 100644 --- a/source/engine/traverse.js +++ b/source/engine/traverse.js @@ -8,7 +8,8 @@ import Grammar from './grammar.ne' import {Node, Leaf} from './traverse-common-jsx' import { mecanismOneOf,mecanismAllOf,mecanismNumericalSwitch,mecanismSum,mecanismProduct, - mecanismScale,mecanismMax,mecanismMin, mecanismError, mecanismComplement + mecanismScale,mecanismMax,mecanismMin, mecanismError, mecanismComplement, + mecanismSelection } from "./mecanisms" import {evaluateNode, rewriteNode, collectNodeMissing, makeJsx} from './evaluation' @@ -330,45 +331,6 @@ let treat = (rules, rule) => rawNode => { {...parsedNode, evaluate: defaultEvaluate} } -let mecanismSelection = (recurse,k,v) => { - let dataSourceName = v['données'] - let dataSearchField = v['dans'] - let dataTargetName = v['renvoie'] - let explanation = recurse(v['cherche']) - - let evaluate = (situationGate, parsedRules, node) => { - let collectMissing = node => collectNodeMissing(node.explanation), - explanation = evaluateNode(situationGate, parsedRules, node.explanation), - dataSource = findRuleByName(parsedRules, dataSourceName), - data = dataSource ? dataSource['data'] : null, - dataKey = explanation.nodeValue, - dataItems = (data && dataKey && dataSearchField) ? R.filter(item => item[dataSearchField] == dataKey, data) : null, - dataItemValues = dataItems ? R.values(dataItems) : null, - // TODO - over-specific! transform the JSON instead - dataItemSubValues = dataItemValues ? dataItemValues[0][dataTargetName]["taux"] : null, - sortedSubValues = dataItemSubValues ? R.sortBy(pair => pair[0], R.toPairs(dataItemSubValues)) : null, - nodeValue = sortedSubValues ? Number.parseFloat(R.last(sortedSubValues)[1]) : null - return rewriteNode(node,nodeValue,explanation,collectMissing) - } - - let jsx = (nodeValue, explanation) => - {makeJsx(explanation)} - : makeJsx(explanation) - } - /> - - return { - evaluate, - explanation, - jsx - } -} - //TODO c'est moche : export let computeRuleValue = (formuleValue, condValue) => condValue === undefined