📝 Implémentation des unités via une nouvelle passe
parent
6154419250
commit
f75f70314a
|
@ -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'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue