开发者

Commons SCXML - Force jump to a given state

I am using Apach开发者_如何学运维e Commons SCXML, and I would like to know if it is possible to tell the state machine (SCXMLExecutor) to jump to a given state.

I can not use the initialstate attribute, because I want the state machine to recover (i.e. from power failures), and the only thing I have is the last state. That is why I was thinking about telling the state machine to make a direct jump to it.


In the general case it's a really bad idea to jump to a state without the state machine's being "aware" of it, because there may be preconditions for a particular state's execution that aren't satisfied (that would be if you reached the state the "normal") way. A better idea is to design the state machine with a "restart" capability, implemented as an input "restart" event and the states and transitions necessary to handle it.


This is an old question, but I just hit this and needed an answer to it as well and thought it might help others to answer it. I am using this as part of unit testing, where it IS extremely useful to just get to a particular state (I want to be sure that if at state A, if a sequence of events happens, it goes to state B - and still goes there after I tinker with the state machine XML!)

I finally found this code in SCXMLTestHelper and it worked. Just call it with the executor and the destination state.

public static void setCurrentState(SCXMLExecutor exec, final String id) throws IllegalArgumentException{
    try {
        exec.reset();
    } catch (ModelException me) {
        throw new IllegalArgumentException("Provided SCXMLExecutor "
                + "instance cannot be reset.");
    }
    TransitionTarget active = (TransitionTarget) exec.getStateMachine().
            getTargets().get(id);
    if (active == null) {
        throw new IllegalArgumentException("No target with id '" + id
                + "' present in state machine.");
    }
    Set current = exec.getCurrentStatus().getStates();
    current.clear();
    current.add(active);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜