åå ã¯ãJGraphT ã¨ããã°ã©ãçè«ã®ã©ã¤ãã©ãªã Gradle ããã¸ã§ã¯ãã§å®è¡ãã¦ãGraphViz ã§å¯è¦åããã¨ããã¾ã§ããã¾ããã
ä»åã¯ãååã®è£è¶³ã¨ãã¦ããµã³ãã«ã½ã¼ã¹ã®è§£èª¬ã¨ãå¯è¦åã®ã½ã¼ã¹ãå®æããããã¨æãã¾ãã
ããã§ã¯ãã£ã¦ããã¾ãï¼
åèæç®
ã¯ããã«
ãJavaã§ãã¶ã¤ã³ãã¿ã¼ã³ãå¦ã¶ãã®è¨äºä¸è¦§ã§ããè¯ãã£ããåèã«ãã¦ãã ããã
Javaã§ãã¶ã¤ã³ãã¿ã¼ã³ã®è¨äºä¸è¦§
JGraphT ã®å
¬å¼ãµã¤ãã¯ä»¥ä¸ã§ãã
jgrapht.org
ããã§ã¯ãã£ã¦ããã¾ãï¼
HelloJGraphT.javaã®èª¬æ
ç°¡åã« HelloJGraphT.java ã®ã¡ã¤ã³ã®å
容ã説æãã¦ããã¾ãã
HelloJGraphT ã¯ã©ã¹ã®å
é ã§ãã
final
ãªã®ã§ãHelloJGraphT ã¯ã©ã¹ãæ´¾çããããã¨ã¯åºæ¥ã¾ããã
ã¾ããã³ã³ã¹ãã©ã¯ã¿ã private
ã«ãããã¨ã§ã¤ã³ã¹ã¿ã³ã¹åãé²ããã¯ã©ã¹ã¡ã½ããã ãã使ãããã«ãã¦ãã¾ãã
public final class HelloJGraphT
{
private HelloJGraphT()
{
}
ã¯ã©ã¹ã¡ã½ããã¨ãã¦ãmain
ã¡ã½ããã®å®ç¾©ã§ãã
æåã¯ãString 㨠DefaultEdge ã® Graph ãä½ãã¡ã½ããã® createStringGraph()
ãå®è¡ãã¦ãã¾ãã
DefaultEdge ã¯ãååã®éããç¹å¥ãªç¨éãå¿
è¦ãªãã¨ãã®ããã©ã«ãã®ã¨ãã¸ï¼è¾ºï¼ã§ãã
public static void main(String[] args)
throws URISyntaxException, ExportException
{
Graph<String, DefaultEdge> stringGraph = createStringGraph();
System.out.println("-- toString output");
System.out.println(stringGraph.toString());
System.out.println();
次ã¯ãURI 㨠DefaultEdge ã® Graph ãä½ãã¡ã½ããã® createHrefGraph()
ãå®è¡ãã¦ãã¾ãã
URIï¼Uniform Resource Indentifierï¼ã¯ã©ã¹ã¯ãJava ã«ç¨æããã¦ããã¯ã©ã¹ã§ãURL ã¨åããã¢ãã¬ã¹ãæ±ãã¯ã©ã¹ã§ãã
ãã®å¾ã¯é£ããã§ããã次ã«ä½¿ãã®ã§ããã¼ãï¼é ç¹ï¼ãã "www.jgrapht.org" ãæ¤ç´¢ãã¦ãã¾ãã
Graph<URI, DefaultEdge> hrefGraph = createHrefGraph();
URI start = hrefGraph
.vertexSet().stream().filter(uri -> uri.getHost().equals("www.jgrapht.org")).findAny()
.get();
ã¡ã¤ã³ã®æå¾ã§ãã
å
ã»ã©æ¤ç´¢ãããã¼ããèµ·ç¹ã¨ãã¦ãã°ã©ããæ·±ãåªå
é ã«èµ°æ»ãã¦ãé ç¹ãåºåãã¾ãã
æå¾ã¯ãååãã使ã£ã¦ããå¯è¦åã®ã¡ã½ãããå®è¡ãã¦ãã¾ãï½ã
System.out.println("-- traverseHrefGraph output");
traverseHrefGraph(hrefGraph, start);
System.out.println();
System.out.println("-- renderHrefGraph output");
renderHrefGraph(hrefGraph);
System.out.println();
dotãã¡ã¤ã«ãåºåããããã«HelloJGraphT.javaãå¤æ´ãã
åå ã¯ãæ¨æºåºåã® DOT å½¢å¼ã®å
容ããã¡ã¤ã«ã«è²¼ãä»ãã¦ãGraphViz ã§ç»åãã¡ã¤ã«ãä½ãã¾ããã
ä»åã¯ãHelloJGraphT.java ãå¤æ´ãã¦ãDOT ãã¡ã¤ã«ãåºåããããã«ãããã¨æãã¾ãã
HelloJGraphT.java ã® DOT å½¢å¼ã®å
容ãä½ã£ã¦ããã®ã¯ä»¥ä¸ã®é¨åã§ãã
Write ã¨ãã¦ãStringWrite ã¯ã©ã¹ã使ã£ã¦ãã¾ãã
Render a graph in DOT format.
@param hrefGraph
private static void renderHrefGraph(Graph<URI, DefaultEdge> hrefGraph)
throws ExportException
{
DOTExporter<URI, DefaultEdge> exporter =
new DOTExporter<>(v -> v.getHost().replace('.', '_'));
exporter.setVertexAttributeProvider((v) -> {
Map<String, Attribute> map = new LinkedHashMap<>();
map.put("label", DefaultAttribute.createAttribute(v.toString()));
return map;
});
Writer writer = new StringWriter();
exporter.exportGraph(hrefGraph, writer);
System.out.println(writer.toString());
}
ãã¡ã¤ã«åºåããã«ã¯ãStringWrite ã¯ã©ã¹ããFileWrite ã¯ã©ã¹ã«å¤æ´ããã°ããã§ãã
Writer writer = new FileWriter("HelloJGraphT.dot");
ã¾ãããã¡ã¤ã«åºåããå ´åã¯ãtry catch ã§å²ãå¿
è¦ãããã¾ãã
çµæã¨ãã¦ã¯ã以ä¸ã®ããã«å¤æ´ãã¾ããã
private static void renderHrefGraph(Graph<URI, DefaultEdge> hrefGraph)
throws ExportException
{
DOTExporter<URI, DefaultEdge> exporter =
new DOTExporter<>(v -> v.getHost().replace('.', '_'));
exporter.setVertexAttributeProvider((v) -> {
Map<String, Attribute> map = new LinkedHashMap<>();
map.put("label", DefaultAttribute.createAttribute(v.toString()));
return map;
});
try {
Writer writer = new FileWriter("HelloJGraphT.dot");
exporter.exportGraph(hrefGraph, writer);
}
catch (IOException e) {
e.printStackTrace();
}
}
ãããå®è¡ããã¨ãããã¸ã§ã¯ãã®ãããã«ãHelloJGraphT.dot ãä½ããã¾ãã
ãã¨ã¯ãåå ã¨åæ§ã«ãGraphViz ã§ãç»åãã¡ã¤ã«ãä½ãã°å®æã§ãã
$ dot -Tpng HelloJGraphT.dot -oHelloJGraphT.png
åå ã¨åããã以ä¸ã®ç»åãã¡ã¤ã«ãåºåããã¾ãã
ã¤ãã§ã«dotãã¡ã¤ã«ããPNGç»åãåºåããããã«HelloJGraphT.javaãå¤æ´ãã
ããã«ãJava ãå®è¡ããå¾ã«ãdot ãå®è¡ããã®ã¯é¢åãªã®ã§ãJava ã®ä¸ã§ãPNG ãã¡ã¤ã«ãåºåãã¾ãã
ChatGPT ã«æãã¦ãããã¾ãããã³ãã³ãã§å®è¡ããã®ã¨åãã§ããããã§ããã
try {
ProcessBuilder pb = new ProcessBuilder("dot", "-Tpng", "graph.dot", "-o", "graph.png");
pb.redirectErrorStream(true);
Process process = pb.start();
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
ä¾å¤ãè¤æ°ã¾ã¨ãã¦è¨è¿°ããæ¹æ³ã¯åãã¦ç¥ãã¾ãããChatGPT ã¯æ¬å½ã«å©ããã¾ãã
æçµçã«ã以ä¸ã®ããã«ãªãã¾ããã
private static void renderHrefGraph(Graph<URI, DefaultEdge> hrefGraph)
throws ExportException
{
DOTExporter<URI, DefaultEdge> exporter =
new DOTExporter<>(v -> v.getHost().replace('.', '_'));
exporter.setVertexAttributeProvider((v) -> {
Map<String, Attribute> map = new LinkedHashMap<>();
map.put("label", DefaultAttribute.createAttribute(v.toString()));
return map;
});
try {
Writer writer = new FileWriter("HelloJGraphT.dot");
exporter.exportGraph(hrefGraph, writer);
ProcessBuilder pb = new ProcessBuilder("dot", "-Tpng", "HelloJGraphT.dot", "-o", "HelloJGraphT.png");
pb.redirectErrorStream(true);
Process process = pb.start();
process.waitFor();
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
ãããã«
ä»åã¯ãååãååã«æ¸ãã¦ãªãã£ãã¨ãããè£è¶³ãã¾ããã
æå¾ã«ãªãã¾ããããã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã