IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

SCXML Invoke

Invokes a compiled nested state machine.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

SCXML Invoke

Image non disponible

Invoke demonstrates how to use the <invoke> element with generated nested state-machines, where the SCXML file is compiled to a C++ class. The <invoke> element is used to create an instance of an external service.

Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Invoking the State Machine

In statemachine.scxml, we specify a state machine with the name DirectionsStateMachine of type http://www.w3.org/TR/scxml/ to invoke:

 
Sélectionnez
&lt;scxml
    xmlns="http://www.w3.org/2005/07/scxml"
    version="1.0"
    name="DirectionsStateMachine"
    initial="anyplace"
&gt;
    &lt;state id="anyplace"&gt;
        &lt;transition event="goNowhere" target="nowhere"/&gt;
        &lt;transition event="goSomewhere" target="somewhere"/&gt;

        &lt;state id="nowhere"/&gt;
        &lt;state id="somewhere"&gt;
            &lt;invoke type="http://www.w3.org/TR/scxml/"&gt;
                &lt;content&gt;
                    &lt;scxml name="anywhere" version="1.0"&gt;
                        &lt;state id="here"&gt;
                            &lt;transition event="goThere" target="there"/&gt;
                        &lt;/state&gt;
                        &lt;state id="there"&gt;
                            &lt;transition event="goHere" target="here"/&gt;
                        &lt;/state&gt;
                    &lt;/scxml&gt;
                &lt;/content&gt;
            &lt;/invoke&gt;
        &lt;/state&gt;
    &lt;/state&gt;
&lt;/scxml&gt;

Compiling the State Machine

We link against the Qt SCXML module by adding the following lines to the example's build files.

invoke.pro when using qmake:

 
Sélectionnez
QT += qml scxml

We then specify the state machine to compile:

 
Sélectionnez
STATECHARTS = statemachine.scxml

CMakeLists.txt when using cmake:

 
Sélectionnez
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Scxml)

target_link_libraries(invoke PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Qml
    Qt6::Scxml
)

We then specify the state machine to compile:

 
Sélectionnez
qt6_add_statecharts(invoke
    statemachine.scxml
)

The statechart directives STATECHARTS or qt6_add_statecharts invoke the Qt SCXML Compiler, qscxmlc, which is run automatically to generate statemachine.h and statemachine.cpp, which are then added appropriately as headers and sources for compilation.

Declaring the state machine as QML element

The state machine is declared as a QML element as follows:

 
Sélectionnez
struct DirectionsStateMachineRegistration
{
    Q_GADGET
    QML_FOREIGN(DirectionsStateMachine)
    QML_NAMED_ELEMENT(DirectionsStateMachine)
    QML_ADDED_IN_VERSION(1, 0)
};

Instantiating the State Machine

We instantiate the generated DirectionsStateMachine element in the MainView.qml file, as follows:

 
Sélectionnez
    DirectionsStateMachine {
        id: stateMachine
        running: true
    }

Example project

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+