首页 > 解决方案 > 如何在 Qt SCXML 状态图中使用条件转换

问题描述

我目前正在尝试了解 Qts scxml 状态图,以及如何将它们正确集成到我的应用程序中。我偶然发现的一个问题是条件转换。为了解释我如何在这里使用条件以及出了什么问题,我做了一个最小的可行示例:

状态图可视化

初始状态s_initial有两个转换到状态s_falses_true。两个转换都是由同一个事件触发的t_button_clicked。根据变量test_var,任何时候都可能只有一个转换。当另一个t_button_clicked事件发生时,状态机返回到s_initial

为了测试状态机,我创建了一个简单的 Qt-Widgets 应用程序,其中一个按钮触发t_button_clicked,一个复选框更改变量test_var

(主窗口.cpp)

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , chart(this)
{
    ui->setupUi(this);
    connect(ui->checkBox, &QCheckBox::clicked, [this](bool checked){
        qDebug() << "> checkbox:" << checked;
        chart.dataModel()->setProperty("test_var", checked);
    });
    connect(ui->pushButton, &QPushButton::released, [this](){
        qDebug() << "> button";
        chart.submitEvent("t_button_clicked");
    });
    chart.start();
}

(testchart.scxml)

<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" xmlns:qt="http://www.qt.io/2015/02/scxml-ext" name="TestChart" qt:editorversion="4.14.1" datamodel="ecmascript" initial="s_initial">
    <qt:editorinfo initialGeometry="213.11;86.67;-20;-20;40;40"/>
    <state id="s_initial">
        <qt:editorinfo scenegeometry="213.11;233.50;153.11;183.50;120;100" geometry="213.11;233.50;-60;-50;120;100"/>
        <transition type="external" event="t_button_clicked" target="s_false" cond="!test_var">
            <qt:editorinfo endTargetFactors="11.79;50.87"/>
        </transition>
        <transition type="external" event="t_button_clicked" target="s_true" cond="test_var">
            <qt:editorinfo movePoint="37.73;-3.06" endTargetFactors="19.26;54.39"/>
        </transition>
        <onentry>
            <log expr="&quot;s_initial&quot;"/>
        </onentry>
    </state>
    <state id="s_false">
        <qt:editorinfo scenegeometry="529.21;233.50;469.21;183.50;120;100" geometry="529.21;233.50;-60;-50;120;100"/>
        <onentry>
            <log expr="&quot;s_false&quot;"/>
        </onentry>
        <transition type="external" event="t_button_clicked" target="s_initial">
            <qt:editorinfo movePoint="3.06;9.18" endTargetFactors="88.28;64.08" startTargetFactors="13.98;61.45"/>
        </transition>
    </state>
    <state id="s_true">
        <qt:editorinfo scenegeometry="529.21;419.09;469.21;369.09;120;100" geometry="529.21;419.09;-60;-50;120;100"/>
        <onentry>
            <log expr="&quot;s_true&quot;"/>
        </onentry>
        <transition type="external" event="t_button_clicked" target="s_initial">
            <qt:editorinfo movePoint="-37.73;6.12" endTargetFactors="68.74;85.18" startTargetFactors="14.04;72.02"/>
        </transition>
    </state>
    <datamodel>
        <data id="test_var" expr="false"/>
    </datamodel>
</scxml>

从 scxml 文件中可以看出,我将日志输出添加到每个 state-onentry 以查看该状态是否已进入。此外,我向按钮和复选框单击添加了调试输出。当我运行应用程序时,控制台输出不是我所期望的:

scxml.statemachine: "" : "s_initial"
> checkbox: false
> button
scxml.statemachine: "" : "s_false"
> button
scxml.statemachine: "" : "s_initial"
> checkbox: true
> button
scxml.statemachine: "" : "s_false"
> button
scxml.statemachine: "" : "s_initial"
> checkbox: false
> button
scxml.statemachine: "" : "s_false"
> button
scxml.statemachine: "" : "s_initial"

值是多少并不重要test_var。状态机总是首先转换到s_false,而不检查我添加的条件保护。据我所知,我在图表中使用了有效的 ecmascript 表达式,并且 scxml 应该能够根据其规范选择正确的转换。我究竟做错了什么?

标签: c++qtstate-machineqtwidgetsscxml

解决方案


  1. 您应该使用setScxmlProperty而不是setProperty

    chart.dataModel()->setScxmlProperty("test_var", checked, "");

  2. 您可以简单地使用_event.data来传递复选框当前值。

    chart.submitEvent("t_button_clicked", ui->checkBox->checked() ? 1:0 );

<scxml datamodel="ecmascript" initial="s_initial" name="TestChart" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
    <state id="s_initial">
        <onentry>
            <log expr="'s_initial'"/>
        </onentry>
        <transition cond="_event.data==1" event="t_button_clicked" target="s_true"/>
        <transition event="t_button_clicked" target="s_false"/>
    </state>
    <state id="s_false">
        <onentry>
            <log expr="'s_false'"/>
        </onentry>
        <transition cond="_event.data==1" event="t_button_clicked" target="s_true"/>
    </state>
    <state id="s_true">
        <onentry>
            <log expr="'s_true'"/>
        </onentry>
        <transition cond="! (_event.data==1)" event="t_button_clicked" target="s_false"/>
    </state>
</scxml>

状态图

PS你可以使用下一个参考资料更好地理解SCXML(我正在推广我自己的网站)

推荐阅读