下面的 JUnit 测试显示了如何使用 apache tinkerpop 转换 graphml 文件。
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>