a state machine design question

T

tat

Hi all,

Sorry if this question is off-topic. I would like have your
suggestion .

I need to write one application which control a machine via serial
port. There are 5 states in the following sequence: active, set,
check, command, and clear. In each state, there are two sub-states: do
and wait. For example, in active state: do() sub-state performs the
activation and the sub-state wait() waits for signal from serial port.
If it is either timeout or the message has bad CRC, it goes back to
substate do(). Otherwise, it will go to set state, etc.

I think that it is more straightforward to have 10 different states.
Because in this case I can just use state pattern with one abstract
state, one context and 10 concrete states.

I also think that it is nicer to use 5 states and each has substate.
However, I am kind of confused if you want to do it this way because
active state now plays both a role of context and a role of concrete
state. In this case , there seems to be two different abstract states
(one for sub-states do() and wait(), the other for five states).
I am not really sure what to do in this case. I would be appreciate
any suggestion. If you happen to know a similar example, I would
really appreciate if you let me know.
Thanks,
tat
 
P

Peter Morris

Going for sub-regions is only really useful in my opinion if you wish to be
able to ask

"Which *main* state am I in?" and get an answer like Active, Set, Check, or
Command. For example, if your object is persisted and you wish to find all
objects that are in the state "Active".


If you don't need to do this (which I don't think you will in this example)
then I see no point in having sub-regions instead of one long list of states

ActiveWait
ActiveDo
SetWait
SetDo
etc

which will be easier to write :)



Pete
 
T

Tom Dacon

Think of it this way: would you rather have one state machine to code, or
six? If you use substates, you've got one state machine to handle
transitions among the five main states, and each one of those states has its
own two-state state machine.

In a relatively simple situation like this, I'd do it as a single state
machine. But that's just me.

Tom Dacon
Dacon Software Consulting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top