Reputation: 339
I would like to use GraphViz to generate some graphs in my program. There is just a problem : I don't want to generate any temporary file.
Here is what I got from here :
public class GraphBuilder{
/* Some code generating a DOT formatted String representing my graph */
/* Generation the graph using the plain format */
Runtime runtime = Runtime.getRuntime();
Process p = null;
try {
p = runtime.exec("neato -Tplain c:\\users\\tristan\\desktop\\test1");
} catch (IOException e) {
System.err.println(e.getMessage());
}
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
try{
while ( (line = br.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
} catch(IOException e){
}
String result = builder.toString();
System.out.println(result);
Ok so, from now, my program is reading a file but I want the neato program to read my String that had been generated earlier by the program.
How should I do ?
Thanks in advance !
Upvotes: 3
Views: 2145
Reputation: 29
You can use GraphvizV8Engine()
for this purpose. This will not create the temp files create by cmdLineEngine
.
Graphviz.useEngine(new GraphvizV8Engine());
This will initialize javascript to display your graph.
2022-11-29 15:01:57.628 INFO 19208 --- [nio-8080-exec-2] g.n.graphviz.engine.V8JavascriptEngine : Starting V8 runtime...
2022-11-29 15:01:58.383 INFO 19208 --- [nio-8080-exec-2] g.n.graphviz.engine.V8JavascriptEngine : Started V8 runtime. Initializing javascript...
2022-11-29 15:01:58.386 INFO 19208 --- [nio-8080-exec-2] g.n.graphviz.engine.V8JavascriptEngine : Initialized javascript.
2022-11-29 15:02:02.709 INFO 19208 --- [nio-8080-exec-2] g.n.graphviz.engine.V8JavascriptEngine : Starting V8 runtime...
2022-11-29 15:02:02.774 INFO 19208 --- [nio-8080-exec-2] g.n.graphviz.engine.V8JavascriptEngine : Started V8 runtime. Initializing javascript...
2022-11-29 15:02:02.775 INFO 19208 --- [nio-8080-exec-2] g.n.graphviz.engine.V8JavascriptEngine : Initialized javascript.
Upvotes: 0
Reputation: 41213
Something like:
// Create the process
Process process = new ProcessBuilder("neato", "-Tplain").start();
// Write to process's stdin
OutputStream osToProcess = process.getOutputStream();
PrintWriter pwToProcess = new PrintWriter(osToProcess);
pwToProcess.write("graph G { node1 -- node2; }"); // for example
pwToProcess.close();
// Read from process's stdout
InputStream isFromProcess = process.getInputStream();
BufferedReader brFromProcess = new BufferedReader(isFromProcess);
// do whatever you want with the reader, then...
brFromProcess.close();
// optionally...
process.waitFor();
I omitted exception handling from this example code -- you'll need to put it in to meet your requirements. It may be sufficient to wrap the whole thing in a try/catch block - it depends what you need.
I validated that neato
reads/writes stdin/stdout at the command line:
$ echo 'graph G { n1 -- n2; }' | neato -Tplain
graph 1 1.3667 1.2872
node n1 0.375 0.25 0.75 0.5 n1 solid ellipse black lightgrey
node n2 0.99169 1.0372 0.75 0.5 n2 solid ellipse black lightgrey
edge n1 n2 4 0.55007 0.47348 0.63268 0.57893 0.7311 0.70457 0.81404 0.81044 solid black
stop
Upvotes: 5