閱讀893 返回首頁    go iPhone_iPad_Mac_apple


AKKA文檔(Java版)—建立有限狀態機角色

建立有限狀態機角色

概述

有限狀態機模式在Erlang design principles裏麵被很好描述出來.簡而言之,它可以被視為一組關係:
State(S) x Event(E) -> Actions (A), State(S’)

這些關係描述為:

如果我們在狀態S 和 時間E 發生,我們應該執行動作A 與轉換到狀態S’.

而Scala 程序語言使構建一個良好內部DSL(領域特定語言)成為可能,後者用於規劃有限狀態機(請見FSM)。對於用同樣方法,由於Java語法冗長不適合構建。本章節介紹通過自身訓練效實現相同關注點分離方法。

狀態應該如何處理

FSM角色實現引用的所有可變字段(或可傳遞的可變數據結構)應該被收集在一個地方及僅通過使用小粒度的的定義良好的一組方法來改變。要做到這一點的一種實現方法是集合所有可變狀態在一個超類中,並且保持其的私有及為改變其提供保護方法。

1 import java.util.ArrayList;
2 import java.util.List;
3 import akka.actor.ActorRef;

 

01 public abstract class MyFSMBase extends UntypedActor {
02  
03   /*
04    * This is the mutable state of this state machine.
05    */
06   protected enum State {
07     IDLE, ACTIVE;
08   }
09  
10   private State state = State.IDLE;
11   private ActorRef target;
12   private List<Object> queue;
13  
14   /*
15    * Then come all the mutator methods:
16    */
17   protected void init(ActorRef target) {
18     this.target = target;
19     queue = new ArrayList<Object>();
20   }
21  
22   protected void setState(State s) {
23     if (state != s) {
24       transition(state, s);
25       state = s;
26     }
27   }
28  
29   protected void enqueue(Object o) {
30     if (queue != null)
31       queue.add(o);
32   }
33  
34   protected List<Object> drainQueue() {
35     final List<Object> q = queue;
36     if (q == null)
37       throw new IllegalStateException("drainQueue(): not yet initialized");
38     queue = new ArrayList<Object>();
39     return q;
40   }
41  
42   /*
43    * Here are the interrogation methods:
44    */
45   protected boolean isInitialized() {
46     return target != null;
47   }
48  
49   protected State getState() {
50     return state;
51   }
52  
53   protected ActorRef getTarget() {
54     if (target == null)
55       throw new IllegalStateException("getTarget(): not yet initialized");
56     return target;
57   }
58  
59   /*
60    * And finally the callbacks (only one in this example: react to state change)
61    */
62   abstract protected void transition(State old, State next);
63 }

上麵方法好處是狀態改變可以表現在一個中心位置之上,當添加到FSM機器時,這使得它不可能忘記插入代碼對於狀態轉變的反應。

消息集束器的例子

上麵顯示的基類被設計支持一個類似例子作為Scala FSM 文檔:一個接收和排隊消息的角色,分批交付給一個可配置的目標角色。涉及的消息是:

01 public final class SetTarget {
02   final ActorRef ref;
03  
04   public SetTarget(ActorRef ref) {
05     this.ref = ref;
06   }
07 }
08  
09 public final class Queue {
10   final Object o;
11  
12   public Queue(Object o) {
13     this.o = o;
14   }
15 }
16  
17 public final Object flush = new Object();
18  
19 public final class Batch {
20   final List<Object> objects;
21  
22   public Batch(List<Object> objects) {
23     this.objects = objects;
24   }
25 }

該角色僅有兩個狀態 IDLE 和 ACTIVE,使他們處理相當直接在來自於基類的具體角色。

1 import akka.event.LoggingAdapter;
2 import akka.event.Logging;
3 import akka.actor.UntypedActor;

 

01 public class MyFSM extends MyFSMBase {
02  
03   private final LoggingAdapter log =
04     Logging.getLogger(getContext().system(), this);
05  
06   @Override
07   public void onReceive(Object o) {
08  
09     if (getState() == State.IDLE) {
10  
11       if (o instanceof SetTarget)
12         init(((SetTarget) o).ref);
13  
14       else
15         whenUnhandled(o);
16  
17     else if (getState() == State.ACTIVE) {
18  
19       if (o == flush)
20         setState(State.IDLE);
21  
22       else
23         whenUnhandled(o);
24     }
25   }
26  
27   @Override
28   public void transition(State old, State next) {
29     if (old == State.ACTIVE) {
30       getTarget().tell(new Batch(drainQueue()), getSelf());
31     }
32   }
33  
34   private void whenUnhandled(Object o) {
35     if (o instanceof Queue && isInitialized()) {
36       enqueue(((Queue) o).o);
37       setState(State.ACTIVE);
38  
39     else {
40       log.warning("received unknown message {} in state {}", o, getState());
41     }
42   }
43 }

這裏技巧是分解出像whenUnhandled 與transition 這樣的基本功能,以便獲得一些定義良好方麵在對於改變或插入記錄做出的反應。

狀態中心 VS 事件中心

在上麵例子中,狀態和事件的主觀複雜性是大致相等的,使得它看起來是是否選擇主派發的問題;在這個例子中基於狀態的派發器被選中。取決於如何均衡構建狀態與事件可行模型,首先處理不同事件,接著辨別狀態,這中做法可能是更實際。一個例子是一個狀態機,它具有多種內部狀態,但隻處理很少不同事件。

 

最後更新:2017-05-23 11:31:49

  上一篇:go  Java Reflection(十一):動態代理
  下一篇:go  Java中的策略模式實例教程