Skip to main content

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 clock;
105mod composition;
106mod error;
107
108pub mod config;
109pub mod observation;
110pub mod persistence;
111pub mod prelude;
112pub mod runtime;
113pub mod state;
114pub mod study;
115pub mod task;
116#[cfg(feature = "terminal-ui")]
117mod ui;
118
119pub use error::WorkflowError;
120pub use scientific_workflow_macros::execution_unit;
121pub use task::{
122    ExecutionUnit, InitializationContext, MemberCompletion, MemberView, SeedError, UnitResult,
123};
124
125/// Loads, preflights, and executes the Workflow project rooted at
126/// `project_root`.
127///
128/// This is the sole ordinary application entry point. Project loading and
129/// Study compilation finish before Runtime receives the validated immutable
130/// Study. Successful completion returns `()`; advanced integrations can load
131/// an embedding integration can load a [`study::Study`] and call
132/// [`runtime::execute`] to
133/// retain a read-only run summary.
134pub fn run(project_root: &std::path::Path) -> Result<(), WorkflowError> {
135    let study = study::Study::load(project_root)?;
136    runtime::execute(study)?;
137    Ok(())
138}
139
140/// Implementation details used by Workflow's declaration macros.
141///
142/// This namespace is public only because procedural macro expansion occurs in
143/// the application crate. It is not a supported application API.
144#[doc(hidden)]
145pub mod __private {
146    pub use crate::task::ExecutionUnitRegistration;
147    pub use inventory;
148}