2018-08-16 08:05:00 +00:00
|
|
|
// This file exports the functions of the public computing library
|
|
|
|
import { analyseMany, parseAll } from './traverse.js'
|
2018-09-24 16:27:43 +00:00
|
|
|
import {
|
|
|
|
rulesFr,
|
|
|
|
collectDefaults,
|
|
|
|
nestedSituationToPathMap,
|
|
|
|
enrichRule
|
|
|
|
} from './rules'
|
|
|
|
import yaml from 'js-yaml'
|
2018-08-15 09:39:10 +00:00
|
|
|
|
2018-09-24 16:27:43 +00:00
|
|
|
let defaultRules = parseAll(rulesFr)
|
2018-08-16 08:05:00 +00:00
|
|
|
|
|
|
|
// The public evaluation function takes a nested object of input values
|
|
|
|
let nestedSituationToStateSelector = nestedSituation => dottedName =>
|
|
|
|
({
|
|
|
|
...collectDefaults(rulesFr),
|
|
|
|
...nestedSituationToPathMap(nestedSituation)
|
|
|
|
}[dottedName])
|
|
|
|
|
2018-09-25 17:22:32 +00:00
|
|
|
let enrichYaml = string => yaml.safeLoad(string).map(enrichRule)
|
|
|
|
|
2018-09-24 15:14:40 +00:00
|
|
|
export default {
|
2018-09-25 17:22:32 +00:00
|
|
|
evaluate: (targetNames, nestedSituation, rulesConfig) => {
|
|
|
|
let rules = rulesConfig
|
|
|
|
? do {
|
|
|
|
let { base, extra } = rulesConfig
|
|
|
|
parseAll([
|
|
|
|
...(base ? enrichYaml(base) : rulesFr),
|
|
|
|
...(extra ? enrichYaml(extra) : [])
|
|
|
|
])
|
|
|
|
}
|
2018-09-24 16:27:43 +00:00
|
|
|
: defaultRules
|
|
|
|
|
|
|
|
return analyseMany(rules, targetNames)(
|
2018-09-24 15:14:40 +00:00
|
|
|
nestedSituationToStateSelector(nestedSituation)
|
2018-09-24 16:27:43 +00:00
|
|
|
)
|
|
|
|
}
|
2018-09-24 15:14:40 +00:00
|
|
|
}
|