0


I'm attempting to define a state machine for a Java application using Apache SCXML. However, I've run into a problem and I'm not sure if this is due to the SCXML framework or me doing something wrong.
I'm basing my test application on the following example (without the android bit):
http://commons.apache.org/scxml/usecases/scxml-stopwatch-on-android.html

The file StopWatch.java (http://commons.apache.org/scxml/xref-test/org/apache/commons/scxml/env/StopWatch.html)

public class StopWatch extends AbstractStateMachine {
    public void reset() {

    }

    public void running() {
    }

    public void paused() {
    }

    public void stopped() {
    }
}

The problem is the above states are only called once per transition. Is this correct? Shouldn't the state function be called continuously as long as the state machine remains in the given state?

Thanks!

3 Answers3

3

Hi just in case other people find this question.

The above example only works in the context of the defined state machine example.

The states do not transition automatically as they are guarded by events. So only if the state machine is in state A and the defined transition event is fired will the state machine advance. This can be seen in the snippet below

<state id="reset">
    <transition event="watch.start" target="running"/>
</state>

As an additional note, the execution of the method with the same name as the state as defined in the StopWatch example is guarded by an EventListener defined in the AbstractStateMachine itself. As part of the initialize method a new Listener is registered.

engine.addListener(stateMachine, new EntryListener());

This listener invokes the method with the corresponding state name onEntry to the new state

public void onEntry(final TransitionTarget entered) {
    invoke(entered.getId());
}

So if you want your state to be continuously called, you just need to remove the transition guards in the state machine (SCXML) description.

Joey
  • 1,349
  • 14
  • 26
0

Why would you expect this behavior? Your state class only needs to know about the transition. Once you've transitioned, you're in a steady state.

Jim Barrows
  • 3,634
  • 1
  • 25
  • 36
  • Hi - thanks for your answer. I've always thought of a state machine as being composed of a while loop and a switch statement i.e. i expect the current state to be executed as long as it's valid - see the answer to http://stackoverflow.com/questions/133214/is-there-a-typical-state-machine-implementation-pattern i could of course have a while loop inside the states above, but i don't think this is the correct thing to do, I think the framework probably expects the state functions to return after handling the transition events. –  Sep 11 '09 at 21:21
0

your are misunderstand the behavior of state machine. the essence of state is during sometimes the state machine object will satisfy certain conditions to perform certain activities or wait for some event . of course you can do you described by defined the loop in the function in stopwatch class ,but is there any significance ?,the running function have a timer thread and a timer task can be regard as a loop, isn't it? the running state execute the thread task,and just wait event to exit this state and stop the task.