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§

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.

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:

Examples found in repository?
src/sim.rs (line 83)
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(())
    }

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?
src/sim.rs (line 79)
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();
    }

Jumps to a specified location in the timeline and evaluates the event at that location.

Errors

SimulationError if an error occurs. Expected variants:

Evaluates the remaining events in the timeline.

Errors

SimulationError if an error occurs. Expected variants:

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:

Truncates the timeline at the current cursor location, dropping all events at and beyond this point.

A reference to the underlying scenario.

Assigns a new scenario, resetting the simulation in the process.

A reference to the current simulation state.

The current cursor location.

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.