代码之家  ›  专栏  ›  技术社区  ›  Guido

commons scxml-强制跳转到给定状态

  •  2
  • Guido  · 技术社区  · 15 年前

    我正在使用 Apache Commons SCXML 我想知道是否可以告诉状态机(scxmleexecutor)跳到给定的状态。

    我不能用 initialstate 属性,因为我希望状态机能够恢复(即从电源故障中恢复),而我唯一拥有的就是最后一个状态。这就是为什么我想让状态机直接跳到它上面。

    2 回复  |  直到 12 年前
        1
  •  3
  •   Jim Garrison    15 年前

    在一般情况下,跳到一个没有状态机“意识到”的状态是一个非常糟糕的主意,因为对于一个特定状态的执行可能有一些不满意的先决条件(如果你以“正常”的方式到达状态的话)。更好的想法是设计具有“重启”功能的状态机,作为输入“重启”事件实现,以及处理它所需的状态和转换。

        2
  •  2
  •   Evan Reynolds    12 年前

    这是一个古老的问题,但我只是碰到了这个问题,我也需要一个答案,并且认为这可能有助于其他人回答它。我将此作为单元测试的一部分使用,在单元测试中,到达特定的状态非常有用(我想确保,如果在状态A,如果发生一系列事件,它将进入状态B,并且在修补状态机XML之后仍然会进入状态B!)

    我终于在scxmltesthelper中找到了这段代码,它工作正常。只需使用执行器和目标状态来调用它。

    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);
    }