swf

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2025 License: AGPL-3.0 Imports: 7 Imported by: 0

README

Simple Workflow Package (SWF)

This package provides a simple, linear workflow management system in Go. It is designed for human-driven processes where steps require manual intervention, approval, or completion by users.

Workflow Example

Overview

The workflow package allows you to create and manage linear, sequential workflows that are typically driven by human interaction. It provides functionality to:

  • Create and manage steps in a sequential workflow
  • Track the current step and who is responsible for it
  • Determine if steps are complete
  • Calculate workflow progress
  • Store and retrieve metadata for steps
  • Serialize and deserialize workflow state
  • Visualize the workflow as a DOT graph

Note: This is a simple linear workflow system designed for human-driven processes. It does not support complex workflows with branching paths or DAGs (Directed Acyclic Graphs). Each step follows the previous one in a straightforward sequence and typically requires manual completion.

When to Use This Package

This package is ideal for:

  • Document approval workflows
  • Employee onboarding processes
  • Manual review workflows
  • Form completion wizards
  • Business process checklists
  • Project stage gates
  • Any process where human intervention is required at each step

It is not suitable for:

  • Automated processing workflows
  • Complex workflows with branching paths
  • DAG-based workflows
  • Parallel processing workflows
  • Workflows requiring conditional branching
  • Automated task execution

Components

Step

A Step represents a single step in a workflow. Each step has:

  • Name: Unique identifier for the step
  • Type: Type of the step (default: "normal")
  • Title: Display title for the step
  • Description: Description of what the step does
  • Responsible: Person or role responsible for completing the step
Workflow

A Workflow manages multiple steps and tracks the workflow state. It provides methods to:

  • Add steps to the workflow
  • Get and set the current step
  • Check if a step is current or complete
  • Calculate workflow progress
  • Store and retrieve metadata for steps
  • Serialize and deserialize workflow state
  • Visualize the workflow as a DOT graph

Usage

package main

import (
    "fmt"
    "github.com/yourusername/workflow"
)

func main() {
    // Create a new linear workflow
    wf := workflow.NewWorkflow()

    // Create steps in sequence
    step1 := workflow.NewStep("step1")
    step1.Title = "Document Review"
    step1.Description = "Review the submitted document"
    step1.Responsible = "John Doe" // Person responsible for completing this step

    step2 := workflow.NewStep("step2")
    step2.Title = "Manager Approval"
    step2.Description = "Manager approval of the document"
    step2.Responsible = "Jane Smith" // Person responsible for completing this step

    // Add steps to the workflow in sequence
    wf.AddStep(step1)
    wf.AddStep(step2)

    // Steps will be executed in the order they were added
    // Each step requires manual completion by the responsible person
    // Get the current step
    currentStep := wf.GetCurrentStep()
    fmt.Printf("Current step: %s\n", currentStep.Name)

    // Move to the next step
    wf.SetCurrentStep(step2)

    // Check if a step is complete
    isComplete := wf.IsStepComplete(step1)
    fmt.Printf("Is step1 complete? %v\n", isComplete)

    // Get progress
    progress := wf.GetProgress()
    fmt.Printf("Progress: %d/%d steps completed (%.2f%%)\n",
        progress.Completed, progress.Total, progress.Percents)

    // Visualize the workflow
    dotGraph := wf.Visualize()
    fmt.Println("Workflow visualization (DOT format):")
    fmt.Println(dotGraph)
}

Visualization

The package provides a visualization feature that generates a DOT graph representation of the workflow. The visualization:

  • Shows steps as boxes with their titles
  • Uses colors to indicate status:
    • White: Pending steps
    • Blue (#2196F3): Current step
    • Green (#4CAF50): Completed steps
  • Shows edges between steps with:
    • Gray (#9E9E9E): Default edge color
    • Green (#4CAF50): Completed path
  • Includes tooltips with step descriptions
  • Arranges the graph left-to-right

You can render the DOT graph using:

  1. Graphviz's dot command: dot -Tpng workflow.dot -o workflow.png
  2. Online tools like Graphviz Online
  3. The Graphviz Visual Editor

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

Example demonstrates how to use the workflow package

Types

type DotEdgeSpec

type DotEdgeSpec struct {
	FromNodeName string
	ToNodeName   string
	Tooltip      string
	Style        string
	Color        string
}

DotEdgeSpec represents an edge in the DOT graph

type DotNodeSpec

type DotNodeSpec struct {
	Name        string
	DisplayName string
	Tooltip     string
	Shape       string
	Style       string
	FillColor   string
}

DotNodeSpec represents a node in the DOT graph

type Progress

type Progress struct {
	Total     int
	Completed int
	Current   int
	Pending   int
	Percents  float64
}

Progress represents workflow progress

type State

type State struct {
	Name        string
	Title       string
	Description string
	Responsible string
}

State represents a workflow state

func NewState

func NewState(name string) *State

NewState creates a new State with the given name

func (s *State) GetActionLink() string

GetActionLink returns the action link for the state

type Step

type Step struct {
	Name        string
	Type        string
	Title       string
	Description string
	Responsible string
}

Step represents a single step in a workflow

func NewStep

func NewStep(name string) *Step

NewStep creates a new Step with the given name

func (s *Step) GetActionLink() string

GetActionLink returns the action link for the step Note: This is a placeholder implementation as the PHP version uses a framework-specific function

type StepDetails

type StepDetails struct {
	Started   string
	Completed string
	Meta      map[string]any
}

StepDetails contains metadata about a step

type Workflow

type Workflow struct {
	// contains filtered or unexported fields
}

Workflow represents a workflow

func NewWorkflow

func NewWorkflow() *Workflow

NewWorkflow creates a new Workflow

func (*Workflow) AddStep

func (w *Workflow) AddStep(step *Step) error

AddStep adds a step to the workflow

Business logic: 1. Check if step already exists 2. Add step to steps map 3. If first step, set it as current step 4. Add step details to step details map

func (*Workflow) FromString

func (w *Workflow) FromString(str string) error

FromString deserializes the workflow state from a string

func (*Workflow) GetCurrentStep

func (w *Workflow) GetCurrentStep() *Step

GetCurrentStep returns the current step

func (*Workflow) GetProgress

func (w *Workflow) GetProgress() *Progress

GetProgress returns the workflow progress

func (*Workflow) GetState

func (w *Workflow) GetState() *WorkflowState

GetState returns the current workflow state

func (*Workflow) GetStep

func (w *Workflow) GetStep(name string) *Step

GetStep returns a step by name

func (*Workflow) GetStepMeta

func (w *Workflow) GetStepMeta(step any, key string) any

GetStepMeta returns step metadata

Business logic: 1. Get step name 2. Get step metadata 3. Return metadata or nil if key not found

func (*Workflow) GetSteps

func (w *Workflow) GetSteps() []*Step

GetSteps returns all steps

func (*Workflow) IsStepComplete

func (w *Workflow) IsStepComplete(step any) bool

IsStepComplete checks if a step is completed

Business logic: 1. Get step name 2. Get step positions 3. If step is before the current step, it's complete 4. If step is explicitly marked as completed, it's complete

func (*Workflow) IsStepCurrent

func (w *Workflow) IsStepCurrent(step any) bool

IsStepCurrent checks if a step is the current step

func (*Workflow) MarkStepAsCompleted

func (w *Workflow) MarkStepAsCompleted(step any) bool

MarkStepAsCompleted marks a step as completed

Business logic: 1. Get step name 2. Mark step as completed 3. Return true if step was marked as completed

func (*Workflow) SetCurrentStep

func (w *Workflow) SetCurrentStep(step any) error

SetCurrentStep sets the current step, can be a step name or a step pointer

Business logic: 1. Check if step exists 2. Mark the current step as completed 3. Set the current step to the new step

func (*Workflow) SetStepMeta

func (w *Workflow) SetStepMeta(step any, key string, value interface{})

SetStepMeta sets step metadata

func (*Workflow) ToString

func (w *Workflow) ToString() (string, error)

ToString serializes the workflow state to a string

func (*Workflow) Visualize

func (w *Workflow) Visualize() string

Visualize returns a DOT graph representation of the workflow

type WorkflowState

type WorkflowState struct {
	CurrentStepName string
	// History is the history of steps that have been completed
	// and the current step, which has been started
	History     []string
	StepDetails map[string]*StepDetails
}

WorkflowState represents the current state of a workflow

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL