rust-rule-engine 1.20.1

A blazing-fast Rust rule engine with RETE algorithm, backward chaining inference, and GRL (Grule Rule Language) syntax. Features: forward/backward chaining, pattern matching, unification, O(1) rule indexing, TMS, expression evaluation, method calls, streaming with Redis state backend, watermarking, and custom functions. Production-ready for business rules, expert systems, real-time stream processing, and decision automation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# Advanced GRL Features

Advanced features of GRL and the rule engine.

## Files

### 1. pattern_matching.grl
**5 rules** - Advanced pattern matching

Features:
- exists, forall, combined patterns
- Complex boolean expressions
- Nested conditions
- Pattern composition

**Example:**
```grl
rule "ComplexPattern" {
    when
        exists(User.Status == "ACTIVE") &&
        forall(Order.IsPaid == true) &&
        !exists(Alert.Level == "CRITICAL")
    then
        ProcessWorkflow();
}
```

### 2. advanced_nested_rules.grl
**3 rules** - Complex boolean operators

Features:
- Deeply nested conditions
- Multiple AND/OR combinations
- Parentheses grouping
- Complex logic trees

**Example:**
```grl
rule "NestedConditions" {
    when
        (User.Age >= 18 && User.Country == "US") ||
        (User.Age >= 21 && User.Country == "JP") ||
        (User.IsVerified == true && User.Premium == true)
    then
        GrantAccess();
}
```

### 3. advanced_method_calls.grl
**4 rules** - Advanced method chaining

Features:
- Method chaining
- Complex method calls
- Return value handling
- Multiple method invocations

**Example:**
```grl
rule "MethodChaining" {
    when
        User.GetProfile().GetPreferences().IsEmailEnabled() == true
    then
        User.GetNotificationService().SendEmail();
}
```

### 4. conflict_resolution_rules.grl
**7 rules** - Conflict resolution strategies

Features:
- Salience (priority) values
- Different complexity levels
- Conflict resolution demo
- Execution order control

**Rule priorities:**
```grl
rule "HighPriority" salience 100 { ... }
rule "MediumPriority" salience 50 { ... }
rule "LowPriority" salience 10 { ... }
```

**Strategies:**
- Salience-based (explicit priority)
- Complexity-based (implicit)
- FIFO (first in, first out)
- LIFO (last in, first out)

### 5. action_handlers.grl
**6 rules** - Action handlers with function calls

Features:
- Custom action handlers
- Function calls in actions
- Side effects management
- External integrations

**Example:**
```grl
rule "NotifyUser" {
    when
        Order.Status == "COMPLETED"
    then
        SendEmailNotification(Order.CustomerEmail, "Order completed");
        LogEvent("ORDER_COMPLETED", Order.Id);
        UpdateInventory(Order.Items);
}
```

### 6. retract_demo.grl
**4 rules** - Fact retraction

Features:
- Retract facts from working memory
- Dynamic fact management
- Clean up after processing
- Memory management

**Example:**
```grl
rule "ProcessAndRetract" {
    when
        TempData.IsProcessed == false
    then
        ProcessData(TempData);
        TempData.IsProcessed = true;
        Retract("TempData");  // Remove from memory
}
```

**Use cases:**
- Temporary data cleanup
- State machine transitions
- Memory optimization
- Event processing

### 7. tms_demo.grl
**8 rules** - Truth Maintenance System

Features:
- Logical dependencies tracking
- Belief revision
- Automatic retraction
- Dependency management

**Example:**
```grl
rule "DerivedFact" {
    when
        Person.Age >= 18
    then
        Assert("Adult", Person);  // Logical assertion
}

rule "Cleanup" {
    when
        Person.Age < 18
    then
        Retract("Adult");  // TMS auto-retracts dependent facts
}
```

**TMS benefits:**
- Automatic consistency maintenance
- Dependency tracking
- No manual cleanup needed
- Prevents inconsistencies

## Advanced Concepts

### 1. Salience (Priority)

Controls execution order when multiple rules are activated:

```grl
// Highest priority - runs first
rule "CriticalValidation" salience 100 {
    when
        Data.IsValid == false
    then
        StopProcessing();
}

// Medium priority
rule "NormalProcessing" salience 50 {
    when
        Data.IsReady == true
    then
        Process();
}

// Lowest priority - cleanup
rule "Cleanup" salience 1 {
    when
        Processing.IsComplete == true
    then
        CleanupResources();
}
```

**Default salience**: 0
**Range**: -1000 to 1000 (typically)

### 2. No-Loop Attribute

Prevent infinite loops:

```grl
rule "UpdateAge" no-loop {
    when
        Person.BirthYear > 0
    then
        Person.Age = CurrentYear - Person.BirthYear;
        // Without no-loop, this might trigger the rule again
}
```

### 3. Agenda Groups

Organize rules into groups:

```grl
rule "Validation" agenda-group "validate" {
    when
        Data.Status == "NEW"
    then
        ValidateData();
}

rule "Processing" agenda-group "process" {
    when
        Data.Status == "VALIDATED"
    then
        ProcessData();
}
```

Execute by group:
```rust
engine.set_focus("validate");
engine.run();
engine.set_focus("process");
engine.run();
```

### 4. Activation Groups

Only one rule in group can fire:

```grl
rule "LargeDiscount" activation-group "discount" {
    when
        Order.Total > 1000
    then
        Order.Discount = 20;
}

rule "MediumDiscount" activation-group "discount" {
    when
        Order.Total > 500
    then
        Order.Discount = 10;
}
// Only the first matching rule fires
```

### 5. Date Effective/Expires

Time-based activation:

```grl
rule "HolidaySale"
    date-effective "2024-12-01"
    date-expires "2024-12-31" {
    when
        Order.Total > 0
    then
        Order.Discount = 15;
}
```

## Pattern Matching Patterns

### Complex Combinations
```grl
rule "ComplexMatch" {
    when
        // At least one premium user
        exists(User.IsPremium == true) &&

        // All orders are paid
        forall(Order.Status == "PAID") &&

        // No pending issues
        !exists(Issue.Status == "OPEN") &&

        // System ready
        System.IsOnline == true
    then
        StartProcessing();
}
```

### Nested Quantifiers
```grl
rule "NestedQuantifiers" {
    when
        exists(
            User.Role == "ADMIN" &&
            forall(User.Permissions.Contains("WRITE"))
        )
    then
        EnableAdminFeatures();
}
```

## Performance Tips

### 1. Specific Patterns First
```grl
// Good - most selective first
when
    User.Id == 12345 && User.IsActive == true

// Less optimal
when
    User.IsActive == true && User.Id == 12345
```

### 2. Minimize Computation in When
```grl
// Good - simple comparison
when
    Order.Total > 1000

// Bad - complex calculation
when
    (Order.ItemCount * Order.AveragePrice * 1.1) > 1000
```

### 3. Use Salience Wisely
Don't overuse - let engine optimize when possible.

### 4. Retract When Done
Clean up temporary facts to reduce memory usage.

## Integration with Rust

```rust
// Load advanced rules
engine.add_grl_file("rules/03-advanced/conflict_resolution_rules.grl")?;

// Set conflict resolution strategy
engine.set_conflict_resolution_strategy(ConflictResolutionStrategy::Salience)?;

// Enable TMS
engine.enable_truth_maintenance(true)?;

// Run with advanced features
engine.run()?;
```

## Run Examples

```bash
# Conflict resolution
cargo run --example conflict_resolution_demo

# Action handlers
cargo run --example action_handlers_demo

# Pattern matching
cargo run --example pattern_matching_from_grl

# TMS demo
cargo run --example tms_demo
```

## Common Pitfalls

### 1. Infinite Loops
```grl
// BAD - will loop forever
rule "UpdateValue" {
    when
        Data.Value < 100
    then
        Data.Value = Data.Value + 1;
}

// GOOD - use no-loop or better condition
rule "UpdateValue" no-loop {
    when
        Data.Value < 100 && Data.Updated == false
    then
        Data.Value = 100;
        Data.Updated = true;
}
```

### 2. Salience Overuse
```grl
// Don't do this for every rule
rule "Rule1" salience 100 { ... }
rule "Rule2" salience 99 { ... }
rule "Rule3" salience 98 { ... }

// Better - only when necessary
rule "CriticalRule" salience 100 { ... }
rule "NormalRule1" { ... }  // default salience
rule "NormalRule2" { ... }
```

### 3. Missing Retraction
```grl
// Memory leak - temporary facts accumulate
rule "CreateTemp" {
    when
        Data.NeedsProcessing == true
    then
        Assert("TempData", new TempData());
}

// Better - retract when done
rule "ProcessTemp" {
    when
        TempData.IsProcessed == true
    then
        Retract("TempData");
}
```

## Next Steps

- `04-use-cases/` - Apply advanced features in production
- `07-advanced-rete/` - RETE-specific advanced features
- Performance tuning in `05-performance/`