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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# Backward Chaining Quick Start Guide

> **Category:** Guides
> **Version:** 1.17.0+
> **Last Updated:** January 19, 2026
> **Estimated Time:** 10 minutes

Goal-driven inference with backward chaining - from zero to queries in 10 minutes!

---

## 🎯 What is Backward Chaining?

**Backward chaining** is goal-driven reasoning: Start with a question, work backwards to find if it's provable.

```
Question: "Is this customer VIP?"
Search for rules that conclude VIP status
Check conditions needed for those rules
Use facts or recursively prove sub-goals
Answer: "Yes, provable!" or "No, not provable"
```

**Use Cases:**
- Expert systems & diagnostics
- Decision support systems
- Query answering with caching
- AI reasoning
- Complex eligibility checks

**Features (v1.17.0+):**
- ✅ Unification with variable bindings
- ✅ Search strategies: DFS, BFS, Iterative Deepening
- ✅ Aggregation: COUNT, SUM, AVG, MIN, MAX
- ✅ Negation (NOT) with closed-world assumption
- ✅ Disjunction (OR) for alternative paths
- ✅ Nested queries (subqueries)
- ✅ Query optimization (automatic goal reordering)
-**ProofGraph caching with TMS integration (NEW!)** 🚀
- Query answering
- AI reasoning
- Complex eligibility checks

---

## 🚀 Quick Start (3 Steps)

### Step 1: Add Dependency

```toml
[dependencies.rust-rule-engine]
version = "1.11"
features = ["backward-chaining"]
```

### Step 2: Create Engine & Add Rules

```rust
use rust_rule_engine::backward::BackwardEngine;
use rust_rule_engine::{KnowledgeBase, Facts, Value};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create knowledge base
    let mut kb = KnowledgeBase::new("demo");

    // Add rules using GRL syntax
    kb.add_rule_from_string(r#"
        rule "VIP Status" {
            when
                Customer.TotalSpent > 5000 &&
                Customer.YearsMember > 2
            then
                Customer.IsVIP = true;
        }
    "#)?;

    kb.add_rule_from_string(r#"
        rule "Adult Check" {
            when
                User.Age >= 18
            then
                User.IsAdult = true;
        }
    "#)?;

    // Create backward chaining engine
    let mut bc_engine = BackwardEngine::new(kb);

    Ok(())
}
```

### Step 3: Query & Get Results

```rust
// Set up facts
let mut facts = Facts::new();
facts.set("Customer.TotalSpent", Value::Number(6000.0));
facts.set("Customer.YearsMember", Value::Integer(3));

// Ask a question
let result = bc_engine.query("Customer.IsVIP == true", &mut facts)?;

// Check result
if result.provable {
    println!("✅ Customer IS VIP!");
    println!("Solutions found: {}", result.solutions.len());
} else {
    println!("❌ Customer is NOT VIP");
}
```

**Output:**
```
✅ Customer IS VIP!
Solutions found: 1
```

---

## ✨ New in v1.11.0

### 1. Nested Queries (Subqueries)

Ask complex questions with WHERE clauses:

```rust
use rust_rule_engine::backward::{GRLQueryParser, GRLQueryExecutor};

let query_str = r#"
query "Find High-Value Active Customers" {
    goal: qualified(?customer) WHERE
        (high_value(?customer) WHERE total_spent(?customer, ?amt) AND ?amt > 10000)
        AND active(?customer)
    enable-optimization: true
    max-depth: 20

    on-success: {
        Customer.Tier = "platinum";
        Print("Found qualified customer");
    }
}
"#;

let query = GRLQueryParser::parse(query_str)?;
let result = GRLQueryExecutor::execute(&query, &mut bc_engine, &mut facts)?;
```

**Features:**
- Multi-level nesting
- Shared variables between queries
- OR/AND combinations
- NOT negation support

### 2. Query Optimization (10-100x Speedup!)

Enable automatic goal reordering for massive performance gains:

```rust
let query_str = r#"
query "Optimized Search" {
    goal: item(?x) AND expensive(?x) AND in_stock(?x) AND category(?x, "Premium")
    enable-optimization: true

    on-success: {
        Results.Add = x;
    }
}
"#;
```

**Without Optimization:**
```
item(?x)        → 10,000 items checked
expensive(?x)   → 3,000 remaining
in_stock(?x)    → 300 remaining
category(?x)    → 50 final results
Total: 13,350 evaluations
```

**With Optimization:**
```
category(?x)    → 500 items (most selective)
in_stock(?x)    → 50 remaining
expensive(?x)   → 30 remaining
item(?x)        → 30 final
Total: 610 evaluations (~22x faster!)
```

---

## 📚 Complete Example: Medical Diagnosis

```rust
use rust_rule_engine::backward::{BackwardEngine, GRLQueryParser, GRLQueryExecutor};
use rust_rule_engine::{KnowledgeBase, Facts, Value};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create knowledge base with medical rules
    let mut kb = KnowledgeBase::new("medical");

    kb.add_rule_from_string(r#"
        rule "Flu Diagnosis" {
            when
                Patient.HasFever == true &&
                Patient.HasCough == true &&
                Patient.HasFatigue == true
            then
                Diagnosis.Disease = "Influenza";
                Diagnosis.Confidence = "high";
        }
    "#)?;

    kb.add_rule_from_string(r#"
        rule "Fever from High WBC" {
            when
                Patient.WhiteBloodCellCount > 11000
            then
                Patient.HasFever = true;
        }
    "#)?;

    kb.add_rule_from_string(r#"
        rule "Fatigue from Fever" {
            when
                Patient.HasFever == true &&
                Patient.DaysSick > 2
            then
                Patient.HasFatigue = true;
        }
    "#)?;

    // Create engine
    let mut bc_engine = BackwardEngine::new(kb);

    // Set patient facts
    let mut facts = Facts::new();
    facts.set("Patient.WhiteBloodCellCount", Value::Number(12000.0));
    facts.set("Patient.HasCough", Value::Boolean(true));
    facts.set("Patient.DaysSick", Value::Integer(3));

    // Query: Does patient have flu?
    let result = bc_engine.query(
        "Diagnosis.Disease == \"Influenza\"",
        &mut facts
    )?;

    if result.provable {
        println!("✅ Diagnosis: Flu confirmed!");
        println!("Reasoning chain:");
        println!("  1. High WBC (12000) → HasFever = true");
        println!("  2. HasFever + DaysSick > 2 → HasFatigue = true");
        println!("  3. HasFever + HasCough + HasFatigue → Influenza");
    } else {
        println!("❌ Cannot confirm flu diagnosis");
    }

    Ok(())
}
```

**Output:**
```
✅ Diagnosis: Flu confirmed!
Reasoning chain:
  1. High WBC (12000) → HasFever = true
  2. HasFever + DaysSick > 2 → HasFatigue = true
  3. HasFever + HasCough + HasFatigue → Influenza
```

---

## 🎯 Key Features

### Aggregation Functions (v1.7.0+)

```rust
let query = r#"
query "Total Sales" {
    goal: sum(?amount) WHERE sale(?id, ?amount) AND ?amount > 100
    on-success: {
        Report.TotalSales = result;
    }
}
"#;
```

**Supported:** COUNT, SUM, AVG, MIN, MAX, FIRST, LAST

### Negation (v1.8.0+)

```rust
let result = bc_engine.query(
    "NOT Customer.IsBanned == true",
    &mut facts
)?;
// Succeeds if customer is NOT banned
```

### Disjunction - OR (v1.10.0+)

```rust
let query = r#"
query "Priority Customer" {
    goal: (Customer.IsVIP == true OR Customer.TotalSpent > 10000)
          AND Customer.IsActive == true
}
"#;
```

### Explanation System (v1.9.0+)

Get proof trees showing reasoning:

```rust
use rust_rule_engine::backward::explanation::ProofTree;

let result = bc_engine.query("goal", &mut facts)?;

// Generate explanation
let tree = result.explanation;
tree.print();  // Console output

// Export formats
let json = tree.to_json()?;
let markdown = tree.to_markdown();
let html = tree.to_html();
```

---

## 🔧 Search Strategies

Choose the right strategy for your use case:

### Depth-First (Default)
```rust
let mut bc_engine = BackwardEngine::new(kb);
bc_engine.set_strategy(SearchStrategy::DepthFirst);
```

**Best for:** Most queries, memory-efficient

### Breadth-First
```rust
bc_engine.set_strategy(SearchStrategy::BreadthFirst);
```

**Best for:** Finding shortest proof path

### Iterative Deepening
```rust
bc_engine.set_strategy(SearchStrategy::IterativeDeepening);
```

**Best for:** Unknown depth queries, optimal solutions

---

## 📝 GRL Query Syntax

Write queries in GRL files for better organization:

**queries/eligibility.grl:**
```grl
query "VIP Eligibility" {
    goal: eligible(?customer) WHERE
        (vip(?customer) OR (premium(?customer) AND loyalty(?customer, ?years) AND ?years > 3))
        AND active(?customer)
        AND NOT suspended(?customer)

    strategy: depth-first
    max-depth: 20
    max-solutions: 10
    enable-optimization: true
    enable-memoization: true

    on-success: {
        Customer.Eligible = true;
        Customer.Benefits = "full_access";
        Print("Customer is eligible");
    }

    on-failure: {
        Customer.Eligible = false;
        Print("Customer not eligible");
    }

    on-missing: {
        Print("Missing required customer data");
        Request.AdditionalInfo = true;
    }
}
```

**Load and execute:**
```rust
use std::fs;

let grl_content = fs::read_to_string("queries/eligibility.grl")?;
let query = GRLQueryParser::parse(&grl_content)?;
let result = GRLQueryExecutor::execute(&query, &mut bc_engine, &mut facts)?;
```

---

## 🚀 Performance Tips

### 1. Enable Optimization for Multi-Goal Queries
```grl
enable-optimization: true  // 10-100x speedup!
```

### 2. Use Memoization
```grl
enable-memoization: true  // Cache proven goals
```

### 3. Set Appropriate Depth Limits
```grl
max-depth: 20  // Prevent infinite loops
```

### 4. Limit Solutions When Appropriate
```grl
max-solutions: 10  // Stop after finding 10
```

### 5. Choose Right Strategy
- **Depth-first**: Most queries (default)
- **Breadth-first**: Shortest path needed
- **Iterative**: Unknown complexity

---

## 🔄 Combine with Forward Chaining

Use both for hybrid reasoning:

```rust
use rust_rule_engine::Engine;

// Forward chaining for reactive rules
let mut fc_engine = Engine::new();
fc_engine.add_rule_from_string(r#"
    rule "Update Status" {
        when Order.Total > 1000
        then Order.Status = "high_value";
    }
"#)?;

// Backward chaining for queries
let mut bc_engine = BackwardEngine::new(kb);

// Run forward chaining first
fc_engine.run(&mut facts)?;

// Then query with backward chaining
let result = bc_engine.query("Order.Status == \"high_value\"", &mut facts)?;
```

See [Integration Guide](guides/BACKWARD_CHAINING_RETE_INTEGRATION.md) for details.

---

## 📖 Next Steps

### Learn More
- **[GRL Query Syntax]api-reference/GRL_QUERY_SYNTAX.md** - Complete query language reference
- **[API Reference]api-reference/API_REFERENCE.md** - Full API documentation
- **[Troubleshooting]guides/TROUBLESHOOTING.md** - Common issues and solutions

### Examples
- **[Nested Queries Demo]../examples/09-backward-chaining/nested_query_demo.rs**
- **[Query Optimization Demo]../examples/09-backward-chaining/optimizer_demo.rs**
- **[GRL File Demo]../examples/09-backward-chaining/nested_grl_file_demo.rs**

### Run Examples
```bash
# Nested queries
cargo run --example nested_query_demo --features backward-chaining

# Query optimization
cargo run --example optimizer_demo --features backward-chaining

# GRL integration
cargo run --example grl_optimizer_demo --features backward-chaining
```

---

## 💡 Common Patterns

### Pattern 1: Eligibility Check
```rust
query "Check Eligibility" {
    goal: eligible(?person) WHERE
        age_requirement(?person) AND
        income_requirement(?person) AND
        NOT disqualified(?person)
}
```

### Pattern 2: Hierarchical Relationships
```rust
query "Find Ancestors" {
    goal: ancestor(?a, ?d) WHERE
        parent(?a, ?p) AND
        (parent(?p, ?d) OR ancestor(?p, ?d))
}
```

### Pattern 3: Complex Business Rules
```rust
query "Approve Transaction" {
    goal: approved(?txn) WHERE
        (low_risk(?txn) OR (medium_risk(?txn) AND manual_review(?txn)))
        AND NOT fraud_detected(?txn)
        AND within_limits(?txn)
}
```

---

## 🐛 Troubleshooting

### Query Not Provable?
1. Check facts are set correctly
2. Verify rule conditions match fact names
3. Check for typos in field names
4. Ensure rules are added to knowledge base

### Performance Issues?
1. Enable optimization: `enable-optimization: true`
2. Reduce max-depth if too high
3. Use memoization: `enable-memoization: true`
4. Profile with statistics

### Need Help?
- 📖 [Troubleshooting Guide]guides/TROUBLESHOOTING.md
- 💬 [GitHub Discussions]https://github.com/KSD-CO/rust-rule-engine/discussions
- 🐛 [Report Issue]https://github.com/KSD-CO/rust-rule-engine/issues

---

## Navigation

📚 **[Documentation Home]README.md** | 📖 **[Getting Started]getting-started/QUICK_START.md** | 🔍 **[GRL Query Syntax]api-reference/GRL_QUERY_SYNTAX.md**

**Related:**
- [Forward Chaining]core-features/FORWARD_CHAINING.md
- [RETE Integration]guides/BACKWARD_CHAINING_RETE_INTEGRATION.md
- [API Reference]api-reference/API_REFERENCE.md