Struct Simulation

Source
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

Source

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.

Source

pub 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.

Runs the entire simulation from start to end. This function might not terminate if the end condition is not satisfied.

Source

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}
Source

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}
Source

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}
Source

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

Source§

fn default() -> Simulation

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.