使用 graphml 和 jung 加载自定义节点和边

loading custom nodes and edges with graphml and jung

我在尝试使用 JUNG 读取和写入文本文件中的图形时遇到了问题。情况如下:给定的是一个包含多图坐标及其权重的文件。

一个例子是:

6346    6728    5911    156 5
6346    6728    6599    156 10
6346    6728    8555    156 5

我用 JUNG 编写了一个转换器,它读取一个包含数百万行的文件并构造一个

DirectedSparseMultigraph<Node, Edge>

节点和边是定制的类如下所列

class Node {
    int id; // good coding practice would have this as private

    public Node(int id) {
        this.id = id;
    }
    public String toString() { // Always a good idea for debuging
        return "V"+id;
   // JUNG2 makes good use of these.
    }        
}

class Edge { 
    Context context;  
    Time time;
    Value value;
    int id;
    public Edge(int id, Context context, Time time, Value value) {
        this.id = id; // This is defined in the outer class.
        this.context = context;
        this.time = time;
        this.value = value;
    } 
    public String toString() { // Always good for debugging
        return "E"+id;
    }
}

根据这些数据在内存中构建图表效果很好。我们离问题更近了。重要的是要注意将构造的图保存到磁盘工作正常通过使用方法 save() 定向稀疏多图对象。在下一步中,构造的 DirectedSparseMultigraph 通过以下代码行从磁盘加载

    GraphMLReader<DirectedSparseMultigraph<Node, Edge>, Node, Edge> gmlr = null;

    try
    {
        gmlr = new GraphMLReader<DirectedSparseMultigraph<Node, Edge>, Node, Edge>();
    } catch (ParserConfigurationException e1)
    {
        e1.printStackTrace();
    } catch (SAXException e1)
    {
        e1.printStackTrace();
    }

    DirectedSparseMultigraph<Node, Edge> g_new = null;
    try
    {
        gmlr.load("bla.sh", g_new);
    } catch (IOException e)
    {
        e.printStackTrace();
    }

这就是问题开始的地方。报错信息如下:

Exception in thread "main" java.lang.IllegalArgumentException: If no edge factory is supplied, edge id may not be null: {source=V6818, target=V2472}
at edu.uci.ics.jung.io.GraphMLReader.createEdge(GraphMLReader.java:693)
at edu.uci.ics.jung.io.GraphMLReader.startElement(GraphMLReader.java:299)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1343)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:333)
at edu.uci.ics.jung.io.GraphMLReader.parse(GraphMLReader.java:241)
at edu.uci.ics.jung.io.GraphMLReader.load(GraphMLReader.java:192)
at edu.uci.ics.jung.io.GraphMLReader.load(GraphMLReader.java:201)
at main.Graph.main(Graph.java:99)

如果您有任何想法或提示如何解决这个问题,我非常期待您的留言。 问候 J 错误

GraphMLReader 可以通过两种方式解析您的文件:

(1) 您提供节点和边缘工厂;在这种情况下,您的节点和边缘类型可以是您想要的任何类型。 (constructor with factories)

(2) 您不提供节点和边缘工厂;在这种情况下,您的节点和边缘类型必须是 Strings。 (constructor without factories)

您的代码不提供节点和边缘工厂,并且您的 Edge 类型与 String 的分配不兼容,所以它爆炸了。诚然,这从错误消息中并不是非常明显,但在 the code.

中相当清楚

在这种情况下,您无法真正提供 NodeEdge 工厂(不重新设计它们),因为您没有无参数构造函数。因此,您要么需要重新设计那些 类,要么使用两阶段过程,即:使用节点和边的简单字符串键解析图 + 填充元数据,然后基于GraphMLReader 提供的数据结构。