pub struct Simulation {
pub state: State,
pub scheduler: Scheduler,
pub components: Components,
}
Expand description
Simulation struct that puts different parts of the simulation together.
See the crate-level documentation for more information.
Fields§
§state: State
Simulation state.
scheduler: Scheduler
Event scheduler.
components: Components
Component container.
Implementations§
Source§impl Simulation
impl Simulation
Sourcepub fn step(&mut self) -> bool
pub fn step(&mut self) -> bool
Performs one step of the simulation. Returns true
if there was in fact an event
available to process, and false
otherwise, which signifies that the simulation
ended.
Sourcepub fn run<F: Fn(&Simulation)>(&mut self, step_function: F)
👎Deprecated since 0.2.0: Handling this in Simulation is susceptible to API breaks and/or making it messy. Use execute instead, which delegates the logic to an external executor.
pub fn run<F: Fn(&Simulation)>(&mut self, step_function: F)
Runs the entire simulation from start to end. This function might not terminate if the end condition is not satisfied.
Sourcepub fn execute<E: Execute>(&mut self, executor: E)
pub fn execute<E: Execute>(&mut self, executor: E)
Runs the entire simulation.
The stopping condition and other execution details depend on the executor used.
See Execute
and Executor
for more details.
Examples found in repository?
examples/simulation.rs (lines 190-194)
168fn main() {
169 let messages = Rc::new(RefCell::new(Vec::<String>::new()));
170 let mut simulation = Simulation::default();
171 let queue = simulation.add_queue(Fifo::default());
172 let working_on = simulation.state.insert::<Option<Product>>(None);
173 let consumer = simulation.add_component(Consumer {
174 incoming: queue,
175 working_on,
176 messages: messages.clone(),
177 });
178 let produced_count = simulation.state.insert(0_usize);
179 let producer = simulation.add_component(Producer {
180 outgoing: queue,
181 consumer,
182 produced_count,
183 messages: messages.clone(),
184 });
185 simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
186 // simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
187 // The above would fail with: ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
188 {
189 let messages = messages.clone();
190 simulation.execute(Executor::unbound().side_effect(move |sim| {
191 messages
192 .borrow_mut()
193 .push(format!("{:?}", sim.scheduler.time()));
194 }));
195 }
196 assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
197}
Sourcepub fn add_component<E: Debug + 'static, C: Component<Event = E> + 'static>(
&mut self,
component: C,
) -> ComponentId<E>
pub fn add_component<E: Debug + 'static, C: Component<Event = E> + 'static>( &mut self, component: C, ) -> ComponentId<E>
Adds a new component.
Examples found in repository?
examples/simulation.rs (lines 173-177)
168fn main() {
169 let messages = Rc::new(RefCell::new(Vec::<String>::new()));
170 let mut simulation = Simulation::default();
171 let queue = simulation.add_queue(Fifo::default());
172 let working_on = simulation.state.insert::<Option<Product>>(None);
173 let consumer = simulation.add_component(Consumer {
174 incoming: queue,
175 working_on,
176 messages: messages.clone(),
177 });
178 let produced_count = simulation.state.insert(0_usize);
179 let producer = simulation.add_component(Producer {
180 outgoing: queue,
181 consumer,
182 produced_count,
183 messages: messages.clone(),
184 });
185 simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
186 // simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
187 // The above would fail with: ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
188 {
189 let messages = messages.clone();
190 simulation.execute(Executor::unbound().side_effect(move |sim| {
191 messages
192 .borrow_mut()
193 .push(format!("{:?}", sim.scheduler.time()));
194 }));
195 }
196 assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
197}
Sourcepub fn add_queue<Q: Queue + 'static>(&mut self, queue: Q) -> QueueId<Q>
pub fn add_queue<Q: Queue + 'static>(&mut self, queue: Q) -> QueueId<Q>
Adds a new unbounded queue.
Examples found in repository?
examples/simulation.rs (line 171)
168fn main() {
169 let messages = Rc::new(RefCell::new(Vec::<String>::new()));
170 let mut simulation = Simulation::default();
171 let queue = simulation.add_queue(Fifo::default());
172 let working_on = simulation.state.insert::<Option<Product>>(None);
173 let consumer = simulation.add_component(Consumer {
174 incoming: queue,
175 working_on,
176 messages: messages.clone(),
177 });
178 let produced_count = simulation.state.insert(0_usize);
179 let producer = simulation.add_component(Producer {
180 outgoing: queue,
181 consumer,
182 produced_count,
183 messages: messages.clone(),
184 });
185 simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
186 // simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
187 // The above would fail with: ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
188 {
189 let messages = messages.clone();
190 simulation.execute(Executor::unbound().side_effect(move |sim| {
191 messages
192 .borrow_mut()
193 .push(format!("{:?}", sim.scheduler.time()));
194 }));
195 }
196 assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
197}
Sourcepub fn schedule<E: Debug + 'static>(
&mut self,
time: Duration,
component: ComponentId<E>,
event: E,
)
pub fn schedule<E: Debug + 'static>( &mut self, time: Duration, component: ComponentId<E>, event: E, )
Schedules a new event to be executed at time time
in component component
.
Examples found in repository?
examples/simulation.rs (line 185)
168fn main() {
169 let messages = Rc::new(RefCell::new(Vec::<String>::new()));
170 let mut simulation = Simulation::default();
171 let queue = simulation.add_queue(Fifo::default());
172 let working_on = simulation.state.insert::<Option<Product>>(None);
173 let consumer = simulation.add_component(Consumer {
174 incoming: queue,
175 working_on,
176 messages: messages.clone(),
177 });
178 let produced_count = simulation.state.insert(0_usize);
179 let producer = simulation.add_component(Producer {
180 outgoing: queue,
181 consumer,
182 produced_count,
183 messages: messages.clone(),
184 });
185 simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
186 // simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
187 // The above would fail with: ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
188 {
189 let messages = messages.clone();
190 simulation.execute(Executor::unbound().side_effect(move |sim| {
191 messages
192 .borrow_mut()
193 .push(format!("{:?}", sim.scheduler.time()));
194 }));
195 }
196 assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
197}
Trait Implementations§
Source§impl Default for Simulation
impl Default for Simulation
Source§fn default() -> Simulation
fn default() -> Simulation
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for Simulation
impl !RefUnwindSafe for Simulation
impl !Send for Simulation
impl !Sync for Simulation
impl Unpin for Simulation
impl !UnwindSafe for Simulation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more