小菜毛毛技術分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
          http://smc.sourceforge.net/SmcManSec1g.htm 教程第一部分:.sm文件的布局
          1.1任務類(對象)
          SMC生成有限狀態機的對象-而不是進程或應用程序,而是一個單獨的對象
          如果你有對象的接收或者異步回調,以及基于對象的狀態如何應對這些回調,SMC提供了一個強大的解決方案.
          (注:這個例子是基于Java和簡單容易翻譯成其他語言。此外,這個例子假設你知道面向對象編程和有限狀態機(FSM--其實就是一個算法:有限狀態機(Finite State Machine)又稱有限狀態自動機或簡稱狀態機,是表示有限個狀態以及在這些狀態之間的轉移和動作等行為的數學模型。
          在下面這個例子中,這是你開發的一個任務類
          package
          com.acme.supercron;
          public final class
          Task
          implements
          TaskEventListener, TimerEventListener {
          public
          Task() {
          // Object initialization.
           }
          // TaskEventListener Interface Implemenation.
          //Time for the incomplete task to continue its work for the
          specified time slice.
          public void start(long timeSlice)
           }
          // Called when a running, incomplete task should suspend
          //
           running even though its time slice is not expired.
          //
           Note: the task's running is also suspended when the time slice expires.
          public void suspend() {  }

          // Called when an incomplete task is blocked. Blocked tasks are able to continue running when unblocked.
          public void block() {  }

          // Called when a blocked task is unblocked and allowed to continue running.
          public void unblock() {  }

          // Called when an incomplete task is permanently stopped. // Stopped tasks are then deleted.
          public void stop() {  }

          // Called when the task is deleted. Tasks are deleted
          //when 
          // either 1) the task has completed running and is now // stopped or 2) when the system is shutting down and all
          // are to terminate immediately.
          public void delete() {  }

          //end of TaskEventListener Interface Implemenation. //TimerEventListener Interface Implementation.
          //
          Called when the time slice timer expires. If running, the task is suspended.
          public void handleTimeout(TimerEvent event) {  }

          //  end of TimerEventListener Interface Implementation. Remainder of class definition.
           }
          任務類應該如何應對的 開始, 暫停,等方法調用依賴于當前的任務是什么做-也就是說,它取決于任務的狀態。
          1.2有限狀態機的任務
          有限狀態機的任務圖
          Task FSM

          任務的狀態是:

          • 運行(Running): 我們的任務是積極做好工作。 這個任務是允許在一個指定的時間片中運行。
          • 暫停(suspended): 我們的任務是等待再次運行它,因為還沒有完成。
          • 停止(stopped): 任務已完成或停止外部的運行。
          • 阻止(Blocked): 尚未完成的任務,阻止從外部再次運行。 它將留在這個狀態,直到停止或暢通。
          • 停止: 任務停止狀態之前,進行資源的的清理。
          • 刪除: 任務是完全停止,所有相關的資源返回。 該任務現在可以被安全地刪除。 這是有限狀態機的結束狀態
          有限狀態機的一些注意事項:
          • 任務對象開始于暫停狀態(suspended)
          • 在過渡期匹配TaskEventListener接口的方法。
          • 當在停止狀態(stopped)時,要么是任務完成或在外部停止。
          • 當在 Stop , Block and Delete transitions不啟動任何指定的狀態

          現在的問題是:如何把這個FSM狀態機加入到你的任務代碼中?

          這第一步就是編碼有限狀態機使用SMC語言



          1.3創建SMC .sm文件
          The .sm listing below is a skeleton with no states or transitions defined
          下面就列出了.sm文件的基本骨架:

          • %{ %} 對之中的文字或者代碼,將被逐字生成到源代碼中,如版權意見
          • 在 %class 的關鍵字,指與這個FSM關聯任務類
          • 在 %package 關鍵字,指定哪個包下面的類屬于這個FSM。 這同樣是任務類的所在的包
          • 在 %fsmclass 關鍵字指定生成的有限狀態機類的名稱。 如果 %fsmclass 未指定,則有限狀態機類的名稱將默認為 TaskContext,這個關鍵字不是必需的。
          • 在 %access 關鍵字用于指定生成的類的可訪問級別(這只有在生成Java和C#代碼)。 在這種情況下只能在 com.acme.supercron 包訪問FSM。
          • 在 %start 關鍵字指定了FSM的開始狀態。 對于這個例子任務FSM是 suspended 狀態。
          • 在 %map 關鍵字是FSM的名字。

          源代碼文件為 TaskFSM.sm, 因為 %fsmclass 指令指定的有限狀態機的類名是TaskFSM。

          注: 在 %fsmclass 指令加入FSM的版本6.0.1。)








          %{
          // // Copyright (c) 2005 Acme, Inc. // All rights reserved. // // Acme - a name you can trust! // // Author: Wil E. Coyote (Hungericus Vulgarus) //
          %}
          // This FSM works for the Task class only and only the Task // class may instantiate it.
          %class Task
          %package com.acme.supercron
          %fsmclass TaskFSM
          %access package
          %start
          TaskFSM::Suspended
          %map
          TaskFSM 
          %%  %%


          1.4 定義FSM(finite statemachine)的狀態

          狀態機的狀態被定義在%%...%%之間,如下



          %{
          //

          //
           Copyright (c) 2005 Acme, Inc.
          //
           All rights reserved.
          //

          //
           Acme - a name you can trust!
          //

          //
           Author: Wil E. Coyote (Hungericus Vulgarus)
          //

          %}
          // This FSM works for the Task class only and only the Task
          // class may instantiate it. 
          %class Task
          %package com.acme.supercron
          %fsmclass TaskFSM
          %access package
          %start TaskFSM::Suspended
          %map TaskFSM
          %%
          Suspended {  }
          Running {  }
          // Wait here to be either unblocked, stopped or deleted.
          Blocked {  }
          Stopping {  }
           Stopped {  }
          Deleted {  }

          %%
          像C語言那樣,如果只有一句表達式,則括號不是必須的;但是最好帶上括號,不然會出現不好調試的錯誤

          1.5 定義FSM(finite statemachine)有限狀態機的轉變方法

          一個轉變方法包括4部分:
          1.名稱
          2.一個可選的轉變方法的保護
          3.轉變方法的結束狀態
          4.在改轉變方法中的action-行為

          %{ // // Copyright (c) 2005 Acme, Inc.
          // All rights reserved. //
          // Acme - a name you can trust! //
          // Author: Wil E. Coyote (Hungericus Vulgarus) //
          %}
          // This FSM works for the Task class only and only the Task // class may instantiate it.
          %class Task
          %package com.acme.supercron
          %package package
          %fsmclass TaskFSM
          %access package
          %start TaskFSM::Suspended
          %map TaskFSM
          %%
          Suspended {
          // Time to do more work.
          // The timeslice duration is passed in as a transition
          // argument.
          Start(timeslice:long)
          // Transition
          Running
          // End state

          // Actions go here
          }
          }
          Running {
          // Wait for another time slice.
          Suspend Suspended {  }
          // Task has completed.
          Done Stopped {  }
          }
          // Wait here to be either unblocked, stopped or deleted. Blocked {
          // The task may continue working now.
          Unblock Suspended {  }
          }
          Stopping {
          // The task is now stopped.
          Stopped Stopped {  }
          }
          Stopped {  }
          Deleted {  }  %%

          1.6定義FSM(finite statemachine)有限狀態機轉變方法中的行為(方法)Action
          轉變行為是首次將FMS與應用任務類緊密的聯系在一起,Actions是應用的任務類(Task)的方法,這些方法必須有如下特性

          1.能夠訪問狀態機FSM,也就是這個方法必須是公共方法或者于FSM在同一個包下。
          2.必須返回void,否則會被FSM忽視

          SMC的內容沒有語法規則的限制,對于轉變方法(transition)里的參數,除了以()和,結束。


          %{
          //
          // Copyright (c) 2005 Acme, Inc.

          // All rights reserved. //
          // Acme - a name you can trust!
          // // Author: Wil E. Coyote (Hungericus Vulgarus)
          //
          %}
          // This FSM works for the Task class only and only the Task // class may instantiate it.
          %class Task
          %package com.acme.supercron
          %fsmclass TaskFSM
          %access package
          %start TaskFSM::Suspended
          %map TaskFSM
          %%
          Suspended {
          // Time to do more work. // The timeslice duration is passed in as a transition // argument. 
            Start(timeslice: long)
              Running {
                continueTask();
                startSliceTimer(timeslice);
                   }
          }

          Running {
          // Wait for another time slice. Suspend
            Suspended {
               stopSliceTimer();
               suspendTask();
            }
          // Task has completed. Done

            Stopped {
              stopSliceTimer();
              releaseResources();
                   }
           }

          // Wait here to be either unblocked, stopped or deleted.
          Blocked {
          // The task may continue working now.
          // No actions needed.
            Unblock
             Suspended {}
          }

          Stopping {
          // The task is now stopped.
          Stopped
            Stopped {
             releaseResources();
          }

           }

          Stopped {  }

          Deleted {  }

          %%
          以下是轉變行為方法中的action對應應用程序任務類中的方法:
          package com.acme.supercron; public final class Task implements TaskEventListener, TimerEventListener { public Task() { // Object initialization.   }  //----------------------------------------------------------- // TaskEventListener Interface Implemenation. // <snip>  // // end of TaskEventListener Interface Implemenation. //----------------------------------------------------------- //----------------------------------------------------------- // TimerEventListener Interface Implementation. // <snip>  // // end of TimerEventListener Interface Implementation. //-----------------------------------------------------------
          //----------------------------------------------------------- // State Machine Actions. // // Activate the underlying task and get it running again. /* package */
          void
          continueTask() { 
          return;
          }
          // Inactivate the underlying task. /* package */
          void
          suspendTask() { 
          return;
          }
          // Start the timeslice timer for the given milliseconds. /* package */
          void
          startSliceTimer(
          long
          timeslice) { 
          return;
          }
          // Stop the timeslice timer. /* package */
          void
          stopSliceTimer() { 
          return;
          }
          // Return system resources from whence they came. /* package */
          void
          releaseResources() { 
          return;
          }
          // // end of State Machine Actions. //-----------------------------------------------------------
          // Remainder of class definition.  }

          1.7定義FSM(finite statemachine)有限狀態機默認的轉變方法
          現在我們來定義神秘的 Stop, Block and Delete狀態的轉變方法,它們的轉變方法沒有開始狀態的原因是因為無論什么狀態,只要被調用就會執行






          posted on 2010-07-26 17:49 小菜毛毛 閱讀(2405) 評論(0)  編輯  收藏 所屬分類: SMC(stateMachine)

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 定边县| 咸阳市| 离岛区| 瑞金市| 泰来县| 阳泉市| 綦江县| 合作市| 措美县| 崇阳县| 乐清市| 永福县| 红河县| 八宿县| 蚌埠市| 达孜县| 彭山县| 清新县| 佛冈县| 张家港市| 黄大仙区| 麻阳| 石渠县| 乐业县| 九江市| 玛纳斯县| 屏东市| 彭州市| 海门市| 玛沁县| 鹿邑县| 南涧| 通许县| 静乐县| 布尔津县| 密山市| 福建省| 厦门市| 金门县| 虞城县| 北流市|