summaryrefslogtreecommitdiffstats
path: root/src/scxml/qscxmlstatemachineinfo.cpp
blob: 4fbbecf299511213729663203c305d05e169208a (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtScxml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qscxmlstatemachineinfo_p.h"
#include "qscxmlstatemachine_p.h"
#include "qscxmlexecutablecontent_p.h"

QT_BEGIN_NAMESPACE

class QScxmlStateMachineInfoPrivate: public QObjectPrivate
{
    Q_DECLARE_PUBLIC(QScxmlStateMachineInfo)

public:
    QScxmlStateMachine *stateMachine() const
    { return qobject_cast<QScxmlStateMachine *>(q_func()->parent()); }

    QScxmlStateMachinePrivate *stateMachinePrivate() const
    { return QScxmlStateMachinePrivate::get(stateMachine()); }

    const QScxmlExecutableContent::StateTable *stateTable() const
    { return stateMachinePrivate()->m_stateTable; }
};

QScxmlStateMachineInfo::QScxmlStateMachineInfo(QScxmlStateMachine *stateMachine)
    : QObject(*new QScxmlStateMachineInfoPrivate, stateMachine)
{
    QScxmlStateMachinePrivate::get(stateMachine)->attach(this);
}

QScxmlStateMachine *QScxmlStateMachineInfo::stateMachine() const
{
    Q_D(const QScxmlStateMachineInfo);

    return d->stateMachine();
}

QVector<QScxmlStateMachineInfo::StateId> QScxmlStateMachineInfo::allStates() const
{
    Q_D(const QScxmlStateMachineInfo);

    QVector<QScxmlStateMachineInfo::StateId> all;
    for (int i = 0, ei = d->stateTable()->stateCount; i < ei; ++i) {
        all.append(i);
    }
    return all;
}

QVector<QScxmlStateMachineInfo::TransitionId> QScxmlStateMachineInfo::allTransitions() const
{
    Q_D(const QScxmlStateMachineInfo);

    QVector<QScxmlStateMachineInfo::TransitionId> all;
    for (int i = 0, ei = d->stateTable()->transitionCount; i < ei; ++i) {
        all.append(i);
    }
    return all;
}

QString QScxmlStateMachineInfo::stateName(int stateId) const
{
    Q_D(const QScxmlStateMachineInfo);

    if (stateId < 0 || stateId >= d->stateTable()->stateCount)
        return QString();

    auto state = d->stateTable()->state(stateId);
    if (state.name >= 0)
        return d->stateMachinePrivate()->m_tableData->string(state.name);
    else
        return QString();
}

QScxmlStateMachineInfo::StateId QScxmlStateMachineInfo::stateParent(StateId stateId) const
{
    Q_D(const QScxmlStateMachineInfo);

    if (stateId < 0 || stateId >= d->stateTable()->stateCount)
        return InvalidStateId;

    auto state = d->stateTable()->state(stateId);
    return state.parent;
}

QScxmlStateMachineInfo::StateType QScxmlStateMachineInfo::stateType(StateId stateId) const
{
    Q_D(const QScxmlStateMachineInfo);

    if (stateId < 0 || stateId >= d->stateTable()->stateCount)
        return InvalidState;

    auto state = d->stateTable()->state(stateId);
    switch (state.type) {
    default: return InvalidState;
    case QScxmlExecutableContent::StateTable::State::Normal: return NormalState;
    case QScxmlExecutableContent::StateTable::State::Parallel: return ParallelState;
    case QScxmlExecutableContent::StateTable::State::Final: return FinalState;
    case QScxmlExecutableContent::StateTable::State::ShallowHistory: return ShallowHistoryState;
    case QScxmlExecutableContent::StateTable::State::DeepHistory: return DeepHistoryState;
    }
}

QVector<QScxmlStateMachineInfo::StateId> QScxmlStateMachineInfo::stateChildren(StateId stateId) const
{
    Q_D(const QScxmlStateMachineInfo);

    int childStates = QScxmlExecutableContent::StateTable::InvalidIndex;
    if (stateId == InvalidStateId)
        childStates = d->stateTable()->childStates;
    if (stateId >= 0 && stateId < d->stateTable()->stateCount)
        childStates = d->stateTable()->state(stateId).childStates;

    QVector<QScxmlStateMachineInfo::StateId> all;
    if (childStates == QScxmlExecutableContent::StateTable::InvalidIndex)
        return all;

    const auto kids = d->stateTable()->array(childStates);
    all.reserve(kids.size());
    for (auto childId : kids) {
        all.append(childId);
    }
    return all;
}

QScxmlStateMachineInfo::TransitionType QScxmlStateMachineInfo::transitionType(QScxmlStateMachineInfo::TransitionId transitionId) const
{
    Q_D(const QScxmlStateMachineInfo);

    if (transitionId < 0 || transitionId >= d->stateTable()->transitionCount)
        return InvalidTransition;

    auto transition = d->stateTable()->transition(transitionId);
    switch (transition.type) {
    default: return InvalidTransition;
    case QScxmlExecutableContent::StateTable::Transition::Invalid: return InvalidTransition;
    case QScxmlExecutableContent::StateTable::Transition::Internal: return InternalTransition;
    case QScxmlExecutableContent::StateTable::Transition::External: return ExternalTransition;
    case QScxmlExecutableContent::StateTable::Transition::Synthetic: return SyntheticTransition;
    }
}

QScxmlStateMachineInfo::TransitionId QScxmlStateMachineInfo::initialTransition(StateId stateId) const
{
    Q_D(const QScxmlStateMachineInfo);

    if (stateId == InvalidStateId)
        return d->stateTable()->initialTransition;

    if (stateId < 0 || stateId >= d->stateTable()->stateCount)
        return InvalidTransitionId;

    return d->stateTable()->state(stateId).initialTransition;
}

QScxmlStateMachineInfo::StateId QScxmlStateMachineInfo::transitionSource(TransitionId transitionId) const
{
    Q_D(const QScxmlStateMachineInfo);

    if (transitionId < 0 || transitionId >= d->stateTable()->transitionCount)
        return InvalidStateId;

    auto transition = d->stateTable()->transition(transitionId);
    return transition.source;
}

QVector<QScxmlStateMachineInfo::StateId> QScxmlStateMachineInfo::transitionTargets(TransitionId transitionId) const
{
    Q_D(const QScxmlStateMachineInfo);

    QVector<QScxmlStateMachineInfo::StateId> targets;
    if (transitionId < 0 || transitionId >= d->stateTable()->transitionCount)
        return targets;

    auto transition = d->stateTable()->transition(transitionId);
    if (transition.targets == QScxmlExecutableContent::StateTable::InvalidIndex)
        return targets;

    for (int target : d->stateTable()->array(transition.targets)) {
        targets.append(target);
    }

    return targets;
}

QVector<QString> QScxmlStateMachineInfo::transitionEvents(TransitionId transitionId) const
{
    Q_D(const QScxmlStateMachineInfo);

    QVector<QString> events;
    if (transitionId < 0 || transitionId >= d->stateTable()->transitionCount)
        return events;

    auto transition = d->stateTable()->transition(transitionId);
    if (transition.events == QScxmlExecutableContent::StateTable::InvalidIndex)
        return events;

    auto eventIds = d->stateTable()->array(transition.events);
    events.reserve(eventIds.size());
    for (auto eventId : eventIds) {
        events.append(d->stateMachinePrivate()->m_tableData->string(eventId));
    }

    return events;
}

QVector<QScxmlStateMachineInfo::StateId> QScxmlStateMachineInfo::configuration() const
{
    Q_D(const QScxmlStateMachineInfo);
    const auto &list = d->stateMachinePrivate()->configuration().list();
    return QVector<StateId>(list.cbegin(), list.cend());
}

QT_END_NAMESPACE