From f75f70314a3d34c5c93ab38da52bdd1cdd849631 Mon Sep 17 00:00:00 2001 From: Mael Date: Wed, 12 Jun 2019 11:46:36 +0200 Subject: [PATCH] =?UTF-8?q?:memo:=20Impl=C3=A9mentation=20des=20unit=C3=A9?= =?UTF-8?q?s=20via=20une=20nouvelle=20passe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/mecanisms.js | 6 ++---- source/engine/traverse.js | 17 +++++++++++++++++ source/engine/treat.js | 10 +--------- source/engine/treatVariable.js | 9 +++------ 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/source/engine/mecanisms.js b/source/engine/mecanisms.js index 0708aad8c..8adf377a4 100644 --- a/source/engine/mecanisms.js +++ b/source/engine/mecanisms.js @@ -593,8 +593,7 @@ export let mecanismProduct = (recurse, k, v) => { explanation, category: 'mecanism', name: 'multiplication', - type: 'numeric', - unit: explanation.assiette.unit + type: 'numeric' } } @@ -771,8 +770,7 @@ export let mecanismMin = (recurse, k, v) => { explanation, type: 'numeric', category: 'mecanism', - name: 'le minimum de', - unit: explanation[0].unit + name: 'le minimum de' } } diff --git a/source/engine/traverse.js b/source/engine/traverse.js index ef85f1d06..289ddb6fb 100644 --- a/source/engine/traverse.js +++ b/source/engine/traverse.js @@ -326,8 +326,25 @@ export let getTargets = (target, rules) => { } export let parseAll = flatRules => { + /* First we parse each rule one by one. When a mechanism is encountered, it is recursively parsed. When a reference to a variable is encountered, a 'variable' node is created, we don't parse variables recursively. */ let treatOne = rule => treatRuleRoot(flatRules, rule) let parsed = map(treatOne, flatRules) + /* Then we need to infer units. Since only references to variables have been created, we need to wait for the latter map to complete before starting this job. Consider this example : + A = B * C + B = D / E + + C unité km + D unité € + E unité km + * + * When parsing A's formula, we don't know the unit of B, since only the final nodes have units (it would be too cumbersome to specify a unit to each variable), and B hasn't been parsed yet. + * + * */ + + + + + return parsed } diff --git a/source/engine/treat.js b/source/engine/treat.js index b85f852b2..bf53b2f4a 100644 --- a/source/engine/treat.js +++ b/source/engine/treat.js @@ -50,8 +50,6 @@ import { treatVariableTransforms } from './treatVariable' -import { inferUnit } from 'Engine/units' - export let nearley = () => new Parser(Grammar.ParserRules, Grammar.ParserStart) export let treatString = (rules, rule) => rawNode => { @@ -176,14 +174,8 @@ let mecanismOperation = (k, operatorFunction, symbol) => (recurse, k, v) => { explanation[0].missingVariables, explanation[1].missingVariables ) - let unit = inferUnit(k, explanation[0].unit, explanation[1].unit) - return rewriteNode( - { ...node, unit }, - nodeValue, - explanation, - missingVariables - ) + return rewriteNode(node, nodeValue, explanation, missingVariables) } let explanation = v.explanation.map(recurse) diff --git a/source/engine/treatVariable.js b/source/engine/treatVariable.js index 99320a1ea..7dace7117 100644 --- a/source/engine/treatVariable.js +++ b/source/engine/treatVariable.js @@ -11,9 +11,7 @@ import { getSituationValue } from './variables' export let treatVariable = (rules, rule, filter) => ({ fragments }) => { let variablePartialName = fragments.join(' . '), - dottedName = disambiguateRuleReference(rules, rule, variablePartialName), - variable = findRuleByDottedName(rules, dottedName), - unit = variable.unit + dottedName = disambiguateRuleReference(rules, rule, variablePartialName) let evaluate = (cache, situation, parsedRules, node) => { let dottedName = node.dottedName, @@ -39,7 +37,7 @@ export let treatVariable = (rules, rule, filter) => ({ fragments }) => { let cacheAndNode = (nodeValue, missingVariables) => { cache[cacheName] = rewriteNode( - { ...node, unit: explanation.unit }, + node, nodeValue, explanation, missingVariables @@ -91,8 +89,7 @@ export let treatVariable = (rules, rule, filter) => ({ fragments }) => { name: variablePartialName, category: 'variable', fragments, - dottedName, - unit + dottedName } }