【问题标题】:Process nodes and edges of a dot format graph in JavaJava中点格式图的处理节点和边
【发布时间】:2018-04-28 03:36:20
【问题描述】:

我有一个点格式的图表作为字符串。我想获取它的节点、边和它们的数据进行处理。我正在寻找一个处理给定点格式图的 Java 库。一个例子将不胜感激。

digraph G { rankdir=TB
    V1 [a=1, b=2, label="V1"]; 
    V2 [a=4, b=0, label="V2"]; 
    V3 [a=1, b=3, label="V3"]; 
    V4 [a=3, b=7, label="V4"];

    V1 -> V2 [path=a, label="a"];  
    V2 -> V3 [path=b, label="b"]; 
    V1 -> V4 [path=c, label="c"];
    V2 -> V4 [path=d, label="d"];
}

【问题讨论】:

  • 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,describe the problem 以及迄今为止为解决它所做的工作。

标签: java graphviz dot directed-acyclic-graphs


【解决方案1】:

假设你想使用 JGraphT - Library,你可以使用这个类:http://jgrapht.org/javadoc/org/jgrapht/io/DOTImporter.html

你需要传递一个VertexProvider和一个EdgeProvider,它必须返回一个顶点和一个边;它们还可以包含其他属性的附加逻辑:

SimpleDirectedGraph< String ,DefaultEdge> labeledGraph = new SimpleDirectedGraph<>(DefaultEdge.class);
DOTImporter<Integer,DefaultEdge> importer = new DOTImporter<>(
    (String label, Map<String, Attribute> attributes) -> {
          // handle the attributes here
         return label;
    },
    (from, to, label, attributes) -> {
         // handle the attributes here
         return labelGraph.addEdge(from, to);
    });
importer.importGraph(labelGraph, yourFileReader)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多