Scxml and qscxml usage Summary

Source: Internet
Author: User

Recently, I came into contact with the scxml state description text. Simply put, it is a table that describes the entire state change process in XML format. One project in QT Labs is qscxml, which is created based on the upper layer of qstatemachine. It can directly read files in scxml format to generate internal State objects and members, and can directly perform State Changes in QT, which is very convenient.

First, we will briefly introduce the scxml format

<scxml initial="FirstState" version="0.9" xmlns="http://www.w3.org/2005/07/scxml">

As the beginning of the entire scxml, the initial next to the scxml tag indicates the first initialization status after the state machine is started. Here I wrote the firststate, indicating that the status enters the firststate when it is started.

<state id="FirstState" initial="FirstChildNode">

Starting with the State label, it indicates the basic concept of the state. The ID is the index number of the state, which is given to you and then written to the target for indexing. At this time, we can see another initial, at this time, firstchildnode indicates that the firststate is not an atomic state, but a parent state of the combination state. Firstchildnode is its sub-state. That is to say, after you enter the firststate, you will immediately enter the firstchildnode. If you call the <onentry> and <onexit> labels, you will find that you have called the <onentry> and <onexit> labels multiple times, in fact, your status is to enter the innermost layer of a layer. Each layer calls <onentry> and <onexit>.

<transition event="Key.A" target="SecondState"></transition><transition event="Key.B" target="ThirdState"></transition>

The <transition> label indicates the real transaction processing process, and the event attribute indicates the qstring passed to the postnamedevent (const qstring) function in qscxml, so I mentioned that the ID is the global index number. The subsequent <target> is naturally easy to understand, that is, when you pass through the event in this state, the target State is reached. In this exampleNo matter which child you are in the firststate,As long as you receive the key. A event, you will jump out of the sub-State or even the parent State and directly jump to the corresponding secondstate. Note: The translation written in the parent state is shared globally for it and its children. If you think you are not satisfied with a certain event on the child node, you want to rewrite it, then you can write it in firstchildnode.

<transition event="Key.A" target="FourhState"></transition><transition event="Key.B" target="FifthState"></transition>

At this time, the state will give priority to the processing of transactions at the child layer. If the state machine finds that the event is not completed at the child layer (including not finding the translation and finding the translation, but the cond is false) all events are passed up to the parent State for processing.

For more information, see <cond>. This label can be placed in <translation> or <if>, while in <translation>.

<transition event="Key.A" target="FourhState" cond="isTrue"></transition>

It indicates that when istrue is true, the target is actually transferred (here istrue can be a simple variable or a script function to return the bool value ).

<translation event="Key.A"> <if cond="isTrue()">  <script>FuncA()</script><elseif cond="isFalse()"/>  <script>FuncB()</script><else/>  <script>FuncB()</script></if></translation>

 

Indicates that when event key. A comes, Cond judgment is performed to call the corresponding script.

In addition, we can also use the state machine to process events in the upper and lower layers for flexible target dynamic conversion.

<transition event="Key.A" target="A" cond="isTrue()" /><transition event="Key.A" target="B" >

You will surely find that the two translation events are the same. In fact, this usage is also mentioned in W3C examples. Because of the processing mechanism of the state machine, you can have a default target when the asserted value is false, when the value is true, you can enter the target you set in advance. This mechanism can be used flexibly for judgment.

In addition, we will introduce two more important labels, as mentioned above: <onentry> and <onexit>,

<onentry>  <script>    enterState("A");  </script></onentry><onexit>  <script>    exitState("B");  </script>

 

 

This indicates the event automatically triggered when you enter and exit the status. The script called by default can flexibly control the work required for status switching.

Qscxml has a very powerful function.

void QScxml::registerObject (QObject* o, const QString & name, bool recursive)

After registration, you can write various script functions in the scxml file. For example, when you register m_scxml-> registerobject (this, "widget", true ), in this case, you can write

<script>  function show()    {
Widget.show();
 }</script>

It indicates that whether you write the show () function by using the <SCRIPT> tag in translation, onentry, or onexit, you will eventually use the powerful class qscxml to allow you to interact with it. If it is difficult for you to write a script function,

You can also directly write in the <SCRIPT> label

Widget.show();

Can run directly. The strength of the script function is not only that it allows you to interact with your object through qscxml, but also that it can be used to make assertion cond judgment, such

function isTrue(){    return Widget.isGood();   }

You can flexibly implement your own class of assertion functions, and use the cond in the previous translation to achieve dynamic target switching, which is very convenient.

Today, I will briefly introduce it here. When I started to contact scxml, I found that there was very little information in China. I wrote this blog post to contribute my own efforts, more content needs to be mined by yourself. I hope you will like this article, leave your footprints and give me support. Thank you :)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.