2018-01-08 15:07:26 +00:00
|
|
|
import { isEmpty, dropLast, last } from 'ramda'
|
2018-01-03 15:54:19 +00:00
|
|
|
import { splitName, joinName } from './rules'
|
2017-03-16 18:30:30 +00:00
|
|
|
|
2017-04-24 18:03:38 +00:00
|
|
|
let evaluateBottomUp = situationGate => startingFragments => {
|
2018-01-03 15:54:19 +00:00
|
|
|
let rec = (parentFragments, childFragments = []) =>
|
|
|
|
parentFragments.length == 0
|
|
|
|
? null
|
|
|
|
: do {
|
|
|
|
let query = joinName(parentFragments),
|
2018-01-08 15:07:26 +00:00
|
|
|
expectedResult = isEmpty(childFragments)
|
2018-01-03 15:54:19 +00:00
|
|
|
? 'oui'
|
|
|
|
: joinName(childFragments)
|
2017-04-24 18:03:38 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
situationGate(query) == null
|
2018-01-08 15:07:26 +00:00
|
|
|
? rec(dropLast(1)(parentFragments), [
|
|
|
|
last(parentFragments),
|
2018-01-03 15:54:19 +00:00
|
|
|
...childFragments
|
2018-05-22 17:12:13 +00:00
|
|
|
])
|
2018-01-03 15:54:19 +00:00
|
|
|
: situationGate(query) == expectedResult
|
2018-05-22 17:12:13 +00:00
|
|
|
}
|
2018-09-04 19:14:56 +00:00
|
|
|
|
2017-04-24 18:03:38 +00:00
|
|
|
return rec(startingFragments)
|
|
|
|
}
|
2018-09-04 19:14:56 +00:00
|
|
|
let formatBooleanValue = { oui: true, non: false }
|
2017-04-24 18:03:38 +00:00
|
|
|
|
2018-09-07 15:30:50 +00:00
|
|
|
export let getSituationValue = (situationGate, variableName, rule) => {
|
|
|
|
// get the current situation value
|
|
|
|
// it's the user input or test input, possibly with default values
|
2017-03-16 18:30:30 +00:00
|
|
|
let value = situationGate(variableName)
|
2017-05-10 14:09:36 +00:00
|
|
|
|
2018-09-07 12:52:39 +00:00
|
|
|
if (rule.API) return typeof value == 'string' ? JSON.parse(value) : value
|
2018-09-07 11:06:55 +00:00
|
|
|
|
2018-09-04 19:14:56 +00:00
|
|
|
if (rule.format != null) return value
|
|
|
|
//boolean variables don't have a format prop, it's the default
|
|
|
|
if (formatBooleanValue[value] !== undefined) return formatBooleanValue[value]
|
|
|
|
if (rule.formule && rule.formule['une possibilité'])
|
|
|
|
return evaluateBottomUp(situationGate)(splitName(variableName))
|
|
|
|
|
|
|
|
return value
|
2017-03-16 18:30:30 +00:00
|
|
|
}
|