State Machine Compiler--教程第一部分

State Machine Compiler--教程第一部分
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有限状态机的任务
有限状态机的任务图
State Machine Compiler--教程第一部分_第1张图片

任务的状态是:

  • 运行(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状态的转变方法,它们的转变方法没有开始状态的原因是因为无论什么状态,只要被调用就会执行






你可能感兴趣的:(State Machine Compiler--教程第一部分)