导入GraphML以在JGraphX中创建图形

时间:2014-05-23 11:56:57

标签: java graph jgraphx graphml

对于我正在处理的项目,我以GraphML格式的形式获取信息(我也可以更改graphML文件)并且我想从该信息创建所需的图形,我能够得到正确的通过使用mxGraphMlCodec.decode(doc,graph)方法,使用适当的边和节点进行布局,但生成的图缺少graphML文件提供的边标签的附加信息。

我找不到任何以这种方式转换为图形的graphML文件的示例。 我刚刚按照GraphML primer中所述的graphML文件指南来创建graphML文件。

这是GraphML代码:

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
<key id="d1" for="edge" attr.name="edgeData" attr.type="double"/>
<graph id="DTMC" edgedefault="directed">
<node id="0"/>
<edge source="0" target="1">
<data key="d1">0.5</data>
</edge>
<edge source="0" target="2">
<data key="d1">0.5</data>
</edge>
<node id="1"/>
<edge source="1" target="3">
<data key="d1">0.5</data>
</edge>
<edge source="1" target="4">
<data key="d1">0.5</data>
</edge>
<node id="2"/>
<edge source="2" target="5">
<data key="d1">0.5</data>
</edge>
<edge source="2" target="6">
<data key="d1">0.5</data>
</edge>
<node id="3"/>
<edge source="3" target="1">
<data key="d1">0.5</data>
</edge>
<edge source="3" target="7">
<data key="d1">0.5</data>
</edge>
<node id="4"/>
<edge source="4" target="8">
<data key="d1">0.5</data>
</edge>
<edge source="4" target="9">
<data key="d1">0.5</data>
</edge>
<node id="5"/>
<edge source="5" target="10">
<data key="d1">0.5</data>
</edge>
<edge source="5" target="11">
<data key="d1">0.5</data>
</edge>
<node id="6"/>
<edge source="6" target="2">
<data key="d1">0.5</data>
</edge>
<edge source="6" target="12">
<data key="d1">0.5</data>
</edge>
<node id="7"/>
<edge source="7" target="7">
<data key="d1">1.0</data>
</edge>
<node id="8"/>
<edge source="8" target="8">
<data key="d1">1.0</data>
</edge>
<node id="9"/>
<edge source="9" target="9">
<data key="d1">1.0</data>
</edge>
<node id="10"/>
<edge source="10" target="10">
<data key="d1">1.0</data>
</edge>
<node id="11"/>
<edge source="11" target="11">
<data key="d1">1.0</data>
</edge>
<node id="12"/>
<edge source="12" target="12">
<data key="d1">1.0</data>
</edge>
</graph>
</graphml>

我只是修改JGraphX examples文件夹中的ClickHandle.java文件,以便可视化图形。

3 个答案:

答案 0 :(得分:1)

jgraphx对边缘标签使用“value”属性:

<mxCell edge="1" id="4" parent="1" style="defaultEdge" source="2" target="3" value="test label">
  <mxGeometry>
    ...
  </mxGeometry>
</mxCell>

您可以尝试添加名为“value”的地址。

答案 1 :(得分:0)

如果您想要做的只是想象图表,您可能希望查看No SQL Unit

的Neo4j部分

答案 2 :(得分:0)

下面的JUnit测试显示了如何使用apache tinkerpop转换graphml文件。

enter image description here

Maven依赖项

<properties>
    <tinkerpop.version>3.3.1</tinkerpop.version>
</properties>
<dependencies>
    <!-- https://github.com/vlsi/jgraphx-publish -->
    <dependency>
        <groupId>com.github.vlsi.mxgraph</groupId>
        <artifactId>jgraphx</artifactId>
        <version>3.9.8.1</version>
    </dependency>
    <!-- JUnit testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.tinkerpop/gremlin-core -->
    <dependency>
        <groupId>org.apache.tinkerpop</groupId>
        <artifactId>gremlin-core</artifactId>
        <version>${tinkerpop.version}</version>
    </dependency>
    <!-- in memory graph database -->
    <dependency>
        <groupId>org.apache.tinkerpop</groupId>
        <artifactId>tinkergraph-gremlin</artifactId>
        <version>${tinkerpop.version}</version>
    </dependency>
</dependencies>

JUnit测试

package com.bitplan.uml2mxgraph;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.IoCore;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.junit.Test;

import com.mxgraph.io.mxCodec;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.view.mxGraph;

public class TestGraphML {

  public class GraphViewer extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = -7809452320542517149L;

    private mxGraph mxGraph;

    public GraphViewer(String title, com.mxgraph.view.mxGraph mxGraph) {
      super(title);
      this.mxGraph = mxGraph;
      final mxGraphComponent graphComponent = new mxGraphComponent(
          this.mxGraph);
      getContentPane().add(graphComponent);

      graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {

        public void mouseReleased(MouseEvent e) {
          Object cell = graphComponent.getCellAt(e.getX(), e.getY());

          if (cell != null) {
            System.out.println("cell=" + mxGraph.getLabel(cell));
          }
        }
      });
    }

  }

  @Test
  public void testGraphML() throws IOException, InterruptedException {
    File graphmlFile = new File("src/test/data/graphexample.xml");
    assertTrue(graphmlFile.exists());
    Graph graph = TinkerGraph.open();
    graph.io(IoCore.graphml()).readGraph(graphmlFile.getPath());
    assertEquals(13, graph.traversal().V().count().next().longValue());
    assertEquals(20, graph.traversal().E().count().next().longValue());
    GraphTraversalSource g = graph.traversal();

    double width = 60;
    double height = 30;

    mxGraph mxGraph = new mxGraph();
    Object parent = mxGraph.getDefaultParent();
    mxGraph.getModel().beginUpdate();
    try {
      double x = width;
      double y = height;
      for (Vertex v : g.V().toList()) {
        String id = v.id().toString();
        String value = v.id().toString();
        Object mxVertex = mxGraph.insertVertex(parent, id, value, x, y, width,
            height);
        v.property("mxVertex", mxVertex);
        x += width;
      }
      for (Edge e : g.E().toList()) {
        Object source = e.inVertex().property("mxVertex").value();
        Object target = e.outVertex().property("mxVertex").value();
        String id = e.id().toString();
        String value = e.property("edgeData").value().toString();
        mxGraph.insertEdge(parent, id, value, source, target);
      }
      new mxHierarchicalLayout(mxGraph).execute(mxGraph.getDefaultParent());
    } finally {
      mxGraph.getModel().endUpdate();
    }
    mxCodec codec = new mxCodec();
    String xml = mxXmlUtils.getXml(codec.encode(mxGraph.getModel()));
    System.out.println(xml);
    GraphViewer frame = new GraphViewer(
        "Importing GraphML to create graph in JGraphX", mxGraph);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(640, 480);
    frame.setVisible(true);
    while (frame.isVisible())
      Thread.sleep(20);
  }

}

mxGraph结果

<mxGraphModel>
  <root><mxCell id="0"/><mxCell id="1" parent="0"/>
    <mxCell id="11" parent="1" value="11" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0"/></mxCell>
    <mxCell id="12" parent="1" value="12" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="90.0" y="160.0"/></mxCell>
    <mxCell id="13" parent="1" value="0" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="225.0" y="240.0"/></mxCell>
    <mxCell id="14" parent="1" value="1" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="270.0" y="160.0"/></mxCell>
    <mxCell id="2" parent="1" value="2" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="180.0" y="160.0"/></mxCell>
    <mxCell id="3" parent="1" value="3" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="180.0" y="80.0"/></mxCell>
    <mxCell id="4" parent="1" value="4" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="300.0" y="80.0"/></mxCell>
    <mxCell id="5" parent="1" value="5" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="90.0" y="80.0"/></mxCell>
    <mxCell id="6" parent="1" value="6" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="135.0" y="240.0"/></mxCell>
    <mxCell id="7" parent="1" value="7" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="180.0"/></mxCell>
    <mxCell id="8" parent="1" value="8" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="270.0"/></mxCell>
    <mxCell id="9" parent="1" value="9" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="360.0"/></mxCell>
    <mxCell id="10" parent="1" value="10" vertex="1"><mxGeometry as="geometry" height="30.0" width="60.0" x="90.0"/></mxCell>
    <mxCell edge="1" id="15" parent="1" source="14" style="noEdgeStyle=1;orthogonal=1" target="13" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="300.0" y="202.0"/><mxPoint x="267.5" y="228.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="16" parent="1" source="2" style="noEdgeStyle=1;orthogonal=1" target="13" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="226.66666666666666" y="204.0"/><mxPoint x="242.5" y="228.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="17" parent="1" source="3" style="noEdgeStyle=1;orthogonal=1" target="14" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="197.5" y="122.0"/><mxPoint x="283.3333333333333" y="148.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="18" parent="1" source="4" style="noEdgeStyle=1;orthogonal=1" target="14" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="330.0" y="122.0"/><mxPoint x="316.6666666666667" y="146.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="19" parent="1" source="5" style="noEdgeStyle=1;orthogonal=1" target="2" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="120.0" y="122.0"/><mxPoint x="210.0" y="148.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="20" parent="1" source="6" style="noEdgeStyle=1;orthogonal=1" target="2" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="165.0" y="226.0"/><mxPoint x="193.33333333333334" y="202.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="21" parent="1" source="14" style="noEdgeStyle=1;orthogonal=1" target="3" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="300.0" y="148.0"/><mxPoint x="222.5" y="122.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="22" parent="1" source="7" style="noEdgeStyle=1;orthogonal=1" target="3" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="210.0" y="42.0"/><mxPoint x="210.0" y="68.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="23" parent="1" source="8" style="noEdgeStyle=1;orthogonal=1" target="4" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="300.0" y="42.0"/><mxPoint x="317.5" y="68.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="24" parent="1" source="9" style="noEdgeStyle=1;orthogonal=1" target="4" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="390.0" y="42.0"/><mxPoint x="342.5" y="68.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="25" parent="1" source="10" style="noEdgeStyle=1;orthogonal=1" target="5" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="120.0" y="42.0"/><mxPoint x="132.5" y="68.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="26" parent="1" source="11" style="noEdgeStyle=1;orthogonal=1" target="5" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="30.0" y="42.0"/><mxPoint x="107.5" y="68.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="27" parent="1" source="2" style="noEdgeStyle=1;orthogonal=1" target="6" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="210.0" y="202.0"/><mxPoint x="181.66666666666666" y="226.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="28" parent="1" source="12" style="noEdgeStyle=1;orthogonal=1" target="6" value="0.5">
      <mxGeometry as="geometry" relative="1">
        <Array as="points"><mxPoint x="120.0" y="202.0"/><mxPoint x="148.33333333333334" y="228.0"/></Array>
      </mxGeometry>
    </mxCell>
    <mxCell edge="1" id="29" parent="1" source="7" target="7" value="1.0"><mxGeometry as="geometry" relative="1"/></mxCell>
    <mxCell edge="1" id="30" parent="1" source="8" target="8" value="1.0"><mxGeometry as="geometry" relative="1"/></mxCell>
    <mxCell edge="1" id="31" parent="1" source="9" target="9" value="1.0"><mxGeometry as="geometry" relative="1"/></mxCell>
    <mxCell edge="1" id="32" parent="1" source="10" target="10" value="1.0"><mxGeometry as="geometry" relative="1"/></mxCell>
    <mxCell edge="1" id="33" parent="1" source="11" target="11" value="1.0"><mxGeometry as="geometry" relative="1"/></mxCell>
    <mxCell edge="1" id="34" parent="1" source="12" target="12" value="1.0"><mxGeometry as="geometry" relative="1"/></mxCell>
  </root>
</mxGraphModel>