From 9b208e967a446779b2a92d0ed97752264a8b445e Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Wed, 9 Dec 2020 17:07:10 +0100 Subject: [PATCH] =?UTF-8?q?:bug:=20R=C3=A9impl=C3=A9mente=20une=20version?= =?UTF-8?q?=20hacky=20de=20isApplicable=20:=20isNotApplicable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit En attendant d'avoir un type propre pour la valeur Non Applicable --- .../pages/Gérer/DemandeMobilite/index.tsx | 1 + .../pages/Simulateurs/ArtisteAuteur.tsx | 10 ++--- publicodes/source/index.ts | 41 ++++++++++++++++++- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/DemandeMobilite/index.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/DemandeMobilite/index.tsx index f99a697c2..29699e70d 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/DemandeMobilite/index.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Gérer/DemandeMobilite/index.tsx @@ -97,6 +97,7 @@ const useFields = ( .map((name) => evaluateRule(engine, name)) .filter( (node) => + node.isNotApplicable !== true && // TODO change this when not applicable value can be differenciated from false value (equals(node.missingVariables, { [node.dottedName]: 1 }) || node.dottedName in situation || diff --git a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx index 2e53f107d..eb85f29d7 100644 --- a/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx +++ b/mon-entreprise/source/sites/mon-entreprise.fr/pages/Simulateurs/ArtisteAuteur.tsx @@ -43,7 +43,7 @@ export default function ArtisteAuteur() { - + @@ -65,7 +65,7 @@ function SimpleField({ dottedName }: SimpleFieldProps) { const dispatch = useDispatch() const engine = useEngine() const situation = useSelector(situationSelector) - const rule = engine.evaluateNode(engine.getParsedRules()[dottedName]) + const rule = evaluateRule(engine, dottedName) const initialRender = useContext(InitialRenderContext) if ( !(dottedName in situation) && @@ -81,10 +81,8 @@ function SimpleField({ dottedName }: SimpleFieldProps) {
diff --git a/publicodes/source/index.ts b/publicodes/source/index.ts index 8796de1a0..2406e60ad 100644 --- a/publicodes/source/index.ts +++ b/publicodes/source/index.ts @@ -12,6 +12,7 @@ import { } from './replacement' import { Rule, RuleNode } from './rule' import * as utils from './ruleUtils' +import { reduceAST, transformAST } from './AST' const emptyCache = () => ({ _meta: { contextRule: [] }, @@ -36,7 +37,7 @@ export type EvaluationOptions = Partial<{ unit: string }> -export { reduceAST, transformAST } from './AST' +export { reduceAST, transformAST } from './AST/index' export * as cyclesLib from './AST/graph' export { Evaluation, Unit } from './AST/types' export * from './components' @@ -181,7 +182,43 @@ export function evaluateRule( const rule = engine.getParsedRules()[dottedName] as RuleNode & { dottedName: DottedName } + + // HACK while waiting for applicability to have its own type + const isNotApplicable = reduceAST( + function (isNotApplicable, node, fn) { + if (isNotApplicable) return isNotApplicable + if (!('nodeValue' in node)) { + return isNotApplicable + } + if (node.nodeKind === 'variations') { + return node.explanation.some( + ({ consequence }) => + fn(consequence) || + ((consequence as any).nodeValue === false && + (consequence as any).dottedName !== dottedName) + ) + } + if (node.nodeKind === 'reference' && node.dottedName === dottedName) { + return fn(engine.evaluateNode(rule)) + } + if (node.nodeKind === 'applicable si') { + return (node.explanation.condition as any).nodeValue === false + } + if (node.nodeKind === 'non applicable si') { + return ( + (node.explanation.condition as any).nodeValue !== false && + (node.explanation.condition as any).nodeValue !== null + ) + } + }, + false, + evaluation + ) + if (dottedName === 'artiste-auteur . revenus . BNC . micro-bnc') { + console.log(dottedName, isNotApplicable) + } return { + isNotApplicable, ...rule.rawNode, ...rule, ...evaluation, @@ -195,6 +232,6 @@ export type EvaluatedRule = EvaluatedNode & }) & (ASTNode & { nodeKind: 'rule' - })['rawNode'] & { dottedName: Name }, + })['rawNode'] & { dottedName: Name; isApplicable: boolean | null }, 'nodeKind' >