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