0


我正在尝试使用 Apache SCXML 为 Java 应用程序定义状态机。但是,我遇到了一个问题,我不确定这是由于 SCXML 框架还是我做错了什么。
我的测试应用程序基于以下示例(没有 android 位):
http ://commons.apache.org/scxml/usecases/scxml-stopwatch-on-android.html

文件 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() {
    }
}

问题是上述状态每次转换只调用一次。这个对吗?只要状态机保持在给定状态,状态函数不应该被连续调用吗?

谢谢!

4

3 回答 3

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.

于 2011-02-16T23:04:48.877 回答
0

你为什么会期待这种行为?你的状态类只需要知道转换。一旦你转变了,你就处于稳定状态。

于 2009-09-11T19:54:59.027 回答
0

您误解了状态机的行为。状态的本质是在有时状态机对象会满足某些条件来执行某些活动或等待某个事件。当然你可以通过在秒表类的函数中定义循环来描述,但是有什么意义吗?,正在运行的函数有一个定时器线程,一个定时器任务可以看作是一个循环,不是吗?运行状态执行线程任务,等待事件退出该状态并停止任务。

于 2015-11-28T02:15:46.047 回答