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//! ) -> TaskResult<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) -> TaskResult {
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//! - The private UI subsystem owns automatic terminal presentation of Runtime facts.
85//! - [`WorkflowError`] composes complete-workflow Study/Runtime failures.
86//!
87//! The crate-level [`run`] facade performs the sole ordinary transition from
88//! project root to Study to Runtime. `Study` is the ultimate coordinator of
89//! declared intent; runtime is the ultimate coordinator of active execution.
90//! Inspection and embedding integrations import APIs directly from their
91//! owning module roots. [`prelude`] contains only ordinary unit-authoring APIs
92//! and crate conveniences.
93//!
94//! Persistence write construction and output allocation are internal and are
95//! not available to execution-unit authors.
96//!
97//! This crate is pre-1.0 test software and may make coordinated API changes.
98
99#![deny(missing_docs, rustdoc::broken_intra_doc_links)]
100
101extern crate self as scientific_workflow;
102
103mod clock;
104mod error;
105
106pub mod config;
107pub mod observation;
108pub mod persistence;
109pub mod prelude;
110pub mod runtime;
111pub mod state;
112pub mod study;
113mod task;
114mod ui;
115
116pub use error::WorkflowError;
117pub use scientific_workflow_macros::execution_unit;
118pub use task::{
119 ExecutionUnit, InitializationContext, MemberCompletion, MemberView, SeedError, TaskResult,
120};
121
122/// Loads, preflights, and executes the Workflow project rooted at
123/// `project_root`.
124///
125/// This is the sole ordinary application entry point. Project loading and
126/// Study compilation finish before Runtime receives the validated immutable
127/// Study. Successful completion returns `()`; advanced integrations can load
128/// an embedding integration can load a [`study::Study`] and call
129/// [`runtime::execute`] to
130/// retain a read-only run summary.
131pub fn run(project_root: &std::path::Path) -> Result<(), WorkflowError> {
132 let study = study::Study::load(project_root)?;
133 runtime::execute(study)?;
134 Ok(())
135}
136
137/// Implementation details used by Workflow's declaration macros.
138///
139/// This namespace is public only because procedural macro expansion occurs in
140/// the application crate. It is not a supported application API.
141#[doc(hidden)]
142pub mod __private {
143 pub use crate::task::ExecutionUnitRegistration;
144 pub use inventory;
145}