-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GraphToDot.java
31 lines (25 loc) · 1.04 KB
/
GraphToDot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.graylog.plugins.enterprise.search.util;
import com.google.common.graph.Graph;
import info.leadinglight.jdot.Node;
import info.leadinglight.jdot.enums.GraphType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.Function;
public class GraphToDot {
private static final Logger LOG = LoggerFactory.getLogger(GraphToDot.class);
private GraphToDot() {
}
public static <N> String toDot(Graph<N> graph, Function<N, String> idFunction, Function<N, String> labelFunction) {
final info.leadinglight.jdot.Graph g = new info.leadinglight.jdot.Graph("QueryPlan");
g.setType(graph.isDirected() ? GraphType.digraph : GraphType.graph);
graph.nodes().forEach(node -> {
final Node n = new Node(idFunction.apply(node));
n.setLabel(labelFunction.apply(node));
g.addNode(n);
});
graph.edges().forEach(edge -> {
g.addEdge(idFunction.apply(edge.source()), idFunction.apply(edge.target()));
});
return g.toDot();
}
}