
package galileo.graph;

import java.io.OutputStream;
import java.io.PrintStream;

/**
 * Provides functionality for visualizing graphs using Graphviz.  This class
 * outputs the graphs in DOT format.
 *
 * @author malensek
 */
public class DOT {

    /**
     * Traverses the graph provided by a root vertex, writing out the graph
     * representation in DOT format.
     *
     * @param vertex root vertex of the graph to convert
     * @param out OutputStream to write the graph to.
     */
    public static void toDOT(Vertex<?, ?> vertex, OutputStream out) {
        DOT.toDOT(vertex, "Untitled", out);
    }

    /**
     * Traverses the graph provided by a root vertex, writing out the graph
     * representation in DOT format.
     *
     * @param vertex root vertex of the graph to convert
     * @param title title of the graph
     * @param out OutputStream to write the graph to.
     */
    public static void toDOT(Vertex<?, ?> vertex, String title,
            OutputStream out) {

        PrintStream ps = new PrintStream(out);
        writeGraphHeader(title, ps);
        writeVertex(vertex, ps);
        writeGraphFooter(ps);
    }

    private static void writeVertex(Vertex<?, ?> v, PrintStream ps) {
        ps.println("\"" + v.hashCode() + "\""
                + " [label=\"" + v.getLabel() + "\"];");

        for (Vertex<?, ?> child : v.getAllNeighbors()) {
            connect(v.hashCode(), child.hashCode(), ps);
        }

        for (Vertex<?, ?> child : v.getAllNeighbors()) {
            writeVertex(child, ps);
        }
    }

    private static void connect(int from, int to, PrintStream ps) {
        ps.println("\"" + from + "\" -- \"" + to + "\"");
    }

    private static void writeGraphHeader(String title, PrintStream ps) {
        ps.println("graph " + title + " {");
        ps.println("node [fontname=\"Helvetica\" fontsize=\"20\"]");
    }

    private static void writeGraphFooter(PrintStream ps) {
        ps.println("}");
    }

}

@@ -1138,32 +1138,100 @@
 ter(ps);%0A    %7D%0A%0A
+    /**%0A     * Writes a vertex's information to a DOT file.%0A     */%0A
     private stat
@@ -1631,136 +1631,545 @@
     
-private static void connect(int from, int to, PrintStream ps) %7B%0A        ps.println(%22%5C%22%22 + from + %22%5C%22 -- %5C%22%22 + to + %22%5C%22%22);%0A    %7D%0A
+/**%0A     * Connects two vertices in DOT format (v1 -- v2).%0A     *%0A     * @param from source vertex.  Generally the hashCode() of the object can be%0A     * used as a unique identifier for the vertex.%0A     * @param to destination vertex.  In general, use hashCode().%0A     */%0A    private static void connect(int from, int to, PrintStream ps) %7B%0A        ps.println(%22%5C%22%22 + from + %22%5C%22 -- %5C%22%22 + to + %22%5C%22%22);%0A    %7D%0A%0A    /**%0A     * Writes graph header information, such as the graph title and settings.%0A     *%0A     * @param title Graph title%0A     */
 %0A   
@@ -2355,24 +2355,147 @@
 %5D%22);%0A    %7D%0A%0A
+    /**%0A     * Write any concluding DOT format information.  This includes the closing%0A     * brace for the graph.%0A     */%0A
     private 

Improve DOT export javadoc