-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (205 loc) · 9.71 KB
/
main.cpp
File metadata and controls
221 lines (205 loc) · 9.71 KB
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
/* Copyright 2007-2015 QReal Research Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
#include <ctime>
#include <QtCore/QDir>
#include <QtCore/QCommandLineParser>
#include <QtCore/QTranslator>
#include <QtCore/QDirIterator>
#include <QtWidgets/QApplication>
#include <qrkernel/logging.h>
#include <sanitizers/sanitizers.h>
#include <qrkernel/platformInfo.h>
#include "runner.h"
constexpr int maxLogSize = 10 * 1024 * 1024; // 10 MB
constexpr int TWO_D_MODEL_RUNNER_INTERPRET_ERROR = 2;
constexpr int TWO_D_MODEL_RUNNER_GENERATE_ERROR = 3;
constexpr int TWO_D_MODEL_RUNNER_GENERATE_MODE_NOT_EXIST = 4;
const QString description = QObject::tr(
"Emulates robot`s behaviour on TRIK Studio 2D model separately from programming environment. "\
"Passed .qrs will be interpreted just like when 'Run' button was pressed in TRIK Studio. \n"\
"In background mode the session will be terminated just after the execution ended and return code "
"will then contain binary information about program correctness."
"Example: \n") +
" 2D-model -b --platform minimal --report report.json --trajectory trajectory.fifo example.qrs";
bool loadTranslators(const QString &locale)
{
QDir translationsDirectory(qReal::PlatformInfo::invariantSettingsPath("pathToTranslations") + "/" + locale);
QDirIterator directories(translationsDirectory, QDirIterator::Subdirectories);
bool hasTranslations = false;
while (directories.hasNext()) {
for (auto &&translatorFile : QDir(directories.next()).entryInfoList(QDir::Files)) {
auto *translator = new QTranslator(qApp);
translator->load(translatorFile.absoluteFilePath());
QCoreApplication::installTranslator(translator);
hasTranslations = true;
}
}
return hasTranslations;
}
void setDefaultLocale()
{
const QString lang = QLocale().name().left(2);
if (lang.isEmpty()) {
return;
}
// Reset to default country for this language
QLocale::setDefault(QLocale(lang));
if (!loadTranslators(lang)) {
QLOG_INFO() << "Missing translations for language" << lang;
}
}
int main(int argc, char *argv[])
{
qReal::PlatformInfo::enableHiDPISupport();
qsrand(time(nullptr));
QScopedPointer<QApplication> app(new QApplication(argc, argv));
QCoreApplication::setApplicationName("2D-model");
QCoreApplication::setApplicationVersion(interpreterCore::Customizer::trikStudioVersion());
const auto &defaultPlatformConfigPath = qReal::PlatformInfo::defaultPlatformConfigPath();
if (!defaultPlatformConfigPath.isEmpty()) {
// Loading default settings for concrete platform if such exist.
qReal::SettingsManager::instance()->loadSettings(defaultPlatformConfigPath);
}
qReal::Logger logger;
const QDir logsDir(qReal::PlatformInfo::invariantSettingsPath("pathToLogs"));
if (logsDir.mkpath(logsDir.absolutePath())) {
logger.addLogTarget(logsDir.filePath("2d-model.log"), maxLogSize, 2);
#ifdef HAS_SANITIZER_INTERFACE
const auto sanitizersPath = logsDir.filePath("sanitizer.log").toLocal8Bit();
initSanitizerPath(sanitizersPath.constData());
#endif
}
QLOG_INFO() << "------------------- APPLICATION STARTED --------------------";
QLOG_INFO() << "Running on" << QSysInfo::prettyProductName() << QSysInfo::currentCpuArchitecture();
QLOG_INFO() << "Arguments:" << app->arguments();
QLOG_INFO() << "Setting default locale to" << QLocale().name();
setDefaultLocale();
// Hack to switch on default robot model
for (auto &&kit : {"trikV62", "trikV6", "ev3", "nxt"}) {
qReal::SettingsManager::setValue(QString("SelectedModelFor%1Kit").arg(kit), QVariant());
}
QCommandLineParser parser;
parser.setApplicationDescription(description);
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("qrs-file", QObject::tr("Save file to be interpreted."));
QCommandLineOption backgroundOption({"b", "background"}, QObject::tr("Run emulation in background."));
QCommandLineOption reportOption({"r","report"}
, QObject::tr("A path to file where checker results will be written (JSON).")
, "path-to-report", "report.json");
QCommandLineOption trajectoryOption({"t", "trajectory"}
, QObject::tr("A path to file where robot`s trajectory will be"\
" written. The writing will not be performed not immediately, each trajectory point will be written"\
" just when obtained by checker, so FIFOs are recommended to be targets for this option.")
, "path-to-trajectory", "trajectory.fifo");
QCommandLineOption inputOption({"i", "input"}, QObject::tr("Inputs for JavaScript solution.")// probably others too
, "path-to-input", "inputs.txt");
QCommandLineOption modeOption({"m", "mode"}, QObject::tr("Set to \"script\" for"\
" execution of js/py from the project or set to \"diagram\" for block diagram.")
, "mode", "diagram");
QCommandLineOption speedOption({"s", "speed"}
, QObject::tr("Speed factor, try from 5 to 20, or even 1000 (at your own risk!).")
, "speed", "0");
QCommandLineOption closeOnSuccessOption("close-on-success"
, QObject::tr("Close the window and exit if the diagram/script"\
" finishes without errors."));
QCommandLineOption closeOnFinishOption("close"
, QObject::tr("Close the window and exit after diagram/script"\
" finishes."));
QCommandLineOption showConsoleOption({"c", "console"}, QObject::tr("Shows robot's console."));
QCommandLineOption showDisplayOption({"d", "display"}, QObject::tr("Shows robot's display."));
QCommandLineOption generatePathOption("generate-path"
, QObject::tr("The complete file path, including the filename"\
", to save the generated JavaScript or Python code.")
, "path-to-save-code", QString());
QCommandLineOption generateModeOption("generate-mode", QObject::tr(R"(Select "python" or "javascript".)")
, "generate-mode", "javascript");
QCommandLineOption directScriptExecutionPathOption("script-path"
, QObject::tr("The path to the Python or JavaScript file that will be used for interpretation.")
, "script-path", QString());
QCommandLineOption onlyGenerateOption("only-generate"
, QObject::tr("Do not run the interpretation in any mode, "\
"this is a parameter that is only used to generate a file."));
QCommandLineOption delayOption("delay-before-exit"
, QObject::tr("Add a delay in milliseconds after executing "
"the script before closing the window")
, "Delay in ms", "0");
parser.addOption(backgroundOption);
parser.addOption(reportOption);
parser.addOption(trajectoryOption);
parser.addOption(inputOption);
parser.addOption(modeOption);
parser.addOption(speedOption);
parser.addOption(closeOnFinishOption);
parser.addOption(closeOnSuccessOption);
parser.addOption(showConsoleOption);
parser.addOption(showDisplayOption);
parser.addOption(generatePathOption);
parser.addOption(generateModeOption);
parser.addOption(directScriptExecutionPathOption);
parser.addOption(onlyGenerateOption);
parser.addOption(delayOption);
parser.process(*app);
const QStringList positionalArgs = parser.positionalArguments();
if (positionalArgs.size() != 1) {
parser.showHelp();
}
const QString &qrsFile = positionalArgs.first();
const bool backgroundMode = parser.isSet(backgroundOption);
const QString report = parser.isSet(reportOption) ? parser.value(reportOption) : QString();
const QString trajectory = parser.isSet(trajectoryOption) ? parser.value(trajectoryOption) : QString();
const QString input = parser.isSet(inputOption) ? parser.value(inputOption) : QString();
const QString mode = parser.isSet(modeOption) ? parser.value(modeOption) : QString("diagram");
const bool closeOnSuccessMode = parser.isSet(closeOnSuccessOption);
const bool closeOnFinishMode = backgroundMode || parser.isSet(closeOnFinishOption);
const bool showConsoleMode = parser.isSet(showConsoleOption);
const auto showDisplayMode = parser.isSet(showDisplayOption);
const QString generatePath = parser.value(generatePathOption);
const QString generateMode = parser.value(generateModeOption);
const QString scriptFilePath = parser.value(directScriptExecutionPathOption);
const bool onlyGenerate = parser.isSet(onlyGenerateOption);
const auto &delayStr = parser.value(delayOption);
auto delayIsCorrect = false;
auto delay = delayStr.toInt(&delayIsCorrect);
if (!delayIsCorrect || delay < 0) {
delay = 0;
}
QScopedPointer<twoDModel::Runner> runner(
new twoDModel::Runner(report, trajectory, input, mode, qrsFile, delay));
auto speedFactor = parser.value(speedOption).toInt();
if (!generatePath.isEmpty()) {
if (generateMode != "python" and generateMode != "javascript"
and generateMode != "nxt") {
parser.showHelp();
QLOG_ERROR() << "Problem with generate code to " << generatePath;
return TWO_D_MODEL_RUNNER_GENERATE_MODE_NOT_EXIST;
}
if (!runner->generate(generatePath, generateMode)) {
QLOG_ERROR() << "Problem with generate code to " << generatePath;
return TWO_D_MODEL_RUNNER_GENERATE_ERROR;
}
}
if (onlyGenerate || mode == "nxt") {
return 0;
}
if (!runner->interpret(backgroundMode, speedFactor, closeOnFinishMode,
closeOnSuccessMode, showConsoleMode, showDisplayMode, scriptFilePath)) {
return TWO_D_MODEL_RUNNER_INTERPRET_ERROR;
}
const int exitCode = app->exec();
runner.reset();
app.reset();
QLOG_INFO() << "------------------- APPLICATION FINISHED -------------------";
return exitCode;
}