UNPKG

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