Visualising Java Data Structures as Graphs John Hamer Department of Computer Science University of Auckland J.Hamer@cs.auckland.ac.nz John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 1/21
The Idea Student code calls the static method Dot.drawGraph(whatever) whatever can be any Java object. Dot.drawGraph traverses the object s fields using Java reflection outputs a GraphViz format graph description to a text file runs the GraphViz processor to produce a PNG (or EPS, etc.) picture Student views the sequence of pictures using a standard viewer John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 2/21
Example public static void main( String[] args ) { List xs = new LinkedList( ); for( int i = 0; i < 4; i++ ) { } } Dot.drawGraph( xs ); xs.add( new Integer(i+100) ); John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 3/21
Frame #1 LinkedList size: 0 header Entry element: null next previous John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 4/21
Frame #2 LinkedList size: 1 header Entry element: null next previous next previous Entry element: 100 John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 5/21
Frame #3 next LinkedList size: 2 header Entry element: null previous Entry element: 100 previous next next previous Entry element: 101 John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 6/21
Frame #4 LinkedList size: 3 header Entry element: null next previous Entry element: 100 next previous previous next Entry element: 101 next previous Entry element: 102 John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 7/21
Frame #4 coloured LinkedList size: 3 header Entry element: null next previous Entry element: 100 next previous previous next Entry element: 101 next previous Entry element: 102 Dot.Context ctx = Dot.defaultContext( ); ctx.setfieldattribute( "next", "color=blue" ); ctx.setfieldattribute( "previous", "color=red" ); John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 8/21
About GraphViz GraphViz is a widely used, freely available graph drawing program, developed at ATT; see www.graphviz.org Layout is completely automatic and (generally) æsthetically pleasing. Text input for nodes and edges, with optional attributes (colour, node shape, labels, fonts, etc.). Output to a variety of formats (PNG, EPS, SVG,... ) John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 9/21
Related work GraphViz Brocard Perl interface to GraphViz for visualising data structures; also regular expressions, grammars, XML, call graph, profiling,.... Visualisation North & Koutsofios visual debugger, vdbx Thomas Naps Visualiser class. Canned collection of visualisations: numeric arrays (bar, scattergram, data views), general arrays, stacks, queues, linked lists, binary trees, general trees, graphs, networks. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 10/21
Principles Students must be engaged in active learning; tools need to be simple to use; avoid distracting students from substantive course material; for instructors, minimise the effort required to integrate tools into the curriculum; software must be reliable. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 11/21
Features of our tool trivial to setup and easy to use (source < 600 lines); active learning students decide where to place the calls to drawgraph, what to elide; connects code with the Java data model; usable on any Java program; no specific programming conventions necessary; allows wrong data structures to be viewed (as well as correct ones); configuration allows broad and precise elision of detail; visualisations can be incorporated in reports, www pages, and presentations. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 12/21
Overcoming student misconceptions Java has a simple data model,? Strings are objects, but string constants look like primitive values. Assignment of objects is by reference, primitive types by value. Object arrays hold references, not values. 2-dimensional arrays are constructed from 1-d arrays (is it row or column order?) Static fields are not part of any object. Inheritance means objects are often not the same as their declared types. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 13/21
Visualising the Java data model HashMap size: 3 threshold: 8 loadfactor: 2.0 modcount: 3 table Arrays are displayed with elements juxtaposed. Values in primitive arrays are shown inline. Object arrays just contain links. 0 2 Primitive fields are shown inside the object s node. Entry key: three value: 3 hash: -741826716 Entry key: two value: 2 hash: -1000502134 Object fields are shown as labelled arcs. next Entry key: one value: 1 hash: -953555362 John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 14/21
Degrees of faithfulness Three different views of String Show the full internal state of String. Acknowledge String is an object, but hide the internal state. Pretend String is a primitive value (not an object). These views apply to any object, not just String. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 15/21
The Full Monty String x = "Hello"; String y = new String(x); Dot.drawGraph( new String[]{ x, y } ); String 0 offset: 0 count: 5 hash: 0 value String H e l l o 1 offset: 0 count: 5 hash: 0 value + Useful in explaining the memory consumption of substring operations, or as an example of a sharing data structure. Clutters the visualisation. Details are a distraction (e.g., explaining hash). John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 16/21
Hide the internal state Hello 0 1 Hello + Visualisation respects reference semantics. + More compact. Internal sharing is not shown. Can be used with any object, by calling the tostring method. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 17/21
Pretend it s primitive Hello Hello + Most compact. Visualisation contradicts reference semantics. Can be used with any object, by calling the tostring method. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 18/21
Limitations and future work GraphViz has limited support for node shapes, label placement,.... Graphs of, e.g., Java AWT components, can be immense. Drawing even a simple Button will bring in every interface component! Work in progress on integration with a debugger (Jacob Tseng). Extended a Java IDE debugger with a draw command. Graphs are updated at each breakpoint. Also, draw command extension to the BeanShell (an interactive Java interpreter), provided by a first-year student. More elision controls. Experimental features for dynamically selecting attributes (e.g., red nodes in a red-black tree are displayed in red). Interactive graphs select a node and expand or elide. John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 19/21
Summary and conclusions Light-weight, general purpose visualisation tool for Java. Useful in elucidating the Java data model, especially reference semantics. Less suitable for classical array data structures (c.f., Naps), or OOP (but see, e.g., UMLGraph http://www.spinellis.gr/sw/umlgraph/) Freely available from http://www.cs.auckland.ac.nz/~j-hamer John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 20/21
A view of an Arne Tree {0->"0",1->"1",10->"10",11->"11",12->"12",13->"13",14->"14",15->"15",16->"16",17->"17",18->"18",19->"19",2->"2",3->"3",4->"4",5->"5",6->"6",7->"7",8->"8",9->"9",a->"a",b->"b"} root key: 3 value: "3" level: 4 key: 11 value: "11" level: 3 key: 15 value: "15" level: 3 key: 7 value: "7" level: 3 key: 17 value: "17" level: 2 key: a value: "a" level: 2 key: 1 value: "1" level: 2 key: 13 value: "13" level: 2 key: 19 value: "19" level: 2 key: 5 value: "5" level: 2 key: 8 value: "8" key: 0 value: "0" key: 10 value: "10" key: 12 value: "12" key: 14 value: "14" key: 16 value: "16" key: 18 value: "18" key: 2 value: "2" key: 4 value: "4" key: 6 value: "6" key: 9 value: "9" key: b value: "b" key: b value: null level: -1 John Hamer, January 15, 2004 ACE 2004 Visualising Java Data Structures as Graphs p. 21/21