drools-builder 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,543 @@
1
+ /**
2
+ * Typed enum for every constraint operator supported by Drools.
3
+ * Values are the exact DRL tokens, so an Operator member is directly
4
+ * assignable to the ConstraintOperator string-literal union in types.ts.
5
+ *
6
+ * @example
7
+ * fact('Player', '$p').field('score', Operator.Gte, '100')
8
+ */
9
+ declare enum Operator {
10
+ Eq = "==",
11
+ Neq = "!=",
12
+ Gt = ">",
13
+ Lt = "<",
14
+ Gte = ">=",
15
+ Lte = "<=",
16
+ Contains = "contains",
17
+ NotContains = "not contains",
18
+ MemberOf = "memberOf",
19
+ NotMemberOf = "not memberOf",
20
+ Matches = "matches",
21
+ NotMatches = "not matches"
22
+ }
23
+ /**
24
+ * Typed enum for the built-in Drools accumulate functions.
25
+ * Use these as the second argument to AccumulateBuilder.fn().
26
+ *
27
+ * @example
28
+ * accumulate(fact('Transaction', '$tx'))
29
+ * .fn('$total', Aggregate.Sum, '$tx.amount')
30
+ */
31
+ declare enum Aggregate {
32
+ Sum = "sum",
33
+ Count = "count",
34
+ Min = "min",
35
+ Max = "max",
36
+ Average = "average",
37
+ CollectList = "collectList",
38
+ CollectSet = "collectSet",
39
+ CountDistinct = "countDistinct"
40
+ }
41
+
42
+ type ConstraintOperator = '==' | '!=' | '>' | '<' | '>=' | '<=' | 'contains' | 'not contains' | 'memberOf' | 'not memberOf' | 'matches' | 'not matches';
43
+ interface FieldConstraint {
44
+ kind: 'FieldConstraint';
45
+ field: string;
46
+ operator: ConstraintOperator;
47
+ value: string;
48
+ binding?: string;
49
+ }
50
+ interface BindingConstraint {
51
+ kind: 'BindingConstraint';
52
+ binding: string;
53
+ field: string;
54
+ }
55
+ interface RawConstraint {
56
+ kind: 'RawConstraint';
57
+ expression: string;
58
+ }
59
+ type Constraint = FieldConstraint | BindingConstraint | RawConstraint;
60
+ type FactType = string;
61
+ interface FactPattern {
62
+ kind: 'FactPattern';
63
+ factType: FactType;
64
+ binding?: string;
65
+ constraints: Constraint[];
66
+ }
67
+ interface UnboundPattern {
68
+ kind: 'UnboundPattern';
69
+ factType: FactType;
70
+ constraints: Constraint[];
71
+ }
72
+ interface AndCondition {
73
+ kind: 'And';
74
+ conditions: Condition[];
75
+ }
76
+ interface OrCondition {
77
+ kind: 'Or';
78
+ conditions: Condition[];
79
+ }
80
+ interface NotCondition {
81
+ kind: 'Not';
82
+ condition: UnboundPattern | EvalCondition | RawCondition;
83
+ }
84
+ interface ExistsCondition {
85
+ kind: 'Exists';
86
+ condition: UnboundPattern | EvalCondition | RawCondition;
87
+ }
88
+ interface ForallCondition {
89
+ kind: 'Forall';
90
+ condition: Condition;
91
+ }
92
+ interface AccumulateFunction {
93
+ binding: string;
94
+ function: string;
95
+ argument: string;
96
+ }
97
+ interface AccumulatePattern {
98
+ kind: 'Accumulate';
99
+ source: Condition;
100
+ functions: AccumulateFunction[];
101
+ resultConstraint?: string;
102
+ }
103
+ interface FromCondition {
104
+ kind: 'From';
105
+ pattern: FactPattern;
106
+ expression: string;
107
+ }
108
+ interface EvalCondition {
109
+ kind: 'Eval';
110
+ expression: string;
111
+ }
112
+ interface RawCondition {
113
+ kind: 'RawCondition';
114
+ drl: string;
115
+ }
116
+ type Condition = FactPattern | UnboundPattern | AndCondition | OrCondition | NotCondition | ExistsCondition | ForallCondition | AccumulatePattern | FromCondition | EvalCondition | RawCondition;
117
+ interface Modification {
118
+ method: string;
119
+ args: string[];
120
+ }
121
+ interface ModifyConsequence {
122
+ kind: 'ModifyConsequence';
123
+ binding: string;
124
+ modifications: Modification[];
125
+ }
126
+ interface InsertConsequence {
127
+ kind: 'InsertConsequence';
128
+ objectExpression: string;
129
+ }
130
+ interface RetractConsequence {
131
+ kind: 'RetractConsequence';
132
+ binding: string;
133
+ }
134
+ interface SetGlobalConsequence {
135
+ kind: 'SetGlobalConsequence';
136
+ expression: string;
137
+ }
138
+ interface RawConsequence {
139
+ kind: 'RawConsequence';
140
+ code: string;
141
+ }
142
+ type Consequence = ModifyConsequence | InsertConsequence | RetractConsequence | SetGlobalConsequence | RawConsequence;
143
+ interface Rule {
144
+ name: string;
145
+ salience?: number;
146
+ agendaGroup?: string;
147
+ noLoop?: boolean;
148
+ lockOnActive?: boolean;
149
+ ruleFlowGroup?: string;
150
+ conditions: Condition[];
151
+ consequences: Consequence[];
152
+ }
153
+ interface DroolsFile {
154
+ name: string;
155
+ imports: string[];
156
+ rules: Rule[];
157
+ }
158
+
159
+ /**
160
+ * Chainable builder for a FactPattern (a bound or unbound pattern in the LHS).
161
+ *
162
+ * Create via the fact() factory function:
163
+ * fact('Player', '$p').field('score', Operator.Gte, '100')
164
+ */
165
+ declare class PatternBuilder {
166
+ private readonly _factType;
167
+ private readonly _binding;
168
+ private readonly _constraints;
169
+ constructor(factType: string, binding?: string);
170
+ /**
171
+ * Add a field constraint: field operator value.
172
+ * Optionally binds the result to a variable: $v : field op value.
173
+ */
174
+ field(field: string, operator: ConstraintOperator, value: string, binding?: string): this;
175
+ /** Bind a field to a variable: $binding : field */
176
+ bind(binding: string, field: string): this;
177
+ /** Emit a verbatim DRL constraint expression. */
178
+ raw(expression: string): this;
179
+ build(): FactPattern;
180
+ }
181
+ /**
182
+ * Chainable builder for an UnboundPattern (used inside not() and exists()
183
+ * where Drools forbids variable bindings).
184
+ *
185
+ * Create via the unbound() factory function:
186
+ * not(unbound('FraudAlert').field('status', Operator.Eq, '"UNRESOLVED"'))
187
+ */
188
+ declare class UnboundPatternBuilder {
189
+ private readonly _factType;
190
+ private readonly _constraints;
191
+ constructor(factType: string);
192
+ field(field: string, operator: ConstraintOperator, value: string): this;
193
+ raw(expression: string): this;
194
+ build(): UnboundPattern;
195
+ }
196
+
197
+ /**
198
+ * Chainable builder for an AccumulatePattern.
199
+ *
200
+ * Create via the accumulate() factory function:
201
+ *
202
+ * accumulate(fact('Transaction', '$tx').field('amount', Operator.Gt, '0'))
203
+ * .fn('$total', Aggregate.Sum, '$tx.amount')
204
+ * .resultConstraint('$total > 1000')
205
+ */
206
+ declare class AccumulateBuilder {
207
+ private readonly _source;
208
+ private readonly _functions;
209
+ private _resultConstraint?;
210
+ constructor(source: Condition);
211
+ /**
212
+ * Register an accumulate function.
213
+ * @param binding - the variable the result is bound to, e.g. '$total'
214
+ * @param func - function name or Aggregate enum, e.g. Aggregate.Sum / 'collectList'
215
+ * @param argument - the argument expression, e.g. '$tx.amount'
216
+ */
217
+ fn(binding: string, func: Aggregate | string, argument: string): this;
218
+ /** Optional result constraint applied to the accumulated value, e.g. '$total > 50' */
219
+ resultConstraint(expression: string): this;
220
+ build(): AccumulatePattern;
221
+ }
222
+
223
+ /**
224
+ * Chainable builder for a ModifyConsequence.
225
+ *
226
+ * Create via the modify() factory function:
227
+ *
228
+ * modify('$account')
229
+ * .call('setStatus', 'Account.Status.FROZEN')
230
+ * .call('setRemarks', '"Frozen by rule"')
231
+ */
232
+ declare class ModifyBuilder {
233
+ private readonly _binding;
234
+ private readonly _modifications;
235
+ constructor(binding: string);
236
+ /** Call a setter/method on the bound fact with the given arguments. */
237
+ call(method: string, ...args: string[]): this;
238
+ build(): ModifyConsequence;
239
+ }
240
+
241
+ /**
242
+ * Restricted builder for the inner condition of not() and exists().
243
+ * Drools only allows UnboundPattern, eval(), or raw DRL inside these wrappers.
244
+ */
245
+ declare class InnerPatternBuilder {
246
+ private _inner?;
247
+ fact(factType: string, fn?: (p: UnboundPatternBuilder) => void): this;
248
+ eval(expression: string): this;
249
+ raw(drl: string): this;
250
+ /** @internal */
251
+ _build(): UnboundPattern | EvalCondition | RawCondition;
252
+ }
253
+ /**
254
+ * Imperative builder for the when-block of a rule.
255
+ * Obtained via RuleBuilder.when(lhs => { ... }).
256
+ *
257
+ * Every method returns `this` for optional chaining.
258
+ */
259
+ declare class LHSBuilder {
260
+ /** @internal collected conditions — consumed by RuleBuilder */
261
+ readonly _conditions: Condition[];
262
+ /** Add a FactPattern with an optional variable binding. */
263
+ fact(factType: string, binding: string, fn?: (p: PatternBuilder) => void): this;
264
+ fact(factType: string, fn?: (p: PatternBuilder) => void): this;
265
+ /** Add a not() wrapper. Inner condition must be unbound (no variable binding). */
266
+ not(fn: (b: InnerPatternBuilder) => void): this;
267
+ /** Add an exists() wrapper. Inner condition must be unbound. */
268
+ exists(fn: (b: InnerPatternBuilder) => void): this;
269
+ /** Add an or() group. Each child is added via the nested LHSBuilder. */
270
+ or(fn: (lhs: LHSBuilder) => void): this;
271
+ /** Add an and() group. Useful when composing nested boolean logic. */
272
+ and(fn: (lhs: LHSBuilder) => void): this;
273
+ /** Add a forall() wrapper. */
274
+ forall(fn: (lhs: LHSBuilder) => void): this;
275
+ /**
276
+ * Add an accumulate pattern.
277
+ * @param sourceFn - builds the source condition (the pattern being accumulated)
278
+ * @param fn - configures the accumulate functions and result constraint
279
+ */
280
+ accumulate(sourceFn: (lhs: LHSBuilder) => void, fn: (acc: AccumulateBuilder) => void): this;
281
+ /**
282
+ * Add a from condition: FactType( constraints ) from expression
283
+ * @param factType - the type to match
284
+ * @param binding - variable binding for the matched fact
285
+ * @param expression - the source expression (collection, method call, etc.)
286
+ * @param fn - optional constraint configuration
287
+ */
288
+ from(factType: string, binding: string, expression: string, fn?: (p: PatternBuilder) => void): this;
289
+ /** Add an eval() condition with a raw boolean expression. */
290
+ eval(expression: string): this;
291
+ /** Add a verbatim DRL condition, emitted as-is. */
292
+ raw(drl: string): this;
293
+ }
294
+
295
+ /**
296
+ * Imperative builder for the then-block of a rule.
297
+ * Obtained via RuleBuilder.then(rhs => { ... }).
298
+ */
299
+ declare class RHSBuilder {
300
+ /** @internal collected consequences — consumed by RuleBuilder */
301
+ readonly _consequences: Consequence[];
302
+ /** modify($binding) { ... } — calls setters on an existing fact. */
303
+ modify(binding: string, fn: (m: ModifyBuilder) => void): this;
304
+ /** insert(objectExpression) — inserts a new fact into working memory. */
305
+ insert(objectExpression: string): this;
306
+ /** retract($binding) — removes a fact from working memory. */
307
+ retract(binding: string): this;
308
+ /** Assign a value to a global variable. */
309
+ global(expression: string): this;
310
+ /** Emit a verbatim DRL statement, emitted as-is. */
311
+ raw(code: string): this;
312
+ }
313
+
314
+ interface Buildable$1<T> {
315
+ build(): T;
316
+ }
317
+ /**
318
+ * Fluent builder for a Drools Rule.
319
+ *
320
+ * Entry point: createRule(name)
321
+ *
322
+ * Two usage styles are supported and can be freely mixed:
323
+ *
324
+ * Style 1 — factory functions (composable, works well for complex rules):
325
+ * createRule('Award Badge')
326
+ * .salience(10)
327
+ * .addCondition(fact('Player', '$p').field('score', Operator.Gte, '100'))
328
+ * .addConsequence(modify('$p').call('awardBadge', '"gold"'))
329
+ * .build()
330
+ *
331
+ * Style 2 — callback blocks (mirrors DRL when/then structure):
332
+ * createRule('Award Badge')
333
+ * .salience(10)
334
+ * .when(lhs => {
335
+ * lhs.fact('Player', '$p', p => p.field('score', Operator.Gte, '100'))
336
+ * })
337
+ * .then(rhs => {
338
+ * rhs.modify('$p', m => m.call('awardBadge', '"gold"'))
339
+ * })
340
+ * .build()
341
+ *
342
+ * Plain metamodel objects are also accepted everywhere a builder is expected,
343
+ * so hand-crafted conditions/consequences can be mixed freely with builders.
344
+ */
345
+ declare class RuleBuilder {
346
+ private readonly _rule;
347
+ constructor(name: string);
348
+ salience(value: number): this;
349
+ agendaGroup(value: string): this;
350
+ /** Defaults to true when called without an argument. */
351
+ noLoop(value?: boolean): this;
352
+ /** Defaults to true when called without an argument. */
353
+ lockOnActive(value?: boolean): this;
354
+ ruleFlowGroup(value: string): this;
355
+ /**
356
+ * Append a single condition.
357
+ * Accepts a plain Condition object or any builder that exposes build().
358
+ */
359
+ addCondition(condition: Condition | Buildable$1<Condition>): this;
360
+ /**
361
+ * Append a single consequence.
362
+ * Accepts a plain Consequence object or any builder that exposes build().
363
+ */
364
+ addConsequence(consequence: Consequence | Buildable$1<Consequence>): this;
365
+ /** Configure the when-block using an LHSBuilder. Appends to existing conditions. */
366
+ when(fn: (lhs: LHSBuilder) => void): this;
367
+ /** Configure the then-block using an RHSBuilder. Appends to existing consequences. */
368
+ then(fn: (rhs: RHSBuilder) => void): this;
369
+ build(): Rule;
370
+ }
371
+ /** Create a new RuleBuilder for a rule with the given name. */
372
+ declare function createRule(name: string): RuleBuilder;
373
+
374
+ /**
375
+ * Chainable builder for a DroolsFile (a collection of rules with shared imports).
376
+ *
377
+ * Create via the createFile() factory:
378
+ *
379
+ * createFile('fraud-rules')
380
+ * .import('com.example.model.Account')
381
+ * .import('com.example.model.FraudAlert')
382
+ * .addRule(
383
+ * createRule('Detect Fraud').when(...).then(...),
384
+ * )
385
+ * .build()
386
+ */
387
+ declare class DroolsFileBuilder {
388
+ private readonly _name;
389
+ private readonly _imports;
390
+ private readonly _rules;
391
+ constructor(name: string);
392
+ /**
393
+ * Add a fully-qualified Java class import.
394
+ * Can be called multiple times — order is preserved.
395
+ *
396
+ * @example
397
+ * .import('com.example.model.Player')
398
+ */
399
+ import(className: string): this;
400
+ /**
401
+ * Add a rule to the file.
402
+ * Accepts a plain Rule object or a RuleBuilder (auto-resolved via .build()).
403
+ *
404
+ * @example
405
+ * .addRule(createRule('My Rule').when(...).then(...))
406
+ * .addRule({ name: 'My Rule', conditions: [], consequences: [] })
407
+ */
408
+ addRule(rule: Rule | RuleBuilder): this;
409
+ build(): DroolsFile;
410
+ }
411
+ /** Create a new DroolsFileBuilder with the given file name. */
412
+ declare function createFile(name: string): DroolsFileBuilder;
413
+
414
+ /**
415
+ * drools-builder — public API
416
+ *
417
+ * Import from this module to access both the metamodel types and the builder API.
418
+ */
419
+
420
+ interface Buildable<T> {
421
+ build(): T;
422
+ }
423
+ type NotExistsInput = UnboundPattern | EvalCondition | RawCondition | Buildable<UnboundPattern>;
424
+ /**
425
+ * Start building a FactPattern.
426
+ *
427
+ * @param factType - the Java class name to match (e.g. 'Player')
428
+ * @param binding - optional variable binding (e.g. '$p')
429
+ *
430
+ * @example
431
+ * fact('Player', '$p').field('score', Operator.Gte, '100')
432
+ */
433
+ declare function fact(factType: string, binding?: string): PatternBuilder;
434
+ /**
435
+ * Start building an UnboundPattern for use inside not() or exists().
436
+ *
437
+ * @example
438
+ * not(unbound('FraudAlert').field('status', Operator.Eq, '"UNRESOLVED"'))
439
+ */
440
+ declare function unbound(factType: string): UnboundPatternBuilder;
441
+ /**
442
+ * Wrap a condition in a Drools not().
443
+ * The inner condition must be unbound (UnboundPatternBuilder / UnboundPattern),
444
+ * an eval(), or a raw condition.
445
+ */
446
+ declare function not(condition: NotExistsInput): NotCondition;
447
+ /**
448
+ * Wrap a condition in a Drools exists().
449
+ * Same constraints as not().
450
+ */
451
+ declare function exists(condition: NotExistsInput): ExistsCondition;
452
+ /**
453
+ * Group conditions with OR semantics.
454
+ *
455
+ * @example
456
+ * or(fact('A').field('x', Operator.Eq, '1'), fact('B').field('y', Operator.Eq, '2'))
457
+ */
458
+ declare function or(...conditions: Array<Condition | Buildable<Condition>>): OrCondition;
459
+ /**
460
+ * Group conditions with AND semantics.
461
+ * Mostly used when building nested boolean logic inside or().
462
+ */
463
+ declare function and(...conditions: Array<Condition | Buildable<Condition>>): AndCondition;
464
+ /**
465
+ * Wrap a condition in a Drools forall().
466
+ *
467
+ * @example
468
+ * forall(fact('Transaction', '$tx').field('validated', Operator.Eq, 'true'))
469
+ */
470
+ declare function forall(condition: Condition | Buildable<Condition>): ForallCondition;
471
+ /**
472
+ * Start building an AccumulatePattern.
473
+ *
474
+ * @example
475
+ * accumulate(fact('Transaction', '$tx').field('amount', Operator.Gt, '0'))
476
+ * .fn('$total', Aggregate.Sum, '$tx.amount')
477
+ * .resultConstraint('$total > 1000')
478
+ */
479
+ declare function accumulate(source: Condition | Buildable<Condition>): AccumulateBuilder;
480
+ /**
481
+ * Build a FromCondition: pattern from expression.
482
+ *
483
+ * @example
484
+ * from_(fact('Transaction', '$tx'), '$recentTxs')
485
+ */
486
+ declare function from_(pattern: PatternBuilder, expression: string): FromCondition;
487
+ /**
488
+ * Build an EvalCondition with a raw boolean expression.
489
+ *
490
+ * @example
491
+ * eval_('$p.getScore() > threshold')
492
+ */
493
+ declare function eval_(expression: string): EvalCondition;
494
+ /**
495
+ * Build a RawCondition — verbatim DRL, emitted as-is.
496
+ * Use as an escape hatch for constructs not covered by the builder.
497
+ */
498
+ declare function rawCondition(drl: string): RawCondition;
499
+ /**
500
+ * Start building a ModifyConsequence.
501
+ *
502
+ * @example
503
+ * modify('$account').call('setStatus', 'Account.Status.FROZEN')
504
+ */
505
+ declare function modify(binding: string): ModifyBuilder;
506
+ /**
507
+ * Build an InsertConsequence — inserts a new fact into working memory.
508
+ *
509
+ * @example
510
+ * insert('new FraudAlert()')
511
+ */
512
+ declare function insert(objectExpression: string): InsertConsequence;
513
+ /**
514
+ * Build a RetractConsequence — removes a fact from working memory.
515
+ *
516
+ * @example
517
+ * retract('$obsoleteAlert')
518
+ */
519
+ declare function retract(binding: string): RetractConsequence;
520
+ /**
521
+ * Build a SetGlobalConsequence — assign a value to a global variable.
522
+ *
523
+ * @example
524
+ * setGlobal('alertService.notify($account)')
525
+ */
526
+ declare function setGlobal(expression: string): SetGlobalConsequence;
527
+ /**
528
+ * Build a RawConsequence — verbatim Java/DRL code, emitted as-is.
529
+ * Use as an escape hatch for logic not covered by the builder.
530
+ */
531
+ declare function rawConsequence(code: string): RawConsequence;
532
+
533
+ declare const DRLToMetaTransformer: {
534
+ parse(drl: string): DroolsFile;
535
+ parseRule(block: string): Rule;
536
+ };
537
+
538
+ declare const MetaToDRLTransformer: {
539
+ generate(file: DroolsFile): string;
540
+ generateRule(rule: Rule): string;
541
+ };
542
+
543
+ export { AccumulateBuilder, type AccumulateFunction, type AccumulatePattern, Aggregate, type AndCondition, type BindingConstraint, type Condition, type Consequence, type Constraint, type ConstraintOperator, DRLToMetaTransformer, type DroolsFile, DroolsFileBuilder, type EvalCondition, type ExistsCondition, type FactPattern, type FactType, type FieldConstraint, type ForallCondition, type FromCondition, type InsertConsequence, LHSBuilder, MetaToDRLTransformer, type Modification, ModifyBuilder, type ModifyConsequence, type NotCondition, Operator, type OrCondition, PatternBuilder, RHSBuilder, type RawCondition, type RawConsequence, type RawConstraint, type RetractConsequence, type Rule, RuleBuilder, type SetGlobalConsequence, type UnboundPattern, UnboundPatternBuilder, accumulate, and, createFile, createRule, eval_, exists, fact, forall, from_, insert, modify, not, or, rawCondition, rawConsequence, retract, setGlobal, unbound };