Skip to main content

scientific_workflow/task/
unit.rs

1//! Application-owned scientific execution contract.
2
3use serde::de::DeserializeOwned;
4
5use crate::observation::advanced::ObservationPlan;
6use crate::state::advanced::{SystemState, SystemStateSchema};
7
8use super::result::TaskResult;
9
10/// A borrowed view of one independently stateful model inside an execution unit.
11///
12/// The identity, state owner, and schema allocation must remain stable at the
13/// same model index for the complete execution. `complete` and
14/// `target_iteration` are declarations inspected by Workflow; constructing a
15/// view has no side effects.
16#[derive(Clone, Copy, Debug)]
17pub struct ModelView<'a> {
18    identity: &'a str,
19    state: &'a SystemState,
20    complete: bool,
21    target_iteration: Option<u64>,
22}
23
24impl<'a> ModelView<'a> {
25    /// Describes one model owned by an [`ExecutionUnit`].
26    ///
27    /// `identity` must be nonempty, contain no surrounding whitespace, and be
28    /// unique within the unit. `state` must remain at the same address and use
29    /// the schema supplied to [`ExecutionUnit::initialize`]. A target, when
30    /// present, must not precede the state's current iteration.
31    pub const fn new(
32        identity: &'a str,
33        state: &'a SystemState,
34        complete: bool,
35        target_iteration: Option<u64>,
36    ) -> Self {
37        Self {
38            identity,
39            state,
40            complete,
41            target_iteration,
42        }
43    }
44
45    /// Returns the stable identity of this model within its execution unit.
46    pub const fn identity(self) -> &'a str {
47        self.identity
48    }
49
50    /// Borrows this model's directly owned canonical state.
51    pub const fn state(self) -> &'a SystemState {
52        self.state
53    }
54
55    /// Returns whether this model requires no further transition.
56    pub const fn is_complete(self) -> bool {
57        self.complete
58    }
59
60    /// Returns this model's optional expected final iteration.
61    pub const fn target_iteration(self) -> Option<u64> {
62        self.target_iteration
63    }
64}
65
66/// One schedulable scientific execution containing one or more models.
67///
68/// Workflow manages every implementation through the same lifecycle and does
69/// not distinguish a standalone model from a coordinated ensemble. A normal
70/// model returns one [`ModelView`]; an ensemble returns one view per member and
71/// keeps all internal parallelism, shared inputs, and synchronization private.
72/// Each exposed model owns a distinct [`SystemState`].
73///
74/// Model count, index order, identities, state owners, and schema allocations
75/// must remain stable after initialization. One successful [`Self::step`] must
76/// strictly advance at least one incomplete model and must never advance a
77/// model that was already complete. Other incomplete models may wait during a
78/// coordinated step, which permits synchronized ensembles and restored members
79/// at different iterations.
80pub trait ExecutionUnit: Send + Sized + 'static {
81    /// One complete set of constants supplied by Config.
82    type Constants: DeserializeOwned + 'static;
83
84    /// Defines the observations recorded independently for every model.
85    ///
86    /// All members of one execution unit use the state schema selected by the
87    /// task and this common observation plan. The default records every field
88    /// at every iteration. This preflight operation must have no external side
89    /// effects.
90    fn observation_plan(_constants: &Self::Constants) -> TaskResult<ObservationPlan> {
91        Ok(ObservationPlan::all_fields())
92    }
93
94    /// Builds a fully initialized standalone model or ensemble.
95    ///
96    /// Every state subsequently exposed through [`Self::model`] must have been
97    /// created from this exact schema allocation.
98    fn initialize(constants: Self::Constants, schema: &SystemStateSchema) -> TaskResult<Self>;
99
100    /// Returns the stable positive number of independently stateful models.
101    fn model_count(&self) -> usize;
102
103    /// Borrows one model by stable zero-based index.
104    ///
105    /// The method must be side-effect free. Workflow calls it repeatedly before
106    /// and after coordinated steps. An index below [`Self::model_count`] must
107    /// always return `Some`; all other indices must return `None`.
108    fn model(&self, index: usize) -> Option<ModelView<'_>>;
109
110    /// Performs one coordinated scientific transition.
111    ///
112    /// A standalone model advances itself. An ensemble may advance members in
113    /// parallel or share generated inputs, but it must return only after the
114    /// complete logical step is visible through [`Self::model`].
115    fn step(&mut self) -> TaskResult;
116}