java state machine_State Machine Compiler--教程第一部分
发布日期:2021-06-24 17:42:00 浏览次数:2 分类:技术文章

本文共 6977 字,大约阅读时间需要 23 分钟。

http://smc.sourceforge.net/SmcManSec1g.htm

教程第一部分:.sm文件的布局

,如果你有对象的接收或者异步回调,以及基于对象的状态如何应对这些回调,SMC提供了一个强大的解决方案.(注:这个例子是基于Java和简单容易翻译成其他语言。此外,这个例子假设你知道面向对象编程和有限状态机(FSM--其实就是一个算法:有限状态机(Finite State Machine)又称有限状态自动机或简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型。)

在下面这个例子中,这是你开发的一个任务类

packagecom.acme.supercron;publicfinalclassTaskimplementsTaskEventListener, TimerEventListener {publicTask() {//Object initialization.9b8a8a44dd1c74ae49c20a7cd451974e.png }//TaskEventListener Interface Implemenation.//Time for the incomplete task to continue its work for thespecified time slice.publicvoidstart(longtimeSlice)

{

9b8a8a44dd1c74ae49c20a7cd451974e.png }//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() { 

9b8a8a44dd1c74ae49c20a7cd451974e.png }//Called when an incomplete task is blocked. Blocked tasksare able to continue running when unblocked.publicvoidblock() { 

9b8a8a44dd1c74ae49c20a7cd451974e.png }//Called when a blocked task is unblocked and allowedto continue running.publicvoidunblock() { 

9b8a8a44dd1c74ae49c20a7cd451974e.png }//Called when an incomplete task is permanently stopped.//Stopped tasks are then deleted.publicvoidstop() { 

9b8a8a44dd1c74ae49c20a7cd451974e.png }//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() {

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

9b8a8a44dd1c74ae49c20a7cd451974e.png }//end of TimerEventListener Interface Implementation.Remainder of class definition.9b8a8a44dd1c74ae49c20a7cd451974e.png }

任务类应该如何应对的开始,暂停,等方法调用依赖于当前的任务是什么做-也就是说,它取决于任务的状态。

978707adc8a524cb8e9d4f80b8cab1b9.png任务的状态是:

运行(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%%9b8a8a44dd1c74ae49c20a7cd451974e.png%%

%{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 {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

Running {

9b8a8a44dd1c74ae49c20a7cd451974e.png }//Wait here to be either unblocked, stopped or deleted.Blocked { 

9b8a8a44dd1c74ae49c20a7cd451974e.png }

Stopping {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

Stopped {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

Deleted {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

9b8a8a44dd1c74ae49c20a7cd451974e.png%%

像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

{

9b8a8a44dd1c74ae49c20a7cd451974e.png

// Actions go here

}

}

Running {

// Wait for another time slice.

Suspend Suspended {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

// Task has completed.

Done Stopped {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

}

// Wait here to be either unblocked, stopped or deleted. Blocked {

// The task may continue working now.

Unblock Suspended {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

}

Stopping {

// The task is now stopped.

Stopped Stopped {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

}

Stopped {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

Deleted {

9b8a8a44dd1c74ae49c20a7cd451974e.png } 

9b8a8a44dd1c74ae49c20a7cd451974e.png %%

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 {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

Deleted {

9b8a8a44dd1c74ae49c20a7cd451974e.png }

9b8a8a44dd1c74ae49c20a7cd451974e.png

%%

以下是转变行为方法中的action对应应用程序任务类中的方法:

packagecom.acme.supercron;publicfinalclassTaskimplementsTaskEventListener, TimerEventListener {publicTask() {//Object initialization.

9b8a8a44dd1c74ae49c20a7cd451974e.png }//-----------------------------------------------------------//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() { 

9b8a8a44dd1c74ae49c20a7cd451974e.pngreturn;

}//Inactivate the underlying task. /* package */voidsuspendTask() {

9b8a8a44dd1c74ae49c20a7cd451974e.pngreturn;

}//Start the timeslice timer for the given milliseconds. /* package */voidstartSliceTimer(longtimeslice) {

9b8a8a44dd1c74ae49c20a7cd451974e.pngreturn;

}//Stop the timeslice timer. /* package */voidstopSliceTimer() {

9b8a8a44dd1c74ae49c20a7cd451974e.pngreturn;

}//Return system resources from whence they came. /* package */voidreleaseResources() {

9b8a8a44dd1c74ae49c20a7cd451974e.pngreturn;

}end of State Machine Actions.//-----------------------------------------------------------//Remainder of class definition.

9b8a8a44dd1c74ae49c20a7cd451974e.png }

Stop, Block and Delete状态的转变方法,它们的转变方法没有开始状态的原因是因为无论什么状态,只要被调用就会执行

转载地址:https://blog.csdn.net/weixin_34190683/article/details/114558542 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java io有哪些实现类_广州Java面试宝典之Java IO篇
下一篇:java class .method_在Java中,this.method()和method()之间有什么区别?

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月17日 13时32分35秒