Engine
API / publicodes / Engine
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>
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
>
Defined in
packages/core/src/engine/index.ts:137
Methods
evaluate()
evaluate(value): EvaluatedNode
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.
Defined in
packages/core/src/engine/index.ts:346
getParsedRules()
getParsedRules(): ParsedRules<RuleNames>
Returns
ParsedRules
<RuleNames
>
the parsed rules used by the engine
Remarks
The private rules are not included in the parsed rules
Defined in
packages/core/src/engine/index.ts:315
getRule()
getRule(dottedName): RuleNode
Get the rule with the given dottedName.
Parameters
Parameter | Type |
---|---|
dottedName | RuleNames |
Returns
Throws
PublicodesError if the rule does not exist or is private
Defined in
packages/core/src/engine/index.ts:289
getSituation()
getSituation(): Partial<Record<RuleNames, ASTNode | PublicodesExpression>>
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
Defined in
packages/core/src/engine/index.ts:328
inversionFail()
inversionFail(): boolean
Returns
boolean
true if the engine has encountered an inversion error during the last evaluation
Defined in
packages/core/src/engine/index.ts:279
resetCache()
resetCache(): void
Reset the engine cache. This will clear all the cached evaluations.
Returns
void
Defined in
packages/core/src/engine/index.ts:177
setSituation()
setSituation(situation, options): Engine<RuleNames>
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 | object | - |
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
>
Defined in
packages/core/src/engine/index.ts:188
shallowCopy()
shallowCopy(): Engine<RuleNames>
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.