UNPKG

@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.

89 lines 3.51 kB
import type { Logger } from '@o3r/logger'; import { Observable } from 'rxjs'; import { EngineDebugger } from './debug/engine.debug'; import { RulesEngineOptions } from './engine.interface'; import type { Fact, Facts } from './fact/index'; import { Operator, UnaryOperator } from './operator/index'; import { ActionBlock, Ruleset } from './structure'; /** Rules engine */ export declare class RulesEngine { /** Map of registered fact stream, this map is mutated by the ruleset executors */ private readonly factMap; /** Subject containing the rulesets and the results stream*/ private readonly rulesetMapSubject; /** Map of available operators */ operators: Record<string, Operator<unknown, unknown>>; /** List of events for the current state of the rules engine */ readonly events$: Observable<ActionBlock[]>; /** Delay before fact stream defaulting value */ factDefaultDelay?: number; /** * Instance of engine debug object; Undefined if debugMode is not active */ readonly engineDebug?: EngineDebugger; /** Name of the rules engine instance */ readonly rulesEngineInstanceName: string; /** * Performance reporter to use for performance measurements. * @default window.performance on browser only, undefined on node */ readonly performance: import("./engine.interface").CrossPlatformPerformance | undefined; /** * Log the engine errors */ readonly logger?: Logger; /** * Flag to check if the run is in debug mode or not */ get debugMode(): boolean; /** * Rules engine * @param options rules engine options */ constructor(options?: RulesEngineOptions); /** * Attach debug events to actions stream if debug engine is activated */ private handleActionsStreamOutput; /** * Create the actions stream event based on provided active rulesets ids; Handle debug too * @param ruleSets */ private prepareActionsStream; /** * Create or retrieve a fact stream * The fact stream created will be registered in the engine * @param id ID of the fact to retrieve * @param factValue$ Value stream for the fact */ retrieveOrCreateFactStream<T = Facts>(id: string, factValue$?: Observable<T>): Observable<T | undefined>; /** * Retrieve the promise of the latest value of a fact. * Return undefined if the fact is not defined. * @param id ID of the fact to retrieve */ retrieveFactValue<T = unknown>(id: string): Promise<T | undefined> | undefined; /** * Update or insert fact in rules engine * @param facts fact list to add / update */ upsertFacts<T = unknown>(facts: Fact<T> | Fact<T>[]): void; /** * Update or insert rule in rules engine * @param rulesets */ upsertRulesets(rulesets: Ruleset[]): void; /** * Update or insert operator in rules engine * @param operators operator list to add / update */ upsertOperators(operators: (Operator<any, any> | UnaryOperator<any>)[]): void; /** * Operator to apply on a stream of rulesets ids * Returns a stream of actions outputted by the rules engine, corresponding to the rulesetsIds */ getEventStream<T extends ActionBlock = ActionBlock>(): (rulesetsIds$: Observable<string[] | undefined>) => Observable<T[]>; /** Get the list of registered facts names */ getRegisteredFactsNames(): string[]; } //# sourceMappingURL=engine.d.ts.map