Skip to main content

bpmn_engine/engine/
instance.rs

1//! Process Instance
2//!
3//! Process instance management for BPMN processes.
4
5use crate::engine::context::ExecutionContext;
6use crate::model::ProcessDefinition;
7use std::sync::Arc;
8use tokio::sync::RwLock;
9
10/// Process Instance
11///
12/// Represents a running instance of a BPMN process.
13#[derive(Debug, Clone)]
14pub struct ProcessInstance {
15    /// Instance ID
16    pub id: String,
17    /// Process definition
18    pub definition: Arc<ProcessDefinition>,
19    /// Execution context
20    pub context: Arc<RwLock<ExecutionContext>>,
21}
22
23impl ProcessInstance {
24    /// Create a new process instance
25    pub fn new(definition: Arc<ProcessDefinition>, instance_id: String) -> Self {
26        let context = ExecutionContext::new((*definition).clone(), instance_id.clone());
27        Self {
28            id: instance_id,
29            definition,
30            context: Arc::new(RwLock::new(context)),
31        }
32    }
33
34    /// Get the instance ID
35    pub fn id(&self) -> &str {
36        &self.id
37    }
38
39    /// Get the process definition
40    pub fn definition(&self) -> &Arc<ProcessDefinition> {
41        &self.definition
42    }
43
44    /// Get a read lock on the execution context
45    pub async fn context(&self) -> tokio::sync::RwLockReadGuard<'_, ExecutionContext> {
46        self.context.read().await
47    }
48
49    /// Get a write lock on the execution context
50    pub async fn context_mut(&self) -> tokio::sync::RwLockWriteGuard<'_, ExecutionContext> {
51        self.context.write().await
52    }
53}
54