scxml - 如何在scxml中定义一个子程序
问题描述
我试图弄清楚如何在 SCXML 中编码子例程。作为子程序,我的意思是可以从不同的进程(状态)调用并返回给调用者的进程/例程/函数。
起初我尝试使用历史状态,但返回时调用者状态将重新启动,因此进入循环。那是草图。
<scxml>
<final id="Final">
<onexit>
<transition target="ProcA" />
<transition target="ProcB" />
</onexit>
</final>
<state id="Sub">
<transition target="History" />
</state>
<state>
<history id="History" />
<final id="ProcA">
<onexit>
<transition target="Sub" />
</onexit>
</final>
<final id="ProcB">
<onexit>
<transition target="Sub" />
</onexit>
</final>
</state>
</scxml>
解决方案
简短的回答是使用 SCXML 的调用标签,它允许解释器产生一个新的 SCXML 会话。Invoke 也可以通过 type 属性进行扩展,因此如果解释器支持,它可以生成其他类型的子进程。调用的语义要求父会话在被调用会话开始的状态下等待,直到被调用会话到达最终状态。您可以在此处查看并行调用的两个会话并使用 SCXML 的发送标记相互通信的简短示例:
https://jsbin.com/hegiyuk/edit?output
<scxml
datamodel="ecmascript"
xmlns="http://www.w3.org/2005/07/scxml"
version="1.0">
<parallel id="p">
<state id="1">
<invoke id="session_1">
<content>
<scxml
datamodel="ecmascript"
xmlns="http://www.w3.org/2005/07/scxml"
version="1.0">
<state id="session-1-foo">
<onentry>
<log label="here1"/>
<send event="ping-from-1" delay="2s" target="#_session_2"/>
</onentry>
<transition event="ping-from-2" target="session-1-bar"/>
</state>
<state id="session-1-bar">
<onentry>
<log label="here3"/>
<send event="pong-from-1" delay="2s" target="#_session_2"/>
</onentry>
<transition event="pong-from-2" target="session-1-foo"/>
</state>
</scxml>
</content>
</invoke>
</state>
<state id="2">
<invoke id="session_2">
<content>
<scxml
datamodel="ecmascript"
xmlns="http://www.w3.org/2005/07/scxml"
version="1.0">
<state id="session-2-foo">
<onentry>
<log label="here2"/>
<send event="ping-from-2" delay="2s" target="#_session_1"/>
</onentry>
<transition event="ping-from-1" target="session-2-bar"/>
</state>
<state id="session-2-bar">
<onentry>
<log label="here4"/>
<send event="pong-from-2" delay="2s" target="#_session_1"/>
</onentry>
<transition event="pong-from-1" target="session-2-foo"/>
</state>
</scxml>
</content>
</invoke>
</state>
</parallel>
</scxml>
推荐阅读
- math - 贴花的感知宽度取决于墙壁的旋转角度
- angular - 从角度应用程序拨打电话时无法识别 Cookie
- excel - 奇怪的 ActiveX 组合框和按钮行为
- macos - 在 m1 MAC OS 上构建 Bitcoind 时出错
- java - 为什么使用存储在 HDFS 中的文件时显示“(没有此类文件或目录)”?
- selenium - 我得到元素不可交互异常
- azure-data-factory - NULL 从管道传递到链接服务 baseURL
- python - Google Calender API 仅获取日历的第一个事件
- caching - 其中一个 CPU 中是否缺少 L1 缓存?(HP ProLiant 服务器,双 Xeon E5-2620)
- amazon-web-services - Waiter ChangeSetCreateComplete failed: Waiter 遇到终端故障状态