2019-04-11 13:23:46 +00:00
|
|
|
import { dropLast, isEmpty, last } from 'ramda'
|
2020-03-26 15:03:19 +00:00
|
|
|
import { joinName, splitName } from './ruleUtils'
|
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
|
2019-09-11 08:04:19 +00:00
|
|
|
: (function() {
|
2018-01-03 15:54:19 +00:00
|
|
|
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
|
|
|
|
2019-09-11 08:04:19 +00:00
|
|
|
return 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
|
2019-09-11 08:04:19 +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
|
|
|
|
2019-07-04 16:13:40 +00:00
|
|
|
if (rule.unit != null) {
|
2019-11-05 15:02:08 +00:00
|
|
|
return value == undefined ? value : +value
|
2019-07-04 16:13:40 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 17:26:33 +00:00
|
|
|
// a leaf variable with an unit attribute is not boolean
|
2018-09-04 19:14:56 +00:00
|
|
|
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
|
|
|
}
|