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
//! Abstract syntax tree definition for Signal Temporal Logic (STL) formulas.
//!
//! [`FormulaDefinition`] models predicate, boolean, and temporal operators used
//! by the monitor and parser layers.

use crate::core::{SignalIdentifier, TimeInterval};

use std::fmt::Display;

/// An enum representing the definition of an STL formula.
///
/// Predicates can use either constant values (e.g., `x > 5.0`) or variables
/// (e.g., `x > A` where `A` is a named variable that can be updated at runtime).
#[derive(Clone, Debug, PartialEq)]
pub enum FormulaDefinition {
    /// Signal greater than a constant: signal > value
    GreaterThan(&'static str, f64),
    /// Signal less than a constant: signal < value
    LessThan(&'static str, f64),
    /// Signal greater than a variable: signal > var_name
    GreaterThanVar(&'static str, &'static str),
    /// Signal less than a variable: signal < var_name
    LessThanVar(&'static str, &'static str),
    /// Boolean constant `True`.
    True,
    /// Boolean constant `False`.
    False,
    /// Boolean conjunction: `lhs ∧ rhs`.
    And(Box<FormulaDefinition>, Box<FormulaDefinition>),
    /// Boolean disjunction: `lhs ∨ rhs`.
    Or(Box<FormulaDefinition>, Box<FormulaDefinition>),
    /// Boolean negation: `¬f`.
    Not(Box<FormulaDefinition>),
    /// Boolean implication: `lhs → rhs`.
    Implies(Box<FormulaDefinition>, Box<FormulaDefinition>),
    /// Temporal eventually operator: `F[a,b] f`.
    Eventually(TimeInterval, Box<FormulaDefinition>),
    /// Temporal globally operator: `G[a,b] f`.
    Globally(TimeInterval, Box<FormulaDefinition>),
    /// Temporal until operator: `lhs U[a,b] rhs`.
    Until(TimeInterval, Box<FormulaDefinition>, Box<FormulaDefinition>),
}

/// Renders formulas using compact mathematical notation.
///
/// Temporal operators are printed as `F[start, end](...)`, `G[start, end](...)`,
/// and `(...) U[start, end] (...)`, where interval bounds are shown in seconds.
impl Display for FormulaDefinition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                FormulaDefinition::True => "True".to_string(),
                FormulaDefinition::False => "False".to_string(),
                FormulaDefinition::Not(f) => format!("¬({f})"),
                FormulaDefinition::And(f1, f2) => format!("({f1}) ∧ ({f2})"),
                FormulaDefinition::Or(f1, f2) => format!("({f1}) v ({f2})"),
                FormulaDefinition::Globally(interval, f) => format!(
                    "G[{}, {}]({})",
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64(),
                    f
                ),
                FormulaDefinition::Eventually(interval, f) => format!(
                    "F[{}, {}]({})",
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64(),
                    f
                ),
                FormulaDefinition::Until(interval, f1, f2) => format!(
                    "({}) U[{}, {}] ({})",
                    f1,
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64(),
                    f2
                ),
                FormulaDefinition::Implies(f1, f2) => format!("({f1}) → ({f2})"),
                FormulaDefinition::GreaterThan(s, val) => format!("{s} > {val}"),
                FormulaDefinition::LessThan(s, val) => format!("{s} < {val}"),
                FormulaDefinition::GreaterThanVar(s, var) => format!("{s} > ${var}"),
                FormulaDefinition::LessThanVar(s, var) => format!("{s} < ${var}"),
            }
        )
    }
}

impl SignalIdentifier for FormulaDefinition {
    /// Returns all signal identifiers referenced by the formula.
    ///
    /// The traversal is recursive and collects unique signal names from both
    /// constant and variable predicates.
    fn get_signal_identifiers(&mut self) -> std::collections::HashSet<&'static str> {
        let mut signals = std::collections::HashSet::new();
        fn collect_signals(
            node: &FormulaDefinition,
            signals: &mut std::collections::HashSet<&'static str>,
        ) {
            match node {
                FormulaDefinition::GreaterThan(s, _)
                | FormulaDefinition::LessThan(s, _)
                | FormulaDefinition::GreaterThanVar(s, _)
                | FormulaDefinition::LessThanVar(s, _) => {
                    signals.insert(*s);
                }
                FormulaDefinition::True | FormulaDefinition::False => {}
                FormulaDefinition::Not(f) => {
                    collect_signals(f, signals);
                }
                FormulaDefinition::And(f1, f2)
                | FormulaDefinition::Or(f1, f2)
                | FormulaDefinition::Implies(f1, f2) => {
                    collect_signals(f1, signals);
                    collect_signals(f2, signals);
                }
                FormulaDefinition::Eventually(_, f) | FormulaDefinition::Globally(_, f) => {
                    collect_signals(f, signals);
                }
                FormulaDefinition::Until(_, f1, f2) => {
                    collect_signals(f1, signals);
                    collect_signals(f2, signals);
                }
            }
        }
        collect_signals(self, &mut signals);
        signals
    }
}

impl FormulaDefinition {
    /// Collects all variable identifiers used in the formula.
    ///
    /// Only variable predicates (`GreaterThanVar`/`LessThanVar`) contribute.
    pub fn get_variable_identifiers(&self) -> std::collections::HashSet<&'static str> {
        let mut variables = std::collections::HashSet::new();
        fn collect_variables(
            node: &FormulaDefinition,
            variables: &mut std::collections::HashSet<&'static str>,
        ) {
            match node {
                FormulaDefinition::GreaterThanVar(_, var)
                | FormulaDefinition::LessThanVar(_, var) => {
                    variables.insert(*var);
                }
                FormulaDefinition::GreaterThan(_, _)
                | FormulaDefinition::LessThan(_, _)
                | FormulaDefinition::True
                | FormulaDefinition::False => {}
                FormulaDefinition::Not(f) => {
                    collect_variables(f, variables);
                }
                FormulaDefinition::And(f1, f2)
                | FormulaDefinition::Or(f1, f2)
                | FormulaDefinition::Implies(f1, f2) => {
                    collect_variables(f1, variables);
                    collect_variables(f2, variables);
                }
                FormulaDefinition::Eventually(_, f) | FormulaDefinition::Globally(_, f) => {
                    collect_variables(f, variables);
                }
                FormulaDefinition::Until(_, f1, f2) => {
                    collect_variables(f1, variables);
                    collect_variables(f2, variables);
                }
            }
        }
        collect_variables(self, &mut variables);
        variables
    }

    /// Builds an ASCII/Unicode tree representation of the formula Abastract Syntax Tree (AST).
    ///
    /// # Arguments
    /// * `indent` - Number of leading spaces for the root node.
    ///
    /// # Returns
    /// A multi-line string with `tree`-style connectors (`├──`, `└──`, `│`).
    ///
    /// # Example
    /// A conjunction may look like:
    ///
    /// ```text
    /// And
    /// ├── x > 1
    /// └── y < 2
    /// ```
    pub fn to_tree_string(&self, indent: usize) -> String {
        // Produce a tree-like multi-line representation using characters similar to `tree`:
        // ├──  branch
        // └──  last branch
        // │    vertical continuation
        let padding = " ".repeat(indent);

        fn label(node: &FormulaDefinition) -> String {
            match node {
                FormulaDefinition::True => "True".to_string(),
                FormulaDefinition::False => "False".to_string(),
                FormulaDefinition::Not(_) => "Not".to_string(),
                FormulaDefinition::And(_, _) => "And".to_string(),
                FormulaDefinition::Or(_, _) => "Or".to_string(),
                FormulaDefinition::Globally(interval, _) => format!(
                    "Globally[{},{}]",
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64()
                ),
                FormulaDefinition::Eventually(interval, _) => format!(
                    "Eventually[{},{}]",
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64()
                ),
                FormulaDefinition::Until(interval, _, _) => format!(
                    "Until[{},{}]",
                    interval.start.as_secs_f64(),
                    interval.end.as_secs_f64()
                ),
                FormulaDefinition::Implies(_, _) => "Implies".to_string(),
                FormulaDefinition::GreaterThan(s, val) => format!("{} > {}", s, val),
                FormulaDefinition::LessThan(s, val) => format!("{} < {}", s, val),
                FormulaDefinition::GreaterThanVar(s, var) => format!("{} > ${}", s, var),
                FormulaDefinition::LessThanVar(s, var) => format!("{} < ${}", s, var),
            }
        }

        fn write_node(
            node: &FormulaDefinition,
            prefix: &str,
            is_root: bool,
            is_last: bool,
            out: &mut String,
        ) {
            if is_root {
                out.push_str(&format!("{}{}\n", prefix, label(node)));
            } else {
                let connector = if is_last { "└── " } else { "├── " };
                out.push_str(&format!("{}{}{}\n", prefix, connector, label(node)));
            }

            // prepare prefix for children
            let child_prefix = if is_root {
                // root's children prefix depends on whether root had initial padding
                if prefix.trim().is_empty() {
                    if is_last {
                        format!("{}    ", prefix)
                    } else {
                        format!("{}", prefix)
                    }
                } else if is_last {
                    format!("{}    ", prefix)
                } else {
                    format!("{}", prefix)
                }
            } else if is_last {
                format!("{}    ", prefix)
            } else {
                format!("{}", prefix)
            };

            match node {
                FormulaDefinition::Not(child) => {
                    write_node(child, &child_prefix, false, true, out);
                }
                FormulaDefinition::And(l, r)
                | FormulaDefinition::Or(l, r)
                | FormulaDefinition::Implies(l, r)
                | FormulaDefinition::Until(_, l, r) => {
                    write_node(l, &child_prefix, false, false, out);
                    write_node(r, &child_prefix, false, true, out);
                }
                FormulaDefinition::Eventually(_, child) | FormulaDefinition::Globally(_, child) => {
                    write_node(child, &child_prefix, false, true, out);
                }
                // leaves: True, False, GreaterThan, LessThan, GreaterThanVar, LessThanVar - nothing to do
                FormulaDefinition::True
                | FormulaDefinition::False
                | FormulaDefinition::GreaterThan(_, _)
                | FormulaDefinition::LessThan(_, _)
                | FormulaDefinition::GreaterThanVar(_, _)
                | FormulaDefinition::LessThanVar(_, _) => {}
            }
        }

        let mut out = String::new();
        write_node(self, &padding, true, true, &mut out);
        // trim trailing newline for tidiness
        out.trim_end().to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::TimeInterval;
    use std::time::Duration;

    #[test]
    fn test_true_false_display_and_tree() {
        let t = FormulaDefinition::True;
        let f = FormulaDefinition::False;
        assert_eq!(format!("{}", t), "True");
        assert_eq!(t.to_tree_string(0), "True");
        assert_eq!(format!("{}", f), "False");
        assert_eq!(f.to_tree_string(0), "False");
    }

    #[test]
    fn test_predicates_and_not() {
        let gt = FormulaDefinition::GreaterThan("x", 5.0);
        let lt = FormulaDefinition::LessThan("y", 2.5);
        let not = FormulaDefinition::Not(Box::new(FormulaDefinition::GreaterThan("z", 1.0)));

        assert_eq!(format!("{}", gt), "x > 5");
        assert_eq!(gt.to_tree_string(0), "x > 5");

        assert_eq!(format!("{}", lt), "y < 2.5");
        assert_eq!(lt.to_tree_string(0), "y < 2.5");

        assert_eq!(format!("{}", not), "¬(z > 1)");
        assert_eq!(not.to_tree_string(0), "Not\n    └── z > 1");
    }

    #[test]
    fn test_logical_binary_and_or_implies() {
        let a = FormulaDefinition::GreaterThan("x", 1.0);
        let b = FormulaDefinition::LessThan("y", 2.0);

        let and = FormulaDefinition::And(Box::new(a.clone()), Box::new(b.clone()));
        let or = FormulaDefinition::Or(Box::new(a.clone()), Box::new(b.clone()));
        let imp = FormulaDefinition::Implies(Box::new(a.clone()), Box::new(b.clone()));

        assert_eq!(format!("{}", and), "(x > 1) ∧ (y < 2)");
        assert_eq!(and.to_tree_string(0), "And\n    ├── x > 1\n    └── y < 2");

        assert_eq!(format!("{}", or), "(x > 1) v (y < 2)");
        assert_eq!(or.to_tree_string(0), "Or\n    ├── x > 1\n    └── y < 2");

        assert_eq!(format!("{}", imp), "(x > 1) → (y < 2)");
        assert_eq!(
            imp.to_tree_string(0),
            "Implies\n    ├── x > 1\n    └── y < 2"
        );
    }

    #[test]
    fn test_temporal_and_until() {
        let interval = TimeInterval {
            start: Duration::from_secs(0),
            end: Duration::from_secs(2),
        };
        let ev = FormulaDefinition::Eventually(interval, Box::new(FormulaDefinition::True));
        let gl = FormulaDefinition::Globally(
            TimeInterval {
                start: Duration::from_secs(1),
                end: Duration::from_secs(3),
            },
            Box::new(FormulaDefinition::False),
        );
        let until = FormulaDefinition::Until(
            TimeInterval {
                start: Duration::from_secs(0),
                end: Duration::from_secs(5),
            },
            Box::new(FormulaDefinition::GreaterThan("a", 0.0)),
            Box::new(FormulaDefinition::LessThan("b", 10.0)),
        );

        assert_eq!(format!("{}", ev), "F[0, 2](True)");
        assert_eq!(ev.to_tree_string(0), "Eventually[0,2]\n    └── True");

        assert_eq!(format!("{}", gl), "G[1, 3](False)");
        assert_eq!(gl.to_tree_string(0), "Globally[1,3]\n    └── False");

        assert_eq!(format!("{}", until), "(a > 0) U[0, 5] (b < 10)");
        assert_eq!(
            until.to_tree_string(0),
            "Until[0,5]\n    ├── a > 0\n    └── b < 10"
        );
    }

    #[test]
    fn test_variable_predicates() {
        let gt_var = FormulaDefinition::GreaterThanVar("x", "A");
        let lt_var = FormulaDefinition::LessThanVar("y", "B");

        assert_eq!(format!("{}", gt_var), "x > $A");
        assert_eq!(gt_var.to_tree_string(0), "x > $A");

        assert_eq!(format!("{}", lt_var), "y < $B");
        assert_eq!(lt_var.to_tree_string(0), "y < $B");
    }

    #[test]
    fn test_get_variables_nested() {
        // Test that get_variable_identifiers traverses all nesting levels:
        // Not, And, Or, Implies, Eventually, Globally, Until
        let formula = FormulaDefinition::And(
            Box::new(FormulaDefinition::Not(Box::new(
                FormulaDefinition::GreaterThanVar("x", "A"),
            ))),
            Box::new(FormulaDefinition::Or(
                Box::new(FormulaDefinition::Implies(
                    Box::new(FormulaDefinition::LessThanVar("y", "B")),
                    Box::new(FormulaDefinition::GreaterThanVar("z", "C")),
                )),
                Box::new(FormulaDefinition::Eventually(
                    TimeInterval {
                        start: Duration::from_secs(0),
                        end: Duration::from_secs(1),
                    },
                    Box::new(FormulaDefinition::Globally(
                        TimeInterval {
                            start: Duration::from_secs(0),
                            end: Duration::from_secs(2),
                        },
                        Box::new(FormulaDefinition::Until(
                            TimeInterval {
                                start: Duration::from_secs(0),
                                end: Duration::from_secs(3),
                            },
                            Box::new(FormulaDefinition::LessThanVar("a", "D")),
                            Box::new(FormulaDefinition::GreaterThanVar("b", "E")),
                        )),
                    )),
                )),
            )),
        );

        let vars = formula.get_variable_identifiers();
        assert_eq!(vars.len(), 5);
        assert!(vars.contains(&"A"));
        assert!(vars.contains(&"B"));
        assert!(vars.contains(&"C"));
        assert!(vars.contains(&"D"));
        assert!(vars.contains(&"E"));
    }

    #[test]
    fn test_deeply_nested_tree_string() {
        // Test deeply nested formula to exercise tree prefix formatting logic
        let formula = FormulaDefinition::And(
            Box::new(FormulaDefinition::Or(
                Box::new(FormulaDefinition::GreaterThan("x", 1.0)),
                Box::new(FormulaDefinition::LessThan("y", 2.0)),
            )),
            Box::new(FormulaDefinition::Not(Box::new(
                FormulaDefinition::GreaterThan("z", 3.0),
            ))),
        );

        let tree = formula.to_tree_string(0);
        assert!(tree.contains("And"));
        assert!(tree.contains("Or"));
        assert!(tree.contains("Not"));
        assert!(tree.contains("x > 1"));
        assert!(tree.contains("y < 2"));
        assert!(tree.contains("z > 3"));
    }

    #[test]
    fn test_get_variables_no_variables() {
        // Formula with no variable predicates
        let formula = FormulaDefinition::And(
            Box::new(FormulaDefinition::GreaterThan("x", 5.0)),
            Box::new(FormulaDefinition::LessThan("y", 10.0)),
        );
        let vars = formula.get_variable_identifiers();
        assert!(vars.is_empty());
    }
}