-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotter.cpp
More file actions
226 lines (172 loc) · 4.77 KB
/
plotter.cpp
File metadata and controls
226 lines (172 loc) · 4.77 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
/////////////////////////////////////////////////////////////////////////
//
// Bakalářská práce
// Vizualizace datových struktur pro verifikační nástroje
// Michael Holubec
// GNU LGPLv3
//
//////////////////////////////////////////////////////////////////////////
//
// Created by Michael Holubec on 02.03.16.
//
#include "plotter.h"
namespace memgraph {
std::string Plotter::getDot() {
std::string dot = "";
dot += dotGraphHeader();
dot += "{" + new_line;
ident_step++;
dot += std::string(dotGraphAttrs(&graph->attrs));
dot += std::string(dotGraphNodeAttrs(graph->getNodeAttrs()));
dot += std::string(dotGraphEdgeAttrs(graph->getEdgeAttrs()));
dot += std::string(dotNodes(graph->getNodes()));
dot += std::string(dotSubgraphs(graph->getSubgraphs()));
dot += std::string(dotEdges(graph->getEdges()));
ident_step--;
dot += "}";
return dot;
}
std::string Plotter::dotGraphHeader() {
std::string text = "";
if (graph->getType() == digraph) {
text += "digraph";
} else {
text += "graph";
}
text += " G ";
return text;
}
std::string Plotter::dotGraphAttrs(Attributes *attrs) {
std::string text = "";
if (attrs->size() > 0) {
for (auto i : *attrs) {
if (!i.second->isRemoved()) {
text += getIdent() + dotAttribute(i.first, i.second) + ";" + new_line;
}
}
text += new_line;
}
return text;
}
std::string Plotter::dotGraphNodeAttrs(Attributes *attrs) {
std::string text = "";
if (attrs->size() > 0) {
text += getIdent() + "node ";
text += dotAttributes(attrs);
text += ";" + new_line + new_line;
}
return text;
}
std::string Plotter::dotGraphEdgeAttrs(Attributes *attrs) {
std::string text = "";
if (attrs->size() > 0) {
text += getIdent() + "edge ";
text += dotAttributes(attrs);
text += ";" + new_line + new_line;
}
return text;
}
std::string Plotter::dotNodes(nodes_map *nodes) {
std::string text = "";
std::string node = "";
for (auto i : *nodes) {
if (i.second->isRemoved()) continue;
if (i.second->attrs.size() > 0) {
node = getIdent() + std::string(i.second->getName());
node += " ";
node += std::string(dotAttributes(&i.second->attrs));
node += ";" + new_line;
text += node;
}
}
if (text.length() > 0) {
text += new_line;
}
return text;
}
std::string Plotter::dotEdges(edges_vect *edges) {
std::string text = "";
std::string edge = "";
for (auto e : *edges) {
if (e->isRemoved()) continue;
if (e->getFrom()->isRemoved()) {
throw "Can't plot edge - from_node was removed";
}
if (e->getTo()->isRemoved()) {
throw "Can't plot edge - to_node was removed";
}
edge = getIdent() + std::string(e->getFrom()->getName()) + dotEdgeType() +
std::string(e->getTo()->getName());
if (e->attrs.size() > 0) {
edge += " ";
edge += std::string(dotAttributes(&e->attrs));
}
edge += ";" + new_line;
text += edge;
}
return text;
}
std::string Plotter::dotEdgeType() {
if (graph->getType() == std_graph) {
return " -- ";
} else if (graph->getType() == digraph) {
return " -> ";
}
throw "Unknown graph type";
}
std::string Plotter::dotSubgraphs(subgraphs_map *subgraphs) {
std::string text = "";
for (subgraphs_it it = subgraphs->begin(); it != subgraphs->end(); ++it) {
text += dotSubgraph(it->second);
}
return text;
}
std::string Plotter::dotSubgraph(Subgraph *subgraph) {
std::string text = "";
text += getIdent() + "subgraph " + std::string(subgraph->getName()) + " {" + new_line;
ident_step++;
text += std::string(dotGraphAttrs(&subgraph->attrs));
text += std::string(dotGraphNodeAttrs(subgraph->getNodeAttrs()));
text += std::string(dotGraphEdgeAttrs(subgraph->getEdgeAttrs()));
text += std::string(dotNodes(subgraph->getNodes()));
text += std::string(dotSubgraphs(subgraph->getSubgraphs()));
text += std::string(dotEdges(subgraph->getEdges()));
ident_step--;
text += getIdent() + "}" + new_line + new_line;
return text;
}
std::string Plotter::dotAttributes(Attributes *attrs) {
if (attrs->size() == 0) {
return "";
}
attributes_it it = attrs->begin();
std::string attributes = "[";
while (true) {
if (!it->second->isRemoved()) {
attributes += dotAttribute(it->first, it->second);
}
if (++it == attrs->end()) {
break;
}
if (!it->second->isRemoved()) {
attributes += ",";
}
}
attributes += "]";
return attributes.c_str();
}
std::string Plotter::dotAttribute(std::string name, Attribute *attr) {
std::string attribute = "";
std::string str_name = name;
std::string value = attr->getValue();
attribute += str_name + "=" + value;
return attribute;
}
std::string Plotter::getIdent() {
std::string text = "";
for (int i = 0; i < ident_step; i++) {
text += tab;
}
return text;
}
}