des_sim/execution/strategy/continue_strategy.rs
1//! The `continue_strategy` module defines the `ContinueStrategy` trait and its associated types.
2//!
3//! This trait allows for custom logic to determine whether the simulation should continue
4//! after each micro-step, providing a flexible way to implement termination conditions
5//! or error handling.
6
7mod always_continue;
8mod limit_abort;
9mod limit_discard;
10
11pub use always_continue::*;
12pub use limit_abort::*;
13pub use limit_discard::*;
14
15use crate::context::ActiveExecutorContext;
16use crate::execution::phase::UncheckedActiveExecutor;
17use crate::modeling::model::Model;
18
19/// The result type for simulation continuation strategies.
20///
21/// On success (`Ok`), it returns an `ActiveExecutorContext` to proceed to the
22/// next simulation step. On failure (`Err`), it returns the current context
23/// along with a strategy-specific error, allowing the simulation to terminate
24/// gracefully or be debugged.
25pub type ContinuousStrategyResult<E, M, Err> =
26 Result<ActiveExecutorContext<E, M>, (ActiveExecutorContext<E, M>, Err)>;
27
28/// A trait for defining strategies that decide whether to continue simulation
29/// execution after each micro-step.
30///
31/// By implementing this trait, you can flexibly customize simulation termination
32/// conditions, such as:
33/// - Limiting the total number of micro-steps.
34/// - Aborting execution when specific criteria are met.
35/// - Enforcing continuous execution regardless of state.
36pub trait ContinueStrategy<E, M: Model<E>, RunnerError> {
37 /// The error type associated with this strategy.
38 type Err;
39
40 /// Invoked at the end of a micro-step to determine whether to continue.
41 ///
42 /// # Arguments
43 /// * `model` - A reference to the current model state.
44 /// * `unchecked_executor` - The execution engine awaiting validation to
45 /// proceed to the next step.
46 ///
47 /// # Returns
48 /// * `Ok` - The simulation is authorized to proceed to the next step.
49 /// * `Err` - The simulation must be terminated based on the strategy's logic.
50 #[allow(clippy::result_large_err)]
51 fn handle_micro_step_continue(
52 &mut self,
53 model: &M,
54 unchecked_executor: UncheckedActiveExecutor<E, M>,
55 ) -> ContinuousStrategyResult<E, M, Self::Err>;
56}