akka fsm matchEvent实现

时间:2016-10-31 12:39:14

标签: java events akka fsm

我试图了解AKKA fsm" matchEvent" :

public final <ET> FSMStateFunctionBuilder<S, D> matchEvent(Class<ET> eventType, TypedPredicate2<ET, D> predicate, Apply2<ET, D, State<S, D>> apply) {
        return (new FSMStateFunctionBuilder()).event(eventType, predicate, apply);
    }
查看http://doc.akka.io/japi/akka/2.3.4/index.html?akka/actor/AbstractFSM.html后的

这就是我发现的:

public <P> FSMStateFunctionBuilder<S,D> event(java.lang.Class<P> eventType,
                                              FI.TypedPredicate2<P,D> predicate,
                                              FI.Apply2<P,D,FSM.State<S,D>> apply)
  

添加一个与事件类型和谓词匹配的case语句   火柴。参数:eventType - 要在谓词上匹配的事件类型    - 将对数据进行评估并应用事件的谓词 - 如果存在匹配则应用于事件和状态数据的操作   返回:添加了case语句的构建器

有人可以告诉我,事件的匹配是如何实现的?

2 个答案:

答案 0 :(得分:0)

摘自文档simple example

...
public class Buncher extends AbstractFSM<State, Data> {
{
  startWith(Idle, Uninitialized);

  when(Idle,
    matchEvent(SetTarget.class, Uninitialized.class,
      (setTarget, uninitialized) ->
        stay().using(new Todo(setTarget.getRef(), new LinkedList<>()))));

  // transition elided ...

  when(Active, Duration.create(1, "second"),
    matchEvent(Arrays.asList(Flush.class, StateTimeout()), Todo.class,
      (event, todo) -> goTo(Idle).using(todo.copy(new LinkedList<>()))));

  // unhandled elided ...

  initialize();
  }
}

答案 1 :(得分:0)

如果您想根据传入的事件字段或数据(或组合)表达保护条件,请使用类似......的内容:

when(Active, Duration.create(1, "second"),
    matchEvent(Arrays.asList(Flush.class, StateTimeout()), Todo.class,
      (event, todo) -> todo.getQueue != null //any logical condition using event or data, 

      (event, todo) -> goTo(Idle).using(todo.copy(new LinkedList<>()))));