Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java @ 11637:126e20d36563
Truffle IGV printer: add edge labels.
author | Andreas Woess <andreas.woess@jku.at> |
---|---|
date | Sat, 14 Sep 2013 21:18:36 +0200 |
parents | 494b818b527c |
children | b16ec83edc73 |
comparison
equal
deleted
inserted
replaced
11636:136eaa90ef41 | 11637:126e20d36563 |
---|---|
216 if (!nodeInfo.shortName().isEmpty()) { | 216 if (!nodeInfo.shortName().isEmpty()) { |
217 setNodeProperty(node, "shortName", nodeInfo.shortName()); | 217 setNodeProperty(node, "shortName", nodeInfo.shortName()); |
218 } | 218 } |
219 } | 219 } |
220 setNodeProperty(node, "class", node.getClass().getSimpleName()); | 220 setNodeProperty(node, "class", node.getClass().getSimpleName()); |
221 readNodeProperties((Node) node); | 221 if (node instanceof Node) { |
222 copyDebugProperties(node); | 222 readNodeProperties((Node) node); |
223 copyDebugProperties((Node) node); | |
224 } | |
223 } | 225 } |
224 } | 226 } |
225 | 227 |
226 private Element getPropertyElement(Object node, String propertyName) { | 228 private Element getPropertyElement(Object node, String propertyName) { |
227 Element nodeElem = getElementByObject(node); | 229 Element nodeElem = getElementByObject(node); |
250 nodeElem.getElementsByTagName("properties").item(0).appendChild(propElem); | 252 nodeElem.getElementsByTagName("properties").item(0).appendChild(propElem); |
251 } | 253 } |
252 propElem.setTextContent(String.valueOf(value)); | 254 propElem.setTextContent(String.valueOf(value)); |
253 } | 255 } |
254 | 256 |
255 private void copyDebugProperties(Object node) { | 257 private void copyDebugProperties(Node node) { |
256 if (node instanceof Node) { | 258 Map<String, Object> debugProperties = node.getDebugProperties(); |
257 Map<String, Object> debugProperties = ((Node) node).getDebugProperties(); | 259 for (Map.Entry<String, Object> property : debugProperties.entrySet()) { |
258 for (Map.Entry<String, Object> property : debugProperties.entrySet()) { | 260 setNodeProperty(node, property.getKey(), property.getValue()); |
259 setNodeProperty(node, property.getKey(), property.getValue()); | |
260 } | |
261 } | 261 } |
262 } | 262 } |
263 | 263 |
264 private void readNodeProperties(Node node) { | 264 private void readNodeProperties(Node node) { |
265 NodeField[] fields = NodeClass.get(node.getClass()).getFields(); | 265 NodeField[] fields = NodeClass.get(node.getClass()).getFields(); |
272 } | 272 } |
273 } | 273 } |
274 } | 274 } |
275 } | 275 } |
276 | 276 |
277 protected void connectNodes(Object a, Object b) { | 277 protected void connectNodes(Object a, Object b, String label) { |
278 if (nodeMap.get(a) == null || nodeMap.get(b) == null) { | 278 if (nodeMap.get(a) == null || nodeMap.get(b) == null) { |
279 return; | 279 return; |
280 } | 280 } |
281 | 281 |
282 String fromId = nodeMap.get(a).getAttribute("id"); | 282 String fromId = nodeMap.get(a).getAttribute("id"); |
292 | 292 |
293 Element edgeElem = dom.createElement("edge"); | 293 Element edgeElem = dom.createElement("edge"); |
294 edgeElem.setAttribute("from", fromId); | 294 edgeElem.setAttribute("from", fromId); |
295 edgeElem.setAttribute("to", toId); | 295 edgeElem.setAttribute("to", toId); |
296 edgeElem.setAttribute("index", String.valueOf(count)); | 296 edgeElem.setAttribute("index", String.valueOf(count)); |
297 if (label != null) { | |
298 edgeElem.setAttribute("label", label); | |
299 } | |
297 edgesElement.appendChild(edgeElem); | 300 edgesElement.appendChild(edgeElem); |
298 edgeList.add(edgeElem); | 301 edgeList.add(edgeElem); |
299 } | 302 } |
300 | 303 |
301 public GraphPrintVisitor visit(Object node) { | 304 public GraphPrintVisitor visit(Object node) { |
302 if (null == graphElement) { | 305 if (null == graphElement) { |
303 beginGraph("truffle tree"); | 306 beginGraph("truffle tree"); |
304 } | 307 } |
305 | 308 |
306 // if node is visited once again, skip | 309 // if node is visited once again, skip |
307 if (getElementByObject(node) == null || NodeUtil.findAnnotation(node.getClass(), GraphDuplicate.class) != null) { | 310 if (getElementByObject(node) != null && NodeUtil.findAnnotation(node.getClass(), GraphDuplicate.class) == null) { |
308 visitAny(node); | 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 } | |
309 } | 335 } |
310 | 336 |
311 return this; | 337 return this; |
312 } | 338 } |
313 | 339 |
314 private void visitAny(Object node) { | 340 private static LinkedHashMap<String, Node> findNamedNodeChildren(Node node) { |
315 // respect node's custom handler | 341 LinkedHashMap<String, Node> nodes = new LinkedHashMap<>(); |
316 if (NodeUtil.findAnnotation(node.getClass(), NullGraphPrintHandler.class) != null) { | 342 NodeClass nodeClass = NodeClass.get(node.getClass()); |
317 return; | 343 |
318 } | 344 for (NodeField field : nodeClass.getFields()) { |
319 if (NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class) != null) { | 345 NodeFieldKind kind = field.getKind(); |
320 Class<? extends GraphPrintHandler> gpHandlerClass = NodeUtil.findAnnotation(node.getClass(), CustomGraphPrintHandler.class).handler(); | 346 if (kind == NodeFieldKind.CHILD || kind == NodeFieldKind.CHILDREN) { |
321 try { | 347 Object value = field.loadValue(node); |
322 GraphPrintHandler gpHandler = gpHandlerClass.newInstance(); | 348 if (value != null) { |
323 gpHandler.visit(node, new GraphPrintAdapter()); | 349 if (kind == NodeFieldKind.CHILD) { |
324 } catch (InstantiationException e) { | 350 nodes.put(field.getName(), (Node) value); |
325 assert false; | 351 } else if (kind == NodeFieldKind.CHILDREN) { |
326 } catch (IllegalAccessException e) { | 352 Object[] children = (Object[]) value; |
327 assert false; | 353 for (int i = 0; i < children.length; i++) { |
328 } | 354 if (children[i] != null) { |
329 return; | 355 nodes.put(field.getName() + "[" + i + "]", (Node) children[i]); |
330 } | 356 } |
331 | 357 } |
332 // default handler | 358 } |
333 createElementForNode(node); | 359 } |
334 | 360 } |
335 List<Node> children = NodeUtil.findNodeChildren((Node) node); | 361 } |
336 for (Object child : children) { | 362 |
337 if (child == null) { | 363 return nodes; |
338 continue; | |
339 } else if (child instanceof Node) { | |
340 visit(child); | |
341 } else { | |
342 continue; | |
343 } | |
344 connectNodes(node, child); | |
345 } | |
346 } | 364 } |
347 | 365 |
348 public class GraphPrintAdapter { | 366 public class GraphPrintAdapter { |
349 | 367 |
350 public void createElementForNode(Object node) { | 368 public void createElementForNode(Object node) { |
354 public void visit(Object node) { | 372 public void visit(Object node) { |
355 GraphPrintVisitor.this.visit(node); | 373 GraphPrintVisitor.this.visit(node); |
356 } | 374 } |
357 | 375 |
358 public void connectNodes(Object node, Object child) { | 376 public void connectNodes(Object node, Object child) { |
359 GraphPrintVisitor.this.connectNodes(node, child); | 377 GraphPrintVisitor.this.connectNodes(node, child, null); |
360 } | 378 } |
361 | 379 |
362 public void setNodeProperty(Object node, String propertyName, Object value) { | 380 public void setNodeProperty(Object node, String propertyName, Object value) { |
363 GraphPrintVisitor.this.setNodeProperty(node, propertyName, value); | 381 GraphPrintVisitor.this.setNodeProperty(node, propertyName, value); |
364 } | 382 } |