oxirs-stream 0.2.4

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
//! # CQELS (Continuous Query Evaluation over Linked Streams)
//!
//! Implementation of CQELS for native continuous query evaluation over RDF streams.
//!
//! CQELS features:
//! - Native streaming operators
//! - Continuous incremental evaluation
//! - Physical vs. logical time windows
//! - Stream-stream and stream-static joins
//! - Efficient memory management
//!
//! ## References
//! - Le-Phuoc et al. "A Native and Adaptive Approach for Unified Processing of Linked Streams and Linked Data"
//! - <https://github.com/cqels/cqels>

use anyhow::{anyhow, Result};
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, info};

use crate::store_integration::{QueryResult, RdfStore, Triple};
use crate::StreamEvent;

/// CQELS query engine for continuous evaluation
pub struct CqelsEngine {
    /// RDF store for static data
    store: Arc<dyn RdfStore>,
    /// Active streams
    streams: Arc<RwLock<HashMap<String, CqelsStream>>>,
    /// Registered queries
    queries: Arc<RwLock<HashMap<String, CqelsQuery>>>,
    /// Query execution plans
    plans: Arc<RwLock<HashMap<String, ExecutionPlan>>>,
    /// Configuration
    config: CqelsConfig,
    /// Statistics
    stats: Arc<RwLock<CqelsStats>>,
}

/// CQELS configuration
#[derive(Debug, Clone)]
pub struct CqelsConfig {
    /// Maximum concurrent queries
    pub max_queries: usize,
    /// Enable incremental evaluation
    pub incremental_evaluation: bool,
    /// Enable adaptive optimization
    pub adaptive_optimization: bool,
    /// Window buffer size
    pub window_buffer_size: usize,
    /// Join buffer size
    pub join_buffer_size: usize,
    /// Enable physical timestamps
    pub physical_timestamps: bool,
}

impl Default for CqelsConfig {
    fn default() -> Self {
        Self {
            max_queries: 100,
            incremental_evaluation: true,
            adaptive_optimization: true,
            window_buffer_size: 10000,
            join_buffer_size: 10000,
            physical_timestamps: true,
        }
    }
}

/// CQELS stream representation
pub struct CqelsStream {
    /// Stream identifier
    pub id: String,
    /// Stream URI
    pub uri: String,
    /// Event buffer
    pub buffer: VecDeque<StreamTriple>,
    /// Schema information
    pub schema: Option<StreamSchema>,
    /// Stream metadata
    pub metadata: StreamMetadata,
}

/// Triple with stream metadata
#[derive(Debug, Clone)]
pub struct StreamTriple {
    pub triple: Triple,
    pub timestamp: DateTime<Utc>,
    pub sequence_id: u64,
    pub source_id: String,
}

/// Stream schema
#[derive(Debug, Clone)]
pub struct StreamSchema {
    pub predicates: HashSet<String>,
    pub value_types: HashMap<String, ValueType>,
}

/// Value type enumeration
#[derive(Debug, Clone, PartialEq)]
pub enum ValueType {
    IRI,
    Literal,
    Integer,
    Float,
    Boolean,
    DateTime,
}

/// Stream metadata
#[derive(Debug, Clone)]
pub struct StreamMetadata {
    pub created_at: DateTime<Utc>,
    pub event_count: u64,
    pub avg_event_rate: f64,
    pub last_event_time: Option<DateTime<Utc>>,
}

/// CQELS query
#[derive(Debug, Clone)]
pub struct CqelsQuery {
    /// Query identifier
    pub id: String,
    /// Query string
    pub query_string: String,
    /// Parsed operators
    pub operators: Vec<CqelsOperator>,
    /// Query metadata
    pub metadata: QueryMetadata,
    /// Execution state
    pub state: ExecutionState,
}

/// CQELS operators
#[derive(Debug, Clone)]
pub enum CqelsOperator {
    /// Stream scan
    StreamScan {
        stream_uri: String,
        window: WindowDefinition,
    },
    /// Static data scan
    StaticScan {
        graph_uri: Option<String>,
        pattern: TriplePattern,
    },
    /// Stream-Stream join
    StreamJoin {
        left: Box<CqelsOperator>,
        right: Box<CqelsOperator>,
        condition: JoinCondition,
    },
    /// Stream-Static join
    HybridJoin {
        stream_op: Box<CqelsOperator>,
        static_op: Box<CqelsOperator>,
        condition: JoinCondition,
    },
    /// Filter operator
    Filter {
        input: Box<CqelsOperator>,
        condition: FilterCondition,
    },
    /// Projection
    Project {
        input: Box<CqelsOperator>,
        variables: Vec<String>,
    },
    /// Aggregation
    Aggregate {
        input: Box<CqelsOperator>,
        functions: Vec<AggregateFunction>,
        group_by: Vec<String>,
    },
}

/// Window definition for CQELS
#[derive(Debug, Clone)]
pub struct WindowDefinition {
    /// Window type
    pub window_type: CqelsWindowType,
    /// Time-based window size
    pub time_range: Option<Duration>,
    /// Count-based window size
    pub triple_count: Option<usize>,
    /// Window slide interval
    pub slide: Option<Duration>,
}

/// CQELS window types
#[derive(Debug, Clone, PartialEq)]
pub enum CqelsWindowType {
    /// Time-based tumbling window
    TimeTumbling,
    /// Time-based sliding window
    TimeSliding,
    /// Count-based tumbling window
    CountTumbling,
    /// Count-based sliding window
    CountSliding,
    /// Now window (current snapshot)
    Now,
}

/// Triple pattern
#[derive(Debug, Clone)]
pub struct TriplePattern {
    pub subject: PatternNode,
    pub predicate: PatternNode,
    pub object: PatternNode,
}

/// Pattern node
#[derive(Debug, Clone)]
pub enum PatternNode {
    Variable(String),
    IRI(String),
    Literal(String),
    Blank(String),
}

/// Join condition
#[derive(Debug, Clone)]
pub struct JoinCondition {
    pub left_var: String,
    pub right_var: String,
    pub join_type: JoinType,
}

/// Join types
#[derive(Debug, Clone, PartialEq)]
pub enum JoinType {
    Inner,
    LeftOuter,
    RightOuter,
    FullOuter,
}

/// Filter condition
#[derive(Debug, Clone)]
pub enum FilterCondition {
    Equals { var: String, value: String },
    NotEquals { var: String, value: String },
    LessThan { var: String, value: String },
    GreaterThan { var: String, value: String },
    Regex { var: String, pattern: String },
    And(Box<FilterCondition>, Box<FilterCondition>),
    Or(Box<FilterCondition>, Box<FilterCondition>),
    Not(Box<FilterCondition>),
}

/// Aggregate function
#[derive(Debug, Clone)]
pub struct AggregateFunction {
    pub function: AggregateFunctionType,
    pub variable: String,
    pub alias: String,
}

/// Aggregate function types
#[derive(Debug, Clone, PartialEq)]
pub enum AggregateFunctionType {
    Count,
    Sum,
    Avg,
    Min,
    Max,
}

/// Query metadata
#[derive(Debug, Clone)]
pub struct QueryMetadata {
    pub name: Option<String>,
    pub description: Option<String>,
    pub created_at: DateTime<Utc>,
    pub owner: Option<String>,
}

/// Execution state
#[derive(Debug, Clone, PartialEq)]
pub enum ExecutionState {
    Idle,
    Running,
    Paused,
    Completed,
    Failed(String),
}

/// Query execution plan
#[derive(Debug, Clone)]
pub struct ExecutionPlan {
    /// Plan identifier
    pub id: String,
    /// Root operator
    pub root: CqelsOperator,
    /// Operator statistics
    pub stats: HashMap<String, OperatorStats>,
    /// Optimization hints
    pub hints: Vec<OptimizationHint>,
}

/// Operator statistics
#[derive(Debug, Clone, Default)]
pub struct OperatorStats {
    pub input_tuples: u64,
    pub output_tuples: u64,
    pub execution_time_ms: f64,
    pub memory_usage_bytes: usize,
}

/// Optimization hints
#[derive(Debug, Clone)]
pub enum OptimizationHint {
    PushDownFilter,
    MaterializeJoin,
    UseIndex(String),
    ParallelExecution,
}

/// CQELS statistics
#[derive(Debug, Clone, Default)]
pub struct CqelsStats {
    pub queries_registered: u64,
    pub queries_executed: u64,
    pub total_stream_triples: u64,
    pub total_static_triples: u64,
    pub total_joins_performed: u64,
    pub avg_query_latency_ms: f64,
    pub memory_usage_bytes: usize,
}

impl CqelsEngine {
    /// Create a new CQELS engine
    pub fn new(store: Arc<dyn RdfStore>, config: CqelsConfig) -> Self {
        Self {
            store,
            streams: Arc::new(RwLock::new(HashMap::new())),
            queries: Arc::new(RwLock::new(HashMap::new())),
            plans: Arc::new(RwLock::new(HashMap::new())),
            config,
            stats: Arc::new(RwLock::new(CqelsStats::default())),
        }
    }

    /// Register a new stream
    pub async fn register_stream(&self, uri: String) -> Result<String> {
        let stream_id = uuid::Uuid::new_v4().to_string();

        let stream = CqelsStream {
            id: stream_id.clone(),
            uri,
            buffer: VecDeque::with_capacity(self.config.window_buffer_size),
            schema: None,
            metadata: StreamMetadata {
                created_at: Utc::now(),
                event_count: 0,
                avg_event_rate: 0.0,
                last_event_time: None,
            },
        };

        let mut streams = self.streams.write().await;
        streams.insert(stream_id.clone(), stream);

        info!("Registered CQELS stream: {}", stream_id);
        Ok(stream_id)
    }

    /// Register a CQELS query
    pub async fn register_query(&self, query_string: String) -> Result<String> {
        let query_id = uuid::Uuid::new_v4().to_string();

        // Parse CQELS query
        let operators = self.parse_cqels_query(&query_string)?;

        let query = CqelsQuery {
            id: query_id.clone(),
            query_string,
            operators,
            metadata: QueryMetadata {
                name: None,
                description: None,
                created_at: Utc::now(),
                owner: None,
            },
            state: ExecutionState::Idle,
        };

        // Create execution plan
        let plan = self.create_execution_plan(&query)?;

        let mut queries = self.queries.write().await;
        if queries.len() >= self.config.max_queries {
            return Err(anyhow!("Maximum number of queries reached"));
        }
        queries.insert(query_id.clone(), query);

        let mut plans = self.plans.write().await;
        plans.insert(query_id.clone(), plan);

        let mut stats = self.stats.write().await;
        stats.queries_registered += 1;

        info!("Registered CQELS query: {}", query_id);
        Ok(query_id)
    }

    /// Process a stream event
    pub async fn process_event(&self, stream_uri: &str, event: &StreamEvent) -> Result<()> {
        let triples = self.extract_triples_from_event(event)?;

        let mut streams = self.streams.write().await;
        let stream = streams
            .values_mut()
            .find(|s| s.uri == stream_uri)
            .ok_or_else(|| anyhow!("Stream not found: {}", stream_uri))?;

        for triple in &triples {
            let stream_triple = StreamTriple {
                triple: triple.clone(),
                timestamp: Utc::now(),
                sequence_id: stream.metadata.event_count,
                source_id: stream.id.clone(),
            };

            stream.buffer.push_back(stream_triple);
            stream.metadata.event_count += 1;
            stream.metadata.last_event_time = Some(Utc::now());

            // Evict old triples if buffer is full
            if stream.buffer.len() > self.config.window_buffer_size {
                stream.buffer.pop_front();
            }
        }

        let mut stats = self.stats.write().await;
        stats.total_stream_triples += triples.len() as u64;

        Ok(())
    }

    /// Execute a registered query
    pub async fn execute_query(&self, query_id: &str) -> Result<QueryResult> {
        let queries = self.queries.read().await;
        let _query = queries
            .get(query_id)
            .ok_or_else(|| anyhow!("Query not found: {}", query_id))?;

        let plans = self.plans.read().await;
        let plan = plans
            .get(query_id)
            .ok_or_else(|| anyhow!("Execution plan not found: {}", query_id))?;

        // Execute the plan
        let result = self.execute_plan(plan).await?;

        let mut stats = self.stats.write().await;
        stats.queries_executed += 1;

        Ok(result)
    }

    /// Parse CQELS query string
    fn parse_cqels_query(&self, query: &str) -> Result<Vec<CqelsOperator>> {
        // Simplified parser - in production, use a full CQELS parser
        let mut operators = Vec::new();

        // Parse SELECT/CONSTRUCT
        if query.to_uppercase().contains("SELECT") {
            // Create stream scan operator
            let stream_scan = CqelsOperator::StreamScan {
                stream_uri: "http://example.org/stream".to_string(),
                window: WindowDefinition {
                    window_type: CqelsWindowType::TimeSliding,
                    time_range: Some(Duration::from_secs(60)),
                    triple_count: None,
                    slide: Some(Duration::from_secs(10)),
                },
            };
            operators.push(stream_scan);
        }

        Ok(operators)
    }

    /// Create execution plan for query
    fn create_execution_plan(&self, query: &CqelsQuery) -> Result<ExecutionPlan> {
        let plan_id = uuid::Uuid::new_v4().to_string();

        // Create a simple plan with the first operator as root
        let root = query
            .operators
            .first()
            .cloned()
            .ok_or_else(|| anyhow!("No operators in query"))?;

        let plan = ExecutionPlan {
            id: plan_id,
            root,
            stats: HashMap::new(),
            hints: Vec::new(),
        };

        // Apply optimizations if enabled
        if self.config.adaptive_optimization {
            self.optimize_plan(&plan)
        } else {
            Ok(plan)
        }
    }

    /// Optimize execution plan
    fn optimize_plan(&self, plan: &ExecutionPlan) -> Result<ExecutionPlan> {
        let mut optimized = plan.clone();

        // Add optimization hints
        optimized.hints.push(OptimizationHint::PushDownFilter);

        debug!("Optimized execution plan: {}", optimized.id);
        Ok(optimized)
    }

    /// Execute an execution plan
    async fn execute_plan(&self, plan: &ExecutionPlan) -> Result<QueryResult> {
        debug!("Executing plan: {}", plan.id);

        // Execute the root operator
        self.execute_operator(&plan.root).await
    }

    /// Execute a single operator
    fn execute_operator<'a>(
        &'a self,
        operator: &'a CqelsOperator,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<QueryResult>> + 'a>> {
        Box::pin(async move { self.execute_operator_impl(operator).await })
    }

    /// Execute operator implementation
    async fn execute_operator_impl(&self, operator: &CqelsOperator) -> Result<QueryResult> {
        match operator {
            CqelsOperator::StreamScan { stream_uri, window } => {
                self.execute_stream_scan(stream_uri, window).await
            }
            CqelsOperator::StaticScan { graph_uri, pattern } => {
                self.execute_static_scan(graph_uri.as_deref(), pattern)
                    .await
            }
            CqelsOperator::StreamJoin {
                left,
                right,
                condition,
            } => self.execute_stream_join(left, right, condition).await,
            CqelsOperator::HybridJoin {
                stream_op,
                static_op,
                condition,
            } => {
                self.execute_hybrid_join(stream_op, static_op, condition)
                    .await
            }
            CqelsOperator::Filter { input, condition } => {
                self.execute_filter(input, condition).await
            }
            CqelsOperator::Project { input, variables } => {
                self.execute_project(input, variables).await
            }
            CqelsOperator::Aggregate {
                input,
                functions,
                group_by,
            } => self.execute_aggregate(input, functions, group_by).await,
        }
    }

    /// Execute stream scan operator
    async fn execute_stream_scan(
        &self,
        stream_uri: &str,
        window: &WindowDefinition,
    ) -> Result<QueryResult> {
        let streams = self.streams.read().await;
        let stream = streams
            .values()
            .find(|s| s.uri == stream_uri)
            .ok_or_else(|| anyhow!("Stream not found: {}", stream_uri))?;

        // Apply window to get relevant triples
        let triples = self.apply_window(&stream.buffer, window)?;

        debug!("Stream scan returned {} triples", triples.len());

        Ok(QueryResult {
            bindings: Vec::new(),
        })
    }

    /// Execute static scan operator
    async fn execute_static_scan(
        &self,
        _graph_uri: Option<&str>,
        _pattern: &TriplePattern,
    ) -> Result<QueryResult> {
        // Query static RDF store
        debug!("Executing static scan");

        Ok(QueryResult {
            bindings: Vec::new(),
        })
    }

    /// Execute stream-stream join
    async fn execute_stream_join(
        &self,
        _left: &CqelsOperator,
        _right: &CqelsOperator,
        _condition: &JoinCondition,
    ) -> Result<QueryResult> {
        // Simplified non-recursive implementation for foundational version
        // In production, implement proper recursive execution with Box::pin
        let left_result = QueryResult {
            bindings: Vec::new(),
        };
        let right_result = QueryResult {
            bindings: Vec::new(),
        };

        debug!(
            "Stream join: {} x {} bindings",
            left_result.bindings.len(),
            right_result.bindings.len()
        );

        let mut stats = self.stats.write().await;
        stats.total_joins_performed += 1;

        Ok(QueryResult {
            bindings: Vec::new(),
        })
    }

    /// Execute hybrid join (stream-static)
    async fn execute_hybrid_join(
        &self,
        _stream_op: &CqelsOperator,
        _static_op: &CqelsOperator,
        _condition: &JoinCondition,
    ) -> Result<QueryResult> {
        // Simplified non-recursive implementation for foundational version
        // In production, implement proper recursive execution with Box::pin
        let stream_result = QueryResult {
            bindings: Vec::new(),
        };
        let static_result = QueryResult {
            bindings: Vec::new(),
        };

        debug!(
            "Hybrid join: {} stream x {} static bindings",
            stream_result.bindings.len(),
            static_result.bindings.len()
        );

        Ok(QueryResult {
            bindings: Vec::new(),
        })
    }

    /// Execute filter operator
    async fn execute_filter(
        &self,
        _input: &CqelsOperator,
        _condition: &FilterCondition,
    ) -> Result<QueryResult> {
        // Simplified non-recursive implementation for foundational version
        let input_result = QueryResult {
            bindings: Vec::new(),
        };

        debug!("Filter applied to {} bindings", input_result.bindings.len());

        Ok(input_result)
    }

    /// Execute project operator
    async fn execute_project(
        &self,
        _input: &CqelsOperator,
        _variables: &[String],
    ) -> Result<QueryResult> {
        // Simplified non-recursive implementation for foundational version
        let input_result = QueryResult {
            bindings: Vec::new(),
        };

        debug!(
            "Project applied to {} bindings",
            input_result.bindings.len()
        );

        Ok(input_result)
    }

    /// Execute aggregate operator
    async fn execute_aggregate(
        &self,
        _input: &CqelsOperator,
        _functions: &[AggregateFunction],
        _group_by: &[String],
    ) -> Result<QueryResult> {
        // Simplified non-recursive implementation for foundational version
        let input_result = QueryResult {
            bindings: Vec::new(),
        };

        debug!(
            "Aggregate applied to {} bindings",
            input_result.bindings.len()
        );

        Ok(QueryResult {
            bindings: Vec::new(),
        })
    }

    /// Apply window to stream buffer
    fn apply_window(
        &self,
        buffer: &VecDeque<StreamTriple>,
        window: &WindowDefinition,
    ) -> Result<Vec<Triple>> {
        let now = Utc::now();
        let mut triples = Vec::new();

        match window.window_type {
            CqelsWindowType::TimeSliding | CqelsWindowType::TimeTumbling => {
                if let Some(time_range) = window.time_range {
                    let cutoff = now - ChronoDuration::from_std(time_range)?;
                    for stream_triple in buffer {
                        if stream_triple.timestamp > cutoff {
                            triples.push(stream_triple.triple.clone());
                        }
                    }
                }
            }
            CqelsWindowType::CountSliding | CqelsWindowType::CountTumbling => {
                if let Some(count) = window.triple_count {
                    triples.extend(buffer.iter().rev().take(count).map(|st| st.triple.clone()));
                }
            }
            CqelsWindowType::Now => {
                // Return all current triples
                triples.extend(buffer.iter().map(|st| st.triple.clone()));
            }
        }

        Ok(triples)
    }

    /// Extract triples from event
    fn extract_triples_from_event(&self, event: &StreamEvent) -> Result<Vec<Triple>> {
        let mut triples = Vec::new();

        match event {
            StreamEvent::TripleAdded {
                subject,
                predicate,
                object,
                graph,
                ..
            } => {
                triples.push(Triple {
                    subject: subject.clone(),
                    predicate: predicate.clone(),
                    object: object.clone(),
                    graph: graph.clone(),
                });
            }
            StreamEvent::QuadAdded {
                subject,
                predicate,
                object,
                graph,
                ..
            } => {
                triples.push(Triple {
                    subject: subject.clone(),
                    predicate: predicate.clone(),
                    object: object.clone(),
                    graph: Some(graph.clone()),
                });
            }
            _ => {}
        }

        Ok(triples)
    }

    /// Get statistics
    pub async fn get_stats(&self) -> CqelsStats {
        self.stats.read().await.clone()
    }

    /// Start a query
    pub async fn start_query(&self, query_id: &str) -> Result<()> {
        let mut queries = self.queries.write().await;
        if let Some(query) = queries.get_mut(query_id) {
            query.state = ExecutionState::Running;
            info!("Started CQELS query: {}", query_id);
            Ok(())
        } else {
            Err(anyhow!("Query not found: {}", query_id))
        }
    }

    /// Stop a query
    pub async fn stop_query(&self, query_id: &str) -> Result<()> {
        let mut queries = self.queries.write().await;
        if let Some(query) = queries.get_mut(query_id) {
            query.state = ExecutionState::Completed;
            info!("Stopped CQELS query: {}", query_id);
            Ok(())
        } else {
            Err(anyhow!("Query not found: {}", query_id))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_cqels_config_defaults() {
        let config = CqelsConfig::default();
        assert_eq!(config.max_queries, 100);
        assert!(config.incremental_evaluation);
        assert!(config.adaptive_optimization);
    }

    #[tokio::test]
    async fn test_window_definition() {
        let window = WindowDefinition {
            window_type: CqelsWindowType::TimeSliding,
            time_range: Some(Duration::from_secs(60)),
            triple_count: None,
            slide: Some(Duration::from_secs(10)),
        };

        assert_eq!(window.window_type, CqelsWindowType::TimeSliding);
        assert!(window.time_range.is_some());
    }

    #[tokio::test]
    async fn test_execution_state() {
        let state = ExecutionState::Idle;
        assert_eq!(state, ExecutionState::Idle);

        let state = ExecutionState::Running;
        assert_eq!(state, ExecutionState::Running);
    }

    #[tokio::test]
    async fn test_cqels_stats() {
        let stats = CqelsStats {
            queries_registered: 10,
            queries_executed: 50,
            total_stream_triples: 10000,
            total_static_triples: 5000,
            total_joins_performed: 25,
            avg_query_latency_ms: 12.5,
            memory_usage_bytes: 1024 * 1024,
        };

        assert_eq!(stats.queries_registered, 10);
        assert_eq!(stats.total_stream_triples, 10000);
    }
}