scientific_workflow/lib.rs
1//! Configuration-driven scientific workflow execution.
2//!
3//! An ordinary application defines registered [`task::basic::ExecutionUnit`]
4//! implementations, writes `wf_configs/study.json`, its declared named
5//! state-schema documents, and the central `wf_configs/parameters.json`, then
6//! calls [`run`] with the project root. Workflow infers task
7//! instances, phase membership, identities, output paths, progress boundaries,
8//! recording lifecycle, and execution mechanics.
9//!
10//! # Ordinary use
11//!
12//! ```no_run
13//! use std::path::Path;
14//!
15//! fn main() -> Result<(), scientific_workflow::WorkflowError> {
16//! scientific_workflow::run(Path::new("."))
17//! }
18//! ```
19//!
20//! A model carries its stable manifest key at its implementation:
21//!
22//! ```
23//! use serde::Deserialize;
24//! use scientific_workflow::prelude::basic::*;
25//!
26//! #[derive(Deserialize)]
27//! struct Constants { initial: u64, steps: u64 }
28//!
29//! struct Model { state: SystemState, target_iteration: u64 }
30//!
31//! #[scientific_workflow::execution_unit("example")]
32//! impl ExecutionUnit for Model {
33//! type Constants = Constants;
34//!
35//! fn initialize(constants: Constants, schema: &SystemStateSchema) -> TaskResult<Self> {
36//! let mut state = schema.create_empty_state(StateTime::from_iteration(0));
37//! state.initialize_payload("population", constants.initial)?;
38//! state.initialize_payload("cumulative_births", 0_u64)?;
39//! Ok(Self {
40//! state,
41//! target_iteration: constants.steps,
42//! })
43//! }
44//!
45//! fn model_count(&self) -> usize { 1 }
46//! fn model(&self, index: usize) -> Option<ModelView<'_>> {
47//! (index == 0).then(|| ModelView::new(
48//! "example",
49//! &self.state,
50//! self.state.time().iteration() >= self.target_iteration,
51//! Some(self.target_iteration),
52//! ))
53//! }
54//! fn step(&mut self) -> TaskResult {
55//! let (population, cumulative_births) = self
56//! .state
57//! .borrow_payloads_mut::<(u64, u64)>(
58//! ("population", "cumulative_births"),
59//! )?;
60//! *population += 1;
61//! *cumulative_births += 1;
62//! self.state.advance_time(None)?;
63//! Ok(())
64//! }
65//! }
66//! ```
67//!
68//! # Ownership boundaries
69//!
70//! - [`config`] alone reads and parses project JSON and supplies typed model
71//! constants.
72//! - [`study`] composes parsed declarations, state semantics, and compiled model
73//! registrations into immutable, output-free intent.
74//! - [`runtime`] consumes a completed Study and owns active execution/output.
75//! - [`task`] owns the model contract and automatic observation boundaries.
76//! - [`state`] owns canonical scientific state and schema.
77//! - [`observation`] owns scientific observation meaning, not persistence mechanics.
78//! - [`persistence`] owns automatic durable output and verified reading.
79//! - [`ui`] owns automatic terminal presentation of Runtime facts.
80//! - [`error`] owns complete-workflow Study/Runtime error composition.
81//!
82//! The crate-level [`run`] facade performs the sole ordinary transition from
83//! project root to Study to Runtime. `Study` is the ultimate coordinator of
84//! declared intent; runtime is the ultimate coordinator of active execution.
85//! Advanced integrations use each module's `advanced` scope. [`prelude`] only
86//! aggregates those module-owned APIs and crate conveniences.
87//!
88//! Persistence write construction and output allocation are internal and are
89//! not available to model authors.
90//!
91//! This crate is pre-1.0 test software and may make coordinated API changes.
92
93#![deny(missing_docs, rustdoc::broken_intra_doc_links)]
94
95extern crate self as scientific_workflow;
96
97mod clock;
98pub mod error;
99
100pub mod config;
101pub mod observation;
102pub mod persistence;
103pub mod prelude;
104pub mod runtime;
105pub mod state;
106pub mod study;
107pub mod task;
108pub mod ui;
109
110pub use error::basic::WorkflowError;
111pub use scientific_workflow_macros::{execution_unit, model};
112
113/// Loads, preflights, and executes the Workflow project rooted at
114/// `project_root`.
115///
116/// This is the sole ordinary application entry point. Project loading and
117/// Study compilation finish before Runtime receives the validated immutable
118/// Study. Successful completion returns `()`; advanced integrations can load
119/// a [`study::advanced::Study`] and call [`runtime::advanced::execute`] to
120/// retain a read-only run summary.
121pub fn run(project_root: &std::path::Path) -> Result<(), WorkflowError> {
122 let study = study::advanced::Study::load(project_root)?;
123 runtime::advanced::execute(study)?;
124 Ok(())
125}
126
127/// Implementation details used by Workflow's declaration macros.
128///
129/// This namespace is public only because procedural macro expansion occurs in
130/// the application crate. It is not a supported application API.
131#[doc(hidden)]
132pub mod __private {
133 pub use crate::task::advanced::ModelRegistration;
134 pub use inventory;
135}