workflow/README.md

50 lines
1.1 KiB
Markdown
Raw Normal View History

2019-12-08 13:40:57 +03:00
# Workflow for Go
2019-12-08 13:47:02 +03:00
[![GoDoc](https://godoc.org/github.com/neonxp/workflow?status.svg)](https://godoc.org/github.com/neonxp/workflow)
2019-12-08 13:40:57 +03:00
Simple state machine. Inspired by [Symfony Workflow](https://github.com/symfony/workflow).
## Example usage
```go
o := new(ObjectImplementedPlaceer)
2020-07-15 15:26:24 +03:00
w := NewWorkflow("Start")
w.AddTransition("Start", "A")
w.AddTransition("Start", "B")
w.AddTransition("A", "C")
w.AddTransition("B", "D")
w.AddTransition( "C", "D")
w.AddTransition("C", "Finish")
w.AddTransition("D", "Finish")
w.Can(o, "A") // == nil
w.Can(o, "C") // == ErrTransitionNotFound
w.GetEnabledTransitions(o) // []Place{"A", "B"}
w.Apply(o, "A") // o now at "A" place
w.GetEnabledTransitions(o) // []Place{"C"}
2019-12-08 13:40:57 +03:00
w.DumpToDot() // See above
```
## Dump result
```
digraph {
2020-07-15 15:26:24 +03:00
Start[color="blue"]
Start -> A[label="Start → A"];
Start -> B[label="Start → B"];
A -> C[label="A → C"];
B -> D[label="B → D"];
C -> D[label="C → D"];
C -> Finish[label="C → Finish"];
D -> Finish[label="D → Finish"];
2019-12-08 13:40:57 +03:00
}
```
Visualization:
![Workflow visualization](images/example.png)