longjohnsilver
longjohnsilver

Reputation: 23

Importing GraphML to create graph in JGraphX

For a project that I working on, I am getting information in the form of GraphML format(I can change the graphML file as well) and I want to create the desired graph from that information, I am able to get the correct layout, with appropriate edges and nodes by using the mxGraphMlCodec.decode(doc, graph) method, but the generated graph lacks the additional information of edge labels as provided with the graphML file.

I could not find any example of a graphML file which is converted in this way to a graph. I have just followed the guidelines for a graphML file as stated in the GraphML primer to create the graphML file.

Here is the GraphML code:

<?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>

I am just modifying the ClickHandle.java file from the JGraphX examples folder in order to visualize the graph.

Upvotes: 2

Views: 1225

Answers (3)

Wolfgang Fahl
Wolfgang Fahl

Reputation: 15769

The JUnit test below shows how you could convert the graphml file using apache tinkerpop.

enter image description here

maven dependencies

<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 Test

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 result

<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>

Upvotes: 0

sysoutkoula
sysoutkoula

Reputation: 340

jgraphx uses "value" attribute for the label of an edge:

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

You could try adding an attibute named "value".

Upvotes: 1

Stewart
Stewart

Reputation: 18302

If all you want to do is visualise the graph, you may wish to look at the Neo4j section of No SQL Unit

Upvotes: 0

Related Questions