本文共 6977 字,大约阅读时间需要 23 分钟。
http://smc.sourceforge.net/SmcManSec1g.htm
教程第一部分:.sm文件的布局
,如果你有对象的接收或者异步回调,以及基于对象的状态如何应对这些回调,SMC提供了一个强大的解决方案.(注:这个例子是基于Java和简单容易翻译成其他语言。此外,这个例子假设你知道面向对象编程和有限状态机(FSM--其实就是一个算法:有限状态机(Finite State Machine)又称有限状态自动机或简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型。)
在下面这个例子中,这是你开发的一个任务类
packagecom.acme.supercron;publicfinalclassTaskimplementsTaskEventListener, TimerEventListener {publicTask() {//Object initialization. }//TaskEventListener Interface Implemenation.//Time for the incomplete task to continue its work for thespecified time slice.publicvoidstart(longtimeSlice)
{
}//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 timeslice expires.publicvoidsuspend() {
}//Called when an incomplete task is blocked. Blocked tasksare able to continue running when unblocked.publicvoidblock() {
}//Called when a blocked task is unblocked and allowedto continue running.publicvoidunblock() {
}//Called when an incomplete task is permanently stopped.//Stopped tasks are then deleted.publicvoidstop() {
}//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.publicvoiddelete() {
}//end of TaskEventListener Interface Implemenation.//TimerEventListener Interface Implementation.//Called when the time slice timer expires. If running,the task is suspended.publicvoidhandleTimeout(TimerEvent event) {
}//end of TimerEventListener Interface Implementation.Remainder of class definition. }
任务类应该如何应对的开始,暂停,等方法调用依赖于当前的任务是什么做-也就是说,它取决于任务的状态。
任务的状态是:
运行(Running):我们的任务是积极做好工作。 这个任务是允许在一个指定的时间片中运行。
暂停(suspended):我们的任务是等待再次运行它,因为还没有完成。
停止(stopped):任务已完成或停止外部的运行。
阻止(Blocked):尚未完成的任务,阻止从外部再次运行。 它将留在这个状态,直到停止或畅通。
停止:任务停止状态之前,进行资源的的清理。
删除:任务是完全停止,所有相关的资源返回。 该任务现在可以被安全地删除。 这是有限状态机的结束状态
有限状态机的一些注意事项:在任务对象开始于暂停状态(suspended)。
在过渡期匹配TaskEventListener接口的方法。
当在停止状态(stopped)时,要么是任务完成或在外部停止。
当在Stop , Block and Delete transitions不启动任何指定的状态
现在的问题是:如何把这个FSM状态机加入到你的任务代码中?
这第一步就是编码有限状态机使用SMC语言
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.%classTask%packagecom.acme.supercron%fsmclass TaskFSM%accesspackage%start
TaskFSM::Suspended%map
TaskFSM%%%%
%{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.%classTask%packagecom.acme.supercron%fsmclass TaskFSM%accesspackage%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对应应用程序任务类中的方法:
packagecom.acme.supercron;publicfinalclassTaskimplementsTaskEventListener, TimerEventListener {publicTask() {//Object initialization.
}//-----------------------------------------------------------//TaskEventListener Interface Implemenation.//end of TaskEventListener Interface Implemenation.//-----------------------------------------------------------//-----------------------------------------------------------//TimerEventListener Interface Implementation.//end of TimerEventListener Interface Implementation.//-----------------------------------------------------------//-----------------------------------------------------------//State Machine Actions.Activate the underlying task and get it running again. /* package */voidcontinueTask() {
return;
}//Inactivate the underlying task. /* package */voidsuspendTask() {
return;
}//Start the timeslice timer for the given milliseconds. /* package */voidstartSliceTimer(longtimeslice) {
return;
}//Stop the timeslice timer. /* package */voidstopSliceTimer() {
return;
}//Return system resources from whence they came. /* package */voidreleaseResources() {
return;
}end of State Machine Actions.//-----------------------------------------------------------//Remainder of class definition.
}
Stop, Block and Delete状态的转变方法,它们的转变方法没有开始状态的原因是因为无论什么状态,只要被调用就会执行
转载地址:https://blog.csdn.net/weixin_34190683/article/details/114558542 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!