-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathflow.go
105 lines (90 loc) · 1.95 KB
/
flow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package gotaskflow
import (
"fmt"
)
var builder = flowBuilder{}
type flowBuilder struct{}
// Condition Wrapper
type Condition struct {
handle func() uint
mapper map[uint]*innerNode
}
// Static Wrapper
type Static struct {
handle func()
}
// Subflow Wrapper
type Subflow struct {
handle func(sf *Subflow)
g *eGraph
}
// only for visualizer
func (sf *Subflow) instantiate() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf(" instantiate may failed or paniced")
}
}()
if sf.g.instantiated {
return nil
}
sf.g.instantiated = true
sf.handle(sf)
return nil
}
// Push pushs all tasks into subflow
func (sf *Subflow) push(tasks ...*Task) {
for _, task := range tasks {
sf.g.push(task.node)
}
}
func (tf *flowBuilder) NewStatic(name string, f func()) *innerNode {
node := newNode(name)
node.ptr = &Static{
handle: f,
}
node.Typ = nodeStatic
return node
}
func (fb *flowBuilder) NewSubflow(name string, f func(sf *Subflow)) *innerNode {
node := newNode(name)
node.ptr = &Subflow{
handle: f,
g: newGraph(name),
}
node.Typ = nodeSubflow
return node
}
func (fb *flowBuilder) NewCondition(name string, f func() uint) *innerNode {
node := newNode(name)
node.ptr = &Condition{
handle: f,
mapper: make(map[uint]*innerNode),
}
node.Typ = nodeCondition
return node
}
// NewStaticTask returns a static task
func (sf *Subflow) NewTask(name string, f func()) *Task {
task := &Task{
node: builder.NewStatic(name, f),
}
sf.push(task)
return task
}
// NewSubflow returns a subflow task
func (sf *Subflow) NewSubflow(name string, f func(sf *Subflow)) *Task {
task := &Task{
node: builder.NewSubflow(name, f),
}
sf.push(task)
return task
}
// NewCondition returns a condition task. The predict func return value determines its successor.
func (sf *Subflow) NewCondition(name string, predict func() uint) *Task {
task := &Task{
node: builder.NewCondition(name, predict),
}
sf.push(task)
return task
}