flow/flow.go
2019-11-07 17:33:47 +08:00

196 lines
3.2 KiB
Go

package flow
type FlowType int
const (
TNode FlowType = 1
TRoot FlowType = 2
)
type ExecuteStatus int
const (
SFailure ExecuteStatus = 0
SSuccess ExecuteStatus = 1
SAgain ExecuteStatus = 2
)
type IFlow interface {
GetName() string
GetRootFlow() IFlow
SetRootFlow(IFlow)
SetContext(*FlowContext)
GetContext() *FlowContext
GetPath() string
SetPath(path string)
GetNext() IFlow
SetNext(IFlow)
GetPrev() IFlow
SetPrev(IFlow)
Execute() ExecuteStatus
}
// FlowNode 流程节点
type FlowNode struct {
IFlow
Name string
Path string
RootFlow IFlow
prev IFlow
next IFlow
Task func(cxt *FlowContext) ExecuteStatus
}
func (fn *FlowNode) GetRootFlow() IFlow {
return fn.RootFlow
}
func (fn *FlowNode) SetRootFlow(fl IFlow) {
fn.RootFlow = fl
}
// GetPath Get return Path string
func (fn *FlowNode) GetPath() string {
return fn.Path
}
// SetPath Set Path string
func (fn *FlowNode) SetPath(Path string) {
fn.Path = Path
}
// GetName Get return Name string
func (fn *FlowNode) GetName() string {
return fn.Name
}
func (fn *FlowNode) GetPrev() IFlow {
return fn.prev
}
func (fn *FlowNode) SetPrev(prev IFlow) {
fn.prev = prev
}
func (fn *FlowNode) GetNext() IFlow {
return fn.next
}
func (fn *FlowNode) SetNext(next IFlow) {
fn.next = next
}
// GetContext Get return Context *FlowContext
func (fn *FlowNode) GetContext() *FlowContext {
return fn.GetRootFlow().GetContext()
}
// SetContext Set Context *FlowContext
func (fn *FlowNode) SetContext(Context *FlowContext) {
fn.GetRootFlow().SetContext(Context)
}
// Execute Get return Name string
func (fn *FlowNode) Execute() ExecuteStatus {
return fn.Task(fn.GetContext())
}
// Flow 流程
type Flow struct {
IFlow
RootFlow IFlow
Name string
Context *FlowContext // 执行流程时候的上下文
Head IFlow
Tail IFlow
}
func (flow *Flow) GetRootFlow() IFlow {
return flow.RootFlow
}
func (flow *Flow) SetRootFlow(fl IFlow) {
flow.RootFlow = fl
}
// GetContext Get return Context *FlowContext
func (flow *Flow) GetContext() *FlowContext {
return flow.Context
}
// SetContext Set Context *FlowContext
func (flow *Flow) SetContext(Context *FlowContext) {
flow.Context = Context
}
// GetName Get return Name string
func (flow *Flow) GetName() string {
return flow.Name
}
// Execute Get return Name string
// func (flow *Flow) Execute() {
// flow.Context.
// }
// New 创建一个流程, 相当于例子 `干洗`
func New(name string) *Flow {
f := &Flow{Name: name, Context: &FlowContext{}}
return f
}
func (flow *Flow) GetPrev() IFlow {
return flow.Head.GetPrev()
}
func (flow *Flow) SetPrev(prev IFlow) {
// panic("flow can't call SetPrev")
flow.Head.SetPrev(prev)
}
func (flow *Flow) GetNext() IFlow {
return flow.Tail.GetNext()
}
func (flow *Flow) SetNext(next IFlow) {
// panic("flow can't call SetNext")
flow.Tail.SetNext(next)
}
// Add 添加
func (flow *Flow) Add(fl IFlow) {
if flow.Head == nil {
flow.Head = fl
flow.Tail = fl
fl.SetRootFlow(flow)
fl.SetContext(flow.GetContext())
fl.SetPath(fl.GetName())
return
}
fl.SetPath(flow.Tail.GetPath() + ">" + flow.Tail.GetName())
fl.SetRootFlow(flow)
fl.SetContext(flow.GetContext())
fl.SetPrev(flow.Tail)
flow.Tail.SetNext(fl)
flow.Tail = fl
if fl.GetNext() != nil {
panic("tail next is not nil")
}
}