export interface EngineOptions { allowUndefinedFacts: boolean; } export interface EngineResult { events: Event[]; almanac: Almanac; } export default function engineFactory( rules: Array, options?: EngineOptions ): Engine; export class Engine { constructor(rules?: Array, options?: EngineOptions); addRule(rule: RuleProperties): this; removeRule(rule: Rule): boolean; addOperator(operator: Operator): Map; addOperator( operatorName: string, callback: OperatorEvaluator ): Map; removeOperator(operator: Operator | string): boolean; addFact(fact: Fact): this; addFact( id: string, valueCallback: DynamicFactCallback | T, options?: FactOptions ): this; removeFact(factOrId: string | Fact): boolean; getFact(factId: string): Fact; on(eventName: "success", handler: EventHandler): this; on(eventName: "failure", handler: EventHandler): this; on(eventName: string, handler: EventHandler): this; run(facts?: Record): Promise; stop(): this; } export interface OperatorEvaluator { (factValue: A, compareToValue: B): boolean; } export class Operator { public name: string; constructor( name: string, evaluator: OperatorEvaluator, validator?: (factValue: A) => boolean ); } export class Almanac { factValue( factId: string, params?: Record, path?: string ): Promise; addRuntimeFact(factId: string, value: any): void; } export type FactOptions = { cache?: boolean; priority?: number; }; export type DynamicFactCallback = ( params: Record, almanac: Almanac ) => T; export class Fact { id: string; priority: number; options: FactOptions; value?: T; calculationMethod?: DynamicFactCallback; constructor( id: string, value: T | DynamicFactCallback, options?: FactOptions ); } export interface Event { type: string; params?: Record; } export type EventHandler = ( event: Event, almanac: Almanac, ruleResult: RuleResult ) => void; export interface RuleProperties { conditions: TopLevelCondition; event: Event; name?: string; priority?: number; onSuccess?: EventHandler; onFailure?: EventHandler; } export type RuleSerializable = Pick< Required, "conditions" | "event" | "name" | "priority" >; export interface RuleResult { name: string; conditions: TopLevelCondition; event?: Event; priority?: number; result: any; } export class Rule implements RuleProperties { constructor(ruleProps: RuleProperties | string); name: string; conditions: TopLevelCondition; event: Event; priority: number; setConditions(conditions: TopLevelCondition): this; setEvent(event: Event): this; setPriority(priority: number): this; toJSON(): string; toJSON( stringify: T ): T extends true ? string : RuleSerializable; } interface ConditionProperties { fact: string; operator: string; value: { fact: string } | any; path?: string; priority?: number; params?: Record; } type NestedCondition = ConditionProperties | TopLevelCondition; type AllConditions = { all: NestedCondition[] }; type AnyConditions = { any: NestedCondition[] }; export type TopLevelCondition = AllConditions | AnyConditions;