Skip to main content

scientific_workflow/
lib.rs

1//! Configuration-driven scientific workflow execution.
2//!
3//! An ordinary application defines registered [`task::basic::ScientificModel`]
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::model("example")]
32//! impl ScientificModel 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 state(&self) -> &SystemState { &self.state }
46//!     fn is_complete(&self) -> bool {
47//!         self.state.time().iteration() >= self.target_iteration
48//!     }
49//!     fn target_iteration(&self) -> Option<u64> { Some(self.target_iteration) }
50//!     fn step(&mut self) -> TaskResult {
51//!         let (population, cumulative_births) = self
52//!             .state
53//!             .borrow_payloads_mut::<(u64, u64)>(
54//!                 ("population", "cumulative_births"),
55//!             )?;
56//!         *population += 1;
57//!         *cumulative_births += 1;
58//!         self.state.advance_time(None)?;
59//!         Ok(())
60//!     }
61//! }
62//! ```
63//!
64//! # Ownership boundaries
65//!
66//! - [`config`] alone reads and parses project JSON and supplies typed model
67//!   constants.
68//! - [`study`] composes parsed declarations, state semantics, and compiled model
69//!   registrations into immutable, output-free intent.
70//! - [`runtime`] consumes a completed Study and owns active execution/output.
71//! - [`task`] owns the model contract and automatic observation boundaries.
72//! - [`state`] owns canonical scientific state and schema.
73//! - [`observation`] owns scientific observation meaning, not persistence mechanics.
74//! - [`persistence`] owns automatic durable output and verified reading.
75//! - [`ui`] owns automatic terminal presentation of Runtime facts.
76//! - [`error`] owns complete-workflow Study/Runtime error composition.
77//!
78//! The crate-level [`run`] facade performs the sole ordinary transition from
79//! project root to Study to Runtime. `Study` is the ultimate coordinator of
80//! declared intent; runtime is the ultimate coordinator of active execution.
81//! Advanced integrations use each module's `advanced` scope. [`prelude`] only
82//! aggregates those module-owned APIs and crate conveniences.
83//!
84//! Persistence write construction and output allocation are internal and are
85//! not available to model authors.
86//!
87//! This crate is pre-1.0 test software and may make coordinated API changes.
88
89#![deny(missing_docs, rustdoc::broken_intra_doc_links)]
90
91extern crate self as scientific_workflow;
92
93mod clock;
94pub mod error;
95
96pub mod config;
97pub mod observation;
98pub mod persistence;
99pub mod prelude;
100pub mod runtime;
101pub mod state;
102pub mod study;
103pub mod task;
104pub mod ui;
105
106pub use error::basic::WorkflowError;
107pub use scientific_workflow_macros::model;
108
109/// Loads, preflights, and executes the Workflow project rooted at
110/// `project_root`.
111///
112/// This is the sole ordinary application entry point. Project loading and
113/// Study compilation finish before Runtime receives the validated immutable
114/// Study. Successful completion returns `()`; advanced integrations can load
115/// a [`study::advanced::Study`] and call [`runtime::advanced::execute`] to
116/// retain a read-only run summary.
117pub fn run(project_root: &std::path::Path) -> Result<(), WorkflowError> {
118    let study = study::advanced::Study::load(project_root)?;
119    runtime::advanced::execute(study)?;
120    Ok(())
121}
122
123/// Implementation details used by Workflow's declaration macros.
124///
125/// This namespace is public only because procedural macro expansion occurs in
126/// the application crate. It is not a supported application API.
127#[doc(hidden)]
128pub mod __private {
129    pub use crate::task::advanced::ModelRegistration;
130    pub use inventory;
131}