forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDotGraph.java
More file actions
326 lines (284 loc) · 8.54 KB
/
DotGraph.java
File metadata and controls
326 lines (284 loc) · 8.54 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
package soot.util.dot;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2002 Sable Research Group
* %%
* 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 2.1 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 General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Allow a serialized drawing, following steps:
* <ol>
* <li>new DotGraph</li>
* <li>draw(Directed/Undirected)Edge, attachAttributes, addNode</li>
* <li>plot</li>
* </ol>
*/
public class DotGraph extends AbstractDotGraphElement implements Renderable {
private static final Logger logger = LoggerFactory.getLogger(DotGraph.class);
/**
* The extension added to output files, exported so that clients can search for the filenames.
*/
public static final String DOT_EXTENSION = ".dot";
private final HashMap<String, DotGraphNode> nodes;
/* draw elements are sub graphs, edges, commands */
private final List<Renderable> drawElements;
private final boolean isSubGraph;
private boolean dontQuoteNodeNames;
private String graphname;
private DotGraph(String graphname, boolean isSubGraph) {
this.graphname = graphname;
this.isSubGraph = isSubGraph;
this.nodes = new HashMap<String, DotGraphNode>(100);
this.drawElements = new LinkedList<Renderable>();
this.dontQuoteNodeNames = false;
}
/**
* Creates a new graph for drawing.
*
* @param graphname,
* the name used to identify the graph in the dot source.
*/
public DotGraph(String graphname) {
this(graphname, false);
}
/**
* Generates the drawing on canvas to the dot file.
*
* @param filename
* the name for the output file. By convention, it should end with DOT_EXTENSION, but this is not enforced.
*/
public void plot(String filename) {
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename))) {
render(out, 0);
} catch (IOException ioe) {
logger.debug(ioe.getMessage());
}
}
/**
* Draws a directed edge (including the source and end nodes, if they have not already been drawn).
*
* @param from,
* the source node
* @param to,
* the end node
* @return a directed graph edge connecting {@code from} with {@code to}
*/
public DotGraphEdge drawEdge(String from, String to) {
return drawEdge(from, to, true);
}
/**
* Draws a undirected edge (including the nodes, if they have not already been drawn).
*
* @param node1
* @param node2
*
* @return an undirected graph edge connecting {@code node1} with {@code node2}
*/
public DotGraphEdge drawUndirectedEdge(String node1, String node2) {
return drawEdge(node1, node2, false);
}
private DotGraphEdge drawEdge(String from, String to, boolean directed) {
DotGraphNode src = drawNode(from);
DotGraphNode dst = drawNode(to);
DotGraphEdge edge = new DotGraphEdge(src, dst, directed);
this.drawElements.add(edge);
return edge;
}
/**
* Draws a node.
*
* @param name,
* the node to draw.
* @return the {@link DotGraphNode} corresponding to the specified name.
*/
public DotGraphNode drawNode(String name) {
DotGraphNode node = getNode(name);
if (node == null) {
throw new RuntimeException("Assertion failed.");
}
if (!this.drawElements.contains(node)) {
this.drawElements.add(node);
}
return node;
}
/**
* Gets the graph node by name.
*
* @param name,
* unique name of the node.
* @return the node with the specified name, adding a new node to the graph if there is no such node.
*/
public DotGraphNode getNode(String name) {
if (name == null) {
return null;
}
DotGraphNode node = nodes.get(name);
if (node == null) {
node = new DotGraphNode(name, dontQuoteNodeNames);
nodes.put(name, node);
}
return node;
}
/**
* Checks if the graph already contains the node with the specified name.
*
* @param name,
* unique name of the node.
*
* @return true if the node with the specified name is already in the graph, false otherwise
*/
public boolean containsNode(String name) {
return name != null && nodes.containsKey(name);
}
/**
* Checks if the graph already contains the given {@link DotGraphNode}.
*
* @param node
*
* @return true if the node is already in the graph, false otherwise
*/
public boolean containsNode(DotGraphNode node) {
return this.drawElements.contains(node);
}
/**
* NOTE: default is true
*
* @param value
*/
public void quoteNodeNames(boolean value) {
this.dontQuoteNodeNames = !value;
}
/**
* Sets all node shapes, see the list of node shapes in DotGraphConstants.
*
* @param shape,
* the node shape
*/
public void setNodeShape(String shape) {
String command = "node [shape=" + shape + "];";
this.drawElements.add(new DotGraphCommand(command));
}
/**
* Sets all node styles
*
* @param style,
* the node style
*/
public void setNodeStyle(String style) {
String command = "node [style=" + style + "];";
this.drawElements.add(new DotGraphCommand(command));
}
/**
* sets the size of drawing area, in inches
*/
public void setGraphSize(double width, double height) {
String size = "\"" + width + "," + height + "\"";
this.setAttribute("size", size);
}
/**
* sets the pages size, once this is set, the generated graph will be broken into several pages.
*/
public void setPageSize(double width, double height) {
String size = "\"" + width + ", " + height + "\"";
this.setAttribute("page", size);
}
/**
* sets the graph rotation angles
*/
public void setOrientation(String orientation) {
this.setAttribute("orientation", orientation);
}
/**
* sets the graph name
*/
public void setGraphName(String name) {
this.graphname = name;
}
/**
* NOTE: Alias for {@link #setLabel(java.lang.String)}.
*/
public void setGraphLabel(String label) {
this.setLabel(label);
}
/**
* sets any general attributes
*
* NOTE: Alias for {@link #setAttribute(java.lang.String, java.lang.String)}.
*
* @param id
* is the attribute name.
* @param value
* is the attribute value.
*/
public void setGraphAttribute(String id, String value) {
this.setAttribute(id, value);
}
/**
* sets any general attributes
*
* NOTE: Alias for {@link #setAttribute(soot.util.dot.DotGraphAttribute)}.
*
* @param attr
* a {@link DotGraphAttribute} specifying the attribute name and value.
*/
public void setGraphAttribute(DotGraphAttribute attr) {
this.setAttribute(attr);
}
/**
* creates a sub graph.
*
* NOTE: Some renderers require subgraph labels to start with "cluster".
*
* @return the newly created sub graph.
*/
public DotGraph createSubGraph(String label) {
// file name is used as label of sub graph.
DotGraph subgraph = new DotGraph(label, true);
this.drawElements.add(subgraph);
return subgraph;
}
/* implements renderable interface. */
@Override
public void render(OutputStream out, int indent) throws IOException {
// header
if (!isSubGraph) {
DotGraphUtility.renderLine(out, "digraph \"" + this.graphname + "\" {", indent);
} else {
DotGraphUtility.renderLine(out, "subgraph \"" + this.graphname + "\" {", indent);
}
/* render graph attributes */
for (DotGraphAttribute attr : this.getAttributes()) {
DotGraphUtility.renderLine(out, attr.toString() + ';', indent + 4);
}
/* render elements */
for (Renderable element : this.drawElements) {
element.render(out, indent + 4);
}
// close the description
DotGraphUtility.renderLine(out, "}", indent);
}
}