Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java@8dc73c226c63 |
children | 5bc7f7b867ab |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.nodes; | |
26 | |
27 import java.io.*; | |
28 import java.lang.annotation.*; | |
29 import java.net.*; | |
30 import java.util.*; | |
31 | |
32 import javax.xml.parsers.*; | |
33 import javax.xml.transform.*; | |
34 import javax.xml.transform.dom.*; | |
35 import javax.xml.transform.stream.*; | |
36 | |
37 import org.w3c.dom.*; | |
38 | |
39 import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; | |
40 | |
41 /** | |
42 * Utility class for creating output for the ideal graph visualizer. | |
43 */ | |
44 public class GraphPrintVisitor { | |
45 | |
46 public static final String GraphVisualizerAddress = "127.0.0.1"; | |
47 public static final int GraphVisualizerPort = 4444; | |
48 | |
49 private Document dom; | |
50 private Map<Object, Element> nodeMap; | |
51 private List<Element> edgeList; | |
52 private Map<Object, Element> prevNodeMap; | |
53 private int id; | |
54 private Element graphDocument; | |
55 private Element groupElement; | |
56 private Element graphElement; | |
57 private Element nodesElement; | |
58 private Element edgesElement; | |
59 | |
60 public GraphPrintVisitor() { | |
61 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
62 try { | |
63 DocumentBuilder db = dbf.newDocumentBuilder(); | |
64 | |
65 dom = db.newDocument(); | |
66 } catch (ParserConfigurationException ex) { | |
67 throw new RuntimeException(ex); | |
68 } | |
69 | |
70 graphDocument = dom.createElement("graphDocument"); | |
71 dom.appendChild(graphDocument); | |
72 } | |
73 | |
74 public GraphPrintVisitor beginGroup(String groupName) { | |
75 groupElement = dom.createElement("group"); | |
76 graphDocument.appendChild(groupElement); | |
77 Element properties = dom.createElement("properties"); | |
78 groupElement.appendChild(properties); | |
79 | |
80 if (!groupName.isEmpty()) { | |
81 // set group name | |
82 Element propName = dom.createElement("p"); | |
83 propName.setAttribute("name", "name"); | |
84 propName.setTextContent(groupName); | |
85 properties.appendChild(propName); | |
86 } | |
87 | |
88 // forget old nodes | |
89 nodeMap = prevNodeMap = null; | |
90 edgeList = null; | |
91 | |
92 return this; | |
93 } | |
94 | |
95 public GraphPrintVisitor beginGraph(String graphName) { | |
96 if (null == groupElement) { | |
97 beginGroup(""); | |
98 } else if (null != prevNodeMap) { | |
99 // TODO: difference (create removeNode,removeEdge elements) | |
100 } | |
101 | |
102 graphElement = dom.createElement("graph"); | |
103 groupElement.appendChild(graphElement); | |
104 Element properties = dom.createElement("properties"); | |
105 graphElement.appendChild(properties); | |
106 nodesElement = dom.createElement("nodes"); | |
107 graphElement.appendChild(nodesElement); | |
108 edgesElement = dom.createElement("edges"); | |
109 graphElement.appendChild(edgesElement); | |
110 | |
111 // set graph name | |
112 Element propName = dom.createElement("p"); | |
113 propName.setAttribute("name", "name"); | |
114 propName.setTextContent(graphName); | |
115 properties.appendChild(propName); | |
116 | |
117 // save old nodes | |
118 prevNodeMap = nodeMap; | |
119 nodeMap = new IdentityHashMap<>(); | |
120 edgeList = new ArrayList<>(); | |
121 | |
122 return this; | |
123 } | |
124 | |
125 @Override | |
126 public String toString() { | |
127 if (null != dom) { | |
128 try { | |
129 Transformer tr = TransformerFactory.newInstance().newTransformer(); | |
130 tr.setOutputProperty(OutputKeys.INDENT, "yes"); | |
131 tr.setOutputProperty(OutputKeys.METHOD, "xml"); | |
132 tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); | |
133 | |
134 StringWriter strWriter = new StringWriter(); | |
135 tr.transform(new DOMSource(dom), new StreamResult(strWriter)); | |
136 return strWriter.toString(); | |
137 } catch (TransformerException e) { | |
138 e.printStackTrace(); | |
139 } | |
140 } | |
141 return ""; | |
142 } | |
143 | |
144 public void printToFile(File f) { | |
145 try { | |
146 Transformer tr = TransformerFactory.newInstance().newTransformer(); | |
147 tr.setOutputProperty(OutputKeys.INDENT, "yes"); | |
148 tr.setOutputProperty(OutputKeys.METHOD, "xml"); | |
149 tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); | |
150 | |
151 tr.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(f))); | |
152 } catch (TransformerException | FileNotFoundException e) { | |
153 e.printStackTrace(); | |
154 } | |
155 } | |
156 | |
157 public void printToSysout() { | |
158 try { | |
159 Transformer tr = TransformerFactory.newInstance().newTransformer(); | |
160 tr.setOutputProperty(OutputKeys.INDENT, "yes"); | |
161 tr.setOutputProperty(OutputKeys.METHOD, "xml"); | |
162 tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); | |
163 | |
164 tr.transform(new DOMSource(dom), new StreamResult(System.out)); | |
165 } catch (TransformerException e) { | |
166 e.printStackTrace(); | |
167 } | |
168 } | |
169 | |
170 public void printToNetwork(boolean ignoreErrors) { | |
171 try { | |
172 Transformer tr = TransformerFactory.newInstance().newTransformer(); | |
173 tr.setOutputProperty(OutputKeys.METHOD, "xml"); | |
174 | |
175 Socket socket = new Socket(GraphVisualizerAddress, GraphVisualizerPort); | |
176 BufferedOutputStream stream = new BufferedOutputStream(socket.getOutputStream(), 0x4000); | |
177 tr.transform(new DOMSource(dom), new StreamResult(stream)); | |
178 } catch (TransformerException | IOException e) { | |
179 if (!ignoreErrors) { | |
180 e.printStackTrace(); | |
181 } | |
182 } | |
183 } | |
184 | |
185 private String nextId() { | |
186 return String.valueOf(id++); | |
187 } | |
188 | |
189 private String oldOrNextId(Object node) { | |
190 if (null != prevNodeMap && prevNodeMap.containsKey(node)) { | |
191 Element nodeElem = prevNodeMap.get(node); | |
192 return nodeElem.getAttribute("id"); | |
193 } else { | |
194 return nextId(); | |
195 } | |
196 } | |
197 | |
198 protected Element getElementByObject(Object op) { | |
199 return nodeMap.get(op); | |
200 } | |
201 | |
202 protected void createElementForNode(Object node) { | |
203 boolean exists = nodeMap.containsKey(node); | |
204 if (!exists || NodeUtil.findAnnotation(node.getClass(), GraphDuplicate.class) != null) { | |
205 Element nodeElem = dom.createElement("node"); | |
206 nodeElem.setAttribute("id", !exists ? oldOrNextId(node) : nextId()); | |
207 nodeMap.put(node, nodeElem); | |
208 Element properties = dom.createElement("properties"); | |
209 nodeElem.appendChild(properties); | |
210 nodesElement.appendChild(nodeElem); | |
211 | |
212 setNodeProperty(node, "name", node.getClass().getSimpleName().replaceFirst("Node$", "")); | |
213 NodeInfo nodeInfo = node.getClass().getAnnotation(NodeInfo.class); | |
214 if (nodeInfo != null) { | |
215 setNodeProperty(node, "cost", nodeInfo.cost()); | |
216 if (!nodeInfo.shortName().isEmpty()) { | |
217 setNodeProperty(node, "shortName", nodeInfo.shortName()); | |
218 } | |
219 } | |
220 setNodeProperty(node, "class", node.getClass().getSimpleName()); | |
221 if (node instanceof Node) { | |
222 readNodeProperties((Node) node); | |
223 copyDebugProperties((Node) node); | |
224 } | |
225 } | |
226 } | |
227 | |
228 private Element getPropertyElement(Object node, String propertyName) { | |
229 Element nodeElem = getElementByObject(node); | |
230 Element propertiesElem = (Element) nodeElem.getElementsByTagName("properties").item(0); | |
231 if (propertiesElem == null) { | |
232 return null; | |
233 } | |
234 | |
235 NodeList propList = propertiesElem.getElementsByTagName("p"); | |
236 for (int i = 0; i < propList.getLength(); i++) { | |
237 Element p = (Element) propList.item(i); | |
238 if (propertyName.equals(p.getAttribute("name"))) { | |
239 return p; | |
240 } | |
241 } | |
242 return null; | |
243 } | |
244 | |
245 protected void setNodeProperty(Object node, String propertyName, Object value) { | |
246 Element nodeElem = getElementByObject(node); | |
247 Element propElem = getPropertyElement(node, propertyName); // if property exists, replace | |
248 // its value | |
249 if (null == propElem) { // if property doesn't exist, create one | |
250 propElem = dom.createElement("p"); | |
251 propElem.setAttribute("name", propertyName); | |
252 nodeElem.getElementsByTagName("properties").item(0).appendChild(propElem); | |
253 } | |
254 propElem.setTextContent(String.valueOf(value)); | |
255 } | |
256 | |
257 private void copyDebugProperties(Node node) { | |
258 Map<String, Object> debugProperties = node.getDebugProperties(); | |
259 for (Map.Entry<String, Object> property : debugProperties.entrySet()) { | |
260 setNodeProperty(node, property.getKey(), property.getValue()); | |
261 } | |
262 } | |
263 | |
264 private void readNodeProperties(Node node) { | |
265 NodeFieldAccessor[] fields = node.getNodeClass().getFields(); | |
266 for (NodeFieldAccessor field : fields) { | |
267 if (field.getKind() == NodeFieldKind.DATA) { | |
268 String key = field.getName(); | |
269 if (getPropertyElement(node, key) == null) { | |
270 Object value = field.loadValue(node); | |
271 setNodeProperty(node, key, value); | |
272 } | |
273 } | |
274 } | |
275 } | |
276 | |
277 protected void connectNodes(Object a, Object b, String label) { | |
278 if (nodeMap.get(a) == null || nodeMap.get(b) == null) { | |
279 return; | |
280 } | |
281 | |
282 String fromId = nodeMap.get(a).getAttribute("id"); | |
283 String toId = nodeMap.get(b).getAttribute("id"); | |
284 | |
285 // count existing to-edges | |
286 int count = 0; | |
287 for (Element e : edgeList) { | |
288 if (e.getAttribute("to").equals(toId)) { | |
289 ++count; | |
290 } | |
291 } | |
292 | |
293 Element edgeElem = dom.createElement("edge"); | |
294 edgeElem.setAttribute("from", fromId); | |
295 edgeElem.setAttribute("to", toId); | |
296 edgeElem.setAttribute("index", String.valueOf(count)); | |
297 if (label != null) { | |
298 edgeElem.setAttribute("label", label); | |
299 } | |
300 edgesElement.appendChild(edgeElem); | |
301 edgeList.add(edgeElem); | |
302 } | |
303 | |
304 public GraphPrintVisitor visit(Object node) { | |
305 if (null == graphElement) { | |
306 beginGraph("truffle tree"); | |
307 } | |
308 | |
309 // if node is visited once again, skip | |
310 if (getElementByObject(node) != null && NodeUtil.findAnnotation(node.getClass(), GraphDuplicate.class) == null) { | |
311 return this; | |
312 } | |
313 | |
314 // respect node's custom handler | |
315 if (NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class) != null) { | |
316 Class<? extends GraphPrintHandler> customHandlerClass = NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class).handler(); | |
317 try { | |
318 GraphPrintHandler customHandler = customHandlerClass.newInstance(); | |
319 customHandler.visit(node, new GraphPrintAdapter()); | |
320 } catch (InstantiationException | IllegalAccessException e) { | |
321 assert false : e; | |
322 } | |
323 } else if (NodeUtil.findAnnotation(node.getClass(), NullGraphPrintHandler.class) != null) { | |
324 // ignore | |
325 } else { | |
326 // default handler | |
327 createElementForNode(node); | |
328 | |
329 if (node instanceof Node) { | |
330 for (Map.Entry<String, Node> child : findNamedNodeChildren((Node) node).entrySet()) { | |
331 visit(child.getValue()); | |
332 connectNodes(node, child.getValue(), child.getKey()); | |
333 } | |
334 } | |
335 } | |
336 | |
337 return this; | |
338 } | |
339 | |
340 private static LinkedHashMap<String, Node> findNamedNodeChildren(Node node) { | |
341 LinkedHashMap<String, Node> nodes = new LinkedHashMap<>(); | |
342 NodeClass nodeClass = node.getNodeClass(); | |
343 | |
344 for (NodeFieldAccessor field : nodeClass.getFields()) { | |
345 NodeFieldKind kind = field.getKind(); | |
346 if (kind == NodeFieldKind.CHILD || kind == NodeFieldKind.CHILDREN) { | |
347 Object value = field.loadValue(node); | |
348 if (value != null) { | |
349 if (kind == NodeFieldKind.CHILD) { | |
350 nodes.put(field.getName(), (Node) value); | |
351 } else if (kind == NodeFieldKind.CHILDREN) { | |
352 Object[] children = (Object[]) value; | |
353 for (int i = 0; i < children.length; i++) { | |
354 if (children[i] != null) { | |
355 nodes.put(field.getName() + "[" + i + "]", (Node) children[i]); | |
356 } | |
357 } | |
358 } | |
359 } | |
360 } | |
361 } | |
362 | |
363 return nodes; | |
364 } | |
365 | |
366 public class GraphPrintAdapter { | |
367 | |
368 public void createElementForNode(Object node) { | |
369 GraphPrintVisitor.this.createElementForNode(node); | |
370 } | |
371 | |
372 public void visit(Object node) { | |
373 GraphPrintVisitor.this.visit(node); | |
374 } | |
375 | |
376 public void connectNodes(Object node, Object child) { | |
377 GraphPrintVisitor.this.connectNodes(node, child, null); | |
378 } | |
379 | |
380 public void setNodeProperty(Object node, String propertyName, Object value) { | |
381 GraphPrintVisitor.this.setNodeProperty(node, propertyName, value); | |
382 } | |
383 } | |
384 | |
385 public interface GraphPrintHandler { | |
386 | |
387 void visit(Object node, GraphPrintAdapter gPrinter); | |
388 } | |
389 | |
390 public interface ChildSupplier { | |
391 | |
392 /** Supplies an additional child if available. */ | |
393 Object startNode(Object callNode); | |
394 | |
395 void endNode(Object callNode); | |
396 | |
397 } | |
398 | |
399 @Retention(RetentionPolicy.RUNTIME) | |
400 @Target(ElementType.TYPE) | |
401 public @interface CustomGraphPrintHandler { | |
402 | |
403 Class<? extends GraphPrintHandler> handler(); | |
404 } | |
405 | |
406 @Retention(RetentionPolicy.RUNTIME) | |
407 @Target(ElementType.TYPE) | |
408 public @interface NullGraphPrintHandler { | |
409 } | |
410 | |
411 @Retention(RetentionPolicy.RUNTIME) | |
412 @Target(ElementType.TYPE) | |
413 public @interface GraphDuplicate { | |
414 } | |
415 | |
416 @Retention(RetentionPolicy.RUNTIME) | |
417 @Target(ElementType.FIELD) | |
418 public @interface HiddenField { | |
419 } | |
420 } |