Files
qt-creator/tests/manual/tasking/trafficlight/recipe.cpp
Jarek Kobus 7c0b831391 TaskTree: Implement TrafficLight example
Import from:
https://github.com/qt/qtscxml/tree/dev/examples/scxml/trafficlight-common
and implement it in terms of TaskTree.
The recipe() corresponds to trafficlight.scxml file and implements
the business logic.

Used components: TimeoutTask and BarrierTask.

Change-Id: Ifc08b947388abed8cfc85ba082aaa0b18da0a1d7
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
2024-01-11 11:36:06 +00:00

72 lines
2.6 KiB
C++

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "recipe.h"
#include "glueinterface.h"
#include <tasking/barrier.h>
#include <tasking/tasktree.h>
using namespace Tasking;
using namespace std::chrono;
Group recipe(GlueInterface *iface)
{
return Forever {
finishAllAndSuccess,
Group { // "working" state
parallel,
BarrierTask( // transitions to the "broken" state
[iface](Barrier &barrier) {
QObject::connect(iface, &GlueInterface::smashed, &barrier, &Barrier::advance);
},
[] { return DoneResult::Error; }),
Forever {
TimeoutTask( // "red" state
[iface](milliseconds &timeout) {
timeout = 3s;
iface->setRed(true);
},
[iface] { iface->setRed(false); }),
TimeoutTask( // "redGoingGreen" state
[iface](milliseconds &timeout) {
timeout = 1s;
iface->setRed(true);
iface->setYellow(true);
},
[iface] { iface->setRed(false); iface->setYellow(false); }),
TimeoutTask( // "green" state
[iface](milliseconds &timeout) {
timeout = 3s;
iface->setGreen(true);
},
[iface] { iface->setGreen(false); }),
TimeoutTask( // "greenGoingRed" state
[iface](milliseconds &timeout) {
timeout = 1s;
iface->setYellow(true);
},
[iface] { iface->setYellow(false); }),
}
},
Group { // "broken" state
parallel,
BarrierTask( // transitions to the "working" state
[iface](Barrier &barrier) {
QObject::connect(iface, &GlueInterface::repaired, &barrier, &Barrier::advance);
},
[] { return DoneResult::Error; }),
Forever {
TimeoutTask( // "blinking" state
[iface](milliseconds &timeout) {
timeout = 1s;
iface->setYellow(true);
},
[iface] { iface->setYellow(false); }),
TimeoutTask([](milliseconds &timeout) { timeout = 1s; }) // "unblinking" state
}
}
};
}