导入图形时将顶点字符串ID用作实际顶点(jgrapht 1.4) [英] Using vertex String IDs as the actual vertices while importing graph (jgrapht 1.4)

查看:99
本文介绍了导入图形时将顶点字符串ID用作实际顶点(jgrapht 1.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的图

strict digraph G {
  <assembly-raw-file>;
  <dataset-processing>;
  <feature-processing-1>;
  <feature-processing-2>;
  <mh.permute-variables-and-hyper-params>;
  <mh.finish>;
  <assembly-raw-file> -> <dataset-processing>;
  <dataset-processing> -> <feature-processing-1>;
  <dataset-processing> -> <feature-processing-2>;
  <dataset-processing> -> <mh.permute-variables-and-hyper-params>;
  <feature-processing-1> -> <mh.permute-variables-and-hyper-params>;
  <feature-processing-2> -> <mh.permute-variables-and-hyper-params>;
  <mh.permute-variables-and-hyper-params> -> <mh.finish>;
}

我正在尝试使用以下代码导入

I'm trying to import it with the follow code

DirectedAcyclicGraph<String, DefaultEdge> processGraph = new DirectedAcyclicGraph<>(
        SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, false);

DOTImporter<String, DefaultEdge> importer = new DOTImporter<>();
importer.importGraph(processGraph, new StringReader(wpy.processesGraph));

但是此代码将顶点名称更改为此:

but this code is changing names of vertices to this:

strict digraph G {
  0;
  1;
  2;
  3;
  4;
  5;
  0 -> 1;
  1 -> 2;
  1 -> 3;
  1 -> 4;
  2 -> 4;
  3 -> 4;
  4 -> 5;
}

如何导入保留顶点ID的图形?

How to import my graph with preserving id of vertices?

还是更简单的解决方案是将顶点类型从String更改为复杂类?

Or the easier solution is to change type of vertex from String to complex class?

推荐答案

2020年3月4日更新:

如果使用1.4.1 SNAPSHOT版本(或更高版本),则有一种更简单的方法来获得所需的结果:只需使用importer.setVertexFactory(id->id);

仅在用户提供工厂之类的情况下,才使用文件中的顶点标识符来调用它.给定文件中顶点标识符的方法应返回实际的图形顶点.用户返回的顶点将直接添加到图形中,而无需使用顶点供应商.

Only in case the user supplies such as factory, it is called with the vertex identifier from the file. The method given the vertex identifier from the file should return an actual graph vertex. The user returned vertex is added directly into the graph without using the vertex supplier.

如果未提供此类工厂,则默认行为是从图形顶点供应商获取新顶点,并将文件中的原始标识符关联为新顶点的属性.

If no such factory is provided, the default behavior is to acquire a new vertex from the graph vertex supplier and associate the original identifier from the file as an attribute of the new vertex.

将此应用于上面的示例,我们得到:

Applying this to the above example, we get:

String input="strict digraph G {\n" +
        "  <assembly-raw-file>;\n" +
        "  <dataset-processing>;\n" +
        "  <feature-processing-1>;\n" +
        "  <feature-processing-2>;\n" +
        "  <mh.permute-variables-and-hyper-params>;\n" +
        "  <mh.finish>;\n" +
        "  <assembly-raw-file> -> <dataset-processing>;\n" +
        "  <dataset-processing> -> <feature-processing-1>;\n" +
        "  <dataset-processing> -> <feature-processing-2>;\n" +
        "  <dataset-processing> -> <mh.permute-variables-and-hyper-params>;\n" +
        "  <feature-processing-1> -> <mh.permute-variables-and-hyper-params>;\n" +
        "  <feature-processing-2> -> <mh.permute-variables-and-hyper-params>;\n" +
        "  <mh.permute-variables-and-hyper-params> -> <mh.finish>;\n" +
        "}";
Graph<String, DefaultEdge> graph = new SimpleDirectedGraph<>(DefaultEdge.class);
DOTImporter<String, DefaultEdge> importer = new DOTImporter<>();
importer.setVertexFactory(id->id);
importer.importGraph(graph, new StringReader(input));
System.out.println(graph);

原始答案:

这是做到这一点的一种方法.此方法使用2个步骤.第一步,我像您一样阅读图形.顶点名称可以存储在属性图中.在第二步中,我创建了一个新的图,该图使用第一个图的结构,但重命名了顶点.

Original answer:

Here's one way to do it. This approach uses 2 steps. In the first step, I read the graph as you did. The vertex names can be stored in an attribute map. In the second step I create a new graph which uses the structure of the first graph, but renames the vertices.

String input="strict digraph G {\n" +
        "  <assembly-raw-file>;\n" +
        "  <dataset-processing>;\n" +
        "  <feature-processing-1>;\n" +
        "  <feature-processing-2>;\n" +
        "  <mh.permute-variables-and-hyper-params>;\n" +
        "  <mh.finish>;\n" +
        "  <assembly-raw-file> -> <dataset-processing>;\n" +
        "  <dataset-processing> -> <feature-processing-1>;\n" +
        "  <dataset-processing> -> <feature-processing-2>;\n" +
        "  <dataset-processing> -> <mh.permute-variables-and-hyper-params>;\n" +
        "  <feature-processing-1> -> <mh.permute-variables-and-hyper-params>;\n" +
        "  <feature-processing-2> -> <mh.permute-variables-and-hyper-params>;\n" +
        "  <mh.permute-variables-and-hyper-params> -> <mh.finish>;\n" +
        "}";
Graph<String, DefaultEdge> graph = new SimpleDirectedGraph<>(SupplierUtil.createStringSupplier(), SupplierUtil.DEFAULT_EDGE_SUPPLIER, false);

//Import the graph; the vertex names are stored as 'ID' in the attribute map
DOTImporter<String, DefaultEdge> importer = new DOTImporter<>();
Map<String, Map<String, Attribute>> attrs = new HashMap<>();
importer.addVertexAttributeConsumer((p, a) -> {
    Map<String, Attribute> map = attrs.computeIfAbsent(p.getFirst(), k -> new HashMap<>());
    map.put(p.getSecond(), a);
});
importer.importGraph(graph, new StringReader(input));

//Create a new graph, thereby creating String vertices equal to the ID attribute values
Graph<String,DefaultEdge> labeledGraph=new SimpleDirectedGraph<>(DefaultEdge.class);
for(String v : graph.vertexSet())
    labeledGraph.addVertex(attrs.get(v).get("ID").getValue());
for(DefaultEdge e : graph.edgeSet()){
    String source = graph.getEdgeSource(e);
    String target = graph.getEdgeTarget(e);
    String sourceID=attrs.get(source).get("ID").getValue();
    String targetID=attrs.get(target).get("ID").getValue();
    labeledGraph.addEdge(sourceID, targetID);
}

System.out.println(labeledGraph);

这篇关于导入图形时将顶点字符串ID用作实际顶点(jgrapht 1.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆