1 package fr.orsay.lri.varna.models.treealign;
2 import java.io.BufferedReader;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
12 * This class translates a Tree to a graphviz file.
13 * @author Raphael Champeimont
15 * @param <? extends GraphvizDrawableNodeValue> the type of values in the tree
17 public class TreeGraphviz {
20 * Generates a PostScript file using graphviz.
21 * The dot command must be available.
23 public static void treeToGraphvizPostscript(Tree<? extends GraphvizDrawableNodeValue> tree, String filename, String title) throws IOException {
24 // generate graphviz source
25 String graphvizSource = treeToGraphviz(tree, title);
29 fbw = new BufferedWriter(new FileWriter(filename));
32 Process proc = Runtime.getRuntime().exec("dot -Tps");
33 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
34 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
35 BufferedReader bre = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
36 bw.write(graphvizSource);
40 while ((line = br.readLine()) != null) {
41 fbw.write(line + "\n");
46 while ((line = bre.readLine()) != null) {
47 System.err.println(line);
51 // wait for graphviz to end
54 } catch (InterruptedException e) {
63 * Like treeToGraphvizPostscript(Tree,String,String) but with the title
64 * equal to the filename.
66 public static void treeToGraphvizPostscript(Tree<? extends GraphvizDrawableNodeValue> tree, String filename) throws IOException {
67 treeToGraphvizPostscript(tree, filename, filename);
71 * Creates a graphviz source file from a Tree.
72 * @param title the title of the graph
74 public static void treeToGraphvizFile(Tree<? extends GraphvizDrawableNodeValue> tree, String filename, String title) throws IOException {
76 bw = new BufferedWriter(new FileWriter(filename));
77 bw.write(treeToGraphviz(tree, filename));
82 * Like treeToGraphvizFile(Tree,String,String) but with the title
83 * equal to the filename.
85 public static void treeToGraphvizFile(Tree<? extends GraphvizDrawableNodeValue> tree, String filename) throws IOException {
86 treeToGraphvizFile(tree, filename, filename);
90 * Creates a graphviz source from a Tree.
91 * @param title the title of the graph
93 public static String treeToGraphviz(Tree<? extends GraphvizDrawableNodeValue> tree, String title) {
94 return "digraph \"" + title + "\" {\n" + subtreeToGraphviz(tree) + "}\n";
97 private static String subtreeToGraphviz(Tree<? extends GraphvizDrawableNodeValue> tree) {
99 String myId = tree.toGraphvizNodeId();
105 + ((tree.getValue() != null) ? tree.getValue().toGraphvizNodeName() : "null")
107 for (Tree<? extends GraphvizDrawableNodeValue> child: tree.getChildren()) {
108 s += "\"" + myId + "\" -> \"" + child.toGraphvizNodeId() + "\"\n";
109 s += subtreeToGraphviz(child);