注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

akka 有限状态机(FSM)学习参考  

2014-06-01 12:11:29|  分类: Akka |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

http://doc.akka.io/docs/akka/2.3.6/scala/fsm.html
FSM(有限状态机)
akka 有限状态机(FSM)学习参考
有限状态机(FSM) : 由一组状态(State)、一组条件(Condition)、一组转换(Transition)所构成的数学模型。用于程序设计、序列逻辑线路设计。例如:自动贩卖机(Vending machine)、电梯(elevator)、交通红绿灯、组合锁、赌博机、十字转门(turnstile)等等。
FSM假设一台机器(machine):
1. 可以通过一组有限状态来表现;
2. 同一时刻仅出现其中一种(当前状态);
3. 当某一事件(Event)发生,或条件满足;
4. 当前状态(current state)转换为状态集中的另外一种。
可以简化为:State(S) x Event(E) -> Actions (A), State(S')
设计方法:
FSM的设计通常使用
直观的状态转换表(state transition table)。(也可使用方向图directed graph等)。
例如:对
十字转门状态转换表可以表述为:
Current State Input Next State Output
Locked coin Unlocked Release turnstile so customer can push through
push Locked None
Unlocked coin Unlocked None
push Locked When customer has pushed through lock turnstile
更进一步:
对 State(S) x Event(E) -> Actions (A), State(S') 二维
State transition table
Current state →
Input ↓
State A State B State C
Input X ... ... ...
Input Y ... State C ...
Input Z ... ... ...
有了这些准备,对理解akka的trait FSM有一定帮助。也是进行FSM编程前的必要设计。
Akka中的Remote Actor


在akka中的典型模板
object A {
    trait State
    case class One extends State
    case class Two extends State

    case class Data(i : Int)
  }

  class A extends Actor with FSM[A.State, A.Data] {
    import A._
    startWith(One, Data(42))
    when(One) {
        case Event(SomeMsg, Data(x)) => ...
        case Ev(SomeMsg) => ... // convenience when data not needed
    }
    when(Two, stateTimeout = 5 seconds) { ... }
    initialize()
  }
Within the partial function the following values are returned for effecting state transitions:
  • stay for staying in the same state
  • stay using Data(...) for staying in the same state, but with different data
  • stay forMax 5.millis for staying with a state timeout; can be combined with using
  • goto(...) for changing into a different state; also supports using and forMax
  • stop for terminating this FSM actor

    Each of the above also supports the method replying(AnyRef) for sending a reply before changing state.

    While changing state, custom handlers may be invoked which are registered using onTransition. This is meant to enable concentrating different concerns in different places; you may choose to use when for describing the properties of a state, including of course initiating transitions, but you can describe the transitions using onTransition to avoid having to duplicate that code among multiple paths which lead to a transition:

    onTransition {
      case Active -> _ => cancelTimer("activeTimer")
    }
    

    Multiple such blocks are supported and all of them will be called, not only the first matching one.

    Another feature is that other actors may subscribe for transition events by sending a SubscribeTransitionCallback message to this actor. Stopping a listener without unregistering will not remove the listener from the subscription list; use UnsubscribeTransitionCallback before stopping the listener.

    State timeouts set an upper bound to the time which may pass before another message is received in the current state. If no external message is available, then upon expiry of the timeout a StateTimeout message is sent. Note that this message will only be received in the state for which the timeout was set and that any message received will cancel the timeout (possibly to be started again by the next transition).

    Another feature is the ability to install and cancel single-shot as well as repeated timers which arrange for the sending of a user-specified message:

      setTimer("tock", TockMsg, 1 second, true) // repeating
      setTimer("lifetime", TerminateMsg, 1 hour, false) // single-shot
      cancelTimer("tock")
      isTimerActive("tock")
    
  评论这张
 
阅读(918)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017