设为首页 加入收藏

TOP

Akka(7): FSM:通过状态变化来转换运算行为(四)
2017-10-09 14:06:32 】 浏览:4156
Tags:Akka FSM 通过 状态 变化 转换 运算 行为
e.replies.reverse
foreach { r ? sender() ! r } if (currentState.stateName != nextState.stateName || nextState.notifies) { this.nextState = nextState handleTransition(currentState.stateName, nextState.stateName) gossip(Transition(self, currentState.stateName, nextState.stateName)) this.nextState = null } currentState = nextState def scheduleTimeout(d: FiniteDuration): Some[Cancellable] = { import context.dispatcher Some(context.system.scheduler.scheduleOnce(d, self, TimeoutMarker(generation))) } currentState.timeout match { case SomeMaxFiniteDuration ? // effectively disable stateTimeout case Some(d: FiniteDuration) if d.length >= 0 ? timeoutFuture = scheduleTimeout(d) case _ ? val timeout = stateTimeouts(currentState.stateName) if (timeout.isDefined) timeoutFuture = scheduleTimeout(timeout.get) } } }

我们用FSM DSL的stay, goto,using来取得新的FSM状态和数据:

 /** * Produce transition to other state. * Return this from a state function in order to effect the transition. * * This method always triggers transition events, even for `A -> A` transitions. * If you want to stay in the same state without triggering an state transition event use [[#stay]] instead. * * @param nextStateName state designator for the next state * @return state transition descriptor */ final def goto(nextStateName: S): State = FSM.State(nextStateName, currentState.stateData) /** * Produce "empty" transition descriptor. * Return this from a state function when no state change is to be effected. * * No transition event will be triggered by [[#stay]]. * If you want to trigger an event like `S -> S` for `onTransition` to handle use `goto` instead. * * @return descriptor for staying in current state */ final def stay(): State = goto(currentState.stateName).withNotification(false) // cannot directly use currentState because of the timeout field

stay,goto返回结果都是State[S,D]类型。using是State类型的一个方法:

   /** * Modify state transition descriptor with new state data. The data will be * set when transitioning to the new state. */ def using(@deprecatedName('nextStateDate) nextStateData: D): State[S, D] = {
      copy(stateData = nextStateData) }

我们看到using的主要作用是把当前状态数据替换成新状态的数据。

Akka的FSM是一个功能强大的Actor类型,所以配备了一套完整的DSL来方便FSM编程。FSM的DSL语句包括:

  final def startWith(stateName: S, stateData: D, timeout: Timeout = None): Unit = currentState = FSM.State(stateName, stateData, timeout) final def goto(nextStateName: S): State = FSM.State(nextStateName, currentState.stateData) final def stay(): State = goto(currentState.stateName).withNotification(false) final def stop(): State = stop(Normal) final def transform(func: StateFunction): TransformHelper = new TransformHelper(func) ...

State[S,D]也提供了一些比较实用的方法函数:

case class State[S, D](stateName: S, stateData: D, timeout: Option[FiniteDuration] = None, stopReason: Option[Reason] = None, replies: List[Any] = Nil) { ... // defined here to be able to override it in SilentState
    def copy(stateName: S = stateName, stateData: D = stateData, timeout: Option[FiniteDuration] = timeout, stopReason: Option[Reason] = stopReason, replies: List[Any] = replies): State[S, D] = { new State(stateName, stateData, timeout, stopReason, replies) } /** * Modify state transition descriptor to include a state timeout for the * next state. This timeout overrides any default timeout set for the next * state. * * Use Duration.Inf to deactivate an existing timeout. */ def forMax(timeout: Duration): State
首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Akka(6): become/unbecome:运.. 下一篇spark获取时间

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目