package fsm

import (
"log"
) type EvtIf interface {
GetEvtType() string
} type Action interface {
//doAction(evt EvtIf, srcState *State, dstState *State)
doAction(evt EvtIf)
} type Transition struct {
dstState *State
action Action
} type State struct {
name string
isEnd bool
transitionTbl map[string]Transition
} type fsm struct {
InstId uint64
version uint32
startPoint *State
current *State
transitionLog []Transition
stateTbl map[string]*State
} //func StartStateMachine(transtionLog []Transition) *fsm {
//}
//func ReplyStateMachine(transtionLog []Transition) *fsm {
//}
//func Save() *fsm {
//}
//func Load() *fms {
//} func NewState(name string, end bool) *State {
return &State{name, end, map[string]Transition{}}
} func (this *State) GetName() string {
return this.name
} func (this *State) InterestInEvt(evt EvtIf) bool {
if evt == nil {
return false
}
_, exists := this.transitionTbl[evt.GetEvtType()]
return exists
} func (this *State) AddTransition(evt EvtIf, dst *State, action Action) {
if evt == nil || dst == nil || action == nil {
log.Fatal("AddTransition error, parameter invalid!")
}
_, exists := this.transitionTbl[evt.GetEvtType()]
if exists {
log.Fatal("AddTransition error,EvtIf exist")
}
this.transitionTbl[evt.GetEvtType()] = NewTransition(dst, action)
} func (this *State) GetTransition(evt EvtIf) *Transition {
if evt == nil {
return nil
} tran, exists := this.transitionTbl[evt.GetEvtType()]
if exists == false {
log.Fatal("GetTransition error,EvtIf doesn't exist")
}
return &tran
} func NewTransition(dstState *State, action Action) Transition {
return Transition{dstState, action}
} func (this *fsm) RegisterState(state *State) {
if state == nil {
log.Fatal("RegisterState error, parameter invalid!")
}
_, exists := this.stateTbl[state.GetName()]
if exists {
return
log.Fatal("RegisterState error,State exist")
}
this.stateTbl[state.GetName()] = state
} func (this *fsm) AddTransition(srcState *State, evt EvtIf, dstState *State, action Action) {
if srcState == nil || dstState == nil || evt == nil || action == nil {
log.Fatal("AddTransition error, parameter invalid!")
}
this.RegisterState(srcState)
this.RegisterState(dstState)
srcState.AddTransition(evt, dstState, action)
} func (this *fsm) AddInternalTransition(srcState *State, evt EvtIf, action Action) {
this.AddTransition(srcState, evt, srcState, action)
} func (this *fsm) HandleEvt(evt EvtIf) {
if evt == nil {
return
} srcState := this.current
if srcState.InterestInEvt(evt) {
transition := srcState.GetTransition(evt)
log.Printf("Handle Event, srcState:%s, dstState:%s, evt:%s", srcState.GetName(), transition.dstState.GetName(), evt.GetEvtType())
//transition.action.doAction(evt, srcState, transition.dstState)
transition.action.doAction(evt)
this.current = transition.dstState
} else {
log.Printf("parameter invalid, srcState:%s, evt:%s", srcState.GetName(), evt.GetEvtType())
}
} func NewFsm(initialState *State) *fsm {
if initialState == nil {
log.Fatal("NewFsm error, parameter invalid!")
}
return &fsm{initialState, map[string]*State{}}
}

最新文章

  1. Open Auth辅助库(使用ImitateLogin实现登录)
  2. ViewController与outlet绑定
  3. Cygwin之SSH服务安装过程问题
  4. Linux系统文件权限&目录权限
  5. trackr: An AngularJS app with a Java 8 backend – Part I
  6. EF6.0+Mysql的问题
  7. maven自动部署到tomcat的问题
  8. 初读 c# IL中间语言
  9. Oracle中SQL调优(SQL TUNING)之最权威获取SQL执行计划大全
  10. WPF:在DataTemplate中使用DataType
  11. 使用command line测试网速
  12. Python Django CBV下的通用视图函数
  13. MATLAB——神经网络train函数
  14. VC.遍历文件夹中的文件
  15. 99乘法表的正反写 (python的写法)
  16. Linux服务-搭建Nginx
  17. Django实战(二)之模板语言
  18. bash shell输出颜色
  19. fegin 调用源码分析
  20. 使用 ServiceStack 构建跨平台 Web 服务(转)

热门文章

  1. Python全栈工程师之html学习笔记
  2. Mining of Massive Datasets-1
  3. CodeForces 699C - Vacations
  4. Web自动化Selenium2环境配置中Selenium IDE的安装
  5. 163 AJAX
  6. js的setInterval和setTimeout的那些浅坑
  7. es6--之箭头函数
  8. 对比使用Charles和Fiddler两个工具及利用Charles抓取https数据(App)
  9. Matplotlib基本图形之条形图
  10. RN import ** from ** 用法