简体   繁体   中英

Apache Commons SCXML State Machine Framework


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!

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM