首页 > 解决方案 > 如何在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 的调用标签,它允许解释器产生一个新的 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>

推荐阅读