Java的StateMachine(一)說明文件

magic_dreamer發表於2010-06-30
Java的StateMachine(一)說明文件

首頁
http://smc.sourceforge.net/

圖形化工具
http://www.graphviz.org/

I download the Smc version 6.0.1.

smc.pdf Introduction of the State Machine Compiler

FSM(Finite State Machine) is everywhere.
A input source .sm(yacc-like syntax), a output source in your language like java or python.

1.conceptions
A Simple Transition
//State
Idle {
// Trans Next State Actions
Run Running {}
}

A Reflexive Transition
//State
Idle{
// Trans Next State Actions
Timeout Idle {}
}

A Internal Event
// State
Idle {
//Trans Next State Actions
Timeout nil {}
}

A Transition with Actions
// State
Idle{
//Trans
Run
//Actions
{
StopTimer("Idle");
DoWork();
}
}

Transition Guards
conditions that must be met in order for transitions to proceed
// State
Idle{
//Trans
Run
//Guard condition
[ctxt.isValid()]
//Next State
Running
//Actions
{
StopTimer("Idle");
DoWork();
}
Run Idle {RejectRequest();}
}

Transition Arguments
// State
Idle{
//Transition
Run(msg:const Message&)
//Guard condition
[msg.isValid()]
//Next State
Running
//Actions
{
StopTimer("Idle");
DoWork(msg);
}
Run(msg:const Message&)
//Next State Actions
Idle { RejectRequest(msg);}
}

Entry and Exit Actions
Actions performed every time we enter and exit a state,respectively
// State
Idle
Entry {StartTimer("Idle",1); CheckQueue();}
Exit {StopTimer("Idle");}
{
//Transitions
}

2.A case study: a Telephone
demo url:
http://smc.sourceforge.net/SmcDemo.htm

SMC_Tutorial.pdf book
An example of simple turnstile:

1.Description of the normal operation of a turnstile:
Turnstile initially locked
A coin or ticket is inserted(event)
triggers action: turnstile unlocks
A person passes through turnstile(event)
triggers action: turnstile locks again

most important concepts:States,Transitions and Actions

Transition table for turnstile

State Transition Next State Action
Locked coin Unlocked unlock
pass Locked alarm
Unlocked coin Unlocked thankyou
pass Locked lock

I use a sm file to show the workflow Turnstile.sm:
%class Turnstile
%package turnstile

%start MainMap::Locked

%map MainMap
%%
Locked
{
coin Unlocked { unlock(); }
pass nil { alarm(); }
}
Unlocked
{
pass Locked { lock(); }
coin nil { thankyou(); }
}
%%

And I put a bat file on Windows to compile the sm file to java output, BuildSm.bat:
java -jar smc-6.0.1.jar -java -d turnstile Turnstile.sm
rem -jar smc-6.0.1.jar The SMC Compiler
rem -java Specify java language output
rem -d turnstile Optionally specify target directory
rem turnstile.sm Specify the .sm file to process

After we run the bat shell, we get a single file named TurnstileContext.java and we get many classes:
TurnstileContext$MainMap.class
TurnstileContext$MainMap_Default$MainMap_Locked.class
TurnstileContext$MainMap_Default$MainMap_Unlocked.class
TurnstileContext$MainMap_Default.class
TurnstileContext$TurnstileState.class
TurnstileContext.class

Write the AppClass Turnstile.java:
package com.sillycat.sm.turnstile;

public class Turnstile implements TurnstileActions{

TurnstileContext _fsm;
TurnstileActions _actions;

public Turnstile(TurnstileActions actions){
_fsm = new TurnstileContext(this);
_actions = actions;
}

public void coin(){_fsm.coin();}
public void pass(){_fsm.pass();}
public void alarm(){_actions.alarm();}
public void lock() {_actions.lock();}

public void thankyou() {_actions.thankyou();}
public void unlock() {_actions.unlock();}
}

TurnstileActions.java:
package com.sillycat.sm.turnstile;

public interface TurnstileActions {
public void coin();
public void pass();
public void alarm();
public void lock();
public void thankyou();
public void unlock();
}

And we can build sm file into a .dot file and view the picturn of the workflow,GraphSm.bat:
java -jar smc-6.0.1.jar -graph -glevel 1 Turnstile.sm

we can get a Turnstile_sm.dot file after we run the command, and we can install graphviz-2.26.3.msi from URL
http://www.graphviz.org/

And we use Gvedit to open the file Turnstile_sm.dot and then we can see the picture.

相關文章