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
use super::error::TimedOperatorError;
use crate::trace::Trace;
use crate::{Formula, Metric};

pub struct Next<P> {
    phi: P,
}

impl<P> Next<P> {
    pub fn new(phi: P) -> Next<P> {
        Next { phi }
    }
}

impl<T, P> Formula<Trace<T>> for Next<P>
where
    P: Formula<Trace<T>>,
{
    type Error = TimedOperatorError<P::Error>;

    fn satisfied_by(&self, trace: &Trace<T>) -> Result<bool, Self::Error> {
        if trace.len() < 1 {
            return Err(TimedOperatorError::EmptyTrace);
        }

        let second_time = trace.times().nth(1);
        let future_trace = second_time.and_then(|time| trace.clone().split_at(time));

        match future_trace {
            Some(future) => self
                .phi
                .satisfied_by(&future)
                .map_err(TimedOperatorError::SubformulaError),
            None => Ok(false),
        }
    }
}

impl<T, P> Metric<Trace<T>> for Next<P>
where
    P: Metric<Trace<T>>,
{
    type Error = TimedOperatorError<P::Error>;

    fn distance(&self, trace: &Trace<T>) -> Result<f64, Self::Error> {
        if trace.len() < 1 {
            return Err(TimedOperatorError::EmptyTrace);
        }

        let second_time = trace.times().nth(1);
        let future_trace = second_time.and_then(|time| trace.clone().split_at(time));

        match future_trace {
            Some(future) => self.phi.distance(&future).map_err(TimedOperatorError::SubformulaError),
            None => Ok(f64::NEG_INFINITY),
        }
    }
}