AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
Graphviz.java
Go to the documentation of this file.
1package aud.util;
2
3import java.io.*;
4
15public class Graphviz {
16
17 static final Sys.ExternalProgram DOT =
18 new Sys.ExternalProgram("dot (GraphViz graph layout and rendering)",
19 "AUD_DOT",
20 "/usr/bin/dot"+ // standard Unix installation
21 File.pathSeparator+
22 "/usr/local/bin/dot"+ // OS X
23 File.pathSeparator+
24 "/opt/homebrew/bin/dot"+ // macOS Homebrew
25 File.pathSeparator+
26 "/local/usr/bin/dot", // Sun pool
27 true
28 );
29
36 public File renderDotFileToFile(File dotfile,String format) {
37 File out=new File(dotfile.getPath()+"."+format);
38 String command=DOT.getPath()+" -T"+format+" "+dotfile.getPath()+" -o "+out;
39 //System.err.println(command);
40 // note: command is obsolete (used only in error message)
41 try {
42 ProcessBuilder pb = new ProcessBuilder
43 (DOT.getPath(),"-T" + format,dotfile.getPath(),"-o",out.getPath());
44 Process dot=pb.start();
45
46 if (dot.waitFor()!=0) {
47 System.err.println("ERROR: '"+command+"' failed");
48 }
49 }
50 catch (IOException e) {
51 System.err.println("ERROR: "+e.getMessage());
52 return null; // note: probably triggers NullPointerException
53 }
54 catch (InterruptedException e) {
55 System.err.println("ERROR: "+e.getMessage());
56 return null; // note: probably triggers NullPointerException
57 }
58 return out;
59 }
60
65 public void displayAsPDF(String dotCode) {
66 File dotfile=Sys.writeToTempFile(dotCode,".dot");
67 File pdffile=renderDotFileToFile(dotfile,"pdf");
68 //dotfile.delete();
69 Sys.viewPDFFile(pdffile.getPath());
70 }
71
75 public void displayAsPDF(Graphvizable object) {
76 displayAsPDF(object.toDot());
77 }
81 public DotViewer display(String dotCode) {
82 return DotViewer.displayWindow(dotCode,"aud.util.DotViewer");
83 }
88 return DotViewer.displayWindow(object,"aud.util.DotViewer");
89 }
90}
Simple viewer for Graphvizable.
Definition: DotViewer.java:48
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Definition: DotViewer.java:140
Use GraphViz to render graph structures.
Definition: Graphviz.java:15
DotViewer display(String dotCode)
Render and display dotCode.
Definition: Graphviz.java:81
File renderDotFileToFile(File dotfile, String format)
Render dot file.
Definition: Graphviz.java:36
void displayAsPDF(String dotCode)
Render and display dotCode.
Definition: Graphviz.java:65
void displayAsPDF(Graphvizable object)
Display object.
Definition: Graphviz.java:75
DotViewer display(Graphvizable object)
Display object.
Definition: Graphviz.java:87
System related utilities.
Definition: Sys.java:58
static File writeToTempFile(String text, String suffix)
write text to temporary file
Definition: Sys.java:206
static void viewPDFFile(String filename)
open PDF viewer
Definition: Sys.java:255
Interface for GraphViz rendering.