Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

To write a State machine.

Thread view: 
maadhuu - 19 May 2006 16:23 GMT
Can someone tell me the best way to go about writing a state machine in
Java ?

Thank you
maadhuu
Robert Klemme - 19 May 2006 16:40 GMT
> Can someone tell me the best way to go about writing a state machine in
> Java ?

This smells like a homework assignment...  Here's a start

public class StateMachine {
}

Cheers

    robert
maadhuu - 19 May 2006 17:12 GMT
Well definitely not homework. But that was a good start :-)

maadhuu
Gordon Beaton - 19 May 2006 17:10 GMT
> Can someone tell me the best way to go about writing a state machine in
> Java ?

Sounds like homework, but here's the general idea (no code):

- define a set of classes representing the states, each with a similar
 transition method that receives an event, processes it and returns
 the next state.

- define a class representing the state machine, with a method that
 receives events, invokes the transition method of the current state,
 then changes state accordingly.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Dale King - 19 May 2006 20:20 GMT
> Can someone tell me the best way to go about writing a state machine in
> Java ?

Here is a variety of ways of writing state machines:

http://jerry.cs.uiuc.edu/~plop/plop2003/Papers/Adamczyk-State-Machine.pdf

Signature

 Dale King

rapp@acm.org - 21 May 2006 19:04 GMT
> Can someone tell me the best way to go about writing a state machine in
> Java ?
>
> Thank you
> maadhuu

If depends on the state machine's complexity. If you have a simple
state machine of ~6 states and few transitions, then use an enum to
list the states, a class member to store the current state and switch
statements based on the current state to decide how to respond to an
event based on the current state.

I use this solution when I have a simple "connecting, connected,
disconnecting, disconnected" FSM and transitions between states are
straight forward.

But as the number of states and transitions grow, this mechanism
becomes difficult to correctly implement.  The switch statements are
scattered throughout your event processing code. Adding a new state or
transition requires touching all these switch statements.

The State Pattern is one solution to this problem. Each state is a
class and each transition is a state class' method. So all the FSM code
is collected into these states. The downside is writing code for those
state classes and transition methods. The solution to that problem is
to define your FSM in a separate file and use another program to
generate the State Pattern code for you based on your FSM definition.

The open source application SMC does just that. It allows you to define
complex state machines and SMC generates the State Pattern code in
several  different languages - including Java. You learn about SMC at
http://smc.sourceforge.net. The site contains both a Programmer's
Manual and a FAQ.

Charles Rapp
Thomas Schodt - 22 May 2006 09:06 GMT
> Can someone tell me
> the best way to go about
> writing a state machine in Java?

Don't re-invent - use an existing one, like

http://unimod.sourceforge.net/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.