@o3r/rules-engine
Version:
This module provides a rule engine that can be executed on your Otter application to customize your application (translations, placeholders and configs) based on a json file generated by your CMS.
103 lines • 4.5 kB
TypeScript
import { Observable } from 'rxjs';
import type { RulesEngine } from './engine';
import { EngineRuleset } from './engine.interface';
import type { Facts } from './fact';
import { Operator } from './operator/operator.interface';
import { ActionBlock, ActionSetTemporaryFactBlock, AllBlock, GenericOperand, IfElseBlock, NestedCondition, Rule, Ruleset } from './structure';
/** Type of the retrieve fact function */
export type RetrieveFactFuncType = <T = unknown>(id: string) => Observable<T | undefined>;
export declare class RulesetExecutor {
/** retrieveFactFunc */
protected readonly rulesEngine: RulesEngine;
/** Map of available operators */
protected readonly operators: Record<string, Operator<unknown, unknown>>;
/** Delay before fact stream defaulting value */
readonly factDefaultDelay?: number;
/** Ruleset associated to the current executor*/
readonly ruleset: Ruleset;
/** Ruleset plugged to the fact stream, that contains actions stream result */
readonly engineRuleset: EngineRuleset;
private executionCounter;
/**
* Create a new ruleset executor
* @param ruleset Ruleset to evaluate
* @param rulesEngine Instance of the rules engine
*/
constructor(ruleset: Ruleset, rulesEngine: RulesEngine);
/**
* Recursively explores a rule to identify and collect input facts.
* Input facts are identified based on the 'FACT' type and operator-specific implicit dependencies.
* @param currentObject The current object being explored.
* @param ruleInputFacts A set to store the identified input facts for the rule.
*/
private collectRuleInputFacts;
/**
* Report performance mark for a rule run
* @param rule Rule to measure
* @param status status of the rule evaluation
*/
protected performanceMark(rule: Rule, status: 'start' | 'end'): void;
/**
* Get operand value stream according to its type
* @param operand operand of the condition
* @param factsValue
* @param runtimeFactValues
*/
protected getOperandValue(operand: GenericOperand | undefined, factsValue: Record<string, Facts | undefined>, runtimeFactValues: Record<string, Facts>): unknown;
/**
* Process a root rule from a ruleset, and return the associated actions to be processed
* Will also update the runtimeFactValues map that is ruleset wise
* Note that runtimeFactValues will be mutated by all the runtime facts actions executed
* @param rule
* @param factsValue
* @param runtimeFactValues
* @protected
*/
protected evaluateRule(rule: Rule, factsValue: Record<string, Facts | undefined>, runtimeFactValues: Record<string, Facts>): ActionBlock[];
/**
* Recursively process a block to extract all the actions keeping the order
* Note that runtimeFactValues will be mutated by all the runtime facts actions executed
* @param element
* @param factsValue
* @param runtimeFactValues This runtime fact map will be mutated by all the runtime facts actions executed
* @param actions
* @protected
*/
protected evaluateBlock(element: AllBlock, factsValue: Record<string, Facts | undefined>, runtimeFactValues: Record<string, Facts>, actions?: ActionBlock[]): ActionBlock[];
/**
* Returns true if the element is a IfElse block
* @param element
* @protected
*/
protected isIfElseBlock(element: AllBlock): element is IfElseBlock;
/**
* Returns true if the element is an action block
* @param element
* @protected
*/
protected isActionBlock(element: AllBlock): element is ActionBlock;
/**
* Returns true if the action sets a temporary fact
* @param element
* @protected
*/
protected isActionSetTemporaryFactBlock(element: ActionBlock): element is ActionSetTemporaryFactBlock;
/**
* Evaluate a condition block
* @param nestedCondition
* @param factsValue
* @param runtimeFactValues
* @protected
*/
protected evaluateCondition(nestedCondition: NestedCondition, factsValue: Record<string, Facts | undefined>, runtimeFactValues: Record<string, Facts>): boolean;
/**
* Find rule input facts
* @param obj
*/
protected readonly findRuleInputFacts: (obj: AllBlock) => string[];
/**
* Plug ruleset to fact streams and trigger a first evaluation
*/
protected plugRuleset(): EngineRuleset;
}
//# sourceMappingURL=ruleset-executor.d.ts.map