문제

I am using Apache 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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top