首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在scxml中定义子例程

如何在scxml中定义子例程
EN

Stack Overflow用户
提问于 2020-05-23 11:36:06
回答 1查看 103关注 0票数 0

我试图弄清楚如何用SCXML编码子程序。作为soub例程,我指的是可以从不同的进程(状态)调用并返回给调用者的进程/例程/函数。

起初,我尝试使用历史状态,但返回时调用者状态将重新启动,因此进入循环。这就是素描。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-23 16:26:19

简单地说,就是使用SCXML的invoke标记,它允许解释器生成一个新的SCXML会话。Invoke还可以通过type属性进行扩展,因此如果解释器支持它,它可以生成其他类型的子进程。调用的语义要求父会话在启动被调用会话的状态中等待,直到被调用的会话达到最终状态。您可以在下面看到一个简短的示例,其中使用SCXML的send标记并行调用两个会话并相互通信:

https://jsbin.com/hegiyuk/edit?output

代码语言:javascript
复制
 <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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61971407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档