How alibaba/smartengine works
Project Type and Purpose
• SmartEngine is an open-source, lightweight BPMN 2.0 business orchestration engine library/framework.
• It exposes CQRS-style APIs (command vs. query) to start/signal processes and query state, parses BPMN XML definitions, and drives execution via a Pluggable Virtual Machine (PVM) and behavior modules.
• It’s designed for microservice orchestration and traditional approval workflows, with extension points for storage, retry, parsing, delegation, and listeners.
a. API Layer (Commands & Queries)
• Packages: service/command/*, service/query/*
• Responsibility: expose operations (startProcess, signal, taskService, etc.) following CQRS.
b. Core Engine
• SmartEngine entry point under engine/SmartEngine.java
• Subcomponents:
• Configuration (configuration/*)
• BPMN Parser (xml/parser/*, bpmn/assembly/*)
• PVM Runtime (pvm/*)
• Behavior Implementations (bpmn/behavior/*, engine/behavior/impl/*)
• Context & Execution (context/*)
• Interceptors & Listeners (interceptor/*, listener/*)
c. Model & Instance
• model/assembly/*, model/instance/*: in-memory representations of definitions and runtime instances.
• Factories & Storage Interfaces (instance/factory/*, instance/storage/*)
d. Extension Points
• Annotation-driven extension binding (extension/annotation, configuration/scanner/*)
• Smart-specific extensions (smart/*)
e. Persistence Plugins
• Storage‐common (abstract interfaces / helpers)
• Storage-custom (in-memory, file, NoSQL, etc.)
• Storage-mysql (MyBatis + MySQL implementation)
• Retry-common, retry-custom, retry-mysql (retry extension points)
f. External Services
• Database (MySQL or other RDBMS via MyBatis)
• External microservices invoked through ServiceTaskBehavior
• Messaging or event systems via SendTask/ReceiveTaskBehavior
• Client → API Layer (CommandService, QueryService) → SmartEngine
• SmartEngine → BPMN XML Parser → ProcessDefinition model
• SmartEngine → ContextFactory → ExecutionContext
• ExecutionContext → PVM Runtime → Behavior modules → (ServiceTaskBehavior → External Service)
• PVM events/interceptors/listeners → Extension Points
• During execution: InstanceStorage & VariablePersister → Storage Plugins → (Database or Custom)
• Retry extension intercepts failed behaviors and schedules retry records via RetryStorage plugins
Architectural Patterns & Principles
• CQRS (separate command/query packages)
• Plugin architecture (interfaces + multiple implementations, discovered via annotation scanner)
• Strategy & Factory patterns (ConfigurationOption, InstanceFactory, PersisterSession)
• Behavior tree traversal (PVM + BehaviorImplementation classes)
• XML-driven configuration (BPMN 2.0, annotation scanning)
Essential Elements
• Configuration
• BPMN Parser
• PVM Runtime → Behavior Modules
• Context & Execution
• Interceptor / Listener