2019-04-11 15:23:46 +02:00
|
|
|
import { dropLast, isEmpty, last } from 'ramda'
|
2020-03-26 16:03:19 +01:00
|
|
|
import { joinName, splitName } from './ruleUtils'
|
2017-03-16 19:30:30 +01:00
|
|
|
|
2020-05-06 09:23:21 +02:00
|
|
|
let evaluateBottomUp = situation => startingFragments => {
|
2018-01-03 16:54:19 +01:00
|
|
|
let rec = (parentFragments, childFragments = []) =>
|
|
|
|
parentFragments.length == 0
|
|
|
|
? null
|
2019-09-11 10:04:19 +02:00
|
|
|
: (function() {
|
2018-01-03 16:54:19 +01:00
|
|
|
let query = joinName(parentFragments),
|
2018-01-08 16:07:26 +01:00
|
|
|
expectedResult = isEmpty(childFragments)
|
2018-01-03 16:54:19 +01:00
|
|
|
? 'oui'
|
|
|
|
: joinName(childFragments)
|
2017-04-24 20:03:38 +02:00
|
|
|
|
2020-05-06 09:23:21 +02:00
|
|
|
return situation[query] == null
|
2018-01-08 16:07:26 +01:00
|
|
|
? rec(dropLast(1)(parentFragments), [
|
|
|
|
last(parentFragments),
|
2018-01-03 16:54:19 +01:00
|
|
|
...childFragments
|
2018-05-22 19:12:13 +02:00
|
|
|
])
|
2020-05-06 09:23:21 +02:00
|
|
|
: situation[query] == expectedResult
|
2019-09-11 10:04:19 +02:00
|
|
|
})()
|
2018-09-04 21:14:56 +02:00
|
|
|
|
2017-04-24 20:03:38 +02:00
|
|
|
return rec(startingFragments)
|
|
|
|
}
|
2020-05-06 09:23:21 +02:00
|
|
|
export let getSituationValue = (situation, variableName, rule) => {
|
2018-09-07 17:30:50 +02:00
|
|
|
// get the current situation value
|
|
|
|
// it's the user input or test input, possibly with default values
|
2020-05-06 09:23:21 +02:00
|
|
|
let value = situation[variableName]
|
2017-05-10 16:09:36 +02:00
|
|
|
|
2020-05-06 09:23:21 +02:00
|
|
|
if (rule.formule?.['une possibilité'])
|
|
|
|
return evaluateBottomUp(situation)(splitName(variableName))
|
2018-09-04 21:14:56 +02:00
|
|
|
|
|
|
|
return value
|
2017-03-16 19:30:30 +01:00
|
|
|
}
|