Struct sequent::Simulation
source · pub struct Simulation<S> { /* private fields */ }
Expand description
The overarching simulation state. Contains the scenario being modelled, the current state of the simulation, as well as a cursor pointing to the next event in the timeline that is scheduled to be applied.
Implementations§
source§impl<S> Simulation<S>
impl<S> Simulation<S>
Methods for affecting control over the simulation. Most of these emit a broad SimulationError
if something goes wrong, with a specific error variant for each envisaged error type. Not all
methods use every available SimulationError
variant.
Check the documentation of each method for the specific variants that are returned by that method, bearing in mind that the variants that a method is allowed to return may change over time.
sourcepub fn step(&mut self) -> Result<(), SimulationError<S>>
pub fn step(&mut self) -> Result<(), SimulationError<S>>
Evaluates the next event in the timeline, applying it to the current state to transition to the next state.
Errors
SimulationError
if an error occurs. Expected variants:
SimulationError::TimelineExhausted
, if the cursor is already parked at the end of the timeline.SimulationError::Transition
, if the event could not be evaluated.
Examples found in repository?
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
pub fn jump(&mut self, location: usize) -> Result<(), SimulationError<S>>
where
S: Clone,
{
if location > self.scenario.timeline.len() {
return Err(SimulationError::TimelineExhausted);
}
if location < self.cursor {
self.reset();
}
while self.cursor < location {
self.step()?;
}
Ok(())
}
/// Evaluates the remaining events in the timeline.
///
/// # Errors
/// [`SimulationError`] if an error occurs. Expected variants:
///
/// * [`SimulationError::TimelineExhausted`], if the cursor is already parked at the end of the timeline.
/// * [`SimulationError::Transition`], if the event could not be evaluated.
pub fn run(&mut self) -> Result<(), SimulationError<S>> {
while self.cursor < self.scenario.timeline.len() {
self.step()?;
}
Ok(())
}
sourcepub fn reset(&mut self)where
S: Clone,
pub fn reset(&mut self)where
S: Clone,
Resets the simulation, reinitialising the current state from the initial state specified in the simulation scenario, and resetting the cursor to location 0.
Examples found in repository?
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
pub fn jump(&mut self, location: usize) -> Result<(), SimulationError<S>>
where
S: Clone,
{
if location > self.scenario.timeline.len() {
return Err(SimulationError::TimelineExhausted);
}
if location < self.cursor {
self.reset();
}
while self.cursor < location {
self.step()?;
}
Ok(())
}
/// Evaluates the remaining events in the timeline.
///
/// # Errors
/// [`SimulationError`] if an error occurs. Expected variants:
///
/// * [`SimulationError::TimelineExhausted`], if the cursor is already parked at the end of the timeline.
/// * [`SimulationError::Transition`], if the event could not be evaluated.
pub fn run(&mut self) -> Result<(), SimulationError<S>> {
while self.cursor < self.scenario.timeline.len() {
self.step()?;
}
Ok(())
}
/// Appends an event to the timeline at the current cursor location, assuming that there
/// are no events at and beyond that location.
///
/// # Errors
/// [`SimulationError`] if an error occurs. Expected variants:
///
/// * [`SimulationError::TruncationRequired`], if there is already an event
/// at the cursor location. The error returns the event object that is otherwise consumed by
/// this method.
pub fn push_event(&mut self, event: Box<dyn Event<State = S>>) -> Result<(), SimulationError<S>> {
if self.cursor != self.scenario.timeline.len() {
return Err(SimulationError::TruncationRequired(event));
}
self.scenario.timeline.push(event);
Ok(())
}
/// Truncates the timeline at the current cursor location, dropping all events at and beyond
/// this point.
pub fn truncate(&mut self) {
self.scenario.timeline.truncate(self.cursor);
}
/// A reference to the underlying scenario.
pub fn scenario(&self) -> &Scenario<S> {
&self.scenario
}
/// Assigns a new scenario, resetting the simulation in the process.
pub fn set_scenario(&mut self, scenario: Scenario<S>)
where
S: Clone,
{
self.scenario = scenario;
self.reset();
}
sourcepub fn jump(&mut self, location: usize) -> Result<(), SimulationError<S>>where
S: Clone,
pub fn jump(&mut self, location: usize) -> Result<(), SimulationError<S>>where
S: Clone,
Jumps to a specified location in the timeline and evaluates the event at that location.
Errors
SimulationError
if an error occurs. Expected variants:
SimulationError::TimelineExhausted
, if the cursor is already parked at the end of the timeline.SimulationError::Transition
, if the event could not be evaluated.
sourcepub fn run(&mut self) -> Result<(), SimulationError<S>>
pub fn run(&mut self) -> Result<(), SimulationError<S>>
Evaluates the remaining events in the timeline.
Errors
SimulationError
if an error occurs. Expected variants:
SimulationError::TimelineExhausted
, if the cursor is already parked at the end of the timeline.SimulationError::Transition
, if the event could not be evaluated.
sourcepub fn push_event(
&mut self,
event: Box<dyn Event<State = S>>
) -> Result<(), SimulationError<S>>
pub fn push_event(
&mut self,
event: Box<dyn Event<State = S>>
) -> Result<(), SimulationError<S>>
Appends an event to the timeline at the current cursor location, assuming that there are no events at and beyond that location.
Errors
SimulationError
if an error occurs. Expected variants:
SimulationError::TruncationRequired
, if there is already an event at the cursor location. The error returns the event object that is otherwise consumed by this method.
sourcepub fn truncate(&mut self)
pub fn truncate(&mut self)
Truncates the timeline at the current cursor location, dropping all events at and beyond this point.
sourcepub fn set_scenario(&mut self, scenario: Scenario<S>)where
S: Clone,
pub fn set_scenario(&mut self, scenario: Scenario<S>)where
S: Clone,
Assigns a new scenario, resetting the simulation in the process.
sourcepub fn current_state(&self) -> &S
pub fn current_state(&self) -> &S
A reference to the current simulation state.