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
use crate::Simulation;
use std::time::Duration;

/// Simulation execution trait.
pub trait Execute {
    /// Executes the simulation until some stopping condition is reached.
    /// The condition is implementation-specific.
    fn execute(self, sim: &mut Simulation);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EndCondition {
    Time(Duration),
    EmptyQueue,
    Steps(usize),
}

/// Executor is used for simple execution of an entire simulation.
///
/// See the crate level documentation for examples.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Executor {
    end_condition: EndCondition,
}

impl Executor {
    /// Simulation will end only once there is no available events in the queue.
    #[must_use]
    pub fn unbound() -> Self {
        Self {
            end_condition: EndCondition::EmptyQueue,
        }
    }

    /// Simulation will be run no longer than the given time.
    /// It may terminate early if no events are available.
    #[must_use]
    pub fn timed(time: Duration) -> Self {
        Self {
            end_condition: EndCondition::Time(time),
        }
    }

    /// Simulation will execute exactly this many steps, unless we run out of events.
    #[must_use]
    pub fn steps(steps: usize) -> Self {
        Self {
            end_condition: EndCondition::Steps(steps),
        }
    }

    /// Registers a side effect that is called _after_ each simulation step.
    #[must_use]
    pub fn side_effect<F>(self, func: F) -> ExecutorWithSideEffect<F>
    where
        F: Fn(&Simulation),
    {
        ExecutorWithSideEffect {
            end_condition: self.end_condition,
            side_effect: func,
        }
    }
}

impl Execute for Executor {
    fn execute(self, sim: &mut Simulation) {
        run_with(sim, self.end_condition, |_| {});
    }
}

pub struct ExecutorWithSideEffect<F>
where
    F: Fn(&Simulation),
{
    end_condition: EndCondition,
    side_effect: F,
}

impl<F> Execute for ExecutorWithSideEffect<F>
where
    F: Fn(&Simulation),
{
    fn execute(self, sim: &mut Simulation) {
        run_with(sim, self.end_condition, self.side_effect);
    }
}

fn run_with<F>(sim: &mut Simulation, end_condition: EndCondition, side_effect: F)
where
    F: Fn(&Simulation),
{
    let step_fn = |sim: &mut Simulation| {
        let result = sim.step();
        if result {
            side_effect(sim);
        }
        result
    };
    match end_condition {
        EndCondition::Time(time) => execute_until(sim, time, step_fn),
        EndCondition::EmptyQueue => execute_until_empty(sim, step_fn),
        EndCondition::Steps(steps) => execute_steps(sim, steps, step_fn),
    }
}

fn execute_until_empty<F>(sim: &mut Simulation, step: F)
where
    F: Fn(&mut Simulation) -> bool,
{
    while step(sim) {}
}

fn execute_until<F>(sim: &mut Simulation, time: Duration, step: F)
where
    F: Fn(&mut Simulation) -> bool,
{
    while sim.scheduler.peek().map_or(false, |e| e.time() <= time) {
        step(sim);
    }
}

fn execute_steps<F>(sim: &mut Simulation, steps: usize, step: F)
where
    F: Fn(&mut Simulation) -> bool,
{
    for _ in 0..steps {
        if !step(sim) {
            break;
        }
    }
}

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

    struct TestComponent {
        counter: crate::Key<usize>,
    }

    #[derive(Debug)]
    struct TestEvent;

    impl Component for TestComponent {
        type Event = TestEvent;

        fn process_event(
            &self,
            self_id: crate::ComponentId<Self::Event>,
            _event: &Self::Event,
            scheduler: &mut crate::Scheduler,
            state: &mut crate::State,
        ) {
            let counter = state.get_mut(self.counter).unwrap();
            *counter += 1;
            if *counter < 10 {
                scheduler.schedule(Duration::from_secs(2), self_id, TestEvent);
            }
        }
    }

    #[test]
    fn test_create_executor() {
        assert_eq!(
            Executor::unbound(),
            Executor {
                end_condition: EndCondition::EmptyQueue
            }
        );
        assert_eq!(
            Executor::timed(Duration::default()),
            Executor {
                end_condition: EndCondition::Time(Duration::default())
            }
        );
        assert_eq!(
            Executor::steps(7),
            Executor {
                end_condition: EndCondition::Steps(7)
            }
        );
        // Bonus: satisfy codecov on derive
        assert_eq!(&format!("{:?}", TestEvent), "TestEvent");
    }

    #[test]
    fn test_steps() {
        let mut sim = Simulation::default();
        let counter_key = sim.state.insert(0_usize);
        let component = sim.add_component(TestComponent {
            counter: counter_key,
        });
        sim.schedule(Duration::default(), component, TestEvent);
        Executor::steps(10).execute(&mut sim);
        assert_eq!(sim.state.get(counter_key), Some(&10));
    }

    #[test]
    fn test_steps_stops_before() {
        let mut sim = Simulation::default();
        let counter_key = sim.state.insert(0_usize);
        let component = sim.add_component(TestComponent {
            counter: counter_key,
        });
        sim.schedule(Duration::default(), component, TestEvent);
        // After 10 steps there are no events, so it will not execute all 100
        Executor::steps(100).execute(&mut sim);
        assert_eq!(sim.state.get(counter_key), Some(&10));
    }

    #[test]
    fn test_timed() {
        let mut sim = Simulation::default();
        let counter_key = sim.state.insert(0_usize);
        let component = sim.add_component(TestComponent {
            counter: counter_key,
        });
        sim.schedule(Duration::default(), component, TestEvent);
        Executor::timed(Duration::from_secs(6)).execute(&mut sim);
        assert_eq!(sim.state.get(counter_key), Some(&4));
        assert_eq!(sim.scheduler.clock().time(), Duration::from_secs(6));
    }

    #[test]
    fn test_timed_clock_stops_early() {
        let mut sim = Simulation::default();
        let counter_key = sim.state.insert(0_usize);
        let component = sim.add_component(TestComponent {
            counter: counter_key,
        });
        sim.schedule(Duration::default(), component, TestEvent);
        Executor::timed(Duration::from_secs(5)).execute(&mut sim);
        assert_eq!(sim.state.get(counter_key), Some(&3));
        assert_eq!(sim.scheduler.clock().time(), Duration::from_secs(4));
    }
}