des-sim 0.1.0

Classical Event-Driven Simple Simulator Crate for Discrete Event System.
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
//! The `handler` module defines `MicroStepHandler`, a central component for managing
//! the execution flow within a simulation tick.
//!
//! It orchestrates the transitions between different micro-step phases (source and event processing)
//! and ensures type-safe progression of the simulation state.

use crate::context::{ActiveExecutorContext, EventContext, SourceContext};
use crate::execution::phase::{EventPhase, MicroStepResult, SourcePhase, UncheckedActiveExecutor};
use crate::modeling::hook::Hook;
use crate::modeling::model::Model;
use crate::primitive::time::MicroStepStatus;

/// Controls the micro-step execution phases of the simulation.
///
/// This structure owns the simulation state (context) and guarantees phase transitions
/// in a one-way (disposable) manner. By not implementing `Clone`, it ensures the safety
/// of the execution flow at the type-system level.
pub struct MicroStepHandler<CTX> {
    context: CTX,
}

impl<CTX> MicroStepHandler<CTX> {
    /// Creates a new handler.
    pub(crate) fn new(context: CTX) -> MicroStepHandler<CTX> {
        MicroStepHandler { context }
    }

    /// Returns an immutable reference to the context.
    pub fn ref_context(&self) -> &CTX {
        &self.context
    }

    /// Returns a mutable reference to the context.
    pub fn ref_mut_context(&mut self) -> &mut CTX {
        &mut self.context
    }
}

impl<E, M: Model<E>> MicroStepHandler<ActiveExecutorContext<E, M>> {
    /// Starts the source execution phase.
    ///
    /// Generates a `SourcePhase` from the current context and invokes the `before_source_phase` hook.
    pub fn start_source_phase(self, model: &M) -> SourcePhase<E, M> {
        let mut context = self.context;
        context.hook().before_source_phase(
            model,
            context.current_tick_status.current(),
            context.next_micro_step_status.current(),
        );

        let ready_sources = context
            .source_handler
            .drain_ready(context.current_tick_status.current());

        SourcePhase::new(
            SourceContext {
                current_tick_status: context.current_tick_status,
                current_micro_step_status: context.next_micro_step_status,
                hook_delegate: context.hook_delegate,
                source_handler: None,
                event_scheduler: context.event_scheduler,
            },
            context.source_handler,
            ready_sources,
        )
    }
}

impl<E, M: Model<E>> MicroStepHandler<SourceContext<E, M>> {
    /// Transitions to the event execution phase.
    ///
    /// Invokes the `before_event_phase` hook, restores the pending `source_handler`,
    /// and generates an `EventPhase`.
    pub fn to_event_phase(self, model: &M) -> EventPhase<E, M> {
        let mut context = self.context;
        context.hook().before_event_phase(
            model,
            context.current_tick_status.current(),
            context.current_micro_step_status.current(),
        );

        let ready_events = context
            .event_scheduler
            .drain_ready(context.current_tick_status.current());

        EventPhase::new(
            EventContext {
                current_tick_status: context.current_tick_status,
                current_micro_step_status: context.current_micro_step_status,
                hook_delegate: context.hook_delegate,
                source_handler: context
                    .source_handler
                    .expect("Failed to retrieve SourceHandler from SourcePhase."),
                event_scheduler: context.event_scheduler,
            },
            ready_events,
        )
    }
}

impl<E, M: Model<E>> MicroStepHandler<EventContext<E, M>> {
    /// Ends the current micro-step and returns the result (continue or complete).
    ///
    /// Checks for remaining executable events or sources within the current tick and
    /// constructs the context for the next micro-step if necessary.
    pub fn end_micro_step(mut self, model: &M) -> MicroStepResult<E, M> {
        // Flush pending handlers to accurately peek at the next scheduled events/sources.
        self.ref_mut_context().source_handler.flush_pending();
        self.ref_mut_context().event_scheduler.flush_pending();
        let current_tick = self.ref_context().current_tick_status.current();

        // Check the next scheduled time for both events and sources.
        let next_event_at = self.ref_context().event_scheduler.peek_next_time();
        let next_source_at = self.ref_context().source_handler.peek_next_time();

        let has_next_in_current_tick = matches!(next_event_at, Some(t) if t == current_tick)
            || matches!(next_source_at, Some(t) if t == current_tick);

        if has_next_in_current_tick {
            // Still have work to do in the current tick; proceed to the next micro-step.
            let current_micro_step = self.ref_context().current_micro_step_status.current();
            let next_micro_step = current_micro_step.next();

            self.context.hook().after_micro_step(
                model,
                current_tick,
                self.ref_context().current_micro_step_status.current(),
            );

            let active_context = ActiveExecutorContext {
                current_tick_status: self.ref_context().current_tick_status,
                next_micro_step_status: MicroStepStatus::new(next_micro_step),
                hook_delegate: self.context.hook_delegate,
                source_handler: self.context.source_handler,
                event_scheduler: self.context.event_scheduler,
            };

            MicroStepResult::Continue(UncheckedActiveExecutor::new(
                active_context,
                current_micro_step,
            ))
        } else {
            // No more work in the current tick; terminate the micro-step.
            let last_micro_step_status = self.ref_context().current_micro_step_status;

            self.context.hook().after_micro_step(
                model,
                current_tick,
                self.ref_context().current_micro_step_status.current(),
            );

            let active_context = ActiveExecutorContext {
                current_tick_status: self.ref_context().current_tick_status,
                next_micro_step_status: last_micro_step_status,
                hook_delegate: self.context.hook_delegate,
                source_handler: self.context.source_handler,
                event_scheduler: self.context.event_scheduler,
            };

            MicroStepResult::Complete(active_context, last_micro_step_status)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::{ActiveExecutorContext, EventContext, SourceContext};
    use crate::event_scheduler::EventScheduler;
    use crate::modeling::event::{Event, EventPriority};
    use crate::modeling::hook::instance::{HookDelegate, SharedHook};
    use crate::modeling::model::Model;
    use crate::primitive::time::{Duration, MicroStep, MicroStepStatus, SimTime, TickStatus};
    use crate::source_handler::{SourceHandler, SourceReadyEntry, SourceView};
    use std::rc::Rc;
    use std::sync::Mutex;

    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    enum TestEvent {
        A,
    }

    struct TestModel;

    impl Model<TestEvent> for TestModel {
        fn handle_event(
            &mut self,
            _context: &mut EventContext<TestEvent, Self>,
            _event: &Event<TestEvent>,
        ) {
            // No-op
        }
    }

    /// Hook used to track phase transition calls during tests.
    struct TransitionTrackerHook {
        called_before_source_phase: Rc<Mutex<bool>>,
        called_before_event_phase: Rc<Mutex<bool>>,
        called_after_micro_step: Rc<Mutex<bool>>,
    }

    impl Hook<TestEvent, TestModel> for TransitionTrackerHook {
        fn before_simulation(&self, _model: &TestModel) {
            // none
        }
        fn after_simulation(&self, _model: &TestModel, _end_tick: SimTime) {
            // none
        }
        fn before_tick(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _skipped_duration: Duration,
        ) {
            // none
        }
        fn after_tick(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _last_micro_step: MicroStep,
        ) {
            // none
        }
        fn before_micro_step(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
        ) {
            // none
        }
        fn after_micro_step(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
        ) {
            *self.called_after_micro_step.lock().unwrap() = true;
        }
        fn on_discard_remain_micro_step(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _first_discarded_micro_step: MicroStep,
            _discarded_sources: &[SourceReadyEntry],
            _discarded_events: &[Event<TestEvent>],
        ) {
            // none
        }
        fn before_register_source(&self, _model: &TestModel, _name: &str) {
            // none
        }
        fn after_register_source(&self, _model: &TestModel, _name: &str) {
            // none
        }
        fn before_source_phase(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
        ) {
            *self.called_before_source_phase.lock().unwrap() = true;
        }
        fn before_source(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _source_view: &SourceView,
        ) {
            // none
        }
        fn after_source(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _source_view: &SourceView,
            _computed_next_fire: Option<SimTime>,
        ) {
            // none
        }
        fn cancel_source(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _scheduled_at: SimTime,
            _source_view: &SourceView,
        ) {
            // none
        }
        fn discard_source(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _source_view: &SourceView,
        ) {
            // none
        }
        fn after_source_phase(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
        ) {
            // none
        }
        fn before_event_phase(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
        ) {
            *self.called_before_event_phase.lock().unwrap() = true;
        }
        fn before_event(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _event: &Event<TestEvent>,
        ) {
            // none
        }
        fn after_event(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _event: &Event<TestEvent>,
        ) {
            // none
        }
        fn cancel_event(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _scheduled_at: SimTime,
            _event: &Event<TestEvent>,
        ) {
            // none
        }
        fn discard_event(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
            _event: &Event<TestEvent>,
        ) {
            // none
        }
        fn after_event_phase(
            &self,
            _model: &TestModel,
            _current_tick: SimTime,
            _current_micro_step: MicroStep,
        ) {
            // none
        }
    }

    fn setup_tracker_hook() -> SharedHook<TestEvent, TestModel, TransitionTrackerHook> {
        SharedHook::new(TransitionTrackerHook {
            called_before_source_phase: Rc::new(Mutex::new(false)),
            called_before_event_phase: Rc::new(Mutex::new(false)),
            called_after_micro_step: Rc::new(Mutex::new(false)),
        })
    }

    #[test]
    fn test_ref_context_and_mut() {
        let mut handler = MicroStepHandler::new(42);
        assert_eq!(*handler.ref_context(), 42);

        *handler.ref_mut_context() = 100;
        assert_eq!(*handler.ref_context(), 100);
    }

    #[test]
    fn test_start_source_phase() {
        let model = TestModel;
        let hook = setup_tracker_hook();
        let mut hook_delegate = HookDelegate::new();
        hook_delegate.add_shared_hook(hook.clone());

        let active_context = ActiveExecutorContext {
            current_tick_status: TickStatus::initialize(),
            next_micro_step_status: MicroStepStatus::initialize(),
            hook_delegate,
            source_handler: SourceHandler::new(),
            event_scheduler: EventScheduler::new(),
        };

        let handler = MicroStepHandler::new(active_context);
        let mut source_phase = handler.start_source_phase(&model);

        assert!(source_phase.source_handler.is_some());
        assert!(source_phase.get_context().source_handler.is_none());
        assert!(*hook.get_ref().called_before_source_phase.lock().unwrap());
    }

    #[test]
    fn test_to_event_phase() {
        let model = TestModel;
        let hook = setup_tracker_hook();
        let mut hook_delegate = HookDelegate::new();
        hook_delegate.add_shared_hook(hook.clone());

        let source_context = SourceContext {
            current_tick_status: TickStatus::initialize(),
            current_micro_step_status: MicroStepStatus::initialize(),
            hook_delegate,
            source_handler: Some(SourceHandler::new()),
            event_scheduler: EventScheduler::new(),
        };

        let handler = MicroStepHandler::new(source_context);
        let _event_phase = handler.to_event_phase(&model);

        assert!(*hook.get_ref().called_before_event_phase.lock().unwrap());
    }

    #[test]
    fn test_end_micro_step_continue() {
        let model = TestModel;
        let hook = setup_tracker_hook();
        let mut hook_delegate = HookDelegate::new();
        hook_delegate.add_shared_hook(hook.clone());

        let tick_status = TickStatus::initialize();
        let current_time = tick_status.current();

        let mut event_scheduler = EventScheduler::new();
        event_scheduler.schedule(
            current_time,
            Duration::zero(),
            EventPriority::minimum(),
            TestEvent::A,
        );
        event_scheduler.flush_pending();

        assert_eq!(event_scheduler.peek_next_time(), Some(current_time));

        let event_context = EventContext {
            current_tick_status: tick_status,
            current_micro_step_status: MicroStepStatus::initialize(),
            hook_delegate,
            source_handler: SourceHandler::new(),
            event_scheduler,
        };

        let handler = MicroStepHandler::new(event_context);
        let result = handler.end_micro_step(&model);

        match result {
            MicroStepResult::Continue(_) => {
                // pass
            }
            MicroStepResult::Complete(_, _) => {
                panic!("Expected MicroStepResult::Continue, but got Complete");
            }
        }
        assert!(*hook.get_ref().called_after_micro_step.lock().unwrap());
    }

    #[test]
    fn test_end_micro_step_complete() {
        let model = TestModel;
        let hook = setup_tracker_hook();
        let mut hook_delegate = HookDelegate::new();
        hook_delegate.add_shared_hook(hook.clone());

        let event_context = EventContext {
            current_tick_status: TickStatus::initialize(),
            current_micro_step_status: MicroStepStatus::initialize(),
            hook_delegate,
            source_handler: SourceHandler::new(),
            event_scheduler: EventScheduler::new(),
        };

        let handler = MicroStepHandler::new(event_context);
        let result = handler.end_micro_step(&model);

        match result {
            MicroStepResult::Complete(_, _) => {
                // pass
            }
            MicroStepResult::Continue(_) => {
                panic!("Expected MicroStepResult::Complete, but got Continue");
            }
        }
        assert!(*hook.get_ref().called_after_micro_step.lock().unwrap());
    }
}