1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! Process Executor
//!
//! Core execution engine for BPMN processes.
use crate::activity::{Activity, ActivityError, ActivityFactory, ActivityResult, DefaultActivityFactory};
use crate::engine::context::{ExecutionContext, ProcessInstanceState};
use crate::engine::instance::ProcessInstance;
use crate::model::ProcessDefinition;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
/// Engine
///
/// Main BPMN execution engine.
#[derive(Debug)]
pub struct Engine {
/// Process instances (in-memory storage)
instances: Arc<RwLock<HashMap<String, Arc<ProcessInstance>>>>,
/// Activity factory
activity_factory: Arc<dyn ActivityFactory>,
}
impl Engine {
/// Create a new engine
pub fn new() -> Self {
Self {
instances: Arc::new(RwLock::new(HashMap::new())),
activity_factory: Arc::new(DefaultActivityFactory::new()),
}
}
/// Create a new engine with custom activity factory
pub fn with_activity_factory(factory: Arc<dyn ActivityFactory>) -> Self {
Self {
instances: Arc::new(RwLock::new(HashMap::new())),
activity_factory: factory,
}
}
/// Start a new process instance
///
/// # Arguments
/// * `definition` - Process definition to execute
/// * `initial_variables` - Initial process variables
///
/// # Returns
/// * `Ok(ProcessInstance)` - Created process instance
/// * `Err(EngineError)` - Engine error
pub async fn start_process(
&self,
definition: ProcessDefinition,
initial_variables: Option<HashMap<String, serde_json::Value>>,
) -> Result<Arc<ProcessInstance>, EngineError> {
let instance_id = format!("instance_{}", uuid::Uuid::new_v4());
let definition = Arc::new(definition);
let instance = ProcessInstance::new(definition, instance_id.clone());
// Set initial variables
{
let mut context = instance.context_mut().await;
if let Some(vars) = initial_variables {
for (name, value) in vars {
context.set_variable(name, value);
}
}
}
// Store instance
{
let mut instances = self.instances.write().await;
instances.insert(instance_id.clone(), Arc::new(instance.clone()));
}
// Start execution
self.execute_process(Arc::new(instance.clone())).await?;
Ok(Arc::new(instance))
}
/// Execute a process instance
///
/// This is the main execution loop that processes the BPMN process.
async fn execute_process(&self, instance: Arc<ProcessInstance>) -> Result<(), EngineError> {
let definition = instance.definition();
// Find start events
let start_events: Vec<String> = definition
.elements
.values()
.filter_map(|elem| {
match elem {
crate::model::ProcessElement::StartEvent(_) => Some(elem.id().to_string()),
_ => None,
}
})
.collect();
if start_events.is_empty() {
return Err(EngineError::NoStartEvent);
}
// Set current elements to start events
{
let mut context = instance.context_mut().await;
context.set_current_elements(start_events);
}
// Execute process loop
loop {
let should_continue = {
let mut context = instance.context_mut().await;
if context.state != ProcessInstanceState::Active {
break;
}
let current_elements = context.current_elements.clone();
context.clear_current_elements();
// Process each current element
for element_id in current_elements {
// Get element from definition
let element = match definition.get_element(&element_id) {
Some(e) => e,
None => {
context.state = ProcessInstanceState::Failed;
return Err(EngineError::ElementNotFound(element_id));
}
};
// Create activity from element
let activity = match self.activity_factory.create_activity(element) {
Ok(a) => a,
Err(e) => {
context.state = ProcessInstanceState::Failed;
return Err(EngineError::ActivityExecutionError(e));
}
};
// Execute activity
let activity_result = match activity.execute(&mut context).await {
Ok(result) => result,
Err(e) => {
context.state = ProcessInstanceState::Failed;
return Err(EngineError::ActivityExecutionError(e));
}
};
// Record execution step
let step_result = match &activity_result {
ActivityResult::Completed { .. } => {
crate::engine::context::ExecutionStepResult::Completed
}
ActivityResult::Waiting { reason } => {
crate::engine::context::ExecutionStepResult::Waiting(reason.clone())
}
ActivityResult::Continue { .. } => {
crate::engine::context::ExecutionStepResult::Completed
}
};
context.add_execution_step(crate::engine::context::ExecutionStep {
element_id: element_id.clone(),
timestamp: std::time::SystemTime::now(),
result: step_result,
});
// Handle activity result
match activity_result {
ActivityResult::Completed { .. } => {
// Get outgoing flows
let outgoing_flows = definition.get_outgoing_flows(&element_id);
let mut next_elements = Vec::new();
for flow in outgoing_flows {
// Check condition if present
if let Some(_condition) = &flow.condition_expression {
// TODO: Evaluate condition
// For now, assume condition is true
}
next_elements.push(flow.target_ref.clone());
}
// Check if this is an end event
match element {
crate::model::ProcessElement::EndEvent(_) => {
context.state = ProcessInstanceState::Completed;
return Ok(());
}
_ => {
context.current_elements.extend(next_elements);
}
}
}
ActivityResult::Waiting { .. } => {
// Process is waiting, pause execution
break;
}
ActivityResult::Continue { next_elements } => {
// Check if any next element is an end event
let mut has_end_event = false;
for next_id in &next_elements {
if let Some(next_elem) = definition.get_element(next_id) {
if matches!(next_elem, crate::model::ProcessElement::EndEvent(_)) {
has_end_event = true;
break;
}
}
}
if has_end_event {
context.state = ProcessInstanceState::Completed;
return Ok(());
}
context.current_elements.extend(next_elements);
}
}
}
!context.current_elements.is_empty()
};
if !should_continue {
break;
}
}
Ok(())
}
/// Get a process instance by ID
pub async fn get_instance(&self, instance_id: &str) -> Option<Arc<ProcessInstance>> {
let instances = self.instances.read().await;
instances.get(instance_id).cloned()
}
}
impl Default for Engine {
fn default() -> Self {
Self::new()
}
}
/// Engine Builder
///
/// Builder for creating Engine instances with custom configuration.
#[derive(Debug, Default)]
pub struct EngineBuilder {
// Future: Add configuration options
}
impl EngineBuilder {
/// Create a new builder
pub fn new() -> Self {
Self::default()
}
/// Build the engine
pub fn build(self) -> Engine {
Engine::new()
}
}
/// Engine Error
#[derive(Debug, thiserror::Error)]
pub enum EngineError {
#[error("No start event found in process")]
NoStartEvent,
#[error("Element not found: {0}")]
ElementNotFound(String),
#[error("Activity execution error: {0}")]
ActivityExecutionError(#[from] ActivityError),
#[error("Process execution failed: {0}")]
ExecutionFailed(String),
}