-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbatch_script.cpp
More file actions
375 lines (361 loc) · 14.9 KB
/
Copy pathbatch_script.cpp
File metadata and controls
375 lines (361 loc) · 14.9 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
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
/* -----------------------------------------------------------------------------
* The Cyberiada State Machine Editor
* -----------------------------------------------------------------------------
*
* The batch mode edit script interpreter
*
* Copyright (C) 2026 Alexey Fedoseev <aleksey@fedoseev.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/
*
* ----------------------------------------------------------------------------- */
#include <QFile>
#include <QTextStream>
#include <QRegularExpression>
#include "batch_script.h"
#include "cyberiadasm_model.h"
#include "cyberiada_constants.h"
static bool toNumbers(const QStringList& tokens, int from, int count, double* values)
{
if (tokens.size() < from + count) return false;
for (int i = 0; i < count; i++) {
bool ok = false;
values[i] = tokens.at(from + i).toDouble(&ok);
if (!ok) return false;
}
return true;
}
static QString restOfLine(const QStringList& tokens, int from)
{
QString text = tokens.mid(from).join(" ");
// the trailing text field is one physical line; the escape \n embeds
// a newline, \\ a backslash
QString result;
result.reserve(text.size());
for (int i = 0; i < text.size(); i++) {
if (text.at(i) == QChar('\\') && i + 1 < text.size()) {
QChar next = text.at(i + 1);
if (next == QChar('n')) { result += QChar('\n'); i++; continue; }
if (next == QChar('\\')) { result += QChar('\\'); i++; continue; }
}
result += text.at(i);
}
return result;
}
// action text uses the CyberiadaML notation: 'entry/ behaviour',
// 'exit/ behaviour' or 'TRIGGER [guard]/ behaviour'
static bool parseActionText(const QString& text, Cyberiada::ActionType* type,
QString* trigger, QString* guard, QString* behaviour,
QString* error)
{
int slash = -1;
int depth = 0;
for (int i = 0; i < text.length(); i++) {
QChar c = text.at(i);
if (c == QChar('[')) depth++;
else if (c == QChar(']')) depth--;
else if (c == QChar('/') && depth == 0) { slash = i; break; }
}
if (slash < 0) {
*error = "action text requires the 'trigger [guard]/ behaviour' notation";
return false;
}
QString head = text.left(slash).trimmed();
*behaviour = text.mid(slash + 1).trimmed();
*trigger = head;
guard->clear();
int bracket = head.indexOf(QChar('['));
if (bracket >= 0) {
if (!head.endsWith("]")) { *error = "unbalanced guard brackets in the action text"; return false; }
*trigger = head.left(bracket).trimmed();
*guard = head.mid(bracket + 1, head.length() - bracket - 2).trimmed();
}
if (*trigger == "entry" || *trigger == "exit") {
if (!guard->isEmpty()) { *error = "guards are not allowed for entry/exit activities"; return false; }
*type = *trigger == "entry" ? Cyberiada::actionEntry : Cyberiada::actionExit;
trigger->clear();
return true;
}
*type = Cyberiada::actionTransition;
if (trigger->isEmpty()) { *error = "the action trigger is required"; return false; }
return true;
}
static bool runCommand(CyberiadaSMModel* model, const QStringList& tokens, QString* error)
{
const QString& cmd = tokens.first();
double v[4];
if (cmd == "new-state" || cmd == "new-comment" || cmd == "new-formal-comment" ||
cmd == "new-initial" || cmd == "new-final" ||
cmd == "new-choice" || cmd == "new-terminate") {
if (tokens.size() < 2) { *error = cmd + " requires a parent id"; return false; }
Cyberiada::Element* parent = model->idToElement(tokens.at(1));
Cyberiada::ElementCollection* collection = dynamic_cast<Cyberiada::ElementCollection*>(parent);
if (!collection) { *error = "unknown parent id '" + tokens.at(1) + "'"; return false; }
if (cmd == "new-state") {
Cyberiada::Rect r;
int name_from = 2;
if (toNumbers(tokens, 2, 4, v)) {
r = Cyberiada::Rect(v[0], v[1], v[2], v[3]);
name_from = 6;
}
QString name = restOfLine(tokens, name_from);
if (name.isEmpty()) { *error = "new-state requires a name"; return false; }
return model->newState(collection, name.toStdString(), Cyberiada::Action(), r) != NULL;
} else if (cmd == "new-comment" || cmd == "new-formal-comment") {
QString body = restOfLine(tokens, 2);
if (body.isEmpty()) { *error = cmd + " requires a body"; return false; }
if (cmd == "new-comment") {
return model->newComment(collection, body.toStdString()) != NULL;
}
return model->newFormalComment(collection, body.toStdString()) != NULL;
} else if (cmd == "new-choice") {
Cyberiada::Rect r;
if (toNumbers(tokens, 2, 4, v)) {
r = Cyberiada::Rect(v[0], v[1], v[2], v[3]);
}
return model->newChoice(collection, r) != NULL;
} else {
Cyberiada::Point p;
if (toNumbers(tokens, 2, 2, v)) {
p = Cyberiada::Point(v[0], v[1]);
}
if (cmd == "new-initial") {
return model->newInitial(collection, p) != NULL;
}
if (cmd == "new-terminate") {
return model->newTerminate(collection, p) != NULL;
}
return model->newFinal(collection, p) != NULL;
}
} else if (cmd == "new-transition") {
if (tokens.size() < 4) { *error = "new-transition requires <sm> <source> <target>"; return false; }
Cyberiada::StateMachine* sm = dynamic_cast<Cyberiada::StateMachine*>(model->idToElement(tokens.at(1)));
if (!sm) { *error = "unknown state machine id '" + tokens.at(1) + "'"; return false; }
Cyberiada::Element* source = model->idToElement(tokens.at(2));
Cyberiada::Element* target = model->idToElement(tokens.at(3));
if (!source) { *error = "unknown source id '" + tokens.at(2) + "'"; return false; }
if (!target) { *error = "unknown target id '" + tokens.at(3) + "'"; return false; }
Cyberiada::Action action(restOfLine(tokens, 4).toStdString());
return model->newTransition(sm, Cyberiada::transitionExternal, source, target, action) != NULL;
} else if (cmd == "update-meta") {
if (tokens.size() < 3) { *error = "update-meta requires <parameter> <value>"; return false; }
QString param = tokens.at(1);
QString value = restOfLine(tokens, 2);
if ((param == "transitionOrder" && value != "actionFirst" && value != "transitionFirst" &&
value != "exitFirst" && value != METAINFORMATION_VALUE_NONE) ||
(param == "eventPropagation" && value != "propagate" && value != "block" &&
value != METAINFORMATION_VALUE_NONE)) {
*error = "invalid " + param + " value '" + value + "'";
return false;
}
return model->updateMetainformation(model->documentIndex(), param, value);
}
// the remaining commands address an existing element by id
if (cmd != "rename" && cmd != "move" && cmd != "reparent" && cmd != "delete" &&
cmd != "new-action" && cmd != "update-action" && cmd != "delete-action" &&
cmd != "update-comment" && cmd != "update-id" && cmd != "polyline" &&
cmd != "new-subject" && cmd != "delete-subject") {
*error = "unknown command '" + cmd + "'";
return false;
}
if (tokens.size() < 2) { *error = cmd + " requires an element id"; return false; }
Cyberiada::Element* element = model->idToElement(tokens.at(1));
if (!element) { *error = "unknown element id '" + tokens.at(1) + "'"; return false; }
QModelIndex index = model->elementToIndex(element);
if (cmd == "rename") {
QString title = restOfLine(tokens, 2);
if (title.isEmpty()) { *error = "rename requires a title"; return false; }
return model->updateTitle(index, title);
} else if (cmd == "move") {
if (toNumbers(tokens, 2, 4, v)) {
return model->updateGeometry(index, Cyberiada::Rect(v[0], v[1], v[2], v[3]));
}
if (element->get_type() == Cyberiada::elementChoice) {
*error = "the choice requires <x y w h>";
return false;
}
if (toNumbers(tokens, 2, 2, v)) {
return model->updateGeometry(index, Cyberiada::Point(v[0], v[1]));
}
*error = "move requires <x y> or <x y w h>";
return false;
} else if (cmd == "reparent") {
if (tokens.size() != 3) { *error = "reparent requires <id> <new-parent-id>"; return false; }
if (!model->idToElement(tokens.at(2))) {
*error = "unknown parent id '" + tokens.at(2) + "'";
return false;
}
return model->updateParent(index, tokens.at(2).toStdString());
} else if (cmd == "delete") {
return model->deleteElement(index);
} else if (cmd == "update-comment") {
if (element->get_type() != Cyberiada::elementComment &&
element->get_type() != Cyberiada::elementFormalComment) {
*error = "element '" + tokens.at(1) + "' is not a comment";
return false;
}
QString body = restOfLine(tokens, 2);
if (body.isEmpty()) { *error = "update-comment requires a body"; return false; }
return model->updateCommentBody(index, body);
} else if (cmd == "update-id") {
if (tokens.size() != 3) { *error = "update-id requires <id> <new-id>"; return false; }
if (model->idToElement(tokens.at(2))) {
*error = "id '" + tokens.at(2) + "' is already used";
return false;
}
return model->updateID(index, tokens.at(2));
} else if (cmd == "polyline") {
if (element->get_type() != Cyberiada::elementTransition) {
*error = "element '" + tokens.at(1) + "' is not a transition";
return false;
}
int count = tokens.size() - 2;
if (count % 2 != 0) { *error = "polyline requires x y coordinate pairs"; return false; }
Cyberiada::Polyline pl;
for (int i = 0; i < count; i += 2) {
if (!toNumbers(tokens, 2 + i, 2, v)) {
*error = "polyline requires numeric coordinates";
return false;
}
pl.push_back(Cyberiada::Point(v[0], v[1]));
}
return model->updateGeometry(index, pl);
} else if (cmd == "new-subject" || cmd == "delete-subject") {
if (element->get_type() != Cyberiada::elementComment &&
element->get_type() != Cyberiada::elementFormalComment) {
*error = "element '" + tokens.at(1) + "' is not a comment";
return false;
}
if (cmd == "delete-subject") {
bool index_ok = false;
int subject_index = 0;
if (tokens.size() > 2) subject_index = tokens.at(2).toInt(&index_ok);
if (!index_ok) { *error = "delete-subject requires a subject index"; return false; }
const Cyberiada::Comment* comment = static_cast<const Cyberiada::Comment*>(element);
if (subject_index < 0 ||
(size_t)subject_index >= comment->get_subjects().size()) {
*error = QString("subject index %1 out of range").arg(subject_index);
return false;
}
return model->deleteCommentSubject(index, subject_index);
}
if (tokens.size() < 3) { *error = "new-subject requires <comment> <target>"; return false; }
Cyberiada::Element* target = model->idToElement(tokens.at(2));
if (!target) { *error = "unknown element id '" + tokens.at(2) + "'"; return false; }
if (target->get_type() == Cyberiada::elementRoot ||
target->get_type() == Cyberiada::elementSM) {
*error = "'" + tokens.at(2) + "' cannot be the subject of a comment";
return false;
}
Cyberiada::CommentSubjectType type = Cyberiada::commentSubjectElement;
QString fragment;
if (tokens.size() > 3) {
if (tokens.at(3) == "name") type = Cyberiada::commentSubjectName;
else if (tokens.at(3) == "data") type = Cyberiada::commentSubjectData;
else { *error = "the subject type must be 'name' or 'data'"; return false; }
fragment = restOfLine(tokens, 4);
if (fragment.isEmpty()) { *error = "the subject fragment is required"; return false; }
}
return model->newCommentSubject(index, target, type, fragment);
} else if (cmd == "new-action" || cmd == "update-action" || cmd == "delete-action") {
bool is_state = element->get_type() == Cyberiada::elementSimpleState ||
element->get_type() == Cyberiada::elementCompositeState;
bool is_transition = element->get_type() == Cyberiada::elementTransition;
if (!is_state && !is_transition) {
*error = "element '" + tokens.at(1) + "' cannot have actions";
return false;
}
const Cyberiada::State* state = is_state ? static_cast<const Cyberiada::State*>(element) : NULL;
Cyberiada::Transition* trans = is_transition ? static_cast<Cyberiada::Transition*>(element) : NULL;
// new-action appends; the other commands address the action by its
// 0-based index in the state's action list (transitions hold a
// single action, their index must be 0)
int action_index = 0;
int text_from = 2;
if (cmd != "new-action") {
bool index_ok = false;
if (tokens.size() > 2) action_index = tokens.at(2).toInt(&index_ok);
if (!index_ok) { *error = cmd + " requires an action index"; return false; }
text_from = 3;
if (is_state &&
(action_index < 0 || (size_t)action_index >= state->get_actions().size())) {
*error = QString("action index %1 out of range").arg(action_index);
return false;
}
if (is_transition && action_index != 0) {
*error = "a transition has a single action with index 0";
return false;
}
}
if (cmd == "delete-action") {
if (is_transition && !trans->has_action()) { *error = "the transition has no action"; return false; }
return model->deleteAction(index, action_index);
}
QString text = restOfLine(tokens, text_from);
if (text.isEmpty()) { *error = cmd + " requires the action text"; return false; }
Cyberiada::ActionType type;
QString trigger, guard, behaviour;
if (!parseActionText(text, &type, &trigger, &guard, &behaviour, error)) return false;
if (is_transition && type != Cyberiada::actionTransition) {
*error = "entry/exit actions are not allowed on transitions";
return false;
}
if (cmd == "new-action") {
if (is_transition && trans->has_action()) {
*error = "the transition already has an action";
return false;
}
return model->newAction(index, type, trigger, guard, behaviour);
}
if (is_state && state->get_actions().at(action_index).get_type() != type) {
*error = QString("the action text does not match the type of action %1").arg(action_index);
return false;
}
return model->updateAction(index, action_index, trigger, guard, behaviour);
}
*error = "unknown command '" + cmd + "'";
return false;
}
bool runEditScript(CyberiadaSMModel* model, const QString& path, QString* error)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
*error = "cannot open script " + path;
return false;
}
QTextStream in(&file);
int lineno = 0;
while (!in.atEnd()) {
QString line = in.readLine();
lineno++;
QString trimmed = line.trimmed();
if (trimmed.isEmpty() || trimmed.startsWith("#")) continue;
QStringList tokens = trimmed.split(QRegularExpression("\\s+"));
QString message;
bool ok = false;
try {
ok = runCommand(model, tokens, &message);
if (!ok && message.isEmpty()) {
message = "command failed";
}
} catch (const Cyberiada::Exception& e) {
message = QString(e.str().c_str());
}
if (!ok) {
*error = QString("line %1: %2").arg(lineno).arg(message);
return false;
}
}
return true;
}