Java的StateMachine(二)More Examples

magic_dreamer發表於2010-06-30
Java的StateMachine(二)More Examples

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

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

I download the Smc version 6.0.1.

we just have the examples of java one by one

EXAMPLE 1
AppClass.sm is just what the example like, I never change it, it seems that it stands for the 00011110000 things.
AppClass.java I add some comments on the key point :
_fsm.setDebugFlag(true);
I set the debug on to see the logs.

I think the most import part is here:
public void Acceptable() {
// here is the real action we want to write in App, and it will be
// called by AppClassContext
_is_acceptable = true;
}

And I make a little test file AppClassTest.java:
package com.sillycat.sm.appclass;

public class AppClassTest {

public static void main(String[] args) {
String str = "0001111";

AppClass appobject = new AppClass();

System.out.print("The string \"");
System.out.print(str);
System.out.print("\" is ");
if (appobject.CheckString(str) == false) {
System.out.println("not acceptable.");
} else {
System.out.println("acceptable.");
}
}
}

And we get the output like this:
The string "0001111" is
ENTER STATE : Map1.Zeros
ENTER STATE : Map1.Zeros
ENTER STATE : Map1.Zeros
ENTER STATE : Map1.Ones
ENTER STATE : Map1.Ones
ENTER STATE : Map1.Ones
ENTER STATE : Map1.Ones
ENTER STATE : Map1.OK
acceptable.

EXAMPLE 2

We add a new Default transaction.

EXAMPLE 3
This state machine "recognizes" the palindromes (words that read the same backwards as forwards). The words consist of the alphabet {0, 1, c} where the letter 'c' may appear only once and marks the words center.

I make a test java file AppClassTest.java:
package com.sillycat.sm.appclass;

public class AppClassTest {

public static void main(String[] args) {
String str = "0c0";

AppClass appobject = new AppClass();

System.out.print("The string \"");
System.out.print(str);
System.out.print("\" is ");
if (appobject.CheckString(str) == false) {
System.out.println("not acceptable.");
} else {
System.out.println("acceptable.");
}
}
}

And we get the output like this:
The string "0c0" is
PUSH TO STATE : ZerosMap.PushIt
ENTER STATE : ZerosMap.PopIt
POP TO STATE : StartMap.PushIt
ENTER STATE : StartMap.PopIt
ENTER STATE : StartMap.Acceptable
acceptable.


EXAMPLE 7

this example stands for the telephones.

After all the work, I found that our project use the version 4.3, so there must be some small differences.

相關文章