-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚙️ ajoute les variables temporelles pour la variation
- Loading branch information
Johan Girod
committed
Feb 28, 2020
1 parent
71d04cb
commit e06db01
Showing
6 changed files
with
221 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { typeWarning } from 'Engine/error' | ||
import { defaultNode, evaluateNode } from 'Engine/evaluation' | ||
import Variations from 'Engine/mecanismViews/Variations' | ||
import { convertNodeToUnit } from 'Engine/nodeUnits' | ||
import { | ||
liftTemporal2, | ||
mapTemporal, | ||
pureTemporal, | ||
sometime, | ||
temporalAverage | ||
} from 'Engine/period' | ||
import { inferUnit } from 'Engine/units' | ||
import { and, dissoc, not, or } from 'ramda' | ||
import { mergeAllMissing } from './../evaluation' | ||
|
||
/* @devariate = true => This function will produce variations of a same mecanism (e.g. product) that share some common properties */ | ||
export default function parse(recurse, k, v, devariate) { | ||
let explanation = devariate | ||
? devariateExplanation(recurse, k, v) | ||
: v.map(({ si, alors, sinon }) => | ||
sinon !== undefined | ||
? { consequence: recurse(sinon), condition: defaultNode(true) } | ||
: { consequence: recurse(alors), condition: recurse(si) } | ||
) | ||
|
||
// TODO - find an appropriate representation | ||
return { | ||
explanation, | ||
evaluate, | ||
jsx: Variations, | ||
category: 'mecanism', | ||
name: 'variations', | ||
type: 'numeric', | ||
unit: inferUnit( | ||
'+', | ||
explanation.map(r => r.consequence.unit) | ||
) | ||
} | ||
} | ||
|
||
export let devariateExplanation = (recurse, mecanismKey, v) => { | ||
let fixedProps = dissoc('variations')(v), | ||
explanation = v.variations.map(({ si, alors, sinon }) => ({ | ||
consequence: recurse({ | ||
[mecanismKey]: { | ||
...fixedProps, | ||
...(sinon || alors) | ||
} | ||
}), | ||
condition: sinon ? defaultNode(true) : recurse(si) | ||
})) | ||
|
||
return explanation | ||
} | ||
|
||
function evaluate( | ||
cache, | ||
situationGate, | ||
parsedRules, | ||
node: ReturnType<typeof parse> | ||
) { | ||
const evaluate = evaluateNode.bind(null, cache, situationGate, parsedRules) | ||
|
||
const [temporalValue, explanation, unit] = node.explanation.reduce( | ||
( | ||
[evaluation, explanations, unit, previousConditions], | ||
{ condition, consequence }, | ||
i: number | ||
) => { | ||
const previousConditionsAlwaysTrue = !sometime( | ||
value => value !== true, | ||
previousConditions | ||
) | ||
if (previousConditionsAlwaysTrue) { | ||
return [ | ||
evaluation, | ||
[...explanations, { condition, consequence }], | ||
unit, | ||
previousConditions | ||
] | ||
} | ||
const evaluatedCondition = evaluate(condition) | ||
const currentCondition = liftTemporal2( | ||
and, | ||
mapTemporal(not, previousConditions), | ||
evaluatedCondition.temporalValue ?? | ||
pureTemporal(evaluatedCondition.nodeValue) | ||
) | ||
const currentConditionAlwaysFalse = !sometime(Boolean, currentCondition) | ||
if (currentConditionAlwaysFalse) { | ||
return [ | ||
evaluation, | ||
[...explanations, { condition: evaluatedCondition, consequence }], | ||
unit, | ||
previousConditions | ||
] | ||
} | ||
let evaluatedConsequence = evaluate(consequence) | ||
|
||
try { | ||
evaluatedConsequence = convertNodeToUnit(unit, evaluatedConsequence) | ||
} catch (e) { | ||
return typeWarning( | ||
cache._meta.contexRule, | ||
`L'unité de la branche n° ${i} du mécanisme 'variations' n'est pas compatible avec celle d'une branche précédente`, | ||
e | ||
) | ||
} | ||
const currentValue = liftTemporal2( | ||
(cond, value) => cond && value, | ||
currentCondition, | ||
evaluatedConsequence.temporalValue ?? | ||
pureTemporal(evaluatedConsequence.nodeValue) | ||
) | ||
|
||
return [ | ||
liftTemporal2(or, evaluation, currentValue), | ||
[ | ||
...explanations, | ||
{ | ||
condition: evaluatedCondition, | ||
consequence: evaluatedConsequence | ||
} | ||
], | ||
unit || evaluatedConsequence.unit, | ||
liftTemporal2(or, previousConditions, currentCondition) | ||
] | ||
}, | ||
[pureTemporal(false), [], node.unit, pureTemporal(false)] | ||
) | ||
const nodeValue = temporalAverage(temporalValue, unit) | ||
const missingVariables = mergeAllMissing( | ||
explanation.reduce( | ||
(values, { condition, consequence }) => [ | ||
...values, | ||
condition, | ||
consequence | ||
], | ||
[] | ||
) | ||
) | ||
return { | ||
...node, | ||
nodeValue, | ||
unit, | ||
explanation, | ||
missingVariables, | ||
...(temporalValue.length > 1 && { temporalValue }) | ||
// TODO | ||
// missingVariables | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.