README
¶
Order Processing Workflow Example
This example demonstrates a comprehensive order processing workflow using the Petri net-based workflow engine. It showcases realistic business logic including payment processing, inventory management, and order fulfillment.
Workflow Overview
The order processing workflow consists of the following states and transitions:
States (Places)
- pending: Initial state when order is created
- payment_processing: Payment is being processed
- payment_approved: Payment was successful
- payment_failed: Payment failed
- inventory_check: Checking if items are in stock
- inventory_insufficient: Not enough stock available
- shipping: Order is being shipped
- delivered: Order has been delivered
- cancelled: Order was cancelled
- refunded: Order was refunded
Key Transitions
process_payment: pending → payment_processingpayment_success: payment_processing → payment_approvedpayment_failure: payment_processing → payment_failedretry_payment: payment_failed → payment_processingcheck_inventory: payment_approved → inventory_checkinventory_available: inventory_check → shippinginventory_insufficient: inventory_check → inventory_insufficientrestock_and_ship: inventory_insufficient → shippingmark_delivered: shipping → deliveredrefund_order: delivered → refunded
Business Logic Components
Order Management
- Order: Represents an order with items, customer info, and status
- OrderItem: Individual items in an order with quantity and pricing
Payment Processing
- PaymentProcessor: Simulates payment processing with configurable success rate
- Handles payment retries and failure scenarios
- Simulates processing delays for realism
Inventory Management
- Inventory: Manages product stock levels
- Checks availability before shipping
- Handles restocking scenarios
- Prevents overselling
Features Demonstrated
- Event-Driven Architecture: Uses workflow events to trigger business logic
- Error Handling: Comprehensive error handling for each transition
- Context Management: Stores order data and business state in workflow context
- Realistic Simulation: Simulates real-world scenarios like payment failures and inventory shortages
- State Persistence: Maintains order status throughout the workflow
- Visualization: Generates Mermaid diagrams for workflow visualization
Running the Example
cd examples/order_processing
go run main.go
Example Output
The example will demonstrate:
- Order Creation: Creates a sample order with multiple items
- Payment Processing: Simulates payment with success/failure scenarios
- Inventory Check: Verifies stock availability
- Shipping Process: Handles the shipping workflow
- Delivery Confirmation: Marks order as delivered
- Status Reporting: Shows final order status and workflow diagram
Key Learning Points
- Workflow Design: How to design complex business workflows with multiple paths
- Event Handling: Using events to trigger business logic at specific workflow stages
- Error Recovery: Handling failure scenarios and retry logic
- State Management: Managing complex state across multiple workflow stages
- Business Integration: Integrating external systems (payment, inventory) with workflows
Extending the Example
You can extend this example by:
- Adding Database Storage: Integrate with the WorkflowManager for persistent storage
- Web Interface: Add HTTP endpoints for order management
- Notification System: Add email/SMS notifications at key stages
- Analytics: Track workflow performance and bottlenecks
- Parallel Processing: Add concurrent inventory checks for multiple warehouses
Integration with Workflow Manager
This example can be enhanced with the WorkflowManager for production use:
// Create manager with SQLite storage
manager := workflow.NewManager(storage.NewSQLiteStorage("orders.db"))
// Save workflow instance
err := manager.SaveWorkflow(wf)
// Load workflow instance
wf, err := manager.LoadWorkflow("order-123")
This provides persistent storage, workflow instance management, and better scalability for production environments.
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.