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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
/**
* @file
* @author 2012-2013 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 "uscxml/Common.h"
#include "uscxml/UUID.h"
#include "SCXMLDotWriter.h"
#include "uscxml/DOMUtils.h"
#include <boost/algorithm/string.hpp> // replace_all
#include <iomanip>
namespace uscxml {
using namespace Arabica::DOM;
using namespace Arabica::XPath;
SCXMLDotWriter::SCXMLDotWriter() {
_iteration = 0;
_indentation = 0;
}
SCXMLDotWriter::SCXMLDotWriter(Interpreter interpreter,
const std::list<SCXMLDotWriter::StateAnchor>& stateAnchors,
const Element<std::string>& transition) {
_interpreter = interpreter;
_xmlNSPrefix = _interpreter.getNameSpaceInfo().xmlNSPrefix;
_transition = transition;
_anchors = stateAnchors;
NodeList<std::string > scxmlElems = interpreter.getDocument().getElementsByTagName("scxml");
_scxml = (Element<std::string>)scxmlElems.item(0);
if (_anchors.size() == 0) {
StateAnchor anchor;
anchor.element = _scxml;
_anchors.push_back(anchor);
}
for (std::list<StateAnchor>::iterator anchIter = _anchors.begin(); anchIter != _anchors.end(); anchIter++) {
if (!anchIter->element)
anchIter->element = _scxml;
if (anchIter->childDepth >= 0 && anchIter->transDepth == -1)
anchIter->transDepth = anchIter->childDepth;
_portType = anchIter->type;
assembleGraph(anchIter->element, anchIter->childDepth, anchIter->transDepth + 1);
}
_iteration = 0;
_indentation = 0;
}
SCXMLDotWriter::~SCXMLDotWriter() {
}
void SCXMLDotWriter::onStableConfiguration(Interpreter interpreter) {
std::ostringstream fileSS;
fileSS << interpreter.getName() << "." << std::setw(6) << std::setfill('0') << _iteration++ << ".dot";
toDot(fileSS.str(), interpreter);
}
void SCXMLDotWriter::afterCompletion(Interpreter interpreter) {
std::ostringstream fileSS;
fileSS << interpreter.getName() << "." << std::setw(6) << std::setfill('0') << _iteration++ << ".dot";
toDot(fileSS.str(), interpreter);
}
void SCXMLDotWriter::beforeMicroStep(Interpreter interpreter) {
// std::ostringstream fileSS;
// fileSS << interpreter.getName() << "." << std::setw(6) << std::setfill('0') << _iteration++ << ".dot";
// toDot(fileSS.str(), interpreter);
}
void SCXMLDotWriter::beforeTakingTransition(Interpreter interpreter,
const Element<std::string>& transition,
bool moreComing) {
std::ostringstream fileSS;
fileSS << interpreter.getName() << "." << std::setw(6) << std::setfill('0') << _iteration++ << ".dot";
toDot(fileSS.str(), interpreter, transition);
}
std::string SCXMLDotWriter::getPrefix() {
std::string prefix = "";
for (int i = 0; i < _indentation; i++)
prefix += " ";
return prefix;
}
void SCXMLDotWriter::writeTo(std::ostream& os) {
os << "digraph {" << std::endl;
os << " rankdir=LR;" << std::endl;
os << " fontsize=10;" << std::endl;
// outfile << " splines=ortho;" << std::endl;
// outfile << " splines=false;" << std::endl;
// outfile << " nodesep=1.0;" << std::endl;
_indentation++;
writeStateElement(os, _scxml);
// write edges at end of file
for(std::set<DotEdge>::iterator edgeIter = _edges.begin(); edgeIter != _edges.end(); edgeIter++) {
if (edgeIter->from == edgeIter->to)
continue;
if (_histories.find(edgeIter->to) != _histories.end()) {
if (_histories.find(edgeIter->to)->second.from == _histories.find(edgeIter->to)->second.to)
continue;
}
os << getPrefix() << "\"" << edgeIter->from << "\"";
if (edgeIter->fromPort.size() > 0) {
os << std::string(":\"") + edgeIter->fromPort + "\":e";
} else {
os << ":__name";
}
os << " -> ";
if (_histories.find(edgeIter->to) != _histories.end()) {
os << getPrefix() << "\"" << _histories.find(edgeIter->to)->second.to << "\"";
if (_histories.find(edgeIter->to)->second.toPort.size() > 0) {
os << std::string(":\"") + _histories.find(edgeIter->to)->second.toPort + "\"";
} else {
os << ":__name";
}
} else {
os << getPrefix() << "\"" << edgeIter->to << "\"";
if (edgeIter->toPort.size() > 0) {
os << std::string(":\"") + edgeIter->toPort + "\"";
} else {
os << ":__name";
}
}
if (edgeIter->type == EDGE_INITAL) {
os << ":nw [style=\"dashed\", color=\"black\"]";
} else {
os << " [color=\"black\"]";
}
os << std::endl;
}
_indentation--;
os << "}" << std::endl;
}
void SCXMLDotWriter::toDot(const std::string& filename,
Interpreter interpreter,
const std::list<SCXMLDotWriter::StateAnchor>& stateAnchors,
const Element<std::string>& transition) {
std::ofstream outfile(filename.c_str());
SCXMLDotWriter writer(interpreter, stateAnchors, transition);
writer.writeTo(outfile);
}
/**
* Walk the subset of the graph that is reachable and remember the nodes
*/
void SCXMLDotWriter::assembleGraph(const Element<std::string>& state, int32_t childDepth, int32_t transDepth) {
std::string nodeId = idForNode(state);
// this node is neither included per child, nor per transition
if (childDepth <= 0 && transDepth <= 0) {
return;
}
// been here
if (_graph.find(nodeId) != _graph.end())
return;
_graph[nodeId].node = state;
_graph[nodeId].portType = _portType;
if (childDepth == 0 && transDepth == 0) {
_graph[nodeId].isBorder = true;
}
NodeSet<std::string> childElems = InterpreterImpl::filterChildType(Node_base::ELEMENT_NODE, state);
for (int i = 0; i < childElems.size(); i++) {
Element<std::string> childElem(childElems[i]);
// remember histories we passed
if (iequals(TAGNAME(childElem), "history")) {
_histories[ATTR(childElem, "id")].to = ATTR(state, "id");
_histories[ATTR(childElem, "id")].toPort = ATTR(childElem, "id");
}
// follow transitions
if (iequals(TAGNAME(childElem), "transition")) {
_graph[nodeId].transitions.push_back(childElem);
NodeSet<std::string> targetStates = _interpreter.getImpl()->getTargetStates(childElem);
for (int j = 0; j < targetStates.size(); j++) {
std::string remoteNodeId = idForNode(targetStates[j]);
_graph[nodeId].targets.insert(std::make_pair(remoteNodeId, childElem));
// recurse along the transition targets, no weight from child depth
assembleGraph((Element<std::string>)targetStates[j], 0, transDepth - 1);
}
if (targetStates.size() == 0)
_graph[nodeId].targets.insert(std::make_pair(nodeId, childElem));
std::list<std::string> eventNames;
if (HAS_ATTR(childElem, "event"))
eventNames = InterpreterImpl::tokenizeIdRefs(ATTR(childElem, "event"));
if (eventNames.size() == 0)
_graph[nodeId].events.insert(std::make_pair("", childElem));
for (std::list<std::string>::iterator evIter = eventNames.begin(); evIter != eventNames.end(); evIter++) {
_graph[nodeId].events.insert(std::make_pair(*evIter, childElem));
}
}
// follow childs
if (InterpreterImpl::isState(Element<std::string>(childElem))) {
if (_interpreter.getImpl()->isInitial(Element<std::string>(childElem))) {
// add to initial states if it is initial
_graph[nodeId].initialchilds.insert(idForNode(childElem));
} else if (_interpreter.getImpl()->isParallel(Element<std::string>(state))) {
// add to initial states if we are parallel
_graph[nodeId].initialchilds.insert(idForNode(childElem));
}
// in any case, it is a child state
_graph[nodeId].childs.insert(idForNode(childElem));
// recurse (do we really need to?)
assembleGraph(childElem, childDepth - 1, transDepth);
}
}
}
/**
* Walk the complete graph and draw reachable subset
*/
void SCXMLDotWriter::writeStateElement(std::ostream& os, const Element<std::string>& stateElem) {
std::string stateId = idForNode(stateElem);
if (_knownIds.find(stateId) != _knownIds.end())
return;
_knownIds.insert(stateId);
std::list<Node<std::string> > invisParents;
bool isVisible = (_graph.find(stateId) != _graph.end());
bool subgraph = InterpreterImpl::isCompound(stateElem) || InterpreterImpl::isParallel(stateElem);
bool fatherIsParallel = (stateElem.getParentNode() &&
stateElem.getParentNode().getNodeType() == Node_base::ELEMENT_NODE &&
InterpreterImpl::isParallel(Element<std::string>(stateElem.getParentNode())));
if (subgraph) {
_indentation++;
os << std::endl;
os << getPrefix() << "subgraph \"cluster_" << stateId << "\" {" << std::endl;
_indentation++;
os << getPrefix() << "fontsize=14" << std::endl;
os << getPrefix() << "label=<<b>" << nameForNode(stateElem) << "</b>>" << std::endl;
// os << getPrefix() << "rank=\"same\"" << std::endl;
os << getPrefix() << "labeljust=l" << std::endl;
os << getPrefix() << (fatherIsParallel ? "style=dashed" : "style=solid") << std::endl;
// os << getPrefix() << "ranksep=\"equally\"" << std::endl;
}
if (isVisible) {
// this state is visible!
const DotState& dotState = _graph.find(stateId)->second;
// is this a subgraph?
os << std::endl;
os << getPrefix() << "\"" << stateId << "\" [" << std::endl;
_indentation++;
os << getPrefix() << "fontsize=10," << std::endl;
os << getPrefix() << "shape=plaintext," << std::endl;
os << getPrefix() << (fatherIsParallel ? "style=dashed," : "style=solid,") << std::endl;
// is the current state in the basic configuration?
if (InterpreterImpl::isMember(stateElem, _interpreter.getBasicConfiguration()))
os << getPrefix() << "color=red, penwidth=3," << std::endl;
// is the current state in the basic configuration?
if (dotState.isBorder)
os << getPrefix() << "color=blue," << std::endl;
// is this state final?
if (_interpreter.getImpl()->isFinal(stateElem)) {
os << getPrefix() << "shape=doublecircle," << std::endl;
os << getPrefix() << "color=black," << std::endl;
os << getPrefix() << "penwidth=2," << std::endl;
os << getPrefix() << "label=<" << nameForNode(stateElem) << ">" << std::endl;
_indentation--;
os << getPrefix() << "];" << std::endl;
return;
}
// is the state initial?
bool isInitial = _interpreter.getImpl()->isInitial(stateElem);
// if (isInitial)
// os << getPrefix() << "style=filled, fillcolor=lightgrey, " << std::endl;
DotState::mmap_s_e_t::const_iterator destIterF, destIterB;
std::list<std::string> outPorts; // count unique keys
switch (dotState.portType) {
case PORT_TARGET: // outports are per target
for(DotState::mmap_s_e_t::const_iterator it = dotState.targets.begin(), end = dotState.targets.end();
it != end;
it = dotState.targets.upper_bound(it->first)) {
outPorts.push_back(it->first);
}
break;
case PORT_EVENT: // outports are per event
for(DotState::mmap_s_e_t::const_iterator it = dotState.events.begin(), end = dotState.events.end();
it != end;
it = dotState.events.upper_bound(it->first)) {
outPorts.push_back(it->first);
}
break;
case PORT_TRANSITION:
for (int i = 0; i < dotState.transitions.size(); i++) {
outPorts.push_back(idForNode(dotState.transitions[i]));
}
break;
}
os << getPrefix() << "label = < " << std::endl;
/*
<table cellborder="1" border="0" cellspacing="0" cellpadding="2" style="rounded">
<tr><td port="name" rowspan="4"><b>step</b></td></tr>
<tr><td port="foo.error.port" align="right">foo.error.port</td></tr>
<tr><td port="bar" align="right">bar</td></tr>
<tr><td port="baz" align="right">baz</td></tr>
</table>
*/
std::string details = getDetailedLabel(stateElem);
os << "<table " << (isInitial ? "bgcolor=\"orange\" " : "") << "cellborder=\"1\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" >" << std::endl;
os << " <tr><td port=\"__name\" rowspan=\"" << outPorts.size() + 1 << "\"><b>" << nameForNode(stateElem) << "</b></td></tr>" << std::endl;
switch (dotState.portType) {
case PORT_TARGET: // outports are per target
writePerTargetPorts(os, outPorts, dotState);
break;
case PORT_EVENT: // outports are per event
writePerEventPorts(os, outPorts, dotState);
break;
case PORT_TRANSITION:
writePerTransitionPorts(os, outPorts, dotState);
break;
}
// write details of the state
if (details.size() > 0) {
os << " <tr><td colspan=\"" << (outPorts.size() == 0 ? 1 : 2) << "\">" << std::endl;
os << details << std::endl;
os << " </td></tr>" << std::endl;
}
// write history states
NodeSet<std::string> histories = InterpreterImpl::filterChildElements(_xmlNSPrefix + "history", stateElem);
for (int i = 0; i < histories.size(); i++) {
os << " <tr><td port=\"" << ATTR(histories[i], "id") << "\" colspan=\"" << (outPorts.size() == 0 ? 1 : 2) << "\"><b>history: </b>" << ATTR(histories[i], "id") << "</td></tr>" << std::endl;
}
os << "</table>" << std::endl << getPrefix() << ">" << std::endl;
_indentation--;
os << getPrefix() << "];" << std::endl;
for (std::set<std::string>::iterator initIter = dotState.initialchilds.begin(); initIter != dotState.initialchilds.end(); initIter++) {
std::string destId = *initIter;
DotEdge edge(stateId, destId);
edge.type = EDGE_INITAL;
if (_graph.find(destId) != _graph.end())
_edges.insert(edge);
}
}
// recurse into children and search others to draw
NodeSet<std::string> childElems = InterpreterImpl::filterChildType(Node_base::ELEMENT_NODE, stateElem);
for (int i = 0; i < childElems.size(); i++) {
Element<std::string> childElem(childElems[i]);
if (InterpreterImpl::isState(Element<std::string>(childElem))) {
writeStateElement(os, childElem);
}
}
if (subgraph) {
_indentation--;
os << getPrefix() << "} #" << stateId << std::endl;
_indentation--;
}
}
void SCXMLDotWriter::writePerTransitionPorts(std::ostream& os, const std::list<std::string>& outPorts, const DotState& dotState) {
// TODO: Not implemented
}
void SCXMLDotWriter::writePerEventPorts(std::ostream& os, const std::list<std::string>& outPorts, const DotState& dotState) {
// TODO: Not implemented
// outports contain event names
std::string stateId = idForNode(dotState.node);
DotState::mmap_s_e_t::const_iterator destIterF, destIterB;
for(std::list<std::string>::const_iterator nameIter = outPorts.begin(); nameIter != outPorts.end(); nameIter++) {
os << " <tr><td port=\"" << portEscape(*nameIter) << "\" align=\"right\">" << *nameIter << "</td></tr>" << std::endl;
}
}
void SCXMLDotWriter::writePerTargetPorts(std::ostream& os, const std::list<std::string>& outPorts, const DotState& dotState) {
// outports contain remote node ids
std::string stateId = idForNode(dotState.node);
DotState::mmap_s_e_t::const_iterator destIterF, destIterB;
for(std::list<std::string>::const_iterator nameIter = outPorts.begin(); nameIter != outPorts.end(); nameIter++) {
// gather all events that activate the transition
std::string portName = *nameIter;
DotEdge edge(stateId, portName);
edge.fromPort = portName;
std::multimap<std::string, std::string> eventConds; // event to condition
std::pair <DotState::mmap_s_e_t::const_iterator, DotState::mmap_s_e_t::const_iterator> targetKeyRange = dotState.targets.equal_range(portName);
for (destIterB = targetKeyRange.first; destIterB != targetKeyRange.second; ++destIterB) {
const Element<std::string>& transElem = destIterB->second;
std::list<std::string> eventNames = InterpreterImpl::tokenizeIdRefs(ATTR(transElem, "event"));
for (std::list<std::string>::iterator eventIter = eventNames.begin(); eventIter != eventNames.end(); eventIter++) {
eventConds.insert(std::make_pair(*eventIter, ATTR(transElem, "cond")));
}
if (eventNames.size() == 0) {
// spontaneous transition
eventConds.insert(std::make_pair("#", ATTR(transElem, "cond")));
edge.type = EDGE_SPONTANEOUS;
}
}
if (_graph.find(portName) != _graph.end())
_edges.insert(edge);
typedef std::multimap<std::string, std::string>::iterator condIter_t;
std::stringstream outPortSS;
outPortSS << "<b>" << portName << "</b><br align=\"right\" />";
std::string opener = "{";
std::string closer;
std::string seperator;
condIter_t iterA, iterB;
for(iterA = eventConds.begin(); iterA != eventConds.end(); iterA = iterB) {
std::string eventName = iterA->first;
bool hasCondition = false;
std::pair <condIter_t, condIter_t> condRange = eventConds.equal_range(eventName);
for (iterB = condRange.first; iterB != condRange.second; ++iterB) {
hasCondition = true;
}
outPortSS << opener << seperator << eventName << (hasCondition ? "" : "");
seperator = ", ";
opener = "";
closer = "}";
}
outPortSS << closer;
os << " <tr><td port=\"" << portEscape(portName) << "\" align=\"right\">" << outPortSS.str() << "</td></tr>" << std::endl;
}
}
std::string SCXMLDotWriter::getDetailedLabel(const Element<std::string>& elem, int indentation) {
/*
<table>
<tr>
<td colspan="2">onEntry</td>
</tr>
<tr>
<td>Details</td>
<td bgcolor="#eee">
Nested Content
</td>
</tr>
</table>
*/
std::list<struct ElemDetails> content;
NodeList<std::string > childElems = elem.getChildNodes();
for (int i = 0; i < childElems.getLength(); i++) {
if (childElems.item(i).getNodeType() != Node_base::ELEMENT_NODE)
continue;
if (InterpreterImpl::isState(Element<std::string>(childElems.item(i))) ||
iequals(TAGNAME(childElems.item(i)), "transition") ||
iequals(TAGNAME(childElems.item(i)), "initial") ||
false)
continue;
struct ElemDetails details;
details.name = "<b>" + TAGNAME(childElems.item(i)) + ":</b>";
if (iequals(TAGNAME(childElems.item(i)), "history")) {
continue;
}
// provide details for special elements here
// param ---------
if (iequals(TAGNAME(childElems.item(i)), "param")) {
if (HAS_ATTR(childElems.item(i), "name"))
details.name += " " + ATTR(childElems.item(i), "name") + " = ";
if (HAS_ATTR(childElems.item(i), "expr"))
details.name += ATTR(childElems.item(i), "expr");
if (HAS_ATTR(childElems.item(i), "location"))
details.name += ATTR(childElems.item(i), "location");
}
// data ---------
if (iequals(TAGNAME(childElems.item(i)), "data")) {
if (HAS_ATTR(childElems.item(i), "id"))
details.name += " " + ATTR(childElems.item(i), "id");
if (HAS_ATTR(childElems.item(i), "src"))
details.name += ATTR(childElems.item(i), "src");
if (HAS_ATTR(childElems.item(i), "expr"))
details.name += " = " + ATTR(childElems.item(i), "expr");
NodeList<std::string > grandChildElems = childElems.item(i).getChildNodes();
for (int j = 0; j < grandChildElems.getLength(); j++) {
if (grandChildElems.item(j).getNodeType() == Node_base::TEXT_NODE) {
details.name += dotEscape(grandChildElems.item(j).getNodeValue());
}
}
}
// invoke ---------
if (iequals(TAGNAME(childElems.item(i)), "invoke")) {
if (HAS_ATTR(childElems.item(i), "type"))
details.name += "<br />type = " + ATTR(childElems.item(i), "type");
if (HAS_ATTR(childElems.item(i), "typeexpr"))
details.name += "<br />type = " + ATTR(childElems.item(i), "typeexpr");
if (HAS_ATTR(childElems.item(i), "src"))
details.name += "<br />src = " + ATTR(childElems.item(i), "src");
if (HAS_ATTR(childElems.item(i), "srcexpr"))
details.name += "<br />src = " + ATTR(childElems.item(i), "srcexpr");
if (HAS_ATTR(childElems.item(i), "id"))
details.name += "<br />id = " + ATTR(childElems.item(i), "id");
if (HAS_ATTR(childElems.item(i), "idlocation"))
details.name += "<br />id = " + ATTR(childElems.item(i), "idlocation");
}
// send ---------
if (iequals(TAGNAME(childElems.item(i)), "raise")) {
if (HAS_ATTR(childElems.item(i), "event "))
details.name += "<br />event = " + ATTR(childElems.item(i), "event");
}
// send ---------
if (iequals(TAGNAME(childElems.item(i)), "send")) {
if (HAS_ATTR(childElems.item(i), "id"))
details.name += "<br />id = " + ATTR(childElems.item(i), "id");
if (HAS_ATTR(childElems.item(i), "type"))
details.name += "<br />type = " + ATTR(childElems.item(i), "type");
if (HAS_ATTR(childElems.item(i), "typeexpr"))
details.name += "<br />type = " + ATTR(childElems.item(i), "typeexpr");
if (HAS_ATTR(childElems.item(i), "event"))
details.name += "<br />event = " + ATTR(childElems.item(i), "event");
if (HAS_ATTR(childElems.item(i), "eventexpr"))
details.name += "<br />event = " + ATTR(childElems.item(i), "eventexpr");
if (HAS_ATTR(childElems.item(i), "target"))
details.name += "<br />target = " + ATTR(childElems.item(i), "target");
if (HAS_ATTR(childElems.item(i), "targetexpr"))
details.name += "<br />target = " + ATTR(childElems.item(i), "targetexpr");
if (HAS_ATTR(childElems.item(i), "delay"))
details.name += "<br />delay = " + ATTR(childElems.item(i), "delay");
if (HAS_ATTR(childElems.item(i), "delayexpr"))
details.name += "<br />delay = " + ATTR(childElems.item(i), "delayexpr");
}
// cancel ---------
if (iequals(TAGNAME(childElems.item(i)), "cancel")) {
if (HAS_ATTR(childElems.item(i), "sendid"))
details.name += " " + ATTR(childElems.item(i), "sendid");
}
// script ---------
if (iequals(TAGNAME(childElems.item(i)), "script")) {
details.name += " ";
if (HAS_ATTR(childElems.item(i), "src"))
details.name += ATTR(childElems.item(i), "src");
NodeList<std::string > grandChildElems = childElems.item(i).getChildNodes();
for (int j = 0; j < grandChildElems.getLength(); j++) {
if (grandChildElems.item(j).getNodeType() == Node_base::TEXT_NODE) {
details.name += dotEscape(grandChildElems.item(j).getNodeValue());
}
}
}
// if ---------
if (iequals(TAGNAME(childElems.item(i)), "if")) {
if (HAS_ATTR(childElems.item(i), "cond"))
details.name += " cond = " + dotEscape(ATTR(childElems.item(i), "cond"));
}
// elseif ---------
if (iequals(TAGNAME(childElems.item(i)), "elseif")) {
if (HAS_ATTR(childElems.item(i), "cond"))
details.name += " cond = " + dotEscape(ATTR(childElems.item(i), "cond"));
}
// log ---------
if (iequals(TAGNAME(childElems.item(i)), "log")) {
details.name += " ";
if (HAS_ATTR(childElems.item(i), "label"))
details.name += ATTR(childElems.item(i), "label") + " = ";
if (HAS_ATTR(childElems.item(i), "expr"))
details.name += ATTR(childElems.item(i), "expr");
}
// foreach ---------
if (iequals(TAGNAME(childElems.item(i)), "foreach")) {
if (HAS_ATTR(childElems.item(i), "item"))
details.name += "<br /> item = " + ATTR(childElems.item(i), "item");
if (HAS_ATTR(childElems.item(i), "array"))
details.name += "<br /> array = " + ATTR(childElems.item(i), "array");
if (HAS_ATTR(childElems.item(i), "index"))
details.name += "<br /> index = " + ATTR(childElems.item(i), "index");
}
// recurse
details.content = getDetailedLabel((Element<std::string>)childElems.item(i), indentation + 1);
content.push_back(details);
}
std::stringstream ssContent;
if (content.size() > 0) {
ssContent << "<table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" bgcolor=\"#" << colorForIndent(indentation + 1) << "\">";
std::list<struct ElemDetails>::iterator contentIter = content.begin();
while(contentIter != content.end()) {
ssContent << "<tr>";
// ssContent << "<td align=\"left\" colspan=\"2\">" << contentIter->name << "</td>";
ssContent << "<td balign=\"left\" align=\"left\">" << contentIter->name << "</td>";
ssContent << "</tr>";
if (contentIter->content.size() > 0) {
ssContent << "<tr>";
// ssContent << "<td>" << contentIter->details << "</td>";
ssContent << "<td bgcolor=\"#" << colorForIndent(indentation + 1) << "\">" << contentIter->content << "</td>";
ssContent << "</tr>";
}
contentIter++;
}
ssContent << "</table>";
}
return ssContent.str();
}
std::string SCXMLDotWriter::portEscape(const std::string& text) {
std::string escaped(text);
boost::replace_all(escaped, ".", "-");
return text;
}
std::string SCXMLDotWriter::dotEscape(const std::string& text) {
std::string escaped(text);
boost::replace_all(escaped, " ", " ");
boost::replace_all(escaped, "\t", " ");
boost::replace_all(escaped, "<", "<");
boost::replace_all(escaped, ">", ">");
boost::replace_all(escaped, "\"", """);
boost::replace_all(escaped, "\n", "<br />");
return escaped;
}
std::string SCXMLDotWriter::colorForIndent(int indent) {
int color = 255 - (16 * indent);
std::stringstream ss;
ss << std::hex << color;
ss << std::hex << color;
ss << std::hex << color;
return ss.str();
}
std::string SCXMLDotWriter::nameForNode(const Node<std::string>& node) {
std::string elemName;
if (node.getNodeType() == Node_base::ELEMENT_NODE) {
Element<std::string> elem = (Element<std::string>)node;
if (false) {
} else if (elem.getTagName() == "scxml") {
if (elem.hasAttribute("name") && !UUID::isUUID(elem.getAttribute("name"))) {
elemName += elem.getAttribute("name");
} else if (elem.hasAttribute("id") && !UUID::isUUID(elem.getAttribute("id"))) {
elemName += elem.getAttribute("id");
}
} else if (InterpreterImpl::isCompound(elem)) {
elemName = "<i>Compound: </i>" + elem.getAttribute("id");
} else if (InterpreterImpl::isParallel(elem)) {
elemName = "<i>Parallel: </i>" + elem.getAttribute("id");
} else if (elem.hasAttribute("id")) {
elemName += elem.getAttribute("id");
}
}
if (elemName.size() == 0)
elemName = boost::lexical_cast<std::string>(node.getLocalName());
return elemName;
}
std::string SCXMLDotWriter::idForNode(const Node<std::string>& node) {
std::string elemId;
// try to get the id as the name or id attribute
if (node.getNodeType() == Node_base::ELEMENT_NODE) {
Element<std::string> elem = (Element<std::string>)node;
if (elem.hasAttribute("name")) {
elemId = elem.getAttribute("name");
} else if (elem.hasAttribute("id")) {
elemId = elem.getAttribute("id");
}
}
// no luck, create id from position in tree
if (elemId.size() == 0) {
Node<std::string> tmpParent = node;
Node<std::string> tmpIndex;
do {
if (tmpParent.getNodeType() != Node_base::ELEMENT_NODE)
continue;
tmpIndex = tmpParent;
int index = 0;
while((tmpIndex = tmpIndex.getPreviousSibling()))
index++;
std::stringstream ssElemId;
ssElemId << TAGNAME(tmpParent) << index << ".";
elemId = ssElemId.str() + elemId;
} while ((tmpParent = tmpParent.getParentNode()));
// elemId = ssElemId.str();
}
std::replace(elemId.begin(), elemId.end(), '-', '_');
return elemId;
}
}
|