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
# API Reference

Complete API reference for rust-rule-engine.

---

## Core Types

### RustRuleEngine

Main engine for Native mode.

```rust
pub struct RustRuleEngine { /* ... */ }

impl RustRuleEngine {
    // Creation
    pub fn new() -> Self
    
    // Rule Loading
    pub fn load_rules_from_file(&mut self, path: &str) -> Result<()>
    pub fn load_rules_from_string(&mut self, content: &str) -> Result<()>
    pub fn add_rule(&mut self, rule: Rule) -> Result<()>
    
    // Execution
    pub fn execute(&mut self, facts: &mut Facts) -> Result<usize>
    pub fn execute_with_limit(&mut self, facts: &mut Facts, max: usize) -> Result<usize>
    
    // Plugin Management
    pub fn load_plugin(&mut self, plugin: Box<dyn RulePlugin>) -> Result<()>
    pub fn load_default_plugins(&mut self) -> Result<()>
    pub fn unload_plugin(&mut self, name: &str) -> Result<()>
    
    // Query
    pub fn list_rules(&self) -> Vec<&Rule>
    pub fn get_rule(&self, name: &str) -> Option<&Rule>
    pub fn remove_rule(&mut self, name: &str) -> Result<()>
}
```

---

### Facts

Key-value storage for rule data.

```rust
pub struct Facts { /* ... */ }

impl Facts {
    pub fn new() -> Self
    pub fn set(&mut self, key: &str, value: Value)
    pub fn get(&self, key: &str) -> Option<&Value>
    pub fn has(&self, key: &str) -> bool
    pub fn remove(&mut self, key: &str) -> Option<Value>
    pub fn clear(&mut self)
    pub fn keys(&self) -> Vec<&String>
    pub fn len(&self) -> usize
    pub fn is_empty(&self) -> bool
}
```

---

### Value

Supported data types.

```rust
pub enum Value {
    String(String),
    Integer(i64),
    Float(f64),
    Boolean(bool),
    Array(Vec<Value>),
    Object(HashMap<String, Value>),
    Null,
}

impl Value {
    pub fn as_string(&self) -> Option<&String>
    pub fn as_integer(&self) -> Option<i64>
    pub fn as_float(&self) -> Option<f64>
    pub fn as_boolean(&self) -> Option<bool>
    pub fn as_array(&self) -> Option<&Vec<Value>>
    pub fn as_object(&self) -> Option<&HashMap<String, Value>>
}
```

---

## RETE Types

### IncrementalEngine

Main RETE-UL engine.

```rust
pub struct IncrementalEngine { /* ... */ }

impl IncrementalEngine {
    pub fn new() -> Self
    
    // Rule Management
    pub fn add_rule(&mut self, rule: TypedReteUlRule, depends_on: Vec<String>)
    
    // Fact Operations
    pub fn insert(&mut self, fact_type: String, data: TypedFacts) -> FactHandle
    pub fn update(&mut self, handle: FactHandle, data: TypedFacts) -> Result<(), String>
    pub fn retract(&mut self, handle: FactHandle) -> Result<(), String>
    
    // Template Operations (v0.10.0)
    pub fn templates(&self) -> &TemplateRegistry
    pub fn templates_mut(&mut self) -> &mut TemplateRegistry
    pub fn insert_with_template(&mut self, name: &str, data: TypedFacts) -> Result<FactHandle>
    
    // Global Variables (v0.10.0)
    pub fn globals(&self) -> &GlobalsRegistry
    pub fn globals_mut(&mut self) -> &mut GlobalsRegistry
    
    // Execution
    pub fn fire_all(&mut self) -> Vec<String>
    pub fn reset(&mut self)
    
    // Query
    pub fn working_memory(&self) -> &WorkingMemory
    pub fn working_memory_mut(&mut self) -> &mut WorkingMemory
    pub fn agenda(&self) -> &AdvancedAgenda
    pub fn stats(&self) -> IncrementalEngineStats
}
```

---

### TypedFacts

Strongly-typed facts for RETE.

```rust
pub struct TypedFacts { /* ... */ }

impl TypedFacts {
    pub fn new() -> Self
    pub fn set(&mut self, key: &str, value: FactValue)
    pub fn get(&self, key: &str) -> Option<&FactValue>
    pub fn has(&self, key: &str) -> bool
    pub fn remove(&mut self, key: &str) -> Option<FactValue>
}
```

---

### FactValue

RETE fact value types.

```rust
pub enum FactValue {
    String(String),
    Integer(i64),
    Float(f64),
    Boolean(bool),
    Array(Vec<FactValue>),
    Null,
}
```

---

### TemplateBuilder (v0.10.0)

Build type-safe templates.

```rust
pub struct TemplateBuilder { /* ... */ }

impl TemplateBuilder {
    pub fn new(name: impl Into<String>) -> Self
    
    // Field Definitions
    pub fn string_field(self, name: impl Into<String>) -> Self
    pub fn required_string(self, name: impl Into<String>) -> Self
    pub fn integer_field(self, name: impl Into<String>) -> Self
    pub fn float_field(self, name: impl Into<String>) -> Self
    pub fn boolean_field(self, name: impl Into<String>) -> Self
    pub fn array_field(self, name: impl Into<String>, element_type: FieldType) -> Self
    pub fn field_with_default(self, name: impl Into<String>, field_type: FieldType, default: FactValue) -> Self
    
    pub fn build(self) -> Template
}
```

---

### GlobalsRegistry (v0.10.0)

Global variables management.

```rust
pub struct GlobalsRegistry { /* ... */ }

impl GlobalsRegistry {
    pub fn new() -> Self
    
    // Define
    pub fn define(&self, name: impl Into<String>, value: FactValue) -> Result<()>
    pub fn define_readonly(&self, name: impl Into<String>, value: FactValue) -> Result<()>
    
    // Access
    pub fn get(&self, name: &str) -> Result<FactValue>
    pub fn set(&self, name: &str, value: FactValue) -> Result<()>
    pub fn exists(&self, name: &str) -> bool
    
    // Operations
    pub fn increment(&self, name: &str, delta: f64) -> Result<()>
    pub fn remove(&self, name: &str) -> Result<()>
    pub fn list_globals(&self) -> Vec<String>
    pub fn get_all(&self) -> HashMap<String, FactValue>
    pub fn clear(&self)
}
```

---

### GrlReteLoader

Load GRL files into RETE engine.

```rust
pub struct GrlReteLoader;

impl GrlReteLoader {
    pub fn load_from_file<P: AsRef<Path>>(path: P, engine: &mut IncrementalEngine) -> Result<usize>
    pub fn load_from_string(grl_text: &str, engine: &mut IncrementalEngine) -> Result<usize>
}
```

---

## Plugin Trait

### RulePlugin

Custom plugin interface.

```rust
pub trait RulePlugin: Send + Sync {
    // Required
    fn name(&self) -> &str;
    fn version(&self) -> &str;
    fn description(&self) -> &str;
    
    // Optional
    fn register_actions(&self, engine: &mut RustRuleEngine) -> Result<()> {
        Ok(())
    }
    
    fn register_functions(&self, engine: &mut RustRuleEngine) -> Result<()> {
        Ok(())
    }
    
    fn on_load(&mut self) -> Result<()> {
        Ok(())
    }
    
    fn on_unload(&mut self) -> Result<()> {
        Ok(())
    }
    
    fn health_check(&self) -> Result<()> {
        Ok(())
    }
}
```

---

## Error Types

### RuleEngineError

Main error type.

```rust
pub enum RuleEngineError {
    ParseError { message: String },
    EvaluationError { message: String },
    FieldNotFound { field: String },
    IoError(std::io::Error),
    TypeMismatch { expected: String, actual: String },
    InvalidOperator { operator: String },
    InvalidLogicalOperator { operator: String },
    RegexError { message: String },
    ActionError { message: String },
    ExecutionError(String),
    SerializationError { message: String },
    PluginError { message: String },
}

pub type Result<T> = std::result::Result<T, RuleEngineError>;
```

---

## Feature Flags

Enable optional features in `Cargo.toml`:

```toml
[dependencies]
rust-rule-engine = { version = "0.10.0", features = ["async"] }
```

### Available Features
- `async` - Async support for rule execution
- `full` - All features enabled

---

## Examples

### Basic Usage

```rust
use rust_rule_engine::{RustRuleEngine, Facts, Value};

let mut engine = RustRuleEngine::new();
engine.load_rules_from_file("rules.grl")?;

let mut facts = Facts::new();
facts.set("amount", Value::Float(1000.0));

engine.execute(&mut facts)?;
```

### RETE Usage

```rust
use rust_rule_engine::rete::{
    IncrementalEngine, GrlReteLoader, TypedFacts, FactValue
};

let mut engine = IncrementalEngine::new();
GrlReteLoader::load_from_file("rules.grl", &mut engine)?;

let mut facts = TypedFacts::new();
facts.set("amount", FactValue::Float(1000.0));

engine.insert("Order".to_string(), facts);
engine.reset();
engine.fire_all();
```

### Template Usage (v0.10.0)

```rust
use rust_rule_engine::rete::{TemplateBuilder, FieldType};

let template = TemplateBuilder::new("Person")
    .required_string("name")
    .integer_field("age")
    .build();

engine.templates_mut().register(template);

let handle = engine.insert_with_template("Person", person_facts)?;
```

### Global Variables (v0.10.0)

```rust
engine.globals().define("counter", FactValue::Integer(0))?;
engine.globals().increment("counter", 1.0)?;
let count = engine.globals().get("counter")?;
```

---

## Constants

```rust
// Maximum iterations for cycle detection
pub const MAX_ITERATIONS: usize = 1000;

// Default salience
pub const DEFAULT_SALIENCE: i32 = 0;

// Default agenda group
pub const DEFAULT_AGENDA_GROUP: &str = "MAIN";
```

---

## Macros

```rust
// Create facts quickly
facts! {
    "customer.tier" => "gold",
    "order.amount" => 1500.0,
}

// Create typed facts
typed_facts! {
    "name" => FactValue::String("Alice".into()),
    "age" => FactValue::Integer(30),
}
```

---

**Full Documentation**: https://docs.rs/rust-rule-engine

**Examples**: [examples/](../../examples/) directory

**Last Updated**: 2025-10-31 (v0.10.0)