scientific_workflow/lib.rs
1//! Configuration-driven scientific workflow execution.
2//!
3//! An ordinary application defines a registered [`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//! An execution unit carries its stable manifest key at its implementation:
21//!
22//! ```
23//! use serde::Deserialize;
24//! use scientific_workflow::prelude::*;
25//!
26//! #[derive(Deserialize)]
27//! struct Constants { initial: u64, steps: u64 }
28//!
29//! struct ExampleUnit { state: SystemState, target_iteration: u64 }
30//!
31//! #[scientific_workflow::execution_unit("example")]
32//! impl ExecutionUnit for ExampleUnit {
33//! type Constants = Constants;
34//!
35//! fn initialize(
36//! constants: Constants,
37//! schema: &SystemStateSchema,
38//! _context: &InitializationContext,
39//! ) -> UnitResult<Self> {
40//! let mut state = schema.create_empty_state(StateTime::from_iteration(0));
41//! state.initialize_payload("population", constants.initial)?;
42//! state.initialize_payload("cumulative_births", 0_u64)?;
43//! Ok(Self {
44//! state,
45//! target_iteration: constants.steps,
46//! })
47//! }
48//!
49//! fn member_count(&self) -> usize { 1 }
50//! fn member(&self, index: usize) -> Option<MemberView<'_>> {
51//! (index == 0).then(|| MemberView::new(
52//! "example",
53//! &self.state,
54//! (self.state.time().iteration() >= self.target_iteration)
55//! .then_some(MemberCompletion::without_reason()),
56//! Some(self.target_iteration),
57//! ))
58//! }
59//! fn step(&mut self) -> UnitResult {
60//! let (population, cumulative_births) = self
61//! .state
62//! .borrow_payloads_mut::<(u64, u64)>(
63//! ("population", "cumulative_births"),
64//! )?;
65//! *population += 1;
66//! *cumulative_births += 1;
67//! self.state.advance_time(None)?;
68//! Ok(())
69//! }
70//! }
71//! ```
72//!
73//! # Ownership boundaries
74//!
75//! - [`config`] alone reads and parses project JSON and supplies typed execution-unit
76//! constants.
77//! - [`study`] composes parsed declarations, state semantics, and compiled execution-unit
78//! registrations into immutable, output-free intent.
79//! - [`runtime`] consumes a completed Study and owns active execution/output.
80//! - The crate root owns the execution-unit contract and automatic observation boundaries.
81//! - [`state`] owns canonical scientific state and schema.
82//! - [`observation`] owns scientific observation meaning, not persistence mechanics.
83//! - [`persistence`] owns automatic durable output and verified reading.
84//! - Runtime owns lifecycle facts; the private UI subsystem implements their
85//! automatically composed terminal presentation.
86//! - [`WorkflowError`] composes complete-workflow Study/Runtime failures.
87//!
88//! The crate-level [`run`] facade performs the sole ordinary transition from
89//! project root to Study to Runtime. `Study` is the ultimate coordinator of
90//! declared intent; runtime is the ultimate coordinator of active execution.
91//! Inspection and embedding integrations import APIs directly from their
92//! owning module roots. [`prelude`] contains only ordinary unit-authoring APIs
93//! and crate conveniences.
94//!
95//! Persistence write construction and output allocation are internal and are
96//! not available to execution-unit authors.
97//!
98//! This crate is pre-1.0 test software and may make coordinated API changes.
99
100#![deny(missing_docs, rustdoc::broken_intra_doc_links)]
101
102extern crate self as scientific_workflow;
103
104mod composition;
105mod error;
106
107pub mod config;
108pub mod observation;
109pub mod persistence;
110pub mod prelude;
111pub mod runtime;
112pub mod state;
113pub mod study;
114pub mod task;
115#[cfg(feature = "terminal-ui")]
116mod ui;
117
118pub use error::WorkflowError;
119pub use scientific_workflow_macros::execution_unit;
120pub use task::{
121 ExecutionUnit, InitializationContext, MemberCompletion, MemberView, SeedError, UnitResult,
122};
123
124/// Loads, preflights, and executes the Workflow project rooted at
125/// `project_root`.
126///
127/// This is the sole ordinary application entry point. Project loading and
128/// Study compilation finish before Runtime receives the validated immutable
129/// Study. Successful completion returns `()`; advanced integrations can load
130/// an embedding integration can load a [`study::Study`] and call
131/// [`runtime::execute`] to
132/// retain a read-only run summary.
133pub fn run(project_root: &std::path::Path) -> Result<(), WorkflowError> {
134 let study = study::Study::load(project_root)?;
135 runtime::execute(study)?;
136 Ok(())
137}
138
139/// Implementation details used by Workflow's declaration macros.
140///
141/// This namespace is public only because procedural macro expansion occurs in
142/// the application crate. It is not a supported application API.
143#[doc(hidden)]
144pub mod __private {
145 pub use crate::task::ExecutionUnitRegistration;
146 pub use inventory;
147}