xdevs/traits.rs
1/// Trait that defines the methods that a DEVS event bag set must implement.
2///
3/// # Safety
4///
5/// This trait must be implemented via macros. Do not implement it manually.
6pub unsafe trait Bag {
7 /// Returns `true` if the ports are empty.
8 fn is_empty(&self) -> bool;
9
10 /// Clears the ports, removing all values.
11 fn clear(&mut self);
12}
13
14/// Interface for DEVS components. All DEVS components must implement this trait.
15///
16/// # Safety
17///
18/// This trait must be implemented via macros. Do not implement it manually.
19pub unsafe trait Component {
20 /// Input event bag of the model.
21 type Input: Bag;
22
23 /// Output event bag of the model.
24 type Output: Bag;
25
26 /// Returns the last time the component was updated.
27 fn get_t_last(&self) -> f64;
28
29 /// Sets the last time the component was updated.
30 fn set_t_last(&mut self, t_last: f64);
31
32 /// Returns the next time the component will be updated.
33 fn get_t_next(&self) -> f64;
34
35 /// Sets the next time the component will be updated.
36 fn set_t_next(&mut self, t_next: f64);
37
38 /// Returns a reference to the model's input event bag.
39 fn get_input(&self) -> &Self::Input;
40
41 /// Returns a mutable reference to the model's input event bag.
42 fn get_input_mut(&mut self) -> &mut Self::Input;
43
44 /// Returns a reference to the model's output event bag.
45 fn get_output(&self) -> &Self::Output;
46
47 /// Returns a mutable reference to the model's output event bag.
48 fn get_output_mut(&mut self) -> &mut Self::Output;
49
50 /// Clears the input bag, removing all values.
51 #[inline]
52 fn clear_input(&mut self) {
53 self.get_input_mut().clear()
54 }
55
56 /// Clears the output bag, removing all values.
57 #[inline]
58 fn clear_output(&mut self) {
59 self.get_output_mut().clear()
60 }
61}
62
63/// Partial interface for DEVS atomic models.
64/// It is used as a helper trait to implement the [`crate::Atomic`] trait.
65///
66/// # Safety
67///
68/// This trait must be implemented via macros. Do not implement it manually.
69pub unsafe trait PartialAtomic: Component {
70 /// The data type used to represent the state of the model.
71 type State;
72}
73
74/// Interface for simulating DEVS models. All DEVS models must implement this trait.
75///
76/// # Safety
77///
78/// This trait must be implemented via macros. Do not implement it manually.
79pub unsafe trait AbstractSimulator: Component {
80 /// It starts the simulation, setting the initial time to t_start.
81 /// It returns the time for the next state transition of the inner DEVS model.
82 fn start(&mut self, t_start: f64) -> f64;
83
84 /// It stops the simulation, setting the last time to t_stop.
85 fn stop(&mut self, t_stop: f64);
86
87 /// Executes output functions and propagates messages according to EOCs.
88 /// Internally, it checks that the model is imminent before executing.
89 fn lambda(&mut self, t: f64);
90
91 /// Propagates messages according to ICs and EICs and executes model transition functions.
92 /// It also clears all the input and output ports.
93 /// Internally, it checks that the model is imminent before executing.
94 /// Finally, it returns the time for the next state transition of the inner DEVS model.
95 fn delta(&mut self, t: f64) -> f64;
96}