mstlo 0.1.0

A Rust library for online monitoring of Signal Temporal Logic (STL) specifications.
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
//! Naive STL operator backend.
//!
//! This module provides a straightforward recursive evaluator used mainly for:
//! - correctness comparison against the incremental engine, and
//! - tests/debugging.
//!
//! ## Recommendation
//! This backend is **not recommended** for production monitoring workloads.
//! While it may use slightly less memory in some scenarios, it is typically
//! significantly slower, especially for temporal operators (`G`, `F`, `U`).
//!
//! ## Limitations
//! - No RoSI (`RobustnessInterval`) mode in the monitor builder.
//! - No eager short-circuit evaluation mode (only delayed-style evaluation).

use crate::core::{RobustnessSemantics, SignalIdentifier, StlOperatorTrait, TimeInterval};
use crate::ring_buffer::RingBufferTrait;
use crate::ring_buffer::Step;
use std::fmt::Display;
use std::time::Duration;

/// Lightweight AST/operator representation used by the naive evaluator.
#[derive(Debug, Clone)]
pub enum StlOperator {
    // Boolean operators
    Not(Box<StlOperator>),
    And(Box<StlOperator>, Box<StlOperator>),
    Or(Box<StlOperator>, Box<StlOperator>),

    // Temporal operators
    Globally(TimeInterval, Box<StlOperator>),
    Eventually(TimeInterval, Box<StlOperator>),
    Until(TimeInterval, Box<StlOperator>, Box<StlOperator>),

    // logical operators
    Implies(Box<StlOperator>, Box<StlOperator>),

    // Atomic propositions
    True,
    False,
    GreaterThan(&'static str, f64), // signal name, threshold
    LessThan(&'static str, f64),    // signal name, threshold
}

/// Stateful wrapper around [`StlOperator`] for stream-based naive evaluation.
#[derive(Debug, Clone)]
pub struct StlFormula<T, C, Y>
where
    T: 'static,
    C: RingBufferTrait<Value = T> + 'static,
    Y: RobustnessSemantics + 'static,
{
    formula: StlOperator,
    signal: C,
    last_eval_time: Option<Duration>,
    _phantom: std::marker::PhantomData<Y>,
}

impl<T, C, Y> StlFormula<T, C, Y>
where
    T: 'static,
    C: RingBufferTrait<Value = T> + 'static,
    Y: RobustnessSemantics + 'static,
{
    /// Creates a new naive formula evaluator with an external signal buffer.
    pub fn new(formula: StlOperator, signal: C) -> Self {
        Self {
            formula,
            signal,
            last_eval_time: None,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T, C, Y> Display for StlFormula<T, C, Y>
where
    Y: RobustnessSemantics,
    C: RingBufferTrait<Value = T>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.formula)
    }
}

impl<T, C, Y> StlOperatorTrait<T> for StlFormula<T, C, Y>
where
    T: Clone + Copy + Into<f64> + 'static,
    C: RingBufferTrait<Value = T> + Clone + 'static,
    Y: RobustnessSemantics + 'static,
{
    type Output = Y;

    fn get_max_lookahead(&self) -> Duration {
        self.formula.get_max_lookahead()
    }

    /// Ingests one sample and evaluates at the oldest newly-finalizable timestamp.
    ///
    /// The naive engine performs recursive evaluation over buffered samples and
    /// returns at most one output step per call.
    fn update(&mut self, step: &Step<T>) -> Vec<Step<Self::Output>> {
        self.signal.add_step(step.clone());

        let max_lookahead = self.formula.get_max_lookahead();
        if step.timestamp < max_lookahead {
            return vec![];
        }

        let t_eval = step.timestamp.saturating_sub(max_lookahead);

        if self.last_eval_time == Some(t_eval) {
            return vec![]; // We already evaluated this t_eval, triggered by the previous step.
        }

        let robustness = self.formula.robustness_naive(&self.signal, t_eval);

        if robustness.is_some() {
            self.last_eval_time = Some(t_eval);
        }

        self.signal.prune(max_lookahead);

        match robustness {
            Some(robustness_step) => {
                vec![Step::new(
                    "output",
                    robustness_step.value,
                    robustness_step.timestamp,
                )]
            }
            None => vec![],
        }
    }
}

impl StlOperator {
    /// Computes the robustness of the formula at a specific time `t_eval`.
    /// This function assumes all necessary signal data (up to `t_eval + max_lookahead`) is present.
    fn robustness_naive<T, C, Y>(
        &self,
        // FIX: Remove signal_name
        signal: &C,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        C: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        match self {
            StlOperator::True => self.eval_true(t_eval),
            StlOperator::False => self.eval_false(t_eval),
            StlOperator::GreaterThan(name, c) => self.eval_greater_than(*c, name, signal, t_eval),
            StlOperator::LessThan(name, c) => self.eval_less_than(*c, name, signal, t_eval),
            StlOperator::Not(phi) => self.eval_not(phi, signal, t_eval),
            StlOperator::And(phi, psi) => self.eval_and(phi, psi, signal, t_eval),
            StlOperator::Or(phi, psi) => self.eval_or(phi, psi, signal, t_eval),
            StlOperator::Implies(phi, psi) => self.eval_implies(phi, psi, signal, t_eval),
            StlOperator::Eventually(interval, phi) => {
                self.eval_eventually(interval, phi, signal, t_eval)
            }
            StlOperator::Globally(interval, phi) => {
                self.eval_globally(interval, phi, signal, t_eval)
            }
            StlOperator::Until(interval, phi, psi) => {
                self.eval_until(interval, phi, psi, signal, t_eval)
            }
        }
    }
    /// Recursively computes the maximum lookahead required by this formula.
    fn get_max_lookahead(&self) -> Duration {
        match self {
            StlOperator::Globally(interval, f) | StlOperator::Eventually(interval, f) => {
                interval.end + f.get_max_lookahead()
            }
            StlOperator::Until(interval, f1, f2) => {
                interval.end + f1.get_max_lookahead().max(f2.get_max_lookahead())
            }
            StlOperator::Not(f) => f.get_max_lookahead(),
            StlOperator::And(f1, f2) | StlOperator::Or(f1, f2) | StlOperator::Implies(f1, f2) => {
                f1.get_max_lookahead().max(f2.get_max_lookahead())
            }
            StlOperator::True
            | StlOperator::False
            | StlOperator::GreaterThan(_, _)
            | StlOperator::LessThan(_, _) => Duration::ZERO,
        }
    }

    fn eval_true<Y: RobustnessSemantics>(&self, t_eval: Duration) -> Option<Step<Y>> {
        Some(Step::new("output", Y::atomic_true(), t_eval))
    }

    fn eval_false<Y: RobustnessSemantics>(&self, t_eval: Duration) -> Option<Step<Y>> {
        Some(Step::new("output", Y::atomic_false(), t_eval))
    }

    fn eval_greater_than<T, S, Y>(
        &self,
        c: f64,
        name: &'static str,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        signal
            .iter()
            .find(|s| s.timestamp == t_eval && s.signal == name)
            .map(|step| {
                Step::new(
                    "output",
                    Y::atomic_greater_than(step.value.into(), c),
                    t_eval,
                )
            })
    }

    fn eval_less_than<T, S, Y>(
        &self,
        c: f64,
        name: &'static str,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        signal
            .iter()
            .find(|s| s.timestamp == t_eval && s.signal == name)
            .map(|step| Step::new("output", Y::atomic_less_than(step.value.into(), c), t_eval))
    }

    fn eval_not<T, S, Y>(&self, phi: &StlOperator, signal: &S, t_eval: Duration) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        // Recursively call for the same t_eval
        phi.robustness_naive(signal, t_eval)
            .map(|step| Step::new("output", Y::not(step.value), step.timestamp))
    }

    fn eval_and<T, S, Y>(
        &self,
        phi: &StlOperator,
        psi: &StlOperator,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        // Recursively call both children for the same t_eval
        phi.robustness_naive(signal, t_eval)
            .zip(psi.robustness_naive(signal, t_eval))
            .map(|(step1, step2)| {
                Step::new("output", Y::and(step1.value, step2.value), step1.timestamp)
            })
    }

    fn eval_or<T, S, Y>(
        &self,
        phi: &StlOperator,
        psi: &StlOperator,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        phi.robustness_naive(signal, t_eval)
            .zip(psi.robustness_naive(signal, t_eval))
            .map(|(step1, step2)| {
                Step::new("output", Y::or(step1.value, step2.value), step1.timestamp)
            })
    }

    fn eval_implies<T, S, Y>(
        &self,
        phi: &StlOperator,
        psi: &StlOperator,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        phi.robustness_naive(signal, t_eval)
            .zip(psi.robustness_naive(signal, t_eval))
            .map(|(step1, step2)| {
                Step::new(
                    "output",
                    Y::implies(step1.value, step2.value),
                    step1.timestamp,
                )
            })
    }

    fn eval_eventually<T, S, Y>(
        &self,
        interval: &TimeInterval,
        phi: &StlOperator,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        // We are evaluating for t_eval. We need data in the window [t_eval+a, t_eval+b]
        let lower_bound_t_prime = t_eval + interval.start;
        let upper_bound_t_prime = t_eval + interval.end;

        // Check if we have enough data in the signal
        if let Some(back_step) = signal.get_back() {
            if back_step.timestamp < upper_bound_t_prime {
                return None; // Not enough data to evaluate yet
            }
        } else {
            return None; // Signal is empty
        }

        let result = signal
            .iter()
            .filter(|step| {
                step.timestamp >= lower_bound_t_prime && step.timestamp <= upper_bound_t_prime
            })
            // Recursively call for each step's timestamp in the window
            .map(|step| phi.robustness_naive(signal, step.timestamp))
            .try_fold(Y::eventually_identity(), |acc, item| {
                item.map(|step| Y::or(acc, step.value))
            })?;

        // Return the result for the original t_eval
        Some(Step::new("output", result, t_eval))
    }

    fn eval_globally<T, S, Y>(
        &self,
        interval: &TimeInterval,
        phi: &StlOperator,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        let lower_bound_t_prime = t_eval + interval.start;
        let upper_bound_t_prime = t_eval + interval.end;

        if let Some(back_step) = signal.get_back() {
            if back_step.timestamp < upper_bound_t_prime {
                return None;
            }
        } else {
            return None;
        }

        let result = signal
            .iter()
            .filter(|step| {
                step.timestamp >= lower_bound_t_prime && step.timestamp <= upper_bound_t_prime
            })
            .map(|step| phi.robustness_naive(signal, step.timestamp))
            .try_fold(Y::globally_identity(), |acc, item| {
                item.map(|step| Y::and(acc, step.value))
            })?;

        Some(Step::new("output", result, t_eval))
    }

    fn eval_until<T, S, Y>(
        &self,
        interval: &TimeInterval,
        phi: &StlOperator,
        psi: &StlOperator,
        signal: &S,
        t_eval: Duration,
    ) -> Option<Step<Y>>
    where
        S: RingBufferTrait<Value = T>,
        T: Clone + Copy + Into<f64>,
        Y: RobustnessSemantics,
    {
        let lower_bound_t_prime = t_eval + interval.start;
        let upper_bound_t_prime = t_eval + interval.end;

        if let Some(back_step) = signal.get_back() {
            if back_step.timestamp < upper_bound_t_prime {
                return None;
            }
        } else {
            return None;
        }

        let result = signal
            .iter()
            .filter(|step| {
                step.timestamp >= lower_bound_t_prime && step.timestamp <= upper_bound_t_prime
            })
            .map(|step_t_prime| {
                let t_prime = step_t_prime.timestamp;
                let robustness_psi = psi.robustness_naive(signal, t_prime);

                let robustness_phi_g = signal
                    .iter()
                    .filter(|s| s.timestamp >= lower_bound_t_prime && s.timestamp < t_prime) // G is up to t_prime
                    .map(|s| phi.robustness_naive(signal, s.timestamp))
                    .try_fold(Y::globally_identity(), |acc, item| {
                        item.map(|step| Y::and(acc, step.value))
                    });

                robustness_psi
                    .zip(robustness_phi_g)
                    .map(|(r_psi, r_phi_val)| Y::and(r_psi.value, r_phi_val))
            })
            .try_fold(Y::eventually_identity(), |acc, item| {
                // item is Option<Y>
                item.map(|robustness_value| Y::or(acc, robustness_value))
            })?;

        Some(Step::new("output", result, t_eval))
    }
}
impl Display for StlOperator {
    /// Formats the formula in compact mathematical notation.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                StlOperator::True => "True".to_string(),
                StlOperator::False => "False".to_string(),
                StlOperator::Not(f) => format!("¬({f})"),
                StlOperator::And(f1, f2) => format!("({f1}) ∧ ({f2})"),
                StlOperator::Or(f1, f2) => format!("({f1}) v ({f2})"),
                StlOperator::Globally(interval, f) => format!(
                    "G[{}, {}]({})",
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64(),
                    f
                ),
                StlOperator::Eventually(interval, f) => format!(
                    "F[{}, {}]({})",
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64(),
                    f
                ),
                StlOperator::Until(interval, f1, f2) => format!(
                    "({}) U[{}, {}] ({})",
                    f1,
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64(),
                    f2
                ),
                StlOperator::Implies(f1, f2) => format!("({f1}) → ({f2})"),
                StlOperator::GreaterThan(s, val) => format!("{s} > {val}"),
                StlOperator::LessThan(s, val) => format!("{s} < {val}"),
            }
        )
    }
}

impl<T, C, Y> SignalIdentifier for StlFormula<T, C, Y>
where
    T: 'static,
    C: RingBufferTrait<Value = T> + 'static,
    Y: RobustnessSemantics + 'static,
{
    /// Collects all signal identifiers referenced by the formula tree.
    fn get_signal_identifiers(&mut self) -> std::collections::HashSet<&'static str> {
        let mut signals = std::collections::HashSet::new();
        fn collect_signals(
            node: &StlOperator,
            signals: &mut std::collections::HashSet<&'static str>,
        ) {
            match node {
                StlOperator::GreaterThan(s, _) | StlOperator::LessThan(s, _) => {
                    signals.insert(*s);
                }
                StlOperator::True | StlOperator::False => {}
                StlOperator::Not(f) => {
                    collect_signals(f, signals);
                }
                StlOperator::And(f1, f2)
                | StlOperator::Or(f1, f2)
                | StlOperator::Implies(f1, f2) => {
                    collect_signals(f1, signals);
                    collect_signals(f2, signals);
                }
                StlOperator::Globally(_, f) | StlOperator::Eventually(_, f) => {
                    collect_signals(f, signals);
                }
                StlOperator::Until(_, f1, f2) => {
                    collect_signals(f1, signals);
                    collect_signals(f2, signals);
                }
            }
        }
        collect_signals(&self.formula, &mut signals);
        signals
    }
}

#[cfg(test)]
mod tests {

    mod stl_formula_tests {
        use super::*;

        #[test]
        fn test_get_max_lookahead() {
            let formula: StlFormula<f64, RingBuffer<f64>, f64> = StlFormula::new(
                StlOperator::Globally(
                    TimeInterval {
                        start: Duration::from_secs(1),
                        end: Duration::from_secs(2),
                    },
                    Box::new(StlOperator::False),
                ),
                RingBuffer::new(),
            );

            assert_eq!(formula.get_max_lookahead(), Duration::from_secs(2));
        }
    }

    use super::*;
    use crate::step;
    use crate::{naive_operators::StlFormula, ring_buffer::RingBuffer};
    use std::time::Duration;

    // Helper to create a signal from a vector of values and timestamps
    fn create_signal(values: Vec<f64>, timestamps: Vec<u64>) -> RingBuffer<f64> {
        let mut signal = RingBuffer::new();
        for (val, ts) in values.into_iter().zip(timestamps.into_iter()) {
            signal.add_step(step!("x", val, Duration::from_secs(ts)));
        }
        signal
    }

    #[test]
    fn atomic_greater_than_robustness() {
        let formula = StlOperator::GreaterThan("x", 10.0);

        let signal = create_signal(vec![15.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", 5.0, Duration::from_secs(5)))
        );

        let signal2 = create_signal(vec![8.0], vec![5]);
        let robustness2 = formula.robustness_naive::<f64, _, f64>(&signal2, Duration::from_secs(5));
        assert_eq!(
            robustness2,
            Some(step!("output", -2.0, Duration::from_secs(5)))
        );
    }

    #[test]
    fn atomic_less_than_robustness() {
        let formula = StlOperator::LessThan("x", 10.0);

        let signal = create_signal(vec![5.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", 5.0, Duration::from_secs(5)))
        );

        let signal2 = create_signal(vec![12.0], vec![5]);
        let robustness2 = formula.robustness_naive::<f64, _, f64>(&signal2, Duration::from_secs(5));
        assert_eq!(
            robustness2,
            Some(step!("output", -2.0, Duration::from_secs(5)))
        );
    }

    #[test]
    fn atomic_true_robustness() {
        let formula = StlOperator::True;

        let signal = create_signal(vec![0.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", f64::INFINITY, Duration::from_secs(5)))
        );
    }

    #[test]
    fn atomic_false_robustness() {
        let formula = StlOperator::False;

        let signal = create_signal(vec![0.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", f64::NEG_INFINITY, Duration::from_secs(5)))
        );
    }

    #[test]
    fn not_operator_robustness() {
        let formula = StlOperator::Not(Box::new(StlOperator::GreaterThan("x", 10.0)));

        let signal = create_signal(vec![15.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", -5.0, Duration::from_secs(5)))
        );
    }

    #[test]
    fn and_operator_robustness() {
        let formula = StlOperator::And(
            Box::new(StlOperator::GreaterThan("x", 10.0)),
            Box::new(StlOperator::LessThan("x", 20.0)),
        );

        let signal = create_signal(vec![15.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", 5.0, Duration::from_secs(5)))
        );
    }

    #[test]
    fn or_operator_robustness() {
        let formula = StlOperator::Or(
            Box::new(StlOperator::GreaterThan("x", 10.0)),
            Box::new(StlOperator::LessThan("x", 5.0)),
        );

        let signal = create_signal(vec![15.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", 5.0, Duration::from_secs(5)))
        );
    }

    #[test]
    fn implies_operator_robustness() {
        let formula = StlOperator::Implies(
            Box::new(StlOperator::GreaterThan("x", 10.0)),
            Box::new(StlOperator::LessThan("x", 20.0)),
        );

        let signal = create_signal(vec![15.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", 5.0, Duration::from_secs(5)))
        );
    }

    #[test]
    fn eventually_operator_robustness() {
        let formula = StlOperator::Eventually(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(4),
            },
            Box::new(StlOperator::GreaterThan("x", 10.0)),
        );

        // Case 1: Not enough data
        let signal1 = create_signal(vec![15.0, 12.0], vec![0, 2]); // Max timestamp is 2, need up to 4
        let robustness1 = formula.robustness_naive::<f64, _, f64>(&signal1, Duration::from_secs(0));
        assert_eq!(robustness1, None);
        // Case 2: Just enough data for t_eval=0
        let signal2 = create_signal(vec![15.0, 12.0, 8.0], vec![0, 2, 4]); // Max timestamp 4
        // eval at t=0, window [0,4], values are 15, 12, 8. Robustness values: 5, 2, -2. Max is 5.
        let robustness2 = formula.robustness_naive::<f64, _, f64>(&signal2, Duration::from_secs(0));
        assert_eq!(
            robustness2,
            Some(step!("output", 5.0, Duration::from_secs(0)))
        );

        // Case 3: More data, eval at t=2
        let signal3 = create_signal(vec![15.0, 12.0, 8.0, 5.0], vec![0, 2, 4, 6]); // Max timestamp 6
        // eval at t=2, window [2,6], values are 12, 8, 5. Robustness values: 2, -2, -5. Max is 2.
        let robustness3 = formula.robustness_naive::<f64, _, f64>(&signal3, Duration::from_secs(2));
        assert_eq!(
            robustness3,
            Some(step!("output", 2.0, Duration::from_secs(2)))
        );

        // Case 4: Full signal, eval at t=4
        let signal4 = create_signal(vec![15.0, 12.0, 8.0, 5.0, 12.0], vec![0, 2, 4, 6, 8]); // Max timestamp 8
        // eval at t=4, window [4,8], values are 8, 5, 12. Robustness values: -2, -5, 2. Max is 2.
        let robustness4 = formula.robustness_naive::<f64, _, f64>(&signal4, Duration::from_secs(4));
        assert_eq!(
            robustness4,
            Some(step!("output", 2.0, Duration::from_secs(4)))
        );
    }

    #[test]
    fn globally_operator_robustness() {
        let formula = StlOperator::Globally(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(4),
            },
            Box::new(StlOperator::GreaterThan("x", 10.0)),
        );

        // Case 1: Not enough data
        let signal1 = create_signal(vec![15.0, 12.0], vec![0, 2]); // Max timestamp is 2, need up to 4
        let robustness1 = formula.robustness_naive::<f64, _, f64>(&signal1, Duration::from_secs(0));
        assert_eq!(robustness1, None);

        // Case 2: Just enough data for t_eval=0
        let signal2 = create_signal(vec![15.0, 12.0, 8.0], vec![0, 2, 4]); // Max timestamp 4
        // eval at t=0, window [0,4], values are 15, 12, 8. Robustness values: 5, 2, -2. Min is -2.
        let robustness2 = formula.robustness_naive::<f64, _, f64>(&signal2, Duration::from_secs(0));
        assert_eq!(
            robustness2,
            Some(step!("output", -2.0, Duration::from_secs(0)))
        );

        // Case 3: More data, eval at t=2
        let signal3 = create_signal(vec![15.0, 12.0, 8.0, 5.0], vec![0, 2, 4, 6]); // Max timestamp 6
        // eval at t=2, window [2,6], values are 12, 8, 5. Robustness values: 2, -2, -5. Min is -5.
        let robustness3 = formula.robustness_naive::<f64, _, f64>(&signal3, Duration::from_secs(2));
        assert_eq!(
            robustness3,
            Some(step!("output", -5.0, Duration::from_secs(2)))
        );

        // Case 4: Full signal, eval at t=4
        let signal4 = create_signal(vec![15.0, 12.0, 8.0, 5.0, 12.0], vec![0, 2, 4, 6, 8]); // Max timestamp 8
        // eval at t=4, window [4,8], values are 8, 5, 12. Robustness values: -2, -5, 2. Min is -5.
        let robustness4 = formula.robustness_naive::<f64, _, f64>(&signal4, Duration::from_secs(4));
        assert_eq!(
            robustness4,
            Some(step!("output", -5.0, Duration::from_secs(4)))
        );
    }

    #[test]
    fn until_operator_robustness() {
        let formula = StlOperator::Until(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(4),
            },
            Box::new(StlOperator::GreaterThan("x", 0.0)),
            Box::new(StlOperator::GreaterThan("x", 10.0)),
        );

        // Case 1: Not enough data
        let signal1 = create_signal(vec![5.0, 8.0], vec![0, 2]);
        let robustness1 = formula.robustness_naive::<f64, _, f64>(&signal1, Duration::from_secs(0));
        assert_eq!(robustness1, None);

        // Case 2: Enough data - psi satisfied at t=2 (value=12 > 10)
        let signal2 = create_signal(vec![5.0, 12.0, 3.0], vec![0, 2, 4]);
        let robustness2 = formula.robustness_naive::<f64, _, f64>(&signal2, Duration::from_secs(0));
        assert!(robustness2.is_some());
    }

    #[test]
    fn display_impl_all_operators() {
        let gt = StlOperator::GreaterThan("x", 5.0);
        assert_eq!(format!("{gt}"), "x > 5");

        let lt = StlOperator::LessThan("y", 3.0);
        assert_eq!(format!("{lt}"), "y < 3");

        let t = StlOperator::True;
        assert_eq!(format!("{t}"), "True");

        let f = StlOperator::False;
        assert_eq!(format!("{f}"), "False");

        let not = StlOperator::Not(Box::new(StlOperator::GreaterThan("x", 1.0)));
        assert_eq!(format!("{not}"), "¬(x > 1)");

        let and = StlOperator::And(
            Box::new(StlOperator::GreaterThan("x", 1.0)),
            Box::new(StlOperator::LessThan("y", 2.0)),
        );
        assert_eq!(format!("{and}"), "(x > 1) ∧ (y < 2)");

        let or = StlOperator::Or(
            Box::new(StlOperator::GreaterThan("x", 1.0)),
            Box::new(StlOperator::LessThan("y", 2.0)),
        );
        assert_eq!(format!("{or}"), "(x > 1) v (y < 2)");

        let globally = StlOperator::Globally(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(10),
            },
            Box::new(StlOperator::GreaterThan("x", 5.0)),
        );
        assert_eq!(format!("{globally}"), "G[0, 10](x > 5)");

        let eventually = StlOperator::Eventually(
            TimeInterval {
                start: Duration::from_secs(1),
                end: Duration::from_secs(5),
            },
            Box::new(StlOperator::LessThan("y", 3.0)),
        );
        assert_eq!(format!("{eventually}"), "F[1, 5](y < 3)");

        let until = StlOperator::Until(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(10),
            },
            Box::new(StlOperator::GreaterThan("x", 1.0)),
            Box::new(StlOperator::LessThan("y", 2.0)),
        );
        assert_eq!(format!("{until}"), "(x > 1) U[0, 10] (y < 2)");

        let implies = StlOperator::Implies(
            Box::new(StlOperator::GreaterThan("x", 1.0)),
            Box::new(StlOperator::LessThan("y", 2.0)),
        );
        assert_eq!(format!("{implies}"), "(x > 1) → (y < 2)");
    }

    #[test]
    fn get_signal_identifiers_naive() {
        let mut formula: StlFormula<f64, RingBuffer<f64>, f64> = StlFormula::new(
            StlOperator::And(
                Box::new(StlOperator::GreaterThan("x", 5.0)),
                Box::new(StlOperator::Until(
                    TimeInterval {
                        start: Duration::from_secs(0),
                        end: Duration::from_secs(5),
                    },
                    Box::new(StlOperator::LessThan("y", 10.0)),
                    Box::new(StlOperator::Not(Box::new(StlOperator::GreaterThan(
                        "z", 1.0,
                    )))),
                )),
            ),
            RingBuffer::new(),
        );

        let ids = formula.get_signal_identifiers();
        assert!(ids.contains("x"));
        assert!(ids.contains("y"));
        assert!(ids.contains("z"));
        assert_eq!(ids.len(), 3);
    }

    #[test]
    fn eval_naive_full_pipeline() {
        // Test the full StlFormula eval_naive pipeline with Eventually
        let mut formula: StlFormula<f64, RingBuffer<f64>, f64> = StlFormula::new(
            StlOperator::Eventually(
                TimeInterval {
                    start: Duration::from_secs(0),
                    end: Duration::from_secs(2),
                },
                Box::new(StlOperator::GreaterThan("x", 10.0)),
            ),
            RingBuffer::new(),
        );

        // Not enough data yet (timestamp 0 < max_lookahead 2)
        let step1 = step!("x", 15.0, Duration::from_secs(0));
        let result1 = formula.update(&step1);
        assert!(result1.is_empty());

        let step2 = step!("x", 12.0, Duration::from_secs(1));
        let result2 = formula.update(&step2);
        assert!(result2.is_empty());

        // Now t=2, t_eval = 2-2 = 0
        let step3 = step!("x", 8.0, Duration::from_secs(2));
        let result3 = formula.update(&step3);
        assert_eq!(result3.len(), 1);
        assert_eq!(result3[0].timestamp, Duration::from_secs(0));
    }

    #[test]
    fn eval_naive_display_formula() {
        let formula: StlFormula<f64, RingBuffer<f64>, f64> = StlFormula::new(
            StlOperator::Globally(
                TimeInterval {
                    start: Duration::from_secs(0),
                    end: Duration::from_secs(5),
                },
                Box::new(StlOperator::GreaterThan("x", 10.0)),
            ),
            RingBuffer::new(),
        );

        let display = format!("{formula}");
        assert_eq!(display, "G[0, 5](x > 10)");
    }

    #[test]
    fn eval_naive_empty_signal_eventually() {
        // Test evaluation of eventually with empty signal
        let formula = StlOperator::Eventually(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(2),
            },
            Box::new(StlOperator::GreaterThan("x", 10.0)),
        );
        let signal: RingBuffer<f64> = RingBuffer::new();
        let result = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(0));
        assert_eq!(result, None);
    }

    #[test]
    fn eval_naive_empty_signal_globally() {
        let formula = StlOperator::Globally(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(2),
            },
            Box::new(StlOperator::GreaterThan("x", 10.0)),
        );
        let signal: RingBuffer<f64> = RingBuffer::new();
        let result = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(0));
        assert_eq!(result, None);
    }

    #[test]
    fn eval_naive_empty_signal_until() {
        let formula = StlOperator::Until(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(2),
            },
            Box::new(StlOperator::GreaterThan("x", 0.0)),
            Box::new(StlOperator::GreaterThan("x", 10.0)),
        );
        let signal: RingBuffer<f64> = RingBuffer::new();
        let result = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(0));
        assert_eq!(result, None);
    }

    #[test]
    fn less_than_robustness_via_naive() {
        // Ensure LessThan is exercised through robustness_naive (line 216)
        let formula = StlOperator::LessThan("x", 10.0);
        let signal = create_signal(vec![7.0], vec![5]);
        let robustness = formula.robustness_naive::<f64, _, f64>(&signal, Duration::from_secs(5));
        assert_eq!(
            robustness,
            Some(step!("output", 3.0, Duration::from_secs(5)))
        );
    }

    #[test]
    fn get_signal_identifiers_implies_or_eventually_globally() {
        let mut formula: StlFormula<f64, RingBuffer<f64>, f64> = StlFormula::new(
            StlOperator::Implies(
                Box::new(StlOperator::Or(
                    Box::new(StlOperator::GreaterThan("a", 1.0)),
                    Box::new(StlOperator::Globally(
                        TimeInterval {
                            start: Duration::from_secs(0),
                            end: Duration::from_secs(1),
                        },
                        Box::new(StlOperator::LessThan("b", 2.0)),
                    )),
                )),
                Box::new(StlOperator::Eventually(
                    TimeInterval {
                        start: Duration::from_secs(0),
                        end: Duration::from_secs(1),
                    },
                    Box::new(StlOperator::GreaterThan("c", 3.0)),
                )),
            ),
            RingBuffer::new(),
        );

        let ids = formula.get_signal_identifiers();
        assert!(ids.contains("a"));
        assert!(ids.contains("b"));
        assert!(ids.contains("c"));
        assert_eq!(ids.len(), 3);
    }
}