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.
- package/LICENSE +21 -0
- package/README.md +128 -0
- package/dist/index.cjs +1018 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +543 -0
- package/dist/index.d.ts +543 -0
- package/dist/index.js +962 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Luca Francesco Macera
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# drools-builder
|
|
2
|
+
|
|
3
|
+
A TypeScript library for building, parsing, and generating Drools rules programmatically.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install drools-builder
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Building rules
|
|
14
|
+
|
|
15
|
+
Two styles are supported and can be freely mixed.
|
|
16
|
+
|
|
17
|
+
### Factory functions
|
|
18
|
+
|
|
19
|
+
Best for composing and reusing conditions independently.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createFile, createRule, fact, not, unbound, modify, insert, Operator, Aggregate } from 'drools-builder'
|
|
23
|
+
|
|
24
|
+
const rule = createRule('Fraud Detection')
|
|
25
|
+
.salience(100)
|
|
26
|
+
.agendaGroup('fraud-evaluation')
|
|
27
|
+
.noLoop()
|
|
28
|
+
.addCondition(
|
|
29
|
+
fact('Account', '$account')
|
|
30
|
+
.field('status', Operator.Eq, 'Account.Status.ACTIVE')
|
|
31
|
+
.bind('$accountId', 'id'),
|
|
32
|
+
)
|
|
33
|
+
.addCondition(
|
|
34
|
+
not(unbound('FraudAlert').field('accountId', Operator.Eq, '$accountId')),
|
|
35
|
+
)
|
|
36
|
+
.addCondition(
|
|
37
|
+
accumulate(fact('Transaction', '$tx').field('amount', Operator.Gt, '1500.00'))
|
|
38
|
+
.fn('$recentTxs', Aggregate.CollectList, '$tx')
|
|
39
|
+
.resultConstraint('size >= 3'),
|
|
40
|
+
)
|
|
41
|
+
.addConsequence(modify('$account').call('setStatus', 'Account.Status.FROZEN'))
|
|
42
|
+
.addConsequence(insert('new FraudAlert()'))
|
|
43
|
+
.build()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Callback blocks
|
|
47
|
+
|
|
48
|
+
Mirrors the DRL `when`/`then` structure.
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const rule = createRule('Fraud Detection')
|
|
52
|
+
.salience(100)
|
|
53
|
+
.noLoop()
|
|
54
|
+
.when(lhs => {
|
|
55
|
+
lhs.fact('Account', '$account', p => p
|
|
56
|
+
.field('status', Operator.Eq, 'Account.Status.ACTIVE')
|
|
57
|
+
.bind('$accountId', 'id'),
|
|
58
|
+
)
|
|
59
|
+
lhs.not(b => b.fact('FraudAlert', p => p
|
|
60
|
+
.field('accountId', Operator.Eq, '$accountId'),
|
|
61
|
+
))
|
|
62
|
+
})
|
|
63
|
+
.then(rhs => {
|
|
64
|
+
rhs.modify('$account', m => m.call('setStatus', 'Account.Status.FROZEN'))
|
|
65
|
+
rhs.insert('new FraudAlert()')
|
|
66
|
+
})
|
|
67
|
+
.build()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Plain metamodel objects are accepted everywhere a builder is expected.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Building a file
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const file = createFile('fraud-rules')
|
|
78
|
+
.import('com.example.Account')
|
|
79
|
+
.import('com.example.FraudAlert')
|
|
80
|
+
.addRule(rule)
|
|
81
|
+
.addRule(createRule('Another Rule').when(...).then(...))
|
|
82
|
+
.build()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Generating DRL
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { MetaToDRLTransformer } from 'drools-builder'
|
|
91
|
+
|
|
92
|
+
const drl = MetaToDRLTransformer.generate(file) // full file
|
|
93
|
+
const drl = MetaToDRLTransformer.generateRule(rule) // single rule
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Parsing DRL
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { DRLToMetaTransformer } from 'drools-builder'
|
|
102
|
+
|
|
103
|
+
const file = DRLToMetaTransformer.parse(drlString) // full file → DroolsFile
|
|
104
|
+
const rule = DRLToMetaTransformer.parseRule(ruleBlock) // single rule → Rule
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Enums
|
|
110
|
+
|
|
111
|
+
Use `Operator` and `Aggregate` instead of raw strings to avoid typos.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { Operator, Aggregate } from 'drools-builder'
|
|
115
|
+
|
|
116
|
+
Operator.Eq // '=='
|
|
117
|
+
Operator.Gte // '>='
|
|
118
|
+
Operator.NotContains // 'not contains'
|
|
119
|
+
|
|
120
|
+
Aggregate.Sum // 'sum'
|
|
121
|
+
Aggregate.CollectList // 'collectList'
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT
|