Skip to main content

bpmn_engine/engine/
context.rs

1//! Execution Context
2//!
3//! Execution context for BPMN process execution.
4//!
5//! The execution context holds the state of a process instance during execution,
6//! including variables, current element, and execution history.
7
8use crate::model::ProcessDefinition;
9use std::collections::HashMap;
10
11/// Execution Context
12///
13/// Context for executing a BPMN process instance.
14/// Contains process state, variables, and execution information.
15#[derive(Debug, Clone)]
16pub struct ExecutionContext {
17    /// Process definition
18    pub process_definition: ProcessDefinition,
19    /// Process instance ID
20    pub instance_id: String,
21    /// Current element IDs being executed
22    pub current_elements: Vec<String>,
23    /// Process variables
24    pub variables: HashMap<String, serde_json::Value>,
25    /// Execution history
26    pub execution_history: Vec<ExecutionStep>,
27    /// Process state
28    pub state: ProcessInstanceState,
29}
30
31impl ExecutionContext {
32    /// Create a new execution context
33    pub fn new(process_definition: ProcessDefinition, instance_id: String) -> Self {
34        Self {
35            process_definition,
36            instance_id,
37            current_elements: Vec::new(),
38            variables: HashMap::new(),
39            execution_history: Vec::new(),
40            state: ProcessInstanceState::Active,
41        }
42    }
43
44    /// Set a variable
45    pub fn set_variable(&mut self, name: String, value: serde_json::Value) {
46        self.variables.insert(name, value);
47    }
48
49    /// Get a variable
50    pub fn get_variable(&self, name: &str) -> Option<&serde_json::Value> {
51        self.variables.get(name)
52    }
53
54    /// Add execution step to history
55    pub fn add_execution_step(&mut self, step: ExecutionStep) {
56        self.execution_history.push(step);
57    }
58
59    /// Set current elements
60    pub fn set_current_elements(&mut self, element_ids: Vec<String>) {
61        self.current_elements = element_ids;
62    }
63
64    /// Clear current elements
65    pub fn clear_current_elements(&mut self) {
66        self.current_elements.clear();
67    }
68}
69
70/// Process Instance State
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub enum ProcessInstanceState {
73    /// Process is active and executing
74    Active,
75    /// Process is completed
76    Completed,
77    /// Process is terminated
78    Terminated,
79    /// Process is suspended
80    Suspended,
81    /// Process execution failed
82    Failed,
83}
84
85/// Execution Step
86///
87/// Represents a step in the execution history.
88#[derive(Debug, Clone)]
89pub struct ExecutionStep {
90    /// Element ID that was executed
91    pub element_id: String,
92    /// Timestamp of execution
93    pub timestamp: std::time::SystemTime,
94    /// Execution result
95    pub result: ExecutionStepResult,
96}
97
98/// Execution Step Result
99#[derive(Debug, Clone)]
100pub enum ExecutionStepResult {
101    /// Step completed successfully
102    Completed,
103    /// Step failed
104    Failed(String),
105    /// Step is waiting
106    Waiting(String),
107}
108