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
//! Stream Aggregation Functions
//!
//! Provides various aggregation operations for streaming data analysis.

use crate::streaming::event::StreamEvent;
use crate::streaming::window::TimeWindow;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Type of aggregation to perform
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AggregationType {
    /// Count number of events
    Count,
    /// Sum numeric values
    Sum { field: String },
    /// Calculate average
    Average { field: String },
    /// Find minimum value
    Min { field: String },
    /// Find maximum value
    Max { field: String },
    /// Count distinct values
    CountDistinct { field: String },
    /// Calculate standard deviation
    StdDev { field: String },
    /// Calculate percentile
    Percentile { field: String, percentile: f64 },
    /// First event in window
    First,
    /// Last event in window
    Last,
    /// Count by category
    CountBy { field: String },
}

/// Result of an aggregation operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AggregationResult {
    /// Numeric result
    Number(f64),
    /// String result
    Text(String),
    /// Boolean result
    Boolean(bool),
    /// Map of category counts
    CountMap(HashMap<String, usize>),
    /// No result (empty data)
    None,
}

impl AggregationResult {
    /// Convert to numeric value if possible
    pub fn as_number(&self) -> Option<f64> {
        match self {
            AggregationResult::Number(n) => Some(*n),
            _ => None,
        }
    }

    /// Convert to string if possible
    pub fn as_string(&self) -> Option<&str> {
        match self {
            AggregationResult::Text(s) => Some(s),
            _ => None,
        }
    }

    /// Convert to boolean if possible
    pub fn as_boolean(&self) -> Option<bool> {
        match self {
            AggregationResult::Boolean(b) => Some(*b),
            _ => None,
        }
    }
}

/// Aggregator for performing calculations on event streams
#[derive(Debug)]
#[allow(dead_code)]
pub struct Aggregator {
    /// Type of aggregation
    aggregation_type: AggregationType,
    /// Field to aggregate on (if applicable)
    _field: Option<String>,
}

impl Aggregator {
    /// Create a new aggregator
    pub fn new(aggregation_type: AggregationType) -> Self {
        let _field = match &aggregation_type {
            AggregationType::Sum { field }
            | AggregationType::Average { field }
            | AggregationType::Min { field }
            | AggregationType::Max { field }
            | AggregationType::CountDistinct { field }
            | AggregationType::StdDev { field }
            | AggregationType::Percentile { field, .. }
            | AggregationType::CountBy { field } => Some(field.clone()),
            _ => None,
        };

        Self {
            aggregation_type,
            _field,
        }
    }

    /// Perform aggregation on a time window
    pub fn aggregate(&self, window: &TimeWindow) -> AggregationResult {
        let events = window.events();

        match &self.aggregation_type {
            AggregationType::Count => AggregationResult::Number(events.len() as f64),

            AggregationType::Sum { field } => {
                let sum = window.sum(field);
                AggregationResult::Number(sum)
            }

            AggregationType::Average { field } => match window.average(field) {
                Some(avg) => AggregationResult::Number(avg),
                None => AggregationResult::None,
            },

            AggregationType::Min { field } => match window.min(field) {
                Some(min) => AggregationResult::Number(min),
                None => AggregationResult::None,
            },

            AggregationType::Max { field } => match window.max(field) {
                Some(max) => AggregationResult::Number(max),
                None => AggregationResult::None,
            },

            AggregationType::CountDistinct { field } => {
                let distinct_count = self.count_distinct_values(events, field);
                AggregationResult::Number(distinct_count as f64)
            }

            AggregationType::StdDev { field } => {
                let std_dev = self.calculate_std_dev(events, field);
                match std_dev {
                    Some(sd) => AggregationResult::Number(sd),
                    None => AggregationResult::None,
                }
            }

            AggregationType::Percentile { field, percentile } => {
                let value = self.calculate_percentile(events, field, *percentile);
                match value {
                    Some(v) => AggregationResult::Number(v),
                    None => AggregationResult::None,
                }
            }

            AggregationType::First => match events.front() {
                Some(event) => AggregationResult::Text(event.id.clone()),
                None => AggregationResult::None,
            },

            AggregationType::Last => match events.back() {
                Some(event) => AggregationResult::Text(event.id.clone()),
                None => AggregationResult::None,
            },

            AggregationType::CountBy { field } => {
                let counts = self.count_by_field(events, field);
                AggregationResult::CountMap(counts)
            }
        }
    }

    /// Perform aggregation on a slice of events
    pub fn aggregate_events(&self, events: &[StreamEvent]) -> AggregationResult {
        match &self.aggregation_type {
            AggregationType::Count => AggregationResult::Number(events.len() as f64),

            AggregationType::Sum { field } => {
                let sum: f64 = events.iter().filter_map(|e| e.get_numeric(field)).sum();
                AggregationResult::Number(sum)
            }

            AggregationType::Average { field } => {
                let values: Vec<f64> = events.iter().filter_map(|e| e.get_numeric(field)).collect();

                if values.is_empty() {
                    AggregationResult::None
                } else {
                    let avg = values.iter().sum::<f64>() / values.len() as f64;
                    AggregationResult::Number(avg)
                }
            }

            _ => {
                // For other types, create a temporary window
                // This is less efficient but provides compatibility
                AggregationResult::None
            }
        }
    }

    /// Count distinct values in a field
    fn count_distinct_values(
        &self,
        events: &std::collections::VecDeque<StreamEvent>,
        field: &str,
    ) -> usize {
        let mut seen = std::collections::HashSet::new();

        for event in events {
            if let Some(value) = event.data.get(field) {
                seen.insert(format!("{:?}", value));
            }
        }

        seen.len()
    }

    /// Calculate standard deviation
    fn calculate_std_dev(
        &self,
        events: &std::collections::VecDeque<StreamEvent>,
        field: &str,
    ) -> Option<f64> {
        let values: Vec<f64> = events.iter().filter_map(|e| e.get_numeric(field)).collect();

        if values.len() < 2 {
            return None;
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;

        Some(variance.sqrt())
    }

    /// Calculate percentile
    fn calculate_percentile(
        &self,
        events: &std::collections::VecDeque<StreamEvent>,
        field: &str,
        percentile: f64,
    ) -> Option<f64> {
        let mut values: Vec<f64> = events.iter().filter_map(|e| e.get_numeric(field)).collect();

        if values.is_empty() {
            return None;
        }

        values.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let index = (percentile / 100.0 * (values.len() - 1) as f64).round() as usize;
        values.get(index).copied()
    }

    /// Count occurrences by field value
    fn count_by_field(
        &self,
        events: &std::collections::VecDeque<StreamEvent>,
        field: &str,
    ) -> HashMap<String, usize> {
        let mut counts = HashMap::new();

        for event in events {
            if let Some(value) = event.data.get(field) {
                let key = match value {
                    crate::types::Value::String(s) => s.clone(),
                    crate::types::Value::Number(n) => n.to_string(),
                    crate::types::Value::Integer(i) => i.to_string(),
                    crate::types::Value::Boolean(b) => b.to_string(),
                    _ => format!("{:?}", value),
                };

                *counts.entry(key).or_insert(0) += 1;
            }
        }

        counts
    }
}

/// Stream analytics helper for complex aggregations
#[derive(Debug)]
pub struct StreamAnalytics {
    /// Cache of recent calculations
    cache: HashMap<String, (u64, AggregationResult)>,
    /// Cache TTL in milliseconds
    cache_ttl: u64,
}

impl StreamAnalytics {
    /// Create new stream analytics instance
    pub fn new(cache_ttl_ms: u64) -> Self {
        Self {
            cache: HashMap::new(),
            cache_ttl: cache_ttl_ms,
        }
    }

    /// Perform cached aggregation
    pub fn aggregate_cached(
        &mut self,
        key: &str,
        window: &TimeWindow,
        aggregator: &Aggregator,
        current_time: u64,
    ) -> AggregationResult {
        // Check cache
        if let Some((timestamp, result)) = self.cache.get(key) {
            if current_time - timestamp < self.cache_ttl {
                return result.clone();
            }
        }

        // Calculate new result
        let result = aggregator.aggregate(window);
        self.cache
            .insert(key.to_string(), (current_time, result.clone()));

        // Clean old cache entries
        self.cache
            .retain(|_, (timestamp, _)| current_time - *timestamp < self.cache_ttl);

        result
    }

    /// Calculate moving average over multiple windows
    pub fn moving_average(
        &self,
        windows: &[TimeWindow],
        field: &str,
        window_count: usize,
    ) -> Option<f64> {
        if windows.is_empty() {
            return None;
        }

        let recent_windows = if windows.len() > window_count {
            &windows[windows.len() - window_count..]
        } else {
            windows
        };

        let total_sum: f64 = recent_windows.iter().map(|w| w.sum(field)).sum();

        let total_count: usize = recent_windows.iter().map(|w| w.count()).sum();

        if total_count == 0 {
            None
        } else {
            Some(total_sum / total_count as f64)
        }
    }

    /// Detect anomalies using z-score
    pub fn detect_anomalies(
        &self,
        windows: &[TimeWindow],
        field: &str,
        threshold: f64,
    ) -> Vec<String> {
        if windows.len() < 3 {
            return Vec::new();
        }

        // Calculate baseline statistics from historical windows
        let historical_windows = &windows[..windows.len() - 1];
        let values: Vec<f64> = historical_windows
            .iter()
            .flat_map(|w| w.events().iter().filter_map(|e| e.get_numeric(field)))
            .collect();

        if values.len() < 10 {
            return Vec::new();
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;
        let std_dev = variance.sqrt();

        // Check current window for anomalies
        let current_window = windows.last().unwrap();
        let mut anomalies = Vec::new();

        for event in current_window.events() {
            if let Some(value) = event.get_numeric(field) {
                let z_score = (value - mean) / std_dev;
                if z_score.abs() > threshold {
                    anomalies.push(event.id.clone());
                }
            }
        }

        anomalies
    }

    /// Calculate trend direction
    pub fn calculate_trend(&self, windows: &[TimeWindow], field: &str) -> TrendDirection {
        if windows.len() < 2 {
            return TrendDirection::Stable;
        }

        let averages: Vec<f64> = windows.iter().filter_map(|w| w.average(field)).collect();

        if averages.len() < 2 {
            return TrendDirection::Stable;
        }

        let first_half = &averages[..averages.len() / 2];
        let second_half = &averages[averages.len() / 2..];

        let first_avg = first_half.iter().sum::<f64>() / first_half.len() as f64;
        let second_avg = second_half.iter().sum::<f64>() / second_half.len() as f64;

        let change_percent = ((second_avg - first_avg) / first_avg) * 100.0;

        if change_percent > 5.0 {
            TrendDirection::Increasing
        } else if change_percent < -5.0 {
            TrendDirection::Decreasing
        } else {
            TrendDirection::Stable
        }
    }
}

/// Direction of trend analysis
#[derive(Debug, Clone, PartialEq)]
pub enum TrendDirection {
    /// Values are increasing
    Increasing,
    /// Values are decreasing
    Decreasing,
    /// Values are stable
    Stable,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::streaming::event::StreamEvent;
    use crate::types::Value;
    use std::collections::HashMap;

    #[test]
    fn test_count_aggregation() {
        let aggregator = Aggregator::new(AggregationType::Count);
        let events = create_test_events(5);

        let result = aggregator.aggregate_events(&events);
        assert_eq!(result.as_number(), Some(5.0));
    }

    #[test]
    fn test_sum_aggregation() {
        let aggregator = Aggregator::new(AggregationType::Sum {
            field: "value".to_string(),
        });
        let events = create_test_events(5);

        let result = aggregator.aggregate_events(&events);
        assert_eq!(result.as_number(), Some(10.0)); // 0+1+2+3+4
    }

    #[test]
    fn test_average_aggregation() {
        let aggregator = Aggregator::new(AggregationType::Average {
            field: "value".to_string(),
        });
        let events = create_test_events(5);

        let result = aggregator.aggregate_events(&events);
        assert_eq!(result.as_number(), Some(2.0));
    }

    fn create_test_events(count: usize) -> Vec<StreamEvent> {
        (0..count)
            .map(|i| {
                let mut data = HashMap::new();
                data.insert("value".to_string(), Value::Number(i as f64));
                StreamEvent::new("TestEvent", data, "test")
            })
            .collect()
    }
}