summaryrefslogtreecommitdiffstats
path: root/src/scxml/qscxmlevent.cpp
blob: d638211c4a1ade8af3ec6dfbc7972ded61159e64 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/****************************************************************************
**
** 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 "qscxmlexecutablecontent_p.h"
#include "qscxmlevent_p.h"
#include "qscxmlstatemachine_p.h"

QT_USE_NAMESPACE

namespace {
// Do not rely on RegisterMethodArgumentMetaType meta-call to register the QScxmlEvent type.
// This registration is required for the receiving end of the signal emission that carries
// parameters of this type to be able to treat them correctly as a gadget. This is because the
// receiving end of the signal is a generic method in the QML engine, at which point it's too late
// to do a meta-type registration.
const int QScxmlEventMetaTypeId = qMetaTypeId<QScxmlEvent>();
}

using namespace QScxmlExecutableContent;

QEvent::Type QScxmlEvent::scxmlEventType = (QEvent::Type) QEvent::registerEventType();
QEvent::Type QScxmlEvent::ignoreEventType = (QEvent::Type) QEvent::registerEventType();

QAtomicInt QScxmlEventBuilder::idCounter = QAtomicInt(0);

QScxmlEvent *QScxmlEventBuilder::buildEvent()
{
    auto dataModel = stateMachine ? stateMachine->dataModel() : Q_NULLPTR;
    auto tableData = stateMachine ? stateMachine->tableData() : Q_NULLPTR;

    QString eventName = event;
    bool ok = true;
    if (eventexpr != NoEvaluator) {
        eventName = dataModel->evaluateToString(eventexpr, &ok);
        ok = true; // ignore failure.
    }

    QVariant data;
    if ((!params || params->count == 0) && (!namelist || namelist->count == 0)) {
        if (contentExpr == NoEvaluator) {
            data = contents;
        } else {
            data = dataModel->evaluateToString(contentExpr, &ok);
        }
        if (!ok) {
            // expr evaluation failure results in the data property of the event being set to null. See e.g. test528.
            data = QVariant(QMetaType::VoidStar, 0);
        }
    } else {
        QVariantMap keyValues;
        if (evaluate(params, stateMachine, keyValues)) {
            if (namelist) {
                for (qint32 i = 0; i < namelist->count; ++i) {
                    QString name = tableData->string(namelist->const_data()[i]);
                    keyValues.insert(name, dataModel->property(name));
                }
            }
            data = keyValues;
        } else {
            // If the evaluation of the <param> tags fails, set _event.data to an empty string.
            // See test343.
            data = QVariant(QMetaType::VoidStar, 0);
        }
    }

    QString sendid = id;
    if (!idLocation.isEmpty()) {
        sendid = generateId();
        ok = stateMachine->dataModel()->setProperty(idLocation, sendid, tableData->string(instructionLocation));
        if (!ok)
            return Q_NULLPTR;
    }

    QString origin = target;
    if (targetexpr != NoEvaluator) {
        origin = dataModel->evaluateToString(targetexpr, &ok);
        if (!ok)
            return Q_NULLPTR;
    }
    if (origin.isEmpty()) {
        if (eventType == QScxmlEvent::ExternalEvent) {
            origin = QStringLiteral("#_internal");
        }
    } else if (origin == QStringLiteral("#_parent")) {
        // allow sending messages to the parent, independently of whether we're invoked or not.
    } else if (!origin.startsWith(QLatin1Char('#'))) {
        // [6.2.4] and test194.
        submitError(QStringLiteral("error.execution"),
                    QStringLiteral("Error in %1: %2 is not a legal target")
                    .arg(tableData->string(instructionLocation), origin),
                    sendid);
        return Q_NULLPTR;
    } else if (!stateMachine->isDispatchableTarget(origin)) {
        // [6.2.4] and test521.
        submitError(QStringLiteral("error.communication"),
                    QStringLiteral("Error in %1: cannot dispatch to target '%2'")
                    .arg(tableData->string(instructionLocation), origin),
                    sendid);
        return Q_NULLPTR;
    }

    QString origintype = type;
    if (origintype.isEmpty()) {
        // [6.2.5] and test198
        origintype = QStringLiteral("http://www.w3.org/TR/scxml/#SCXMLEventProcessor");
    }
    if (typeexpr != NoEvaluator) {
        origintype = dataModel->evaluateToString(typeexpr, &ok);
        if (!ok)
            return Q_NULLPTR;
    }
    if (!origintype.isEmpty()
            && origintype != QStringLiteral("qt:signal")
            && origintype != QStringLiteral("http://www.w3.org/TR/scxml/#SCXMLEventProcessor")) {
        // [6.2.5] and test199
        submitError(QStringLiteral("error.execution"),
                    QStringLiteral("Error in %1: %2 is not a valid type")
                    .arg(tableData->string(instructionLocation), origintype),
                    sendid);
        return Q_NULLPTR;
    }

    QString invokeid;
    if (stateMachine && stateMachine->isInvoked()) {
        invokeid = stateMachine->sessionId();
    }

    QScxmlEvent *event = new QScxmlEvent;
    event->setName(eventName);
    event->setEventType(eventType);
    event->setData(data);
    event->setSendId(sendid);
    event->setOrigin(origin);
    event->setOriginType(origintype);
    event->setInvokeId(invokeid);
    return event;
}

QScxmlEvent *QScxmlEventBuilder::errorEvent(QScxmlStateMachine *stateMachine, const QString &name,
                                            const QString &message, const QString &sendid)
{
    QScxmlEventBuilder event;
    event.stateMachine = stateMachine;
    event.event = name;
    event.eventType = QScxmlEvent::PlatformEvent; // Errors are platform events. See e.g. test331.
    // _event.data == null, see test528
    event.id = sendid;
    auto error = event();
    error->setErrorMessage(message);
    return error;
}

bool QScxmlEventBuilder::evaluate(const Param &param, QScxmlStateMachine *stateMachine,
                                  QVariantMap &keyValues)
{
    auto dataModel = stateMachine->dataModel();
    auto tableData = stateMachine->tableData();
    if (param.expr != NoEvaluator) {
        bool success = false;
        auto v = dataModel->evaluateToVariant(param.expr, &success);
        keyValues.insert(tableData->string(param.name), v);
        return success;
    }

    QString loc;
    if (param.location != QScxmlExecutableContent::NoString) {
        loc = tableData->string(param.location);
    }

    if (loc.isEmpty()) {
        return false;
    }

    if (dataModel->hasProperty(loc)) {
        keyValues.insert(tableData->string(param.name), dataModel->property(loc));
        return true;
    } else {
        submitError(QStringLiteral("error.execution"),
                    QStringLiteral("Error in <param>: %1 is not a valid location")
                    .arg(loc));
        return false;
    }
}

bool QScxmlEventBuilder::evaluate(const QScxmlExecutableContent::Array<Param> *params, QScxmlStateMachine *stateMachine, QVariantMap &keyValues)
{
    if (!params)
        return true;

    auto paramPtr = params->const_data();
    for (qint32 i = 0; i != params->count; ++i, ++paramPtr) {
        if (!evaluate(*paramPtr, stateMachine, keyValues))
            return false;
    }

    return true;
}

void QScxmlEventBuilder::submitError(const QString &type, const QString &msg, const QString &sendid)
{
    QScxmlStateMachinePrivate::get(stateMachine)->submitError(type, msg, sendid);
}

/*!
 * \class QScxmlEvent
 * \brief Event for an QScxmlStateMachine
 * \since 5.6
 * \inmodule QtScxml
 *
 * See section 5.10.1 "The Internal Structure of Events" in the Scxml specification for more detail.
 *
 * \sa QScxmlStateMachine
 */

/*!
    \enum QScxmlEvent::EventType

    This enum specifies type of event.

    \value PlatformEvent is an event generated internally by the state machine. An example of these
           are errors.
    \value InternalEvent is an event generated by a <raise> tag.
    \value ExternalEvent is an event generated by a <send> tag.
 */

/*!
 * \brief Creates a new external QScxmlEvent.
 */
QScxmlEvent::QScxmlEvent()
    : QEvent(scxmlEventType), d(new QScxmlEventPrivate)
{ }

/*!
 * \brief Destroys a QScxmlEvent.
 */
QScxmlEvent::~QScxmlEvent()
{
    delete d;
}

/*!
 * \return the event type.
 */
QString QScxmlEvent::scxmlType() const
{
    switch (d->eventType) {
    case PlatformEvent:
        return QLatin1String("platform");
    case InternalEvent:
        return QLatin1String("internal");
    case ExternalEvent:
        break;
    }
    return QLatin1String("external");
}

/*!
 * \brief clears the contents of the event.
 */
void QScxmlEvent::clear()
{
    *d = QScxmlEventPrivate();
}

/*!
 * \brief Copies a QScxmlEvent.
 */
QScxmlEvent &QScxmlEvent::operator=(const QScxmlEvent &other)
{
    QEvent::operator=(other);
    *d = *other.d;
    return *this;
}

/*!
 * \brief Copies a QScxmlEvent.
 */
QScxmlEvent::QScxmlEvent(const QScxmlEvent &other)
    : QEvent(other), d(new QScxmlEventPrivate(*other.d))
{
}

/*!
 * \brief The name of the event, used in the event attribute in a <transition>
 * \return the name of the event.
 */
QString QScxmlEvent::name() const
{
    return d->name;
}

/*!
 * \brief Sets the name of the event.
 */
void QScxmlEvent::setName(const QString &name)
{
    d->name = name;
}

/*!
 * \return The id of the event, when specified in Scxml. It is used by <cancel> to identify the
 *         event that has to be canceled. Note that the system will generate a unique ID when the
 *         idlocation attribute is used in <send>
 */
QString QScxmlEvent::sendId() const
{
    return d->sendid;
}

/*!
 * \brief sets the id for this event.
 */
void QScxmlEvent::setSendId(const QString &sendid)
{
    d->sendid = sendid;
}

/*!
 * \brief This is a URI, equivalent to the 'target' attribute on the <send> element.
 */
QString QScxmlEvent::origin() const
{
    return d->origin;
}

void QScxmlEvent::setOrigin(const QString &origin)
{
    d->origin = origin;
}

/*!
 * \brief This is equivalent to the 'type' field on the <send> element.
 */
QString QScxmlEvent::originType() const
{
    return d->originType;
}

/*!
 * \brief Sets the \a origintype of a QScxmlEvent.
 *
 * \sa QScxmlEvent::originType
 */
void QScxmlEvent::setOriginType(const QString &origintype)
{
    d->originType = origintype;
}

/*!
 * \brief If this event is generated by an invoked state machine, it is set to the id of the invoke.
 *        Otherwise it will be empty.
 */
QString QScxmlEvent::invokeId() const
{
    return d->invokeId;
}

/*!
 * \brief Sets the \a invokeid of an event.
 * \sa QScxmlEvent::invokeId
 */
void QScxmlEvent::setInvokeId(const QString &invokeid)
{
    d->invokeId = invokeid;
}

/*!
 * \brief The delay in miliseconds after which this event is to be delivered after processing the
 *        <send>.
 */
int QScxmlEvent::delay() const
{
    return d->delayInMiliSecs;
}

/*!
 * \brief Sets the delay in miliseconds.
 * \sa QScxmlEvent::delay
 */
void QScxmlEvent::setDelay(int delayInMiliSecs)
{
    d->delayInMiliSecs = delayInMiliSecs;
}

/*!
 * \return The type of this event.
 * \sa QScxmlEvent::EventType
 */
QScxmlEvent::EventType QScxmlEvent::eventType() const
{
    return d->eventType;
}

/*!
 * \brief Sets the event type.
 * \sa QScxmlEvent::eventType QScxmlEvent::EventType
 */
void QScxmlEvent::setEventType(const EventType &type)
{
    d->eventType = type;
}

/*!
 * \brief Any data included by the sender.
 *
 * When <param> tags are used in the <send>, the data will contain a QVariantMap where the key is
 * the name attribute, and the value is taken from the expr attribute or the location attribute.
 * When a <content> tag is used, the data will contain a single item with the content's expr or
 * child data.
 */
QVariant QScxmlEvent::data() const
{
    if (isErrorEvent())
        return QVariant();
    return d->data;
}

/*!
 * \brief Sets the payload data.
 * \sa QScxmlEvent::data
 */
void QScxmlEvent::setData(const QVariant &data)
{
    if (!isErrorEvent())
        d->data = data;
}

/*!
 * \return true when this is an error event, false otherwise.
 */
bool QScxmlEvent::isErrorEvent() const
{
    return eventType() == PlatformEvent && name().startsWith(QStringLiteral("error."));
}

/*!
 * \return If this is an error event, it returns the error message. Otherwise it will return an
 *         empty QString.
 */
QString QScxmlEvent::errorMessage() const
{
    if (!isErrorEvent())
        return QString();
    return d->data.toString();
}

/*!
 * \param message If this is an error event, the \a message will be set as the error message.
 */
void QScxmlEvent::setErrorMessage(const QString &message)
{
    if (isErrorEvent())
        d->data = message;
}

void QScxmlEvent::makeIgnorable()
{
    t = ignoreEventType;
}