summaryrefslogtreecommitdiffstats
path: root/src/qscxml/scxmlstatemachine.h
blob: 41b867499bd8b0a000ba292ad6ea2e9d4bcda469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/****************************************************************************
 **
 ** Copyright (c) 2015 Digia Plc
 ** For any questions to Digia, please use contact form at http://qt.digia.com/
 **
 ** All Rights Reserved.
 **
 ** NOTICE: All information contained herein is, and remains
 ** the property of Digia Plc and its suppliers,
 ** if any. The intellectual and technical concepts contained
 ** herein are proprietary to Digia Plc
 ** and its suppliers and may be covered by Finnish and Foreign Patents,
 ** patents in process, and are protected by trade secret or copyright law.
 ** Dissemination of this information or reproduction of this material
 ** is strictly forbidden unless prior written permission is obtained
 ** from Digia Plc.
 ****************************************************************************/

#ifndef SCXMLSTATEMACHINE_H
#define SCXMLSTATEMACHINE_H

#include <QScxml/datamodel.h>
#include <QScxml/executablecontent.h>
#include <QScxml/scxmlevent.h>

#include <QState>
#include <QFinalState>
#include <QAbstractState>
#include <QAbstractTransition>
#include <QHash>
#include <QString>
#include <QStringList>
#include <QVector>
#include <QXmlStreamAttributes>
#include <QRegExp>
#include <QStateMachine>
#include <QJSEngine>
#include <QPointer>
#include <QSharedPointer>
#include <QMetaMethod>
#include <QJsonValue>
#include <QSignalTransition>
#include <QLoggingCategory>
#include <QAbstractTransition>
#include <QUrl>
#include <QVariantList>
#include <functional>

QT_BEGIN_NAMESPACE
class QXmlStreamWriter;
class QTextStream;
class QDebug;

namespace Scxml {
SCXML_EXPORT Q_DECLARE_LOGGING_CATEGORY(scxmlLog)

class ScxmlParser;

namespace ExecutableContent {
class ExecutionEngine;
}

class SCXML_EXPORT TableData
{
public:
    virtual ~TableData();

    virtual QString string(ExecutableContent::StringId id) const = 0;
    virtual QByteArray byteArray(ExecutableContent::ByteArrayId id) const = 0;
    virtual ExecutableContent::Instructions instructions() const = 0;
    virtual EvaluatorInfo evaluatorInfo(EvaluatorId evaluatorId) const = 0;
    virtual AssignmentInfo assignmentInfo(EvaluatorId assignmentId) const = 0;
    virtual ForeachInfo foreachInfo(EvaluatorId foreachId) const = 0;
    virtual ExecutableContent::StringId *dataNames(int *count) const = 0;

    virtual ExecutableContent::ContainerId initialSetup() const = 0;
    virtual QString name() const = 0;
};

class Q_QML_EXPORT ScxmlError
{
public:
    ScxmlError();
    ScxmlError(const QString &fileName, int line, int column, const QString &description);
    ScxmlError(const ScxmlError &);
    ScxmlError &operator=(const ScxmlError &);
    ~ScxmlError();

    bool isValid() const;

    QString fileName() const;
    int line() const;
    int column() const;
    QString description() const;

    QString toString() const;

private:
    class ScxmlErrorPrivate;
    ScxmlErrorPrivate *d;
};

QDebug Q_QML_EXPORT operator<<(QDebug debug, const ScxmlError &error);

class StateMachinePrivate;
class SCXML_EXPORT StateMachine: public QObject
{
    Q_OBJECT
    Q_ENUMS(BindingMethod)

public:
    enum BindingMethod {
        EarlyBinding,
        LateBinding
    };

    static StateMachine *fromFile(const QString &fileName, DataModel *dataModel = Q_NULLPTR);
    static StateMachine *fromData(QIODevice *data, DataModel *dataModel = Q_NULLPTR);
    QVector<ScxmlError> errors() const;

    StateMachine(QObject *parent = 0);
    StateMachine(StateMachinePrivate &dd, QObject *parent);

    /// If this state machine is backed by a QStateMachine, that QStateMachine is returned. Otherwise, a null-pointer is returned.
    QStateMachine *qStateMachine() const;

    int sessionId() const;

    DataModel *dataModel() const;
    void setDataModel(DataModel *dataModel);

    void setDataBinding(BindingMethod b);
    BindingMethod dataBinding() const;

    ExecutableContent::ExecutionEngine *executionEngine() const;
    TableData *tableData() const;
    void setTableData(TableData *tableData);

    void doLog(const QString &label, const QString &msg);

    Q_INVOKABLE bool init();

    bool isRunning() const;

    QString name() const;
    QStringList activeStates(bool compress = true);
    bool isActive(const QString &scxmlStateName) const;
    bool hasState(const QString &scxmlStateName) const;

    using QObject::connect;
    QMetaObject::Connection connect(const QString &scxmlStateName, const char *signal,
                                    const QObject *receiver, const char *method,
                                    Qt::ConnectionType type = Qt::AutoConnection);

    Q_INVOKABLE void submitError(const QByteArray &type, const QString &msg, const QByteArray &sendid);
    Q_INVOKABLE void submitEvent1(const QString &event);
    Q_INVOKABLE void submitEvent2(const QString &event,  QVariant data);

    void submitEvent(ScxmlEvent *e);
    Q_INVOKABLE void submitEvent(const QByteArray &event,
                                 const QVariantList &dataValues = QVariantList(),
                                 const QStringList &dataNames = QStringList(),
                                 ScxmlEvent::EventType type = ScxmlEvent::External,
                                 const QByteArray &sendid = QByteArray(),
                                 const QString &origin = QString(),
                                 const QString &origintype = QString(),
                                 const QByteArray &invokeid = QByteArray());
    void submitDelayedEvent(int delayInMiliSecs,
                            ScxmlEvent *e);
    void cancelDelayedEvent(const QByteArray &event);

    bool isLegalTarget(const QString &target) const;
    bool isDispatchableTarget(const QString &target) const;

Q_SIGNALS:
    void log(const QString &label, const QString &msg);
    void reachedStableState(bool didChange);
    void finished();

public Q_SLOTS:
    void start();

private Q_SLOTS:
    void onFinished();

protected:
    void executeInitialSetup();

private:
    Q_DECLARE_PRIVATE(StateMachine)
};

} // namespace Scxml

Q_DECLARE_TYPEINFO(Scxml::ScxmlError, Q_MOVABLE_TYPE);
QT_END_NAMESPACE

#endif // SCXMLSTATEMACHINE_H