Skip to main content

bpmn_engine/activity/
traits.rs

1//! Activity Traits
2//!
3//! Activity trait definitions for BPMN elements that can be executed.
4//!
5//! Activities represent executable units in a BPMN process.
6//! Each activity implements the Activity trait to define its execution behavior.
7
8use crate::engine::ExecutionContext;
9use crate::model::ProcessElement;
10use async_trait::async_trait;
11use std::sync::Arc;
12
13/// Activity Trait
14///
15/// Represents an executable activity in a BPMN process.
16/// Activities can be tasks, gateways, events, or other executable elements.
17#[async_trait]
18pub trait Activity: Send + Sync {
19    /// Execute the activity
20    ///
21    /// # Arguments
22    /// * `context` - Execution context containing process state, variables, etc.
23    ///
24    /// # Returns
25    /// * `Ok(ActivityResult)` - Activity execution result
26    /// * `Err(ActivityError)` - Activity execution error
27    async fn execute(&self, context: &mut ExecutionContext) -> Result<ActivityResult, ActivityError>;
28
29    /// Get the activity ID
30    fn id(&self) -> &str;
31
32    /// Get the activity name
33    fn name(&self) -> Option<&str>;
34}
35
36/// Activity Result
37///
38/// Result of executing an activity.
39#[derive(Debug, Clone)]
40pub enum ActivityResult {
41    /// Activity completed successfully
42    Completed {
43        /// Output variables (if any)
44        output_variables: Option<std::collections::HashMap<String, serde_json::Value>>,
45    },
46    /// Activity is waiting (e.g., user task waiting for user input)
47    Waiting {
48        /// Wait reason
49        reason: String,
50    },
51    /// Activity needs to continue to next element(s)
52    Continue {
53        /// Next element IDs to execute
54        next_elements: Vec<String>,
55    },
56}
57
58/// Activity Error
59///
60/// Error that occurred during activity execution.
61#[derive(Debug, Clone, thiserror::Error)]
62pub enum ActivityError {
63    #[error("Activity execution failed: {0}")]
64    ExecutionFailed(String),
65    #[error("Invalid input: {0}")]
66    InvalidInput(String),
67    #[error("Activity not found: {0}")]
68    NotFound(String),
69    #[error("Condition evaluation failed: {0}")]
70    ConditionEvaluationFailed(String),
71}
72
73/// Activity Factory
74///
75/// Factory for creating Activity instances from ProcessElement.
76pub trait ActivityFactory: Send + Sync + std::fmt::Debug {
77    /// Create an Activity from a ProcessElement
78    fn create_activity(&self, element: &ProcessElement) -> Result<Arc<dyn Activity>, ActivityError>;
79}
80