README
¶
Simple Flow Example
This example demonstrates a basic workflow using the workflow package. It shows the core concepts of the workflow engine with a simple state machine.
Features
- Basic workflow definition
- Simple state transitions
- Workflow manager for lifecycle management
- Mermaid diagram visualization
- Event handling
- Context data
Workflow States
The workflow consists of the following states:
- Start: Initial state
- Middle: Intermediate state
- End: Final state
Transitions
to_middle: Start → Middleto_end: Middle → End
Prerequisites
- Go 1.16 or later
Installation
- Clone the repository:
git clone https://github.com/ehabterra/workflow.git
cd workflow/examples/simple_flow
- Install dependencies:
go mod download
Running the Example
go run main.go
Implementation Details
Workflow Definition
// Create workflow definition
places := []workflow.Place{
"start",
"middle",
"end",
}
transitions := []workflow.Transition{
{
Name: "to_middle",
From: []workflow.Place{"start"},
To: []workflow.Place{"middle"},
},
{
Name: "to_end",
From: []workflow.Place{"middle"},
To: []workflow.Place{"end"},
},
}
definition, err := workflow.NewDefinition(places, transitions)
Workflow Manager
The example uses the workflow manager to handle workflow lifecycle:
// Initialize the workflow manager
registry := workflow.NewRegistry()
storage := workflow.NewSQLiteStorage("simple_flow.db")
manager := workflow.NewManager(registry, storage)
// Create a new workflow instance
wf, err := manager.CreateWorkflow("flow_1", definition, "start")
if err != nil {
// Handle error
}
// Get a workflow instance
wf, err = manager.GetWorkflow("flow_1", definition)
if err != nil {
// Handle error
}
// Save workflow state
err = manager.SaveWorkflow("flow_1", wf)
if err != nil {
// Handle error
}
Event Handling
The example demonstrates how to use event listeners for workflow transitions:
// Add event listener for before transition
wf.AddEventListener(workflow.EventBeforeTransition, func(event workflow.Event) error {
fmt.Printf("Before transition: %s\n", event.Transition.Name)
return nil
})
// Add event listener for after transition
wf.AddEventListener(workflow.EventAfterTransition, func(event workflow.Event) error {
fmt.Printf("After transition: %s\n", event.Transition.Name)
return nil
})
Context Data
The example shows how to use context data:
// Set context data
wf.SetContext("step", 1)
wf.SetContext("timestamp", time.Now())
// Get context data
step, _ := wf.Context("step")
timestamp, _ := wf.Context("timestamp")
Workflow Diagram
The workflow diagram is generated using Mermaid.js and shows the possible states and transitions:
stateDiagram-v2
classDef currentPlace font-weight:bold,stroke-width:4px
start
middle
end
start --> middle : to-middle
middle --> end : to-end
%% Current places
class end currentPlace
%% Initial place
[*] --> start
Testing
- Create a new workflow instance
- Try different transitions
- Verify the state changes
- Check event handling
- View the workflow diagram
Contributing
Feel free to submit issues and enhancement requests!
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.