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
|
/**
* @file
* @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* 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.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#include <boost/algorithm/string.hpp>
#include "uscxml/Common.h"
#include "uscxml/config.h"
#include "PromelaDataModel.h"
#include "uscxml/DOMUtils.h"
#include "uscxml/Message.h"
#include <glog/logging.h>
#include <cctype>
#include "PromelaParser.h"
#include "parser/promela.tab.hpp"
#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif
namespace uscxml {
using namespace Arabica::XPath;
using namespace Arabica::DOM;
#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool pluginConnect(pluma::Host& host) {
host.add( new PromelaDataModelProvider() );
return true;
}
#endif
PromelaDataModel::PromelaDataModel() {
}
PromelaDataModel::~PromelaDataModel() {
}
boost::shared_ptr<DataModelImpl> PromelaDataModel::create(InterpreterImpl* interpreter) {
boost::shared_ptr<PromelaDataModel> dm = boost::shared_ptr<PromelaDataModel>(new PromelaDataModel());
dm->_interpreter = interpreter;
dm->_lastMType = 0;
return dm;
}
void PromelaDataModel::registerIOProcessor(const std::string& name, const IOProcessor& ioprocessor) {
}
void PromelaDataModel::setSessionId(const std::string& sessionId) {
_sessionId = sessionId;
}
void PromelaDataModel::setName(const std::string& name) {
_name = name;
}
void PromelaDataModel::pushContext() {
// std::cout << "PromelaDataModel::pushContext" << std::endl;
}
void PromelaDataModel::popContext() {
// std::cout << "PromelaDataModel::popContext" << std::endl;
}
void PromelaDataModel::initialize() {
// std::cout << "PromelaDataModel::initialize" << std::endl;
}
void PromelaDataModel::setEvent(const Event& event) {
_event = event;
}
Data PromelaDataModel::getStringAsData(const std::string& content) {
Data data(content, Data::VERBATIM);
return data;
}
bool PromelaDataModel::validate(const std::string& location, const std::string& schema) {
// std::cout << "PromelaDataModel::validate" << std::endl;
return true;
}
bool PromelaDataModel::isLocation(const std::string& expr) {
return true;
}
uint32_t PromelaDataModel::getLength(const std::string& expr) {
if (!isDeclared(expr)) {
ERROR_EXECUTION_THROW("Variable '" + expr + "' was not declared");
}
if (!_variables[expr].hasKey("size")) {
ERROR_EXECUTION_THROW("Variable '" + expr + "' is no array");
}
return strTo<int>(_variables[expr]["size"].atom);
}
void PromelaDataModel::setForeach(const std::string& item,
const std::string& array,
const std::string& index,
uint32_t iteration) {
// assign array element to item
std::stringstream ss;
ss << array << "[" << iteration << "]";
PromelaParser itemParser(item, PromelaParser::PROMELA_EXPR);
PromelaParser arrayParser(ss.str(), PromelaParser::PROMELA_EXPR);
setVariable(itemParser.ast, getVariable(arrayParser.ast));
if (index.length() > 0) {
PromelaParser indexParser(index, PromelaParser::PROMELA_EXPR);
setVariable(indexParser.ast, iteration);
}
}
void PromelaDataModel::eval(const Element<std::string>& scriptElem, const std::string& expr) {
PromelaParser parser(expr, PromelaParser::PROMELA_STMNT);
evaluateStmnt(parser.ast);
// parser.dump();
}
bool PromelaDataModel::evalAsBool(const std::string& expr) {
return evalAsBool(Arabica::DOM::Node<std::string>(), expr);
}
bool PromelaDataModel::evalAsBool(const Arabica::DOM::Node<std::string>& node, const std::string& expr) {
PromelaParser parser(expr, PromelaParser::PROMELA_EXPR);
// parser.dump();
return evaluateExpr(parser.ast) > 0;
}
std::string PromelaDataModel::evalAsString(const std::string& expr) {
if (isDeclared(expr)) {
return Data::toJSON(_variables[expr]);
}
return expr;
}
void PromelaDataModel::assign(const Element<std::string>& assignElem,
const Node<std::string>& node,
const std::string& content) {
PromelaParser parser(content, PromelaParser::PROMELA_DECL);
evaluateDecl(parser.ast);
// parser.dump();
// std::cout << Data::toJSON(_variables) << std::endl;
}
void PromelaDataModel::evaluateDecl(void* ast) {
PromelaParserNode* node = (PromelaParserNode*)ast;
if (false) {
} else if (node->type == PML_DECL) {
std::list<PromelaParserNode*>::iterator opIter = node->operands.begin();
PromelaParserNode* vis = *opIter++;
PromelaParserNode* type = *opIter++;
PromelaParserNode* varlist = *opIter++;
for (std::list<PromelaParserNode*>::iterator nameIter = varlist->operands.begin();
nameIter != varlist->operands.end();
nameIter++) {
Data variable;
variable.compound["vis"] = Data(vis->value, Data::VERBATIM);
variable.compound["type"] = Data(type->value, Data::VERBATIM);
if (false) {
} else if ((*nameIter)->type == PML_NAME) {
// plain variables without initial assignment
if (type->value == "mtype") {
variable.compound["value"] = Data(_lastMType++, Data::INTERPRETED);
} else {
variable.compound["value"] = Data(0, Data::INTERPRETED);
}
_variables.compound[(*nameIter)->value] = variable;
} else if ((*nameIter)->type == PML_ASGN) {
// initially assigned variables
std::list<PromelaParserNode*>::iterator opIterAsgn = (*nameIter)->operands.begin();
PromelaParserNode* name = *opIterAsgn++;
PromelaParserNode* expr = *opIterAsgn++;
variable.compound["value"] = evaluateExpr(expr);
assert(opIterAsgn == (*nameIter)->operands.end());
_variables.compound[name->value] = variable;
} else if ((*nameIter)->type == PML_VAR_ARRAY) {
// variable arrays
std::list<PromelaParserNode*>::iterator opIterAsgn = (*nameIter)->operands.begin();
PromelaParserNode* name = *opIterAsgn++;
int size = evaluateExpr(*opIterAsgn++);
variable.compound["size"] = size;
for (int i = 0; i < size; i++) {
variable.compound["value"].array.push_back(Data(0, Data::INTERPRETED));
}
assert(opIterAsgn == (*nameIter)->operands.end());
_variables.compound[name->value] = variable;
} else {
ERROR_EXECUTION_THROW("Declaring variables via " + PromelaParserNode::typeToDesc((*nameIter)->type) + " not implemented");
}
}
assert(opIter == node->operands.end());
} else if (node->type == PML_DECLLIST) {
for (std::list<PromelaParserNode*>::iterator declIter = node->operands.begin();
declIter != node->operands.end();
declIter++) {
evaluateDecl(*declIter);
}
} else {
ERROR_EXECUTION_THROW("Declaring variables via " + PromelaParserNode::typeToDesc(node->type) + " not implemented");
}
}
int PromelaDataModel::evaluateExpr(void* ast) {
PromelaParserNode* node = (PromelaParserNode*)ast;
std::list<PromelaParserNode*>::iterator opIter = node->operands.begin();
switch (node->type) {
case PML_CONST:
return strTo<int>(node->value);
case PML_NAME:
case PML_VAR_ARRAY:
return getVariable(node);
case PML_PLUS:
return evaluateExpr(*opIter++) + evaluateExpr(*opIter++);
case PML_MINUS:
return evaluateExpr(*opIter++) - evaluateExpr(*opIter++);
case PML_DIVIDE:
return evaluateExpr(*opIter++) / evaluateExpr(*opIter++);
case PML_MODULO:
return evaluateExpr(*opIter++) % evaluateExpr(*opIter++);
case PML_EQ:
return evaluateExpr(*opIter++) == evaluateExpr(*opIter++);
case PML_LT:
return evaluateExpr(*opIter++) < evaluateExpr(*opIter++);
case PML_LE:
return evaluateExpr(*opIter++) <= evaluateExpr(*opIter++);
case PML_GT:
return evaluateExpr(*opIter++) > evaluateExpr(*opIter++);
case PML_GE:
return evaluateExpr(*opIter++) >= evaluateExpr(*opIter++);
case PML_TIMES:
return evaluateExpr(*opIter++) * evaluateExpr(*opIter++);
case PML_LSHIFT:
return evaluateExpr(*opIter++) << evaluateExpr(*opIter++);
case PML_RSHIFT:
return evaluateExpr(*opIter++) >> evaluateExpr(*opIter++);
case PML_AND:
return evaluateExpr(*opIter++) != 0 && evaluateExpr(*opIter++) != 0;
case PML_OR:
return evaluateExpr(*opIter++) != 0 || evaluateExpr(*opIter++) != 0;
default:
ERROR_EXECUTION_THROW("Support for " + PromelaParserNode::typeToDesc(node->type) + " expressions not implemented");
}
return 0;
}
void PromelaDataModel::evaluateStmnt(void* ast) {
PromelaParserNode* node = (PromelaParserNode*)ast;
std::list<PromelaParserNode*>::iterator opIter = node->operands.begin();
switch (node->type) {
case PML_ASGN: {
PromelaParserNode* name = *opIter++;
PromelaParserNode* expr = *opIter++;
setVariable(name, evaluateExpr(expr));
break;
}
case PML_STMNT: {
while(opIter != node->operands.end()) {
evaluateStmnt(*opIter++);
}
break;
}
default:
ERROR_EXECUTION_THROW("No support for " + PromelaParserNode::typeToDesc(node->type) + " statement implemented");
}
}
void PromelaDataModel::setVariable(void* ast, int value) {
PromelaParserNode* node = (PromelaParserNode*)ast;
std::list<PromelaParserNode*>::iterator opIter = node->operands.begin();
switch (node->type) {
case PML_VAR_ARRAY: {
PromelaParserNode* name = *opIter++;
PromelaParserNode* expr = *opIter++;
int index = evaluateExpr(expr);
if (_variables.compound.find(name->value) == _variables.compound.end()) {
ERROR_EXECUTION_THROW("No variable " + name->value + " was declared");
}
if (!_variables[name->value].hasKey("size")) {
ERROR_EXECUTION_THROW("Variable " + name->value + " is no array");
}
if (strTo<int>(_variables[name->value]["size"].atom) <= index) {
ERROR_EXECUTION_THROW("Index " + toStr(index) + " in array " + name->value + "[" + _variables[name->value]["size"].atom + "] is out of bounds");
}
_variables.compound[name->value].compound["value"][index] = Data(value, Data::VERBATIM);
break;
}
case PML_NAME:
_variables.compound[node->value].compound["value"] = Data(value, Data::VERBATIM);
break;
default:
break;
}
// std::cout << Data::toJSON(_variables) << std::endl;
}
int PromelaDataModel::getVariable(void* ast) {
PromelaParserNode* node = (PromelaParserNode*)ast;
// node->dump();
std::list<PromelaParserNode*>::iterator opIter = node->operands.begin();
switch(node->type) {
case PML_NAME:
if (_variables.compound.find(node->value) == _variables.compound.end()) {
ERROR_EXECUTION_THROW("No variable " + node->value + " was declared");
}
if (_variables[node->value].compound.find("size") != _variables[node->value].compound.end()) {
ERROR_EXECUTION_THROW("Type error: Variable " + node->value + " is an array");
}
return strTo<int>(_variables[node->value]["value"].atom);
case PML_VAR_ARRAY: {
PromelaParserNode* name = *opIter++;
PromelaParserNode* expr = *opIter++;
int index = evaluateExpr(expr);
if (_variables.compound.find(name->value) == _variables.compound.end()) {
ERROR_EXECUTION_THROW("No variable " + name->value + " was declared");
}
if (!_variables[name->value].hasKey("size")) {
ERROR_EXECUTION_THROW("Variable " + name->value + " is no array");
}
if (strTo<int>(_variables[name->value]["size"].atom) <= index) {
ERROR_EXECUTION_THROW("Index " + toStr(index) + " in array " + name->value + "[" + _variables[name->value]["size"].atom + "] is out of bounds");
}
return strTo<int>(_variables.compound[name->value].compound["value"][index].atom);
}
default:
ERROR_EXECUTION_THROW("Retrieving value of " + PromelaParserNode::typeToDesc(node->type) + " variable not implemented");
}
return 0;
}
std::string PromelaDataModel::andExpressions(std::list<std::string> expressions) {
if (expressions.size() == 0)
return "";
if (expressions.size() == 1)
return *(expressions.begin());
std::ostringstream exprSS;
exprSS << "(";
std::string conjunction = "";
for (std::list<std::string>::const_iterator exprIter = expressions.begin();
exprIter != expressions.end();
exprIter++) {
exprSS << conjunction << "(" << *exprIter << ")";
conjunction = " && ";
}
exprSS << ")";
return exprSS.str();
}
void PromelaDataModel::assign(const std::string& location, const Data& data) {
// used for e.g. to assign command line parameters
std::cout << "Ignoring " << location << " = " << Data::toJSON(data) << std::endl;
}
void PromelaDataModel::init(const Element<std::string>& dataElem,
const Node<std::string>& node,
const std::string& content) {
// from <datamodel>
assign(dataElem, node, content);
}
void PromelaDataModel::init(const std::string& location, const Data& data) {
assign(location, data);
}
bool PromelaDataModel::isDeclared(const std::string& expr) {
return _variables.compound.find(expr) != _variables.compound.end();
}
}
|