UNPKG

3.47 kBTypeScriptView Raw
1export interface EngineOptions {
2 allowUndefinedFacts: boolean;
3}
4
5export interface EngineResult {
6 events: Event[];
7 almanac: Almanac;
8}
9
10export default function engineFactory(
11 rules: Array<RuleProperties>,
12 options?: EngineOptions
13): Engine;
14
15export class Engine {
16 constructor(rules?: Array<RuleProperties>, options?: EngineOptions);
17
18 addRule(rule: RuleProperties): this;
19 removeRule(rule: Rule): boolean;
20
21 addOperator(operator: Operator): Map<string, Operator>;
22 addOperator<A, B>(
23 operatorName: string,
24 callback: OperatorEvaluator<A, B>
25 ): Map<string, Operator>;
26 removeOperator(operator: Operator | string): boolean;
27
28 addFact<T>(fact: Fact<T>): this;
29 addFact<T>(
30 id: string,
31 valueCallback: DynamicFactCallback<T> | T,
32 options?: FactOptions
33 ): this;
34 removeFact(factOrId: string | Fact): boolean;
35 getFact<T>(factId: string): Fact<T>;
36
37 on(eventName: "success", handler: EventHandler): this;
38 on(eventName: "failure", handler: EventHandler): this;
39 on(eventName: string, handler: EventHandler): this;
40
41 run(facts?: Record<string, any>): Promise<EngineResult>;
42 stop(): this;
43}
44
45export interface OperatorEvaluator<A, B> {
46 (factValue: A, compareToValue: B): boolean;
47}
48
49export class Operator<A = unknown, B = unknown> {
50 public name: string;
51 constructor(
52 name: string,
53 evaluator: OperatorEvaluator<A, B>,
54 validator?: (factValue: A) => boolean
55 );
56}
57
58export class Almanac {
59 factValue<T>(
60 factId: string,
61 params?: Record<string, any>,
62 path?: string
63 ): Promise<T>;
64 addRuntimeFact(factId: string, value: any): void;
65}
66
67export type FactOptions = {
68 cache?: boolean;
69 priority?: number;
70};
71
72export type DynamicFactCallback<T = unknown> = (
73 params: Record<string, any>,
74 almanac: Almanac
75) => T;
76
77export class Fact<T = unknown> {
78 id: string;
79 priority: number;
80 options: FactOptions;
81 value?: T;
82 calculationMethod?: DynamicFactCallback<T>;
83
84 constructor(
85 id: string,
86 value: T | DynamicFactCallback<T>,
87 options?: FactOptions
88 );
89}
90
91export interface Event {
92 type: string;
93 params?: Record<string, any>;
94}
95
96export type EventHandler = (
97 event: Event,
98 almanac: Almanac,
99 ruleResult: RuleResult
100) => void;
101
102export interface RuleProperties {
103 conditions: TopLevelCondition;
104 event: Event;
105 name?: string;
106 priority?: number;
107 onSuccess?: EventHandler;
108 onFailure?: EventHandler;
109}
110export type RuleSerializable = Pick<
111 Required<RuleProperties>,
112 "conditions" | "event" | "name" | "priority"
113>;
114
115export interface RuleResult {
116 name: string;
117 conditions: TopLevelCondition;
118 event?: Event;
119 priority?: number;
120 result: any;
121}
122
123export class Rule implements RuleProperties {
124 constructor(ruleProps: RuleProperties | string);
125 name: string;
126 conditions: TopLevelCondition;
127 event: Event;
128 priority: number;
129 setConditions(conditions: TopLevelCondition): this;
130 setEvent(event: Event): this;
131 setPriority(priority: number): this;
132 toJSON(): string;
133 toJSON<T extends boolean>(
134 stringify: T
135 ): T extends true ? string : RuleSerializable;
136}
137
138interface ConditionProperties {
139 fact: string;
140 operator: string;
141 value: { fact: string } | any;
142 path?: string;
143 priority?: number;
144 params?: Record<string, any>;
145}
146
147type NestedCondition = ConditionProperties | TopLevelCondition;
148type AllConditions = { all: NestedCondition[] };
149type AnyConditions = { any: NestedCondition[] };
150export type TopLevelCondition = AllConditions | AnyConditions;