本文介绍了是否可以在内部监控Akka FSM中向第一状态的过渡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑从状态Idle开始的FSM参与者

Considering an FSM actor that starts at state Idle

startWith(Idle, IdleData)

我想监视向第一个状态的转换(从无状态开始?)

I'd like to monitor the transition to this first state (from no state?)

我尝试

onTransition {
    case _ -> Idle => // Wasn't called
}

根据相关的文档:



onTransition(handler _)

def handler(from: StateType, to: StateType) {
  // handle it here ...
}

鉴于from类型是StateType而不是Option [StateType],我认为这可能是不可能的,但也许我遗漏了一些东西。

Given that the from type is StateType and not Option[StateType] I think that it may not be possible, but maybe I'm missing something.

推荐答案

我最近在搜索类似的内容。

I was searching for something similar recently.

如果我正确理解了您的问题,这是一种监视从初始开始使用FSM的方法状态:

If I understand your question correctly, Here is one way to monitor when you start off your FSM from the initial state:

class ExampleFSM extends Actor with FSM[State, Data] {
  // A FSM begins its existence in Idle state and can move to different states
  startWith(Idle, NoData)

  when(Idle) {
    case Event(SetData(something), NoData) =>
      goto(SomeOtherState) using Data(something)
  }

  onTransition {
    case Idle -> Idle =>
      stateData match {
        case _ =>
          println("Initial transition")
      }
  }
}

在上面的示例中,当我们实例化FSM传递消息 SetData(something)时,它开始于状态 Idle ,您可以监视一个过渡,该过渡是 Idle->空闲

In the above example, When We instantiate the FSM pass-in the message SetData(something), It starts at the state Idle and there is a transition that you can monitor which is Idle -> Idle.

在上述情况下,当我们启动FSM时,可以看到输出初始过渡打印出来,您可以根据需要利用它来做更复杂的事情

In the above case when we start the FSM we can see an output Initial transition printed, you can leverage this to do more complex stuff as per your needs

因此基本上,如果您从情况_更新现有的过渡, =>空闲 case空闲->空闲应该可以工作

So basically if you update your existing transition from case _ => Idle to case Idle -> Idle it should work

注意:可能有多种方法可以做到这一点,我仍在探索Akka FSM的强大功能,因此我的答案只是找到一种可能的方式。

Note: There might be more than one way to do this, I am also still exploring the power of Akka FSM so my answer is just having one possible way of getting this.

这篇关于是否可以在内部监控Akka FSM中向第一状态的过渡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:38