Engine
API / publicodes / Engine
Defined in: packages/core/src/engine/index.ts:131
The engine is used to parse rules and evaluate expressions. It is the main entry point to the publicodes library.
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
RuleNames extends string | string | All rules names. Allows to automatically autocomplete rules names in the engine when using Engine.getRule or Engine.setSituation |
Constructors
new Engine()
new Engine<RuleNames>(rules, options): Engine<RuleNames>
Defined in: packages/core/src/engine/index.ts:150
Creates an evaluation engine with the publicode rules given as arguments.
Parameters
Parameter | Type | Description |
---|---|---|
rules | | ParsedRules <RuleNames > | Partial <Record <RuleNames , RawRule >> | The publicodes model to use (RawPublicodes or ParsedRules) |
options | EngineOptions | Configuration options |
Returns
Engine
<RuleNames
>
Methods
evaluate()
evaluate(value): EvaluatedNode
Defined in: packages/core/src/engine/index.ts:407
Evaluate a publicodes expression.
Use the current situation set by setSituation.
Parameters
Parameter | Type | Description |
---|---|---|
value | | ASTNode | PublicodesExpression | The publicodes expression to evaluate. |
Returns
The ASTNode of the publicodes expression decorated with evaluation results (nodeValue, unit, missingVariables, etc.)
Remarks
To improve performance, the engine will cache the evaluation of the expression, and all its byproducts (intermediate evaluations). The cache is reset when the situation is updated with setSituation. You can also manually reset it with resetCache.
getParsedRules()
getParsedRules(): ParsedRules<RuleNames>
Defined in: packages/core/src/engine/index.ts:331
Returns
ParsedRules
<RuleNames
>
the parsed rules used by the engine
Remarks
The private rules are not included in the parsed rules
getPossibilitiesFor()
getPossibilitiesFor(dottedName, options): null | Possibility[]
Defined in: packages/core/src/engine/index.ts:368
Retrieves the list of possible values for a given rule.
Parameters
Parameter | Type | Description |
---|---|---|
dottedName | RuleNames | The dotted name of the rule to get possibilities for |
options | { filterNotApplicable : boolean ; } | Options object |
options.filterNotApplicable ? | boolean | When true, filters out possibilities that are not applicable based on the current situation. Defaults to false. |
Returns
null
| Possibility
[]
Array of possibility nodes if the rule has possibilities defined, null otherwise
Example
Publicodes
contrat:
une possibilité:
- "'CDI'"
- "'CDD'"
```js
engine.getPossibilitiesFor('contrat')
getRule()
getRule(dottedName): RuleNode
Defined in: packages/core/src/engine/index.ts:305
Get the rule with the given dottedName.
Parameters
Parameter | Type |
---|---|
dottedName | RuleNames |
Returns
Throws
PublicodesError if the rule does not exist or is private
getSituation()
getSituation(): Partial<Record<RuleNames,
| ASTNode
| PublicodesExpression>>
Defined in: packages/core/src/engine/index.ts:344
Retrieve the current situation used for the evaluation
This situation can be slightly different from the one set with setSituation if some values were filtered out because of evaluation errors.
Returns
Partial
<Record
<RuleNames
,
| ASTNode
| PublicodesExpression
>>
Situation used by the engine
inversionFail()
inversionFail(): boolean
Defined in: packages/core/src/engine/index.ts:295
Returns
boolean
true if the engine has encountered an inversion error during the last evaluation
resetCache()
resetCache(): void
Defined in: packages/core/src/engine/index.ts:193
Reset the engine cache. This will clear all the cached evaluations.
Returns
void
setSituation()
setSituation(situation, options): Engine<RuleNames>
Defined in: packages/core/src/engine/index.ts:204
Set the situation used for the evaluation.
All subsequent evaluations will use this situation. Reset the evaluation cache to avoid inconsistencies, so it is costly to call this method frequently with the same situation.
Parameters
Parameter | Type | Description |
---|---|---|
situation | Partial <Record <RuleNames , | ASTNode | PublicodesExpression >> | The situation to set (see Situation) |
options | { keepPreviousSituation : boolean ; strict : boolean ; } | - |
options.keepPreviousSituation ? | boolean | If true, the previous situation is kept and the new values are added to it. Default false |
options.strict ? | boolean | If set to true, the engine will throw when the situation contains invalid values (rules that don’t exists, or values with syntax errors). If set to false, it will log the error and filter the invalid values from the situation Overrides the EngineOptions strict option |
Returns
Engine
<RuleNames
>
shallowCopy()
shallowCopy(): Engine<RuleNames>
Defined in: packages/core/src/engine/index.ts:485
Shallow Engine instance copy. Useful to evaluate the same rules with different situations.
Returns
Engine
<RuleNames
>
a new Engine instance with the same baseContext, context, publicParsedRules, publicSituation and cache attributes.