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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
/**
* @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
#define INVALID_ASSIGNMENT(name) \
name.compare("_sessionid") == 0 || \
name.compare("_name") == 0 || \
name.compare("_ioprocessors") == 0 || \
name.compare("_event") == 0
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;
// session id
Data sessionId;
sessionId.compound["type"] = Data("string", Data::VERBATIM);
sessionId.compound["value"] = Data(interpreter->getSessionId(), Data::VERBATIM);
dm->_variables["_sessionid"] = sessionId;
// name
Data name;
name.compound["type"] = Data("string", Data::VERBATIM);
name.compound["value"] = Data(interpreter->getName(), Data::VERBATIM);
dm->_variables["_name"] = name;
// ioprocessors
Data ioProcs;
ioProcs.compound["type"] = Data("compound", Data::VERBATIM);
std::map<std::string, IOProcessor> ioProcessors = interpreter->getIOProcessors();
for (std::map<std::string, IOProcessor>::iterator iter = ioProcessors.begin(); iter != ioProcessors.end(); iter++) {
ioProcs.compound["value"].compound[iter->first] = iter->second.getDataModelVariables();
}
dm->_variables["_ioprocessors"] = ioProcs;
dm->_lastMType = 0;
return dm;
}
void PromelaDataModel::registerIOProcessor(const std::string& name, const IOProcessor& ioprocessor) {
}
void PromelaDataModel::pushContext() {
// std::cout << "PromelaDataModel::pushContext" << std::endl;
}
void PromelaDataModel::popContext() {
// std::cout << "PromelaDataModel::popContext" << std::endl;
}
void PromelaDataModel::setEvent(const Event& event) {
Data variable;
variable.compound["type"] = Data("compound", Data::VERBATIM);
variable.compound["value"].compound["name"] = Data(event.name, Data::VERBATIM);
variable.compound["value"].compound["origin"] = Data(event.origin, Data::VERBATIM);
variable.compound["value"].compound["origintype"] = Data(event.origintype, Data::VERBATIM);
variable.compound["value"].compound["invokeid"] = Data(event.invokeid, Data::VERBATIM);
if (event.hideSendId) {
variable.compound["value"].compound["sendid"] = Data("", Data::VERBATIM);
} else {
variable.compound["value"].compound["sendid"] = Data(event.sendid, Data::VERBATIM);
}
switch (event.eventType) {
case Event::PLATFORM:
variable.compound["value"].compound["type"] = Data("platform", Data::VERBATIM);
break;
case Event::INTERNAL:
variable.compound["value"].compound["type"] = Data("internal", Data::VERBATIM);
break;
case Event::EXTERNAL:
variable.compound["value"].compound["type"] = Data("external", Data::VERBATIM);
break;
default:
variable.compound["value"].compound["type"] = Data("invalid", Data::VERBATIM);
break;
}
if (event.dom) {
// no support
} else if (event.content.length() > 0) {
// _event.data is a string or JSON
Data json = Data::fromJSON(event.content);
if (!json.empty()) {
variable.compound["value"].compound["data"] = json;
} else {
if (isNumeric(event.content.c_str(), 10)) {
variable.compound["value"].compound["data"] = Data(event.content, Data::INTERPRETED);
} else {
variable.compound["value"].compound["data"] = Data(InterpreterImpl::spaceNormalize(event.content), Data::VERBATIM);
}
}
} else {
// _event.data is KVP
if (!event.data.empty()) {
variable.compound["value"].compound["data"] = event.data;
} else {
// test 343 / test 488
variable.compound["value"].compound["data"];
}
for (Event::params_t::const_iterator start = event.params.begin(), end = event.params.end();
start != end; start = event.params.upper_bound(start->first)) {
// only set first param key
if (isNumeric(start->second.atom.c_str(), 10)) {
variable.compound["value"].compound["data"].compound[start->first] = strTo<int>(start->second.atom);
} else {
variable.compound["value"].compound["data"].compound[start->first] = start->second;
}
}
for (Event::namelist_t::const_iterator iter = event.namelist.begin(); iter != event.namelist.end(); iter++) {
if (isNumeric(iter->second.atom.c_str(), 10)) {
variable.compound["value"].compound["data"].compound[iter->first] = strTo<int>(iter->second.atom);
} else {
variable.compound["value"].compound["data"].compound[iter->first] = iter->second;
}
}
}
// iterate all data elements and adapt type for int if atom is integer
adaptType(variable.compound["value"].compound["data"]);
_variables["_event"] = variable;
}
void PromelaDataModel::adaptType(Data& data) {
if (data.atom.length() > 0 && isInteger(data.atom.c_str(), 10)) {
data.type = Data::INTERPRETED;
return;
}
if (data.array.size() > 0) {
for (std::list<Data>::iterator iter = data.array.begin(); iter != data.array.end(); iter++) {
adaptType(*iter);
}
return;
}
if (data.compound.size() > 0) {
for (std::map<std::string, Data>::iterator iter = data.compound.begin(); iter != data.compound.end(); iter++) {
adaptType(iter->second);
}
return;
}
}
Data PromelaDataModel::getStringAsData(const std::string& content) {
return evaluateExpr(content);
}
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, 1, PromelaParser::PROMELA_EXPR);
if (itemParser.ast->type != PML_NAME)
ERROR_EXECUTION_THROW("Expression '" + item + "' is no valid item");
PromelaParser arrayParser(ss.str(), 1, PromelaParser::PROMELA_EXPR);
setVariable(itemParser.ast, getVariable(arrayParser.ast));
if (index.length() > 0) {
PromelaParser indexParser(index, 1, PromelaParser::PROMELA_EXPR);
setVariable(indexParser.ast, iteration);
}
}
void PromelaDataModel::eval(const Element<std::string>& scriptElem, const std::string& expr) {
PromelaParser parser(expr, 1, PromelaParser::PROMELA_STMNT);
evaluateStmnt(parser.ast);
// parser.dump();
}
bool PromelaDataModel::evalAsBool(const std::string& expr) {
return evalAsBool(Arabica::DOM::Element<std::string>(), expr);
}
bool PromelaDataModel::evalAsBool(const Arabica::DOM::Element<std::string>& node, const std::string& expr) {
PromelaParser parser(expr, 1, PromelaParser::PROMELA_EXPR);
// parser.dump();
Data tmp = evaluateExpr(parser.ast);
if (tmp.atom.compare("false") == 0)
return false;
if (tmp.atom.compare("0") == 0)
return false;
return true;
}
std::string PromelaDataModel::evalAsString(const std::string& expr) {
PromelaParser parser(expr);
return evaluateExpr(parser.ast);
}
void PromelaDataModel::assign(const Element<std::string>& assignElem,
const Node<std::string>& node,
const std::string& content) {
std::string key;
std::string value;
if (node) {
ERROR_EXECUTION_THROW("Assigning DOM node to variable is not supported");
}
if (HAS_ATTR(assignElem, "id")) {
key = ATTR(assignElem, "id");
} else if (HAS_ATTR(assignElem, "location")) {
key = ATTR(assignElem, "location");
}
if (HAS_ATTR(assignElem, "expr")) {
if (key.length() == 0) {
ERROR_EXECUTION_THROW("Assign element has neither id nor location");
}
value = ATTR(assignElem, "expr");
} else {
value = content;
}
if (value.length() == 0)
return;
Data json = Data::fromJSON(value);
if (!json.empty()) {
// simply assign from json to key
assign(key, json);
} else {
std::string expr = key + " = " + value;
PromelaParser parser(expr, 1, PromelaParser::PROMELA_STMNT);
evaluateStmnt(parser.ast);
}
}
void PromelaDataModel::evaluateDecl(const std::string& expr) {
PromelaParser parser(expr, 1, PromelaParser::PROMELA_DECL);
evaluateDecl(parser.ast);
}
Data PromelaDataModel::evaluateExpr(const std::string& expr) {
PromelaParser parser(expr, 1, PromelaParser::PROMELA_EXPR);
return evaluateExpr(parser.ast);
}
void PromelaDataModel::evaluateStmnt(const std::string& expr) {
PromelaParser parser(expr, 1, PromelaParser::PROMELA_STMNT);
evaluateStmnt(parser.ast);
}
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++;
try {
variable.compound["value"] = evaluateExpr(expr);
} catch(uscxml::Event e) {
// test277, declare and throw
_variables.compound[name->value] = variable;
throw e;
}
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 = dataToInt(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::dataToInt(const Data& data) {
if (data.type != Data::INTERPRETED)
ERROR_EXECUTION_THROW("Operand is not integer");
int value = strTo<int>(data.atom);
if (data.atom.compare(toStr(value)) != 0)
ERROR_EXECUTION_THROW("Operand is not integer");
return value;
}
bool PromelaDataModel::dataToBool(const Data& data) {
if (data.atom.size() == 0) // empty string or undefined
return false;
if (data.type == Data::VERBATIM) {
// non-empty string is true
return true;
} else {
if (data.atom.compare("true") == 0) {
// boolean true is true
return true;
} else if (data.atom.compare("false") == 0) {
return false;
} else if (dataToInt(data) != 0) {
return true; // non zero values are true
}
}
return false;
}
Data PromelaDataModel::evaluateExpr(void* ast) {
PromelaParserNode* node = (PromelaParserNode*)ast;
std::list<PromelaParserNode*>::iterator opIter = node->operands.begin();
switch (node->type) {
case PML_CONST:
if (iequals(node->value, "false"))
return Data(false);
if (iequals(node->value, "true"))
return Data(true);
return strTo<int>(node->value);
case PML_NAME:
case PML_VAR_ARRAY:
case PML_CMPND:
return getVariable(node);
case PML_STRING: {
std::string stripped = node->value.substr(1, node->value.size() - 2);
return Data(stripped, Data::VERBATIM);
}
case PML_PLUS:
return dataToInt(evaluateExpr(*opIter++)) + dataToInt(evaluateExpr(*opIter++));
case PML_MINUS:
return dataToInt(evaluateExpr(*opIter++)) - dataToInt(evaluateExpr(*opIter++));
case PML_DIVIDE:
return dataToInt(evaluateExpr(*opIter++)) / dataToInt(evaluateExpr(*opIter++));
case PML_MODULO:
return dataToInt(evaluateExpr(*opIter++)) % dataToInt(evaluateExpr(*opIter++));
case PML_EQ: {
PromelaParserNode* lhs = *opIter++;
PromelaParserNode* rhs = *opIter++;
Data left = evaluateExpr(lhs);
Data right = evaluateExpr(rhs);
if (left == right) // overloaded operator==
return Data(true);
// literal strings or strings in variables
if ((lhs->type == PML_STRING || rhs->type == PML_STRING)
|| (left.type == Data::VERBATIM && right.type == Data::VERBATIM)) {
return (left.atom.compare(right.atom) == 0 ? Data(true) : Data(false));
}
return dataToInt(left) == dataToInt(right);
}
case PML_NEG:
return !dataToBool(evaluateExpr(*opIter++));
case PML_LT:
return dataToInt(evaluateExpr(*opIter++)) < dataToInt(evaluateExpr(*opIter++));
case PML_LE:
return dataToInt(evaluateExpr(*opIter++)) <= dataToInt(evaluateExpr(*opIter++));
case PML_GT:
return dataToInt(evaluateExpr(*opIter++)) > dataToInt(evaluateExpr(*opIter++));
case PML_GE:
return dataToInt(evaluateExpr(*opIter++)) >= dataToInt(evaluateExpr(*opIter++));
case PML_TIMES:
return dataToInt(evaluateExpr(*opIter++)) * dataToInt(evaluateExpr(*opIter++));
case PML_LSHIFT:
return dataToInt(evaluateExpr(*opIter++)) << dataToInt(evaluateExpr(*opIter++));
case PML_RSHIFT:
return dataToInt(evaluateExpr(*opIter++)) >> dataToInt(evaluateExpr(*opIter++));
case PML_AND:
case PML_OR: {
PromelaParserNode* lhs = *opIter++;
PromelaParserNode* rhs = *opIter++;
// std::cout << "-----" << std::endl;
// lhs->dump();
// rhs->dump();
Data left = evaluateExpr(lhs);
Data right = evaluateExpr(rhs);
bool truthLeft = dataToBool(left);
bool truthRight = dataToBool(right);
if (node->type == PML_AND) {
return truthLeft && truthRight;
} else {
return truthLeft || truthRight;
}
}
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;
}
case PML_INCR: {
PromelaParserNode* name = *opIter++;
setVariable(name, strTo<long>(getVariable(name)) + 1);
break;
}
case PML_DECR: {
PromelaParserNode* name = *opIter++;
setVariable(name, strTo<long>(getVariable(name)) - 1);
break;
}
default:
node->dump();
ERROR_EXECUTION_THROW("No support for " + PromelaParserNode::typeToDesc(node->type) + " statement implemented");
}
}
void PromelaDataModel::setVariable(void* ast, Data 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++;
if (INVALID_ASSIGNMENT(name->value)) {
ERROR_EXECUTION_THROW("Cannot assign to " + name->value);
}
int index = dataToInt(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] = value;
break;
}
case PML_NAME:
if (INVALID_ASSIGNMENT(node->value)) {
ERROR_EXECUTION_THROW("Cannot assign to " + node->value);
}
_variables.compound[node->value].compound["value"] = value;
break;
default:
break;
}
// std::cout << Data::toJSON(_variables) << std::endl;
}
Data 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 _variables[node->value]["value"];
case PML_VAR_ARRAY: {
PromelaParserNode* name = *opIter++;
PromelaParserNode* expr = *opIter++;
int index = dataToInt(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 _variables.compound[name->value].compound["value"][index];
}
case PML_CMPND: {
// node->dump();
// std::cout << Data::toJSON(_variables["_event"]);
std::stringstream idPath;
PromelaParserNode* name = *opIter++;
// special case for _x variables
if (name->value.compare("_x") == 0) {
PromelaParserNode* what = *opIter++;
if (what->type == PML_VAR_ARRAY) {
if (what->operands.size() == 2) {
std::string arrName = what->operands.front()->value;
std::string index = what->operands.back()->value;
if (what->operands.back()->type == PML_STRING) {
index = index.substr(1, index.size() - 2); // remove quotes
}
if (arrName.compare("states") == 0) {
return Data(_interpreter->isInState(index));
}
}
}
ERROR_EXECUTION_THROW("No variable " + name->value + " was declared");
}
if (_variables.compound.find(name->value) == _variables.compound.end()) {
ERROR_EXECUTION_THROW("No variable " + name->value + " was declared");
}
Data currData = _variables.compound[name->value]["value"];
idPath << name->value;
while(opIter != node->operands.end()) {
std::string key = (*opIter)->value;
idPath << "." << key;
if (currData.compound.find(key) == currData.compound.end()) {
ERROR_EXECUTION_THROW("No variable " + idPath.str() + " was declared");
}
Data tmp = currData.compound[key];
currData = tmp;
opIter++;
}
return currData;
}
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 and idlocation
PromelaParser parser(location);
setVariable(parser.ast, data);
}
void PromelaDataModel::init(const Element<std::string>& dataElem,
const Node<std::string>& node,
const std::string& content) {
// from <datamodel>
if (HAS_ATTR(dataElem, "id")) {
std::string identifier = ATTR(dataElem, "id");
std::string type = (HAS_ATTR(dataElem, "type") ? ATTR(dataElem, "type") : "int");
std::string arrSize;
size_t bracketPos = type.find("[");
if (bracketPos != std::string::npos) {
arrSize = type.substr(bracketPos, type.length() - bracketPos);
type = type.substr(0, bracketPos);
}
std::string expr = type + " " + identifier + arrSize;
PromelaParser parser(expr, 1, PromelaParser::PROMELA_DECL);
evaluateDecl(parser.ast);
}
assign(dataElem, node, content);
}
void PromelaDataModel::init(const std::string& location, const Data& data) {
assign(location, data);
}
bool PromelaDataModel::isDeclared(const std::string& expr) {
PromelaParser parser(expr);
if (parser.ast->type == PML_VAR_ARRAY)
return _variables.compound.find(parser.ast->operands.front()->value) != _variables.compound.end();
return _variables.compound.find(expr) != _variables.compound.end();
}
}
|