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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
//! Stream Operators - Fluent API for Stream Processing
//!
//! This module provides a fluent, composable API for building stream processing pipelines.
//! Inspired by Apache Flink, Kafka Streams, and Rust iterators.
//!
//! ## Example
//!
//! ```rust,ignore
//! use rust_rule_engine::streaming::*;
//!
//! let result = DataStream::from_events(events)
//!     .filter(|e| e.get_numeric("amount").unwrap_or(0.0) > 100.0)
//!     .map(|e| enhance_event(e))
//!     .key_by(|e| e.get_string("user_id").unwrap_or("unknown").to_string())
//!     .window(WindowConfig::sliding(Duration::from_secs(60)))
//!     .reduce(|acc, e| {
//!         let sum = acc.get_numeric("total").unwrap_or(0.0);
//!         let amount = e.get_numeric("amount").unwrap_or(0.0);
//!         acc.data.insert("total".to_string(), Value::Number(sum + amount));
//!         acc
//!     })
//!     .collect();
//! ```

use crate::streaming::event::StreamEvent;
use crate::streaming::window::{TimeWindow, WindowType};
use crate::types::Value;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

/// A stream of events with chainable operators
#[derive(Clone)]
pub struct DataStream {
    events: Vec<StreamEvent>,
}

impl DataStream {
    /// Create a new data stream from events
    pub fn from_events(events: Vec<StreamEvent>) -> Self {
        Self { events }
    }

    /// Create an empty data stream
    pub fn new() -> Self {
        Self { events: Vec::new() }
    }

    /// Add an event to the stream
    pub fn push(&mut self, event: StreamEvent) {
        self.events.push(event);
    }

    /// Get the number of events in the stream
    pub fn len(&self) -> usize {
        self.events.len()
    }

    /// Check if stream is empty
    pub fn is_empty(&self) -> bool {
        self.events.is_empty()
    }

    /// Filter events based on a predicate
    ///
    /// # Example
    /// ```rust,ignore
    /// stream.filter(|e| e.get_numeric("amount").unwrap_or(0.0) > 100.0)
    /// ```
    pub fn filter<F>(self, predicate: F) -> Self
    where
        F: Fn(&StreamEvent) -> bool,
    {
        let filtered_events = self.events.into_iter().filter(predicate).collect();
        Self {
            events: filtered_events,
        }
    }

    /// Transform each event using a mapping function
    ///
    /// # Example
    /// ```rust,ignore
    /// stream.map(|mut e| {
    ///     e.add_tag("processed", "true");
    ///     e
    /// })
    /// ```
    pub fn map<F>(self, mapper: F) -> Self
    where
        F: Fn(StreamEvent) -> StreamEvent,
    {
        let mapped_events = self.events.into_iter().map(mapper).collect();
        Self {
            events: mapped_events,
        }
    }

    /// Transform each event into multiple events (flatMap)
    ///
    /// # Example
    /// ```rust,ignore
    /// stream.flat_map(|e| {
    ///     // Split event into multiple events
    ///     vec![e.clone(), e]
    /// })
    /// ```
    pub fn flat_map<F>(self, mapper: F) -> Self
    where
        F: Fn(StreamEvent) -> Vec<StreamEvent>,
    {
        let flat_mapped_events = self.events.into_iter().flat_map(mapper).collect();
        Self {
            events: flat_mapped_events,
        }
    }

    /// Key events by a specific field or function
    ///
    /// # Example
    /// ```rust,ignore
    /// stream.key_by(|e| e.get_string("user_id").unwrap_or("default").to_string())
    /// ```
    pub fn key_by<F, K>(self, key_selector: F) -> KeyedStream<K>
    where
        F: Fn(&StreamEvent) -> K,
        K: std::hash::Hash + Eq + Clone,
    {
        let mut keyed_events: HashMap<K, Vec<StreamEvent>> = HashMap::new();

        for event in self.events {
            let key = key_selector(&event);
            keyed_events.entry(key).or_default().push(event);
        }

        KeyedStream { keyed_events }
    }

    /// Apply a window to the stream
    ///
    /// # Example
    /// ```rust,ignore
    /// stream.window(WindowConfig::sliding(Duration::from_secs(60)))
    /// ```
    pub fn window(self, config: WindowConfig) -> WindowedStream {
        WindowedStream::new(self.events, config)
    }

    /// Reduce events to a single result
    ///
    /// # Example
    /// ```rust,ignore
    /// stream.reduce(|acc, e| {
    ///     // Accumulate values
    ///     acc
    /// })
    /// ```
    pub fn reduce<F>(self, reducer: F) -> Option<StreamEvent>
    where
        F: Fn(StreamEvent, StreamEvent) -> StreamEvent,
    {
        self.events.into_iter().reduce(reducer)
    }

    /// Count the number of events
    pub fn count(self) -> usize {
        self.events.len()
    }

    /// Collect events into a vector
    pub fn collect(self) -> Vec<StreamEvent> {
        self.events
    }

    /// Take only the first n events
    pub fn take(self, n: usize) -> Self {
        Self {
            events: self.events.into_iter().take(n).collect(),
        }
    }

    /// Skip the first n events
    pub fn skip(self, n: usize) -> Self {
        Self {
            events: self.events.into_iter().skip(n).collect(),
        }
    }

    /// Process each event with a side effect (doesn't modify the stream)
    ///
    /// # Example
    /// ```rust,ignore
    /// stream.for_each(|e| {
    ///     println!("Processing: {:?}", e);
    /// })
    /// ```
    pub fn for_each<F>(self, action: F) -> Self
    where
        F: Fn(&StreamEvent),
    {
        for event in &self.events {
            action(event);
        }
        self
    }

    /// Union with another stream
    pub fn union(mut self, other: DataStream) -> Self {
        self.events.extend(other.events);
        Self {
            events: self.events,
        }
    }

    /// Find events matching a pattern
    pub fn find<F>(self, predicate: F) -> Option<StreamEvent>
    where
        F: Fn(&StreamEvent) -> bool,
    {
        self.events.into_iter().find(predicate)
    }

    /// Check if any event matches the predicate
    pub fn any<F>(&self, predicate: F) -> bool
    where
        F: Fn(&StreamEvent) -> bool,
    {
        self.events.iter().any(predicate)
    }

    /// Check if all events match the predicate
    pub fn all<F>(&self, predicate: F) -> bool
    where
        F: Fn(&StreamEvent) -> bool,
    {
        self.events.iter().all(predicate)
    }

    /// Sort events by a key function
    pub fn sort_by<F, K>(mut self, key_fn: F) -> Self
    where
        F: Fn(&StreamEvent) -> K,
        K: Ord,
    {
        self.events.sort_by_key(key_fn);
        Self {
            events: self.events,
        }
    }

    /// Group events by a key and apply aggregation
    pub fn group_by<F, K>(self, key_selector: F) -> GroupedStream<K>
    where
        F: Fn(&StreamEvent) -> K,
        K: std::hash::Hash + Eq + Clone,
    {
        let mut grouped: HashMap<K, Vec<StreamEvent>> = HashMap::new();

        for event in self.events {
            let key = key_selector(&event);
            grouped.entry(key).or_default().push(event);
        }

        GroupedStream { groups: grouped }
    }

    /// Apply an aggregation function
    pub fn aggregate<A>(self, aggregator: A) -> AggregateResult
    where
        A: Aggregation,
    {
        aggregator.aggregate(&self.events)
    }
}

impl Default for DataStream {
    fn default() -> Self {
        Self::new()
    }
}

/// A stream of events keyed by a specific field
pub struct KeyedStream<K>
where
    K: std::hash::Hash + Eq,
{
    keyed_events: HashMap<K, Vec<StreamEvent>>,
}

impl<K> KeyedStream<K>
where
    K: std::hash::Hash + Eq + Clone,
{
    /// Reduce events within each key
    pub fn reduce<F>(self, reducer: F) -> HashMap<K, StreamEvent>
    where
        F: Fn(StreamEvent, StreamEvent) -> StreamEvent,
    {
        self.keyed_events
            .into_iter()
            .filter_map(|(key, events)| {
                events
                    .into_iter()
                    .reduce(&reducer)
                    .map(|result| (key, result))
            })
            .collect()
    }

    /// Apply aggregation to each key group
    pub fn aggregate<A>(self, aggregator: A) -> HashMap<K, AggregateResult>
    where
        A: Aggregation + Clone,
    {
        self.keyed_events
            .into_iter()
            .map(|(key, events)| (key, aggregator.clone().aggregate(&events)))
            .collect()
    }

    /// Apply a window to each key group
    pub fn window(self, config: WindowConfig) -> KeyedWindowedStream<K> {
        KeyedWindowedStream {
            keyed_events: self.keyed_events,
            config,
        }
    }

    /// Count events per key
    pub fn count(self) -> HashMap<K, usize> {
        self.keyed_events
            .into_iter()
            .map(|(key, events)| (key, events.len()))
            .collect()
    }

    /// Get all keys
    pub fn keys(&self) -> Vec<K> {
        self.keyed_events.keys().cloned().collect()
    }

    /// Flatten back to a regular stream
    pub fn flatten(self) -> DataStream {
        let events: Vec<StreamEvent> = self
            .keyed_events
            .into_iter()
            .flat_map(|(_, events)| events)
            .collect();

        DataStream { events }
    }
}

/// Window configuration for stream processing
#[derive(Debug, Clone)]
pub struct WindowConfig {
    pub window_type: WindowType,
    pub duration: Duration,
    pub max_events: usize,
}

impl WindowConfig {
    /// Create a sliding window configuration
    pub fn sliding(duration: Duration) -> Self {
        Self {
            window_type: WindowType::Sliding,
            duration,
            max_events: 10000,
        }
    }

    /// Create a tumbling window configuration
    pub fn tumbling(duration: Duration) -> Self {
        Self {
            window_type: WindowType::Tumbling,
            duration,
            max_events: 10000,
        }
    }

    /// Create a session window configuration
    pub fn session(timeout: Duration) -> Self {
        Self {
            window_type: WindowType::Session { timeout },
            duration: timeout,
            max_events: 10000,
        }
    }

    /// Set maximum events per window
    pub fn with_max_events(mut self, max_events: usize) -> Self {
        self.max_events = max_events;
        self
    }
}

/// A stream with windowing applied
pub struct WindowedStream {
    windows: Vec<TimeWindow>,
}

impl WindowedStream {
    /// Create a new windowed stream
    pub fn new(events: Vec<StreamEvent>, config: WindowConfig) -> Self {
        let mut windows = Vec::new();

        if events.is_empty() {
            return Self { windows };
        }

        // Group events into windows based on configuration
        match config.window_type {
            WindowType::Tumbling => {
                // Calculate window boundaries
                let window_ms = config.duration.as_millis() as u64;
                let mut window_map: HashMap<u64, Vec<StreamEvent>> = HashMap::new();

                for event in events {
                    let window_start = (event.metadata.timestamp / window_ms) * window_ms;
                    window_map.entry(window_start).or_default().push(event);
                }

                // Create windows
                for (start_time, mut window_events) in window_map {
                    let mut window = TimeWindow::new(
                        config.window_type.clone(),
                        config.duration,
                        start_time,
                        config.max_events,
                    );

                    for event in window_events.drain(..) {
                        window.add_event(event);
                    }

                    windows.push(window);
                }
            }
            WindowType::Sliding | WindowType::Session { .. } => {
                // For sliding windows, create overlapping windows
                // Simplified implementation: create one window per unique timestamp
                let window_ms = config.duration.as_millis() as u64;

                if !events.is_empty() {
                    let min_time = events.iter().map(|e| e.metadata.timestamp).min().unwrap();
                    let max_time = events.iter().map(|e| e.metadata.timestamp).max().unwrap();

                    let mut current_start = min_time;

                    while current_start <= max_time {
                        let mut window = TimeWindow::new(
                            config.window_type.clone(),
                            config.duration,
                            current_start,
                            config.max_events,
                        );

                        for event in &events {
                            if event.metadata.timestamp >= current_start
                                && event.metadata.timestamp < current_start + window_ms
                            {
                                window.add_event(event.clone());
                            }
                        }

                        if window.count() > 0 {
                            windows.push(window);
                        }

                        // Slide forward (overlap 50%)
                        current_start += window_ms / 2;
                    }
                }
            }
        }

        Self { windows }
    }

    /// Apply aggregation to each window
    pub fn aggregate<A>(self, aggregator: A) -> Vec<AggregateResult>
    where
        A: Aggregation,
    {
        self.windows
            .iter()
            .map(|window| {
                let events: Vec<StreamEvent> = window.events().iter().cloned().collect();
                aggregator.aggregate(&events)
            })
            .collect()
    }

    /// Reduce events within each window
    pub fn reduce<F>(self, reducer: F) -> Vec<StreamEvent>
    where
        F: Fn(StreamEvent, StreamEvent) -> StreamEvent + Clone,
    {
        self.windows
            .into_iter()
            .filter_map(|window| {
                let events: Vec<StreamEvent> = window.events().iter().cloned().collect();
                events.into_iter().reduce(&reducer)
            })
            .collect()
    }

    /// Get all windows
    pub fn windows(&self) -> &[TimeWindow] {
        &self.windows
    }

    /// Count events in each window
    pub fn counts(self) -> Vec<usize> {
        self.windows.iter().map(|w| w.count()).collect()
    }

    /// Flatten all windows back into a stream
    pub fn flatten(self) -> DataStream {
        let events: Vec<StreamEvent> = self
            .windows
            .into_iter()
            .flat_map(|w| w.events().iter().cloned().collect::<Vec<_>>())
            .collect();

        DataStream { events }
    }
}

/// Keyed stream with windowing
pub struct KeyedWindowedStream<K>
where
    K: std::hash::Hash + Eq,
{
    keyed_events: HashMap<K, Vec<StreamEvent>>,
    config: WindowConfig,
}

impl<K> KeyedWindowedStream<K>
where
    K: std::hash::Hash + Eq + Clone,
{
    /// Apply aggregation to each key's window
    pub fn aggregate<A>(self, aggregator: A) -> HashMap<K, Vec<AggregateResult>>
    where
        A: Aggregation + Clone,
    {
        self.keyed_events
            .into_iter()
            .map(|(key, events)| {
                let windowed = WindowedStream::new(events, self.config.clone());
                let results = windowed.aggregate(aggregator.clone());
                (key, results)
            })
            .collect()
    }

    /// Reduce events within each key's window
    pub fn reduce<F>(self, reducer: F) -> HashMap<K, Vec<StreamEvent>>
    where
        F: Fn(StreamEvent, StreamEvent) -> StreamEvent + Clone,
    {
        self.keyed_events
            .into_iter()
            .map(|(key, events)| {
                let windowed = WindowedStream::new(events, self.config.clone());
                let results = windowed.reduce(reducer.clone());
                (key, results)
            })
            .collect()
    }
}

/// Grouped stream for aggregations
pub struct GroupedStream<K>
where
    K: std::hash::Hash + Eq,
{
    groups: HashMap<K, Vec<StreamEvent>>,
}

impl<K> GroupedStream<K>
where
    K: std::hash::Hash + Eq + Clone,
{
    /// Apply aggregation to each group
    pub fn aggregate<A>(self, aggregator: A) -> HashMap<K, AggregateResult>
    where
        A: Aggregation + Clone,
    {
        self.groups
            .into_iter()
            .map(|(key, events)| (key, aggregator.clone().aggregate(&events)))
            .collect()
    }

    /// Count events in each group
    pub fn count(self) -> HashMap<K, usize> {
        self.groups
            .into_iter()
            .map(|(key, events)| (key, events.len()))
            .collect()
    }

    /// Get the first event in each group
    pub fn first(self) -> HashMap<K, StreamEvent> {
        self.groups
            .into_iter()
            .filter_map(|(key, mut events)| {
                if !events.is_empty() {
                    Some((key, events.remove(0)))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Get the last event in each group
    pub fn last(self) -> HashMap<K, StreamEvent> {
        self.groups
            .into_iter()
            .filter_map(|(key, mut events)| events.pop().map(|e| (key, e)))
            .collect()
    }
}

/// Trait for aggregation functions
pub trait Aggregation: Send + Sync {
    fn aggregate(&self, events: &[StreamEvent]) -> AggregateResult;
}

/// Result of an aggregation operation
#[derive(Debug, Clone)]
pub enum AggregateResult {
    Number(f64),
    String(String),
    Map(HashMap<String, Value>),
    List(Vec<Value>),
    None,
}

impl AggregateResult {
    pub fn as_number(&self) -> Option<f64> {
        match self {
            AggregateResult::Number(n) => Some(*n),
            _ => None,
        }
    }

    pub fn as_string(&self) -> Option<&str> {
        match self {
            AggregateResult::String(s) => Some(s),
            _ => None,
        }
    }

    pub fn as_map(&self) -> Option<&HashMap<String, Value>> {
        match self {
            AggregateResult::Map(m) => Some(m),
            _ => None,
        }
    }
}

// Built-in aggregators

/// Count aggregator
#[derive(Clone)]
pub struct Count;

impl Aggregation for Count {
    fn aggregate(&self, events: &[StreamEvent]) -> AggregateResult {
        AggregateResult::Number(events.len() as f64)
    }
}

/// Sum aggregator
#[derive(Clone)]
pub struct Sum {
    pub field: String,
}

impl Sum {
    pub fn new(field: impl Into<String>) -> Self {
        Self {
            field: field.into(),
        }
    }
}

impl Aggregation for Sum {
    fn aggregate(&self, events: &[StreamEvent]) -> AggregateResult {
        let sum: f64 = events
            .iter()
            .filter_map(|e| e.get_numeric(&self.field))
            .sum();
        AggregateResult::Number(sum)
    }
}

/// Average aggregator
#[derive(Clone)]
pub struct Average {
    pub field: String,
}

impl Average {
    pub fn new(field: impl Into<String>) -> Self {
        Self {
            field: field.into(),
        }
    }
}

impl Aggregation for Average {
    fn aggregate(&self, events: &[StreamEvent]) -> AggregateResult {
        let values: Vec<f64> = events
            .iter()
            .filter_map(|e| e.get_numeric(&self.field))
            .collect();

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

/// Min aggregator
#[derive(Clone)]
pub struct Min {
    pub field: String,
}

impl Min {
    pub fn new(field: impl Into<String>) -> Self {
        Self {
            field: field.into(),
        }
    }
}

impl Aggregation for Min {
    fn aggregate(&self, events: &[StreamEvent]) -> AggregateResult {
        events
            .iter()
            .filter_map(|e| e.get_numeric(&self.field))
            .min_by(|a, b| a.partial_cmp(b).unwrap())
            .map(AggregateResult::Number)
            .unwrap_or(AggregateResult::None)
    }
}

/// Max aggregator
#[derive(Clone)]
pub struct Max {
    pub field: String,
}

impl Max {
    pub fn new(field: impl Into<String>) -> Self {
        Self {
            field: field.into(),
        }
    }
}

impl Aggregation for Max {
    fn aggregate(&self, events: &[StreamEvent]) -> AggregateResult {
        events
            .iter()
            .filter_map(|e| e.get_numeric(&self.field))
            .max_by(|a, b| a.partial_cmp(b).unwrap())
            .map(AggregateResult::Number)
            .unwrap_or(AggregateResult::None)
    }
}

/// Custom aggregator using a closure
pub struct CustomAggregator<F>
where
    F: Fn(&[StreamEvent]) -> AggregateResult + Send + Sync,
{
    func: Arc<F>,
}

impl<F> CustomAggregator<F>
where
    F: Fn(&[StreamEvent]) -> AggregateResult + Send + Sync,
{
    pub fn new(func: F) -> Self {
        Self {
            func: Arc::new(func),
        }
    }
}

impl<F> Clone for CustomAggregator<F>
where
    F: Fn(&[StreamEvent]) -> AggregateResult + Send + Sync,
{
    fn clone(&self) -> Self {
        Self {
            func: Arc::clone(&self.func),
        }
    }
}

impl<F> Aggregation for CustomAggregator<F>
where
    F: Fn(&[StreamEvent]) -> AggregateResult + Send + Sync,
{
    fn aggregate(&self, events: &[StreamEvent]) -> AggregateResult {
        (self.func)(events)
    }
}

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

    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));
                data.insert(
                    "user_id".to_string(),
                    Value::String(format!("user_{}", i % 3)),
                );
                StreamEvent::new("TestEvent", data, "test")
            })
            .collect()
    }

    #[test]
    fn test_filter_operator() {
        let events = create_test_events(10);
        let stream = DataStream::from_events(events);

        let filtered = stream.filter(|e| e.get_numeric("value").unwrap_or(0.0) > 5.0);

        assert_eq!(filtered.len(), 4); // 6, 7, 8, 9
    }

    #[test]
    fn test_map_operator() {
        let events = create_test_events(5);
        let stream = DataStream::from_events(events);

        let mapped = stream.map(|mut e| {
            if let Some(value) = e.get_numeric("value") {
                e.data
                    .insert("doubled".to_string(), Value::Number(value * 2.0));
            }
            e
        });

        let collected = mapped.collect();
        assert_eq!(collected[0].get_numeric("doubled"), Some(0.0));
        assert_eq!(collected[1].get_numeric("doubled"), Some(2.0));
    }

    #[test]
    fn test_key_by_operator() {
        let events = create_test_events(9);
        let stream = DataStream::from_events(events);

        let keyed = stream.key_by(|e| e.get_string("user_id").unwrap_or("").to_string());

        let counts = keyed.count();
        assert_eq!(counts.len(), 3); // 3 unique users
        assert_eq!(*counts.get("user_0").unwrap(), 3);
        assert_eq!(*counts.get("user_1").unwrap(), 3);
        assert_eq!(*counts.get("user_2").unwrap(), 3);
    }

    #[test]
    fn test_reduce_operator() {
        let events = create_test_events(5);
        let stream = DataStream::from_events(events);

        let result = stream.reduce(|mut acc, e| {
            let acc_val = acc.get_numeric("value").unwrap_or(0.0);
            let e_val = e.get_numeric("value").unwrap_or(0.0);
            acc.data
                .insert("value".to_string(), Value::Number(acc_val + e_val));
            acc
        });

        assert!(result.is_some());
        assert_eq!(result.unwrap().get_numeric("value"), Some(10.0)); // 0+1+2+3+4
    }

    #[test]
    fn test_count_aggregator() {
        let events = create_test_events(10);
        let result = Count.aggregate(&events);

        assert_eq!(result.as_number(), Some(10.0));
    }

    #[test]
    fn test_sum_aggregator() {
        let events = create_test_events(5);
        let result = Sum::new("value").aggregate(&events);

        assert_eq!(result.as_number(), Some(10.0)); // 0+1+2+3+4
    }

    #[test]
    fn test_average_aggregator() {
        let events = create_test_events(5);
        let result = Average::new("value").aggregate(&events);

        assert_eq!(result.as_number(), Some(2.0)); // (0+1+2+3+4)/5
    }

    #[test]
    fn test_group_by() {
        let events = create_test_events(9);
        let stream = DataStream::from_events(events);

        let grouped = stream.group_by(|e| e.get_string("user_id").unwrap_or("").to_string());

        let counts = grouped.count();
        assert_eq!(counts.len(), 3);
    }

    #[test]
    fn test_chaining_operators() {
        let events = create_test_events(20);
        let stream = DataStream::from_events(events);

        let result = stream
            .filter(|e| e.get_numeric("value").unwrap_or(0.0) >= 5.0)
            .map(|mut e| {
                if let Some(v) = e.get_numeric("value") {
                    e.data.insert("doubled".to_string(), Value::Number(v * 2.0));
                }
                e
            })
            .take(5)
            .collect();

        assert_eq!(result.len(), 5);
        assert_eq!(result[0].get_numeric("doubled"), Some(10.0)); // 5 * 2
    }

    #[test]
    fn test_windowed_stream() {
        let events = create_test_events(10);
        let stream = DataStream::from_events(events);

        let windowed = stream.window(WindowConfig::tumbling(Duration::from_secs(60)));

        assert!(!windowed.windows().is_empty());
    }
}