2017-03-14 10:42:44 +00:00
import React from 'react'
2017-10-24 17:33:55 +00:00
import {
findRuleByDottedName ,
disambiguateRuleReference ,
2018-02-21 17:40:45 +00:00
findRuleByName ,
findRule
2017-10-24 17:33:55 +00:00
} from './rules'
import { evaluateVariable } from './variables'
2018-01-08 15:07:26 +00:00
import {
contains ,
propEq ,
curry ,
chain ,
cond ,
evolve ,
equals ,
concat ,
path ,
divide ,
multiply ,
map ,
intersection ,
keys ,
is ,
propOr ,
always ,
head ,
T ,
gte ,
lte ,
lt ,
gt ,
add ,
subtract
} from 'ramda'
2017-03-03 09:43:43 +00:00
import knownMecanisms from './known-mecanisms.yaml'
2017-03-07 17:25:25 +00:00
import { Parser } from 'nearley'
import Grammar from './grammar.ne'
2017-10-24 17:33:55 +00:00
import { Node , Leaf } from './mecanismViews/common'
2017-07-30 21:50:36 +00:00
import {
2017-10-24 17:33:55 +00:00
mecanismOneOf ,
mecanismAllOf ,
mecanismNumericalSwitch ,
mecanismSum ,
mecanismProduct ,
mecanismScale ,
mecanismMax ,
mecanismMin ,
mecanismError ,
mecanismComplement ,
2017-11-20 12:16:18 +00:00
mecanismSelection ,
2018-02-14 16:50:47 +00:00
mecanismInversion ,
mecanismReduction
2017-10-24 17:33:55 +00:00
} from './mecanisms'
import {
evaluateNode ,
rewriteNode ,
collectNodeMissing ,
makeJsx
} from './evaluation'
import {
anyNull ,
val ,
undefOrTrue ,
applyOrEmpty
} from './traverse-common-functions'
2017-03-07 17:25:25 +00:00
2017-03-08 16:47:12 +00:00
let nearley = ( ) => new Parser ( Grammar . ParserRules , Grammar . ParserStart )
2017-01-09 17:17:51 +00:00
2017-03-06 16:35:30 +00:00
/ *
Dans ce fichier , les règles YAML sont parsées .
Elles expriment un langage orienté expression , les expressions étant
- préfixes quand elles sont des 'mécanismes' ( des mot - clefs représentant des calculs courants dans la loi )
- infixes pour les feuilles : des tests d 'égalité, d' inclusion , des comparaisons sur des variables ou tout simplement la variable elle - même , ou une opération effectuée sur la variable
* /
2017-02-22 16:55:36 +00:00
/ *
- > Notre règle est naturellement un AST ( car notation préfixe dans le YAML )
- > préliminaire : les expression infixes devront être parsées ,
par exemple ainsi : https : //github.com/Engelberg/instaparse#transforming-the-tree
- > Notre règle entière est un AST , qu ' il faut maintenant traiter :
- faire le calcul ( déterminer les valeurs de chaque noeud )
- trouver les branches complètes pour déterminer les autres branches courtcircuitées
- ex . rule . formule est courtcircuitée si rule . non applicable est vrai
2017-04-24 18:03:38 +00:00
- les feuilles de 'une de ces conditions' sont courtcircuitées si l 'une d' elle est vraie
2017-02-22 16:55:36 +00:00
- les feuilles de "toutes ces conditions" sont courtcircuitées si l 'une d' elle est fausse
- ...
( - bonus : utiliser ces informations pour l ' ordre de priorité des variables inconnues )
- si une branche est incomplète et qu 'elle est de type numérique, déterminer les bornes si c' est possible .
Ex . - pour une multiplication , si l 'assiette est connue mais que l ' applicabilité est inconnue ,
les bornes seront [ 0 , multiplication . value = assiette * taux ]
- si taux = effectif entreprise >= 20 ? 1 % : 2 % et que l ' applicabilité est connue ,
bornes = [ assiette * 1 % , assiette * 2 % ]
- transformer l ' arbre en JSX pour afficher le calcul * et son état en prenant en compte les variables renseignées et calculées * de façon sympathique dans un butineur Web tel que Mozilla Firefox .
- surement plein d ' autres applications ...
* /
2017-11-28 22:26:04 +00:00
// TODO - this is becoming overly specific
2017-07-17 17:39:49 +00:00
let fillFilteredVariableNode = ( rules , rule ) => ( filter , parseResult ) => {
2018-01-03 15:54:19 +00:00
let evaluateFiltered = originalEval => (
cache ,
situation ,
parsedRules ,
node
) => {
2017-10-24 17:33:55 +00:00
let newSituation = name => ( name == 'sys.filter' ? filter : situation ( name ) )
2017-12-12 19:10:22 +00:00
return originalEval ( cache , newSituation , parsedRules , node )
2017-07-16 21:58:06 +00:00
}
2017-11-28 22:26:04 +00:00
let node = fillVariableNode ( rules , rule , filter ) ( parseResult ) ,
// Decorate node with who's paying
2018-01-03 15:54:19 +00:00
cotisation = { ... node . cotisation , 'dû par' : filter }
2017-11-28 22:26:04 +00:00
2017-07-16 21:58:06 +00:00
return {
... node ,
2017-11-28 22:26:04 +00:00
cotisation ,
2017-07-16 21:58:06 +00:00
evaluate : evaluateFiltered ( node . evaluate )
}
}
2017-11-23 15:19:24 +00:00
let fillVariableNode = ( rules , rule , filter ) => parseResult => {
2017-12-12 19:10:22 +00:00
let evaluate = ( cache , situation , parsedRules , node ) => {
2017-07-09 14:39:31 +00:00
let dottedName = node . dottedName ,
2017-08-22 08:53:38 +00:00
// On va vérifier dans le cache courant, dict, si la variable n'a pas été déjà évaluée
// En effet, l'évaluation dans le cas d'une variable qui a une formule, est coûteuse !
2017-10-24 17:33:55 +00:00
cacheName = dottedName + ( filter ? '.' + filter : '' ) ,
2017-12-12 19:10:22 +00:00
cached = cache [ cacheName ] ,
2017-08-22 08:53:38 +00:00
// make parsedRules a dict object, that also serves as a cache of evaluation ?
2017-10-24 17:33:55 +00:00
variable = cached
? cached
: findRuleByDottedName ( parsedRules , dottedName ) ,
2017-11-20 12:16:18 +00:00
variableIsCalculable = variable . formule != null ,
2017-10-24 17:33:55 +00:00
parsedRule =
variableIsCalculable &&
2018-01-03 15:54:19 +00:00
( cached
? cached
: evaluateNode ( cache , situation , parsedRules , variable ) ) ,
2017-08-22 08:53:38 +00:00
// evaluateVariable renvoit la valeur déduite de la situation courante renseignée par l'utilisateur
2017-07-16 21:58:06 +00:00
situationValue = evaluateVariable ( situation , dottedName , variable ) ,
2017-10-24 17:33:55 +00:00
nodeValue =
situationValue != null
? situationValue // cette variable a été directement renseignée
2017-11-14 17:34:17 +00:00
: variableIsCalculable
? parsedRule . nodeValue // la valeur du calcul fait foi
: null , // elle restera donc nulle
2017-07-09 14:39:31 +00:00
explanation = parsedRule ,
2017-09-08 10:28:35 +00:00
missingVariables = variableIsCalculable ? [ ] : [ dottedName ]
2017-07-09 14:39:31 +00:00
2017-12-16 11:35:20 +00:00
let collectMissing = node => {
2018-01-03 15:54:19 +00:00
let missingName = cacheName + ':missing' ,
2017-12-16 11:35:20 +00:00
cached = cache [ missingName ]
if ( cached ) return cached
2018-01-03 15:54:19 +00:00
let result =
nodeValue != null // notamment si situationValue != null
? [ ]
: variableIsCalculable
? collectNodeMissing ( parsedRule )
: node . missingVariables
2017-12-16 11:35:20 +00:00
cache [ missingName ] = result
return result
}
2017-10-24 17:33:55 +00:00
2017-11-23 13:48:42 +00:00
if ( cached ) {
return cached
2018-01-03 15:54:19 +00:00
} else {
2017-12-12 19:10:22 +00:00
cache [ cacheName ] = {
2017-10-24 17:33:55 +00:00
... rewriteNode ( node , nodeValue , explanation , collectMissing ) ,
missingVariables
2017-07-09 14:39:31 +00:00
}
2017-12-12 19:10:22 +00:00
return cache [ cacheName ]
2017-11-23 13:48:42 +00:00
}
2017-07-09 14:39:31 +00:00
}
2017-10-24 17:33:55 +00:00
let { fragments } = parseResult ,
2017-03-08 16:49:22 +00:00
variablePartialName = fragments . join ( ' . ' ) ,
2017-07-09 14:39:31 +00:00
dottedName = disambiguateRuleReference ( rules , rule , variablePartialName )
2017-03-08 16:49:22 +00:00
2017-11-07 18:46:40 +00:00
let jsx = nodeValue => (
2018-02-13 17:51:53 +00:00
< Leaf
classes = "variable"
name = { fragments . join ( ' . ' ) }
dottedName = { dottedName }
value = { nodeValue }
/ >
2017-10-24 17:33:55 +00:00
)
2017-07-13 19:55:59 +00:00
2017-03-08 16:49:22 +00:00
return {
2017-07-09 14:39:31 +00:00
evaluate ,
2017-07-13 19:55:59 +00:00
jsx ,
2017-07-09 22:14:22 +00:00
name : variablePartialName ,
2017-03-08 16:49:22 +00:00
category : 'variable' ,
2017-07-13 19:55:59 +00:00
fragments ,
2017-05-09 15:14:13 +00:00
dottedName ,
2017-07-13 19:55:59 +00:00
type : 'boolean | numeric'
2017-03-08 16:49:22 +00:00
}
}
2017-05-09 13:39:42 +00:00
let buildNegatedVariable = variable => {
2017-12-12 19:10:22 +00:00
let evaluate = ( cache , situation , parsedRules , node ) => {
2018-01-03 15:54:19 +00:00
let explanation = evaluateNode (
cache ,
situation ,
parsedRules ,
node . explanation
) ,
2017-07-13 10:12:19 +00:00
nodeValue = explanation . nodeValue == null ? null : ! explanation . nodeValue
let collectMissing = node => collectNodeMissing ( node . explanation )
2017-10-24 17:33:55 +00:00
return rewriteNode ( node , nodeValue , explanation , collectMissing )
2017-07-13 10:12:19 +00:00
}
2017-10-24 17:33:55 +00:00
let jsx = ( nodeValue , explanation ) => (
2017-07-13 19:55:59 +00:00
< Node
2017-05-09 13:39:42 +00:00
classes = "inlineExpression negation"
2017-07-29 08:00:49 +00:00
value = { nodeValue }
2017-05-09 13:39:42 +00:00
child = {
< span className = "nodeContent" >
< span className = "operator" > ¬ < / s p a n >
2017-07-13 19:55:59 +00:00
{ makeJsx ( explanation ) }
2017-05-09 13:39:42 +00:00
< / s p a n >
}
/ >
2017-10-24 17:33:55 +00:00
)
2017-07-13 19:55:59 +00:00
return {
evaluate ,
jsx ,
category : 'mecanism' ,
name : 'négation' ,
type : 'boolean' ,
explanation : variable
2017-05-09 13:39:42 +00:00
}
}
2017-07-17 17:39:49 +00:00
let treat = ( rules , rule ) => rawNode => {
2017-06-27 21:09:03 +00:00
// inner functions
2017-10-24 17:33:55 +00:00
let reTreat = treat ( rules , rule ) ,
2017-06-27 21:09:03 +00:00
treatString = rawNode => {
2017-07-31 14:30:05 +00:00
/ * O n a a f f a i r e à u n s t r i n g , d o n c à u n e e x p r e s s i o n i n f i x e .
Elle sera traité avec le parser obtenu grâce à NearleyJs et notre grammaire ` grammar.ne ` .
2017-06-27 21:09:03 +00:00
On obtient un objet de type Variable ( avec potentiellement un 'modifier' , par exemple temporel ( TODO ) ) , CalcExpression ou Comparison .
Cet objet est alors rebalancé à 'treat' .
* /
2017-03-08 16:47:12 +00:00
2017-06-27 21:09:03 +00:00
let [ parseResult , ... additionnalResults ] = nearley ( ) . feed ( rawNode ) . results
if ( additionnalResults && additionnalResults . length > 0 )
2018-01-03 15:54:19 +00:00
throw "Attention ! L'expression <" +
2017-10-24 17:33:55 +00:00
rawNode +
'> ne peut être traitée de façon univoque'
if (
2018-01-08 15:07:26 +00:00
! contains ( parseResult . category ) ( [
2017-10-24 17:33:55 +00:00
'variable' ,
'calcExpression' ,
'filteredVariable' ,
'comparison' ,
'negatedVariable' ,
'percentage'
] )
)
2018-01-03 15:54:19 +00:00
throw "Attention ! Erreur de traitement de l'expression : " + rawNode
2017-06-27 21:09:03 +00:00
if ( parseResult . category == 'variable' )
2017-07-17 17:39:49 +00:00
return fillVariableNode ( rules , rule ) ( parseResult )
2017-06-29 10:03:01 +00:00
if ( parseResult . category == 'filteredVariable' ) {
2017-10-24 17:33:55 +00:00
return fillFilteredVariableNode ( rules , rule ) (
parseResult . filter ,
parseResult . variable
)
2017-06-29 10:03:01 +00:00
}
2017-06-27 21:09:03 +00:00
if ( parseResult . category == 'negatedVariable' )
return buildNegatedVariable (
2017-07-17 17:39:49 +00:00
fillVariableNode ( rules , rule ) ( parseResult . variable )
2017-06-27 21:09:03 +00:00
)
2017-09-19 11:17:43 +00:00
// We don't need to handle category == 'value' because YAML then returns it as
// numerical value, not a String: it goes to treatNumber
if ( parseResult . category == 'percentage' ) {
return {
nodeValue : parseResult . nodeValue ,
2017-10-24 17:33:55 +00:00
jsx : ( ) => < span className = "percentage" > { rawNode } < / s p a n >
2017-09-19 11:17:43 +00:00
}
}
2017-10-24 17:33:55 +00:00
if (
parseResult . category == 'calcExpression' ||
parseResult . category == 'comparison'
) {
2017-12-12 19:10:22 +00:00
let evaluate = ( cache , situation , parsedRules , node ) => {
2018-01-08 15:07:26 +00:00
let operatorFunction = {
'*' : multiply ,
'/' : divide ,
'+' : add ,
'-' : subtract ,
'<' : lt ,
'<=' : lte ,
'>' : gt ,
'>=' : gte ,
'=' : equals ,
'!=' : ( a , b ) => ! equals ( a , b )
2017-07-09 16:48:00 +00:00
} [ node . operator ] ,
2018-01-08 15:07:26 +00:00
explanation = map (
curry ( evaluateNode ) ( cache , situation , parsedRules ) ,
2017-10-24 17:33:55 +00:00
node . explanation
) ,
2017-07-11 14:00:10 +00:00
value1 = explanation [ 0 ] . nodeValue ,
value2 = explanation [ 1 ] . nodeValue ,
2017-10-24 17:33:55 +00:00
nodeValue =
value1 == null || value2 == null
? null
: operatorFunction ( value1 , value2 )
let collectMissing = node =>
2018-01-08 15:07:26 +00:00
chain ( collectNodeMissing , node . explanation )
2017-10-24 17:33:55 +00:00
return rewriteNode ( node , nodeValue , explanation , collectMissing )
2017-07-09 16:48:00 +00:00
}
2017-10-24 17:33:55 +00:00
let fillFiltered = parseResult =>
fillFilteredVariableNode ( rules , rule ) (
parseResult . filter ,
parseResult . variable
)
2017-07-17 17:39:49 +00:00
let fillVariable = fillVariableNode ( rules , rule ) ,
2017-06-27 21:09:03 +00:00
filledExplanation = parseResult . explanation . map (
2018-01-08 15:07:26 +00:00
cond ( [
[ propEq ( 'category' , 'variable' ) , fillVariable ] ,
[ propEq ( 'category' , 'filteredVariable' ) , fillFiltered ] ,
2017-10-24 17:33:55 +00:00
[
2018-01-08 15:07:26 +00:00
propEq ( 'category' , 'value' ) ,
2017-10-24 17:33:55 +00:00
node => ( {
2017-09-19 11:17:43 +00:00
nodeValue : node . nodeValue ,
2017-10-24 17:33:55 +00:00
jsx : nodeValue => < span className = "value" > { nodeValue } < / s p a n >
2017-07-09 16:48:00 +00:00
} )
2017-09-19 11:17:43 +00:00
] ,
2017-10-24 17:33:55 +00:00
[
2018-01-08 15:07:26 +00:00
propEq ( 'category' , 'percentage' ) ,
2017-10-24 17:33:55 +00:00
node => ( {
2017-09-19 11:17:43 +00:00
nodeValue : node . nodeValue ,
2017-10-24 17:33:55 +00:00
jsx : nodeValue => (
< span className = "value" > { nodeValue * 100 } % < / s p a n >
)
2017-09-19 11:17:43 +00:00
} )
2017-06-27 21:09:03 +00:00
]
] )
) ,
2017-07-09 16:48:00 +00:00
operator = parseResult . operator
2017-06-27 21:09:03 +00:00
2017-10-24 17:33:55 +00:00
let jsx = ( nodeValue , explanation ) => (
2017-07-13 20:45:54 +00:00
< Node
2017-10-24 17:33:55 +00:00
classes = { 'inlineExpression ' + parseResult . category }
2017-07-13 19:55:59 +00:00
value = { nodeValue }
2017-06-27 21:09:03 +00:00
child = {
< span className = "nodeContent" >
2017-07-13 19:55:59 +00:00
{ makeJsx ( explanation [ 0 ] ) }
2017-06-27 21:09:03 +00:00
< span className = "operator" > { parseResult . operator } < / s p a n >
2017-07-13 19:55:59 +00:00
{ makeJsx ( explanation [ 1 ] ) }
2017-06-27 21:09:03 +00:00
< / s p a n >
}
/ >
2017-10-24 17:33:55 +00:00
)
2017-07-13 19:55:59 +00:00
return {
evaluate ,
jsx ,
operator ,
text : rawNode ,
2017-07-28 09:30:23 +00:00
category : parseResult . category ,
2017-10-24 17:33:55 +00:00
type :
parseResult . category == 'calcExpression' ? 'numeric' : 'boolean' ,
2017-07-13 19:55:59 +00:00
explanation : filledExplanation
2017-06-27 21:09:03 +00:00
}
}
} ,
treatNumber = rawNode => {
2017-03-09 15:42:52 +00:00
return {
2017-10-24 17:33:55 +00:00
text : '' + rawNode ,
2017-06-27 21:09:03 +00:00
category : 'number' ,
nodeValue : rawNode ,
2017-03-09 15:42:52 +00:00
type : 'numeric' ,
2017-10-24 17:33:55 +00:00
jsx : < span className = "number" > { rawNode } < / s p a n >
2017-03-09 15:42:52 +00:00
}
2017-06-27 21:09:03 +00:00
} ,
treatOther = rawNode => {
console . log ( ) // eslint-disable-line no-console
2018-02-15 17:12:04 +00:00
throw new Error (
'Cette donnée : ' + rawNode + ' doit être un Number, String ou Object'
)
2017-06-27 21:09:03 +00:00
} ,
2017-06-27 23:02:36 +00:00
treatObject = rawNode => {
2018-01-08 15:07:26 +00:00
let mecanisms = intersection ( keys ( rawNode ) , keys ( knownMecanisms ) )
2017-06-27 23:02:36 +00:00
if ( mecanisms . length != 1 ) {
2017-11-07 18:46:40 +00:00
console . log (
// eslint-disable-line no-console
2017-10-24 17:33:55 +00:00
'Erreur : On ne devrait reconnaître que un et un seul mécanisme dans cet objet' ,
mecanisms ,
rawNode
)
2018-02-15 17:12:04 +00:00
throw new Error ( 'OUPS !' )
2017-06-27 21:09:03 +00:00
}
2017-04-24 18:03:38 +00:00
2018-01-08 15:07:26 +00:00
let k = head ( mecanisms ) ,
2017-06-27 23:02:36 +00:00
v = rawNode [ k ]
2017-06-28 07:06:52 +00:00
let dispatch = {
2017-10-24 17:33:55 +00:00
'une de ces conditions' : mecanismOneOf ,
'toutes ces conditions' : mecanismAllOf ,
'aiguillage numérique' : mecanismNumericalSwitch ,
somme : mecanismSum ,
multiplication : mecanismProduct ,
barème : mecanismScale ,
'le maximum de' : mecanismMax ,
'le minimum de' : mecanismMin ,
complément : mecanismComplement ,
sélection : mecanismSelection ,
2018-01-08 15:07:26 +00:00
'une possibilité' : always ( {
2017-10-24 17:33:55 +00:00
'une possibilité' : 'oui' ,
collectMissing : ( ) => [ rule . dottedName ]
2017-11-20 12:16:18 +00:00
} ) ,
2018-02-14 16:50:47 +00:00
inversion : mecanismInversion ( rule . dottedName ) ,
allègement : mecanismReduction
2017-06-28 07:06:52 +00:00
} ,
2018-01-08 15:07:26 +00:00
action = propOr ( mecanismError , k , dispatch )
2017-04-06 16:34:00 +00:00
2017-10-24 17:33:55 +00:00
return action ( reTreat , k , v )
2017-03-03 09:43:43 +00:00
}
2017-02-22 16:55:36 +00:00
2018-01-08 15:07:26 +00:00
let onNodeType = cond ( [
[ is ( String ) , treatString ] ,
[ is ( Number ) , treatNumber ] ,
[ is ( Object ) , treatObject ] ,
[ T , treatOther ]
2017-06-27 21:09:03 +00:00
] )
2017-07-09 16:48:00 +00:00
2017-12-12 19:10:22 +00:00
let defaultEvaluate = ( cache , situationGate , parsedRules , node ) => node
2017-07-09 16:48:00 +00:00
let parsedNode = onNodeType ( rawNode )
2017-10-24 17:33:55 +00:00
return parsedNode . evaluate
? parsedNode
: { ... parsedNode , evaluate : defaultEvaluate }
2017-02-22 16:55:36 +00:00
}
2017-10-24 13:57:46 +00:00
export let computeRuleValue = ( formuleValue , isApplicable ) =>
isApplicable === true
2017-10-03 12:35:18 +00:00
? formuleValue
2017-10-24 17:33:55 +00:00
: isApplicable === false ? 0 : formuleValue == 0 ? 0 : null
2017-05-07 17:45:44 +00:00
2017-07-17 17:39:49 +00:00
export let treatRuleRoot = ( rules , rule ) => {
2017-11-28 11:45:06 +00:00
/ *
La fonction treatRuleRoot va descendre l 'arbre de la règle `rule` et produire un AST, un objet contenant d' autres objets contenant d ' autres objets ...
Aujourd ' hui , une règle peut avoir ( comme propriétés à parser ) ` non applicable si ` et ` formule ` ,
qui ont elles - mêmes des propriétés de type mécanisme ( ex . barème ) ou des expressions en ligne ( ex . maVariable + 3 ) .
Ces mécanismes où variables sont descendues à leur tour grâce à ` treat() ` .
Lors de ce traitement , des fonctions 'evaluate' , ` collectMissingVariables ` et ` jsx ` sont attachés aux objets de l ' AST
* /
2017-12-12 19:10:22 +00:00
let evaluate = ( cache , situationGate , parsedRules , r ) => {
2018-01-08 15:07:26 +00:00
let evolveRule = curry ( evaluateNode ) ( cache , situationGate , parsedRules ) ,
evaluated = evolve (
2017-10-24 17:33:55 +00:00
{
formule : evolveRule ,
'non applicable si' : evolveRule ,
'applicable si' : evolveRule
} ,
r
) ,
2017-10-24 13:57:46 +00:00
formuleValue = val ( evaluated [ 'formule' ] ) ,
2017-10-24 17:33:55 +00:00
isApplicable = do {
let e = evaluated
2017-10-24 13:57:46 +00:00
val ( e [ 'non applicable si' ] ) === true
? false
: val ( e [ 'applicable si' ] ) === false
? false
2017-10-24 17:33:55 +00:00
: anyNull ( [ e [ 'non applicable si' ] , e [ 'applicable si' ] ] )
2017-10-24 13:57:46 +00:00
? null
2017-10-24 17:33:55 +00:00
: ! val ( e [ 'non applicable si' ] ) &&
undefOrTrue ( val ( e [ 'applicable si' ] ) )
2017-10-24 13:57:46 +00:00
} ,
nodeValue = computeRuleValue ( formuleValue , isApplicable )
2017-10-24 17:33:55 +00:00
return { ... evaluated , nodeValue , isApplicable }
2017-07-11 14:00:10 +00:00
}
2017-10-24 17:47:16 +00:00
2017-11-07 18:46:40 +00:00
let collectMissing = rule => {
2017-11-07 10:07:02 +00:00
let {
formule ,
isApplicable ,
'non applicable si' : notApplicable ,
2017-11-20 12:16:18 +00:00
'applicable si' : applicable
2017-11-07 10:07:02 +00:00
} = rule
2017-11-07 18:46:40 +00:00
let condMissing =
2017-10-24 17:47:16 +00:00
val ( notApplicable ) === true
? [ ]
: val ( applicable ) === false
? [ ]
: [
2018-01-03 15:54:19 +00:00
... applyOrEmpty ( collectNodeMissing ) ( notApplicable ) ,
... applyOrEmpty ( collectNodeMissing ) ( applicable )
] ,
2017-10-24 13:57:46 +00:00
collectInFormule = isApplicable !== false ,
2017-10-24 17:33:55 +00:00
formMissing = applyOrEmpty ( ( ) =>
applyOrEmpty ( collectNodeMissing ) ( formule )
2017-10-24 13:57:46 +00:00
) ( collectInFormule )
2017-10-24 17:47:16 +00:00
2018-01-08 15:07:26 +00:00
return concat ( condMissing , formMissing )
2017-07-09 14:39:31 +00:00
}
2018-01-08 15:07:26 +00:00
let parsedRoot = evolve ( {
2017-10-24 13:57:46 +00:00
// Voilà les attributs d'une règle qui sont aujourd'hui dynamiques, donc à traiter
// Les métadonnées d'une règle n'en font pas aujourd'hui partie
2017-02-22 16:55:36 +00:00
2017-10-24 13:57:46 +00:00
// condition d'applicabilité de la règle
'non applicable si' : evolveCond ( 'non applicable si' , rule , rules ) ,
'applicable si' : evolveCond ( 'applicable si' , rule , rules ) ,
2017-10-24 17:33:55 +00:00
formule : value => {
2017-12-12 19:10:22 +00:00
let evaluate = ( cache , situationGate , parsedRules , node ) => {
2017-07-13 10:25:46 +00:00
let collectMissing = node => collectNodeMissing ( node . explanation )
2018-01-03 15:54:19 +00:00
let explanation = evaluateNode (
cache ,
2017-10-24 17:33:55 +00:00
situationGate ,
parsedRules ,
node . explanation
) ,
2017-07-11 14:00:10 +00:00
nodeValue = explanation . nodeValue
2017-10-24 17:33:55 +00:00
return rewriteNode ( node , nodeValue , explanation , collectMissing )
2017-07-09 16:48:00 +00:00
}
2017-07-17 17:39:49 +00:00
let child = treat ( rules , rule ) ( value )
2017-07-11 14:00:10 +00:00
2017-10-24 17:33:55 +00:00
let jsx = ( nodeValue , explanation ) => makeJsx ( explanation )
2017-07-13 19:55:59 +00:00
2017-03-16 18:30:30 +00:00
return {
2017-07-09 16:48:00 +00:00
evaluate ,
2017-07-13 19:55:59 +00:00
jsx ,
2017-03-16 18:30:30 +00:00
category : 'ruleProp' ,
rulePropType : 'formula' ,
name : 'formule' ,
type : 'numeric' ,
2017-07-13 19:55:59 +00:00
explanation : child
2017-03-16 18:30:30 +00:00
}
2017-03-01 10:21:53 +00:00
}
2017-07-09 14:39:31 +00:00
} ) ( rule )
2017-03-20 11:17:49 +00:00
2017-07-09 14:39:31 +00:00
return {
2017-07-31 14:30:05 +00:00
// Pas de propriété explanation et jsx ici car on est parti du (mauvais) principe que 'non applicable si' et 'formule' sont particuliers, alors qu'ils pourraient être rangé avec les autres mécanismes
2017-07-09 14:39:31 +00:00
... parsedRoot ,
2017-07-11 14:00:10 +00:00
evaluate ,
2017-11-22 15:34:23 +00:00
collectMissing ,
parsed : true
2017-03-16 18:30:30 +00:00
}
2017-07-09 14:39:31 +00:00
}
2017-02-22 16:55:36 +00:00
2017-10-24 13:57:46 +00:00
let evolveCond = ( name , rule , rules ) => value => {
2017-12-12 19:10:22 +00:00
let evaluate = ( cache , situationGate , parsedRules , node ) => {
2017-10-24 13:57:46 +00:00
let collectMissing = node => collectNodeMissing ( node . explanation )
2018-01-03 15:54:19 +00:00
let explanation = evaluateNode (
cache ,
2017-10-24 17:33:55 +00:00
situationGate ,
parsedRules ,
node . explanation
) ,
2017-10-24 13:57:46 +00:00
nodeValue = explanation . nodeValue
2017-10-24 17:33:55 +00:00
return rewriteNode ( node , nodeValue , explanation , collectMissing )
2017-10-24 13:57:46 +00:00
}
let child = treat ( rules , rule ) ( value )
let jsx = ( nodeValue , explanation ) => (
< Node
classes = "ruleProp mecanism cond"
name = { name }
value = { nodeValue }
child = {
explanation . category === 'variable' ? (
< div className = "node" > { makeJsx ( explanation ) } < / d i v >
) : (
makeJsx ( explanation )
)
}
/ >
)
return {
evaluate ,
jsx ,
category : 'ruleProp' ,
rulePropType : 'cond' ,
name ,
type : 'boolean' ,
explanation : child
}
}
2017-11-07 18:46:40 +00:00
export let getTargets = ( target , rules ) => {
2018-01-08 15:07:26 +00:00
let multiSimulation = path ( [ 'simulateur' , 'objectifs' ] ) ( target )
2017-11-07 18:46:40 +00:00
let targets = multiSimulation
? // On a un simulateur qui définit une liste d'objectifs
2018-01-03 15:54:19 +00:00
multiSimulation
. map ( n => disambiguateRuleReference ( rules , target , n ) )
. map ( n => findRuleByDottedName ( rules , n ) )
2017-11-07 18:46:40 +00:00
: // Sinon on est dans le cas d'une simple variable d'objectif
2018-01-03 15:54:19 +00:00
[ target ]
2017-11-07 18:46:40 +00:00
return targets
2017-07-09 22:14:22 +00:00
}
2017-11-28 11:45:06 +00:00
export let parseAll = flatRules => {
let treatOne = rule => treatRuleRoot ( flatRules , rule )
2018-01-08 15:07:26 +00:00
return map ( treatOne , flatRules )
2017-11-28 11:45:06 +00:00
}
export let analyseMany = ( parsedRules , targetNames ) => situationGate => {
2018-01-03 15:54:19 +00:00
// TODO: we should really make use of namespaces at this level, in particular
// setRule in Rule.js needs to get smarter and pass dottedName
2017-12-12 19:10:22 +00:00
let cache = { }
2018-02-21 17:40:45 +00:00
let parsedTargets = targetNames . map ( t => findRule ( parsedRules , t ) ) ,
2018-01-08 15:07:26 +00:00
targets = chain ( pt => getTargets ( pt , parsedRules ) , parsedTargets ) . map ( t =>
2017-12-12 19:10:22 +00:00
evaluateNode ( cache , situationGate , parsedRules , t )
2017-11-07 18:46:40 +00:00
)
2017-01-26 12:19:04 +00:00
2017-11-28 15:00:43 +00:00
// Don't use 'dict' for anything else than ResultsGrid
2018-01-03 15:54:19 +00:00
return { targets , cache }
2017-11-28 11:45:06 +00:00
}
export let analyse = ( parsedRules , target ) => {
return analyseMany ( parsedRules , [ target ] )
2017-07-09 14:39:31 +00:00
}