2020-04-20 21:35:03 +00:00
|
|
|
/**
|
|
|
|
* Note: all here is strictly based on duck typing.
|
|
|
|
* We don't exepect the parent rule to explain the type of the contained formula, for example.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
import R from 'ramda'
|
2020-04-21 12:23:58 +00:00
|
|
|
import { ParsedRule, ParsedRules } from './types'
|
2020-04-20 21:35:03 +00:00
|
|
|
|
|
|
|
// type GraphNode = {
|
|
|
|
// name: string
|
|
|
|
// children?: Array<GraphNode>
|
|
|
|
// }
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
type ASTNode = { [index: string]: {} | undefined }
|
2020-04-20 21:35:03 +00:00
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
// [XXX] - Vaudrait-il mieux utiliser les DottedNames directement ici?
|
|
|
|
// A priori non car on peut imaginer cette lib comme étant indépendante des règles existantes et
|
|
|
|
// fonctionnant par ex en "mode serveur".
|
|
|
|
type RuleNode<Name extends string> = /* ASTNode & */ ParsedRule<Name>
|
2020-04-20 21:35:03 +00:00
|
|
|
|
2020-04-21 12:23:58 +00:00
|
|
|
type Value = ASTNode & {
|
|
|
|
nodeValue: any
|
|
|
|
constant: true
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
2020-04-21 20:56:40 +00:00
|
|
|
export function isValue(node: ASTNode): node is Value {
|
2020-04-21 12:23:58 +00:00
|
|
|
return (
|
|
|
|
(node as Value).nodeValue !== undefined && (node as Value).constant === true
|
|
|
|
)
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 12:23:58 +00:00
|
|
|
type Operation = ASTNode & {
|
|
|
|
operationType: 'comparison' | 'calculation'
|
2020-04-21 20:56:40 +00:00
|
|
|
explanation: Array<ASTNode>
|
2020-04-21 12:23:58 +00:00
|
|
|
}
|
2020-04-21 20:56:40 +00:00
|
|
|
export function isOperation(node: ASTNode): node is Operation {
|
|
|
|
return (
|
|
|
|
(node as Operation).operationType === 'comparison' ||
|
|
|
|
(node as Operation).operationType === 'calculation'
|
2020-04-21 12:23:58 +00:00
|
|
|
)
|
2020-04-21 20:56:40 +00:00
|
|
|
// return R.includes((node as Operation).operationType, [
|
|
|
|
// 'comparison',
|
|
|
|
// 'calculation'
|
|
|
|
// ])
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 12:23:58 +00:00
|
|
|
type Possibilities = ASTNode & {
|
|
|
|
possibilités: Array<string>
|
|
|
|
'choix obligatoire'?: 'oui' | 'non' // [XXX] - This should already be a defined type.
|
|
|
|
'une possibilité': 'oui' | 'non'
|
|
|
|
}
|
2020-04-21 20:56:40 +00:00
|
|
|
export function isPossibilities(node: ASTNode): node is Possibilities {
|
2020-04-21 12:23:58 +00:00
|
|
|
const possibilities = node as Possibilities
|
|
|
|
return (
|
|
|
|
possibilities.possibilités instanceof Array &&
|
|
|
|
possibilities.possibilités.every(it => typeof it === 'string') &&
|
2020-04-21 20:56:40 +00:00
|
|
|
(possibilities['choix obligatoire'] === undefined ||
|
|
|
|
possibilities['choix obligatoire'] === 'oui' ||
|
|
|
|
possibilities['choix obligatoire'] === 'non') &&
|
|
|
|
(possibilities['une possibilité'] === 'oui' ||
|
|
|
|
possibilities['une possibilité'] === 'non')
|
|
|
|
// R.includes(possibilities['choix obligatoire'], [undefined, 'oui', 'non']) &&
|
|
|
|
// R.includes(possibilities['une possibilité'], ['oui', 'non'])
|
2020-04-21 12:23:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
type Reference<Name extends string> = Omit<RuleNode<Name>, 'category'> & {
|
2020-04-21 12:23:58 +00:00
|
|
|
// [XXX] - a priori non pour le omit, il n'y a pas du tout autant de choses que dans RuleNode à l'intérieur d'une reference
|
|
|
|
category: 'reference'
|
2020-04-21 20:56:40 +00:00
|
|
|
partialReference: Name
|
|
|
|
dottedName: Name
|
2020-04-21 12:23:58 +00:00
|
|
|
}
|
2020-04-21 20:56:40 +00:00
|
|
|
export function isReference<Name extends string>(
|
|
|
|
node: ASTNode
|
|
|
|
): node is Reference<Name> {
|
|
|
|
return (
|
|
|
|
(node as Reference<Name>).category === 'reference' &&
|
|
|
|
(node as Reference<Name>).partialReference !== undefined &&
|
|
|
|
(node as Reference<Name>).dottedName !== undefined
|
|
|
|
)
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
type Recalcul<Name extends string> = ASTNode & {
|
2020-04-21 12:23:58 +00:00
|
|
|
explanation: {
|
2020-04-21 20:56:40 +00:00
|
|
|
recalcul: Reference<Name>
|
|
|
|
amendedSituation: Record<Name, Reference<Name>>
|
2020-04-21 12:23:58 +00:00
|
|
|
}
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
2020-04-21 20:56:40 +00:00
|
|
|
export function isRecalcul<Name extends string>(
|
|
|
|
node: ASTNode
|
|
|
|
): node is Recalcul<Name> {
|
|
|
|
const recalcul = node as Recalcul<Name>
|
2020-04-21 12:23:58 +00:00
|
|
|
return (
|
|
|
|
typeof recalcul.explanation === 'object' &&
|
2020-04-21 20:56:40 +00:00
|
|
|
typeof recalcul.explanation.recalcul === 'object' &&
|
2020-04-21 12:23:58 +00:00
|
|
|
isReference(recalcul.explanation.recalcul as ASTNode) &&
|
|
|
|
typeof recalcul.explanation.amendedSituation === 'object' &&
|
2020-04-21 20:56:40 +00:00
|
|
|
R.values(recalcul.explanation.amendedSituation).every(v => v !== undefined) // [XXX] - maybe a bit useless
|
2020-04-21 12:23:58 +00:00
|
|
|
)
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 12:23:58 +00:00
|
|
|
type Mechanism = ASTNode & {
|
|
|
|
category: 'mecanism'
|
|
|
|
}
|
2020-04-21 20:56:40 +00:00
|
|
|
export function isMechanism(node: ASTNode): node is Mechanism {
|
2020-04-20 21:35:03 +00:00
|
|
|
return (node as Mechanism).category === 'mecanism'
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
type FormuleNode<Name> =
|
2020-04-21 12:23:58 +00:00
|
|
|
| Value
|
|
|
|
| Operation
|
|
|
|
| Possibilities
|
2020-04-21 20:56:40 +00:00
|
|
|
| Reference<Name>
|
|
|
|
| Recalcul<Name>
|
2020-04-21 12:23:58 +00:00
|
|
|
| Mechanism
|
2020-04-21 20:56:40 +00:00
|
|
|
export function isFormuleNode<Name extends string>(
|
|
|
|
node: ASTNode
|
|
|
|
): node is FormuleNode<Name> {
|
2020-04-20 21:35:03 +00:00
|
|
|
return (
|
2020-04-21 20:56:40 +00:00
|
|
|
isValue(node) ||
|
|
|
|
isOperation(node) ||
|
|
|
|
isReference(node) ||
|
|
|
|
isPossibilities(node) ||
|
|
|
|
isRecalcul(node) ||
|
|
|
|
isMechanism(node)
|
2020-04-20 21:35:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
type EncadrementMech = Mechanism & {
|
|
|
|
valeur: {
|
|
|
|
explanation: ASTNode
|
|
|
|
[s: string]: {}
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 21:35:03 +00:00
|
|
|
export function isEncadrementMech(
|
|
|
|
mechanism: Mechanism
|
|
|
|
): mechanism is EncadrementMech {
|
|
|
|
return (mechanism as EncadrementMech).explanation.name === 'encadrement'
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
type SommeMech = any // extends Mechanism
|
2020-04-20 21:35:03 +00:00
|
|
|
export function isSommeMech(mechanism: Mechanism): mechanism is SommeMech {
|
|
|
|
return (mechanism as EncadrementMech).explanation.name === 'somme'
|
|
|
|
}
|
|
|
|
|
|
|
|
function logVisit(depth: number, typeName: string, repr: string): void {
|
|
|
|
console.log(' '.repeat(depth) + `visiting ${typeName} node ${repr}`)
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
export function ruleDependenciesOfNode<Name extends string>(
|
2020-04-20 21:35:03 +00:00
|
|
|
depth: number,
|
|
|
|
node: ASTNode
|
2020-04-21 20:56:40 +00:00
|
|
|
): Array<Name> {
|
|
|
|
function ruleDependenciesOfValue(depth: number, value: Value): Array<Name> {
|
2020-04-20 21:35:03 +00:00
|
|
|
logVisit(depth, 'value', value.nodeValue)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
function ruleDependenciesOfOperation(
|
|
|
|
depth: number,
|
|
|
|
operation: Operation
|
2020-04-21 20:56:40 +00:00
|
|
|
): Array<Name> {
|
2020-04-20 21:35:03 +00:00
|
|
|
logVisit(depth, 'operation', operation.operationType)
|
2020-04-21 20:56:40 +00:00
|
|
|
return R.chain<ASTNode, Name>(
|
|
|
|
R.partial<number, ASTNode, Array<Name>>(ruleDependenciesOfNode, [
|
|
|
|
depth + 1
|
|
|
|
])
|
|
|
|
)(operation.explanation)
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ruleDependenciesOfPossibilities(
|
|
|
|
depth: number,
|
|
|
|
possibilities: Possibilities
|
2020-04-21 20:56:40 +00:00
|
|
|
): Array<Name> {
|
|
|
|
logVisit(depth, 'possibilities', possibilities.possibilités.join(', '))
|
2020-04-20 21:35:03 +00:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
function ruleDependenciesOfReference(
|
|
|
|
depth: number,
|
|
|
|
reference: Reference<Name>
|
|
|
|
): Array<Name> {
|
|
|
|
logVisit(depth, 'reference', reference.dottedName)
|
|
|
|
return [reference.dottedName]
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:35:03 +00:00
|
|
|
function ruleDependenciesOfRecalcul(
|
|
|
|
depth: number,
|
2020-04-21 20:56:40 +00:00
|
|
|
recalcul: Recalcul<Name>
|
|
|
|
): Array<Name> {
|
|
|
|
logVisit(
|
|
|
|
depth,
|
|
|
|
'recalcul',
|
|
|
|
recalcul.explanation.recalcul.partialReference as string
|
|
|
|
)
|
|
|
|
return [recalcul.explanation.recalcul.partialReference]
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ruleDependenciesOfMechanism(
|
|
|
|
depth: number,
|
|
|
|
mechanism: Mechanism
|
2020-04-21 20:56:40 +00:00
|
|
|
): Array<Name> {
|
|
|
|
logVisit(depth, 'mechanism', '')
|
|
|
|
debugger
|
2020-04-20 21:35:03 +00:00
|
|
|
// [XXX] - flatten this out in the main function, like the other types
|
2020-04-21 20:56:40 +00:00
|
|
|
// if (isEncadrementMech(mechanism)) {
|
|
|
|
// chain(partial(ruleDependenciesOfNode, [depth + 1]))(
|
|
|
|
// mechanism.valeur.explanation
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
// if (isSommeMech(mechanism)) {
|
|
|
|
// chain(partial(ruleDependenciesOfNode, [depth + 1]))(mechanism.explanation)
|
|
|
|
// }
|
2020-04-20 21:35:03 +00:00
|
|
|
return [] // [XXX]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isValue(node)) {
|
|
|
|
return ruleDependenciesOfValue(depth, node as Value)
|
|
|
|
} else if (isOperation(node)) {
|
|
|
|
return ruleDependenciesOfOperation(depth, node as Operation)
|
|
|
|
} else if (isReference(node)) {
|
2020-04-21 20:56:40 +00:00
|
|
|
return ruleDependenciesOfReference(depth, node as Reference<Name>)
|
2020-04-20 21:35:03 +00:00
|
|
|
} else if (isPossibilities(node)) {
|
|
|
|
return ruleDependenciesOfPossibilities(depth, node as Possibilities)
|
|
|
|
} else if (isRecalcul(node)) {
|
2020-04-21 20:56:40 +00:00
|
|
|
return ruleDependenciesOfRecalcul(depth, node as Recalcul<Name>)
|
2020-04-20 21:35:03 +00:00
|
|
|
} else if (isMechanism(node)) {
|
|
|
|
return ruleDependenciesOfMechanism(depth, node as Mechanism)
|
|
|
|
}
|
|
|
|
return [] // [XXX]
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
function ruleDependenciesOfRule<Name extends string>(
|
2020-04-20 21:35:03 +00:00
|
|
|
depth: number,
|
2020-04-21 20:56:40 +00:00
|
|
|
rule: RuleNode<Name>
|
|
|
|
): Array<Name> {
|
2020-04-21 12:23:58 +00:00
|
|
|
logVisit(depth, 'rule', rule.dottedName as string)
|
2020-04-20 21:35:03 +00:00
|
|
|
if (rule.formule) {
|
2020-04-21 20:56:40 +00:00
|
|
|
const formuleNode: FormuleNode<Name> = rule.formule.explanation
|
2020-04-20 21:35:03 +00:00
|
|
|
// This is for comfort, as the responsibility over structure isn't owned by this piece of code:
|
|
|
|
if (!isFormuleNode(formuleNode)) {
|
|
|
|
debugger
|
2020-04-21 12:23:58 +00:00
|
|
|
// throw Error(
|
|
|
|
// `This rule's formule is not of a known type: ${rule.dottedName}`
|
|
|
|
// )
|
2020-04-20 21:35:03 +00:00
|
|
|
}
|
|
|
|
return ruleDependenciesOfNode(depth + 1, formuleNode)
|
|
|
|
} else return [rule.dottedName]
|
|
|
|
}
|
|
|
|
|
2020-04-21 20:56:40 +00:00
|
|
|
export function buildRulesDependencies<Name extends string>(
|
|
|
|
parsedRules: ParsedRules<Name>
|
|
|
|
): Array<[Name, Array<Name>]> {
|
|
|
|
// This stringPairs thing is necessary because `toPairs` is strictly considering that
|
|
|
|
// object keys are strings (same for `Object.entries`). Maybe we should build our own
|
|
|
|
// `toPairs`?
|
|
|
|
const stringPairs: Array<[string, RuleNode<Name>]> = R.toPairs(parsedRules)
|
|
|
|
const pairs: Array<[Name, RuleNode<Name>]> = stringPairs as Array<
|
|
|
|
[Name, RuleNode<Name>]
|
|
|
|
>
|
|
|
|
const pairsResults: Array<Array<Name>> = R.map(
|
|
|
|
([_, ruleNode]: [Name, RuleNode<Name>]): Array<Name> =>
|
|
|
|
ruleDependenciesOfRule<Name>(0, ruleNode),
|
|
|
|
pairs
|
|
|
|
)
|
|
|
|
console.log(pairsResults)
|
|
|
|
|
|
|
|
return R.map(
|
|
|
|
([dottedName, ruleNode]: [Name, RuleNode<Name>]): [Name, Array<Name>] => [
|
2020-04-20 21:35:03 +00:00
|
|
|
dottedName,
|
2020-04-21 20:56:40 +00:00
|
|
|
ruleDependenciesOfRule<Name>(0, ruleNode)
|
|
|
|
],
|
|
|
|
pairs
|
2020-04-20 21:35:03 +00:00
|
|
|
)
|
|
|
|
}
|