Skip to content

Using JUNG

jbmusso edited this page Jul 11, 2016 · 5 revisions

Attention: this Wiki hosts an outdated version of the TinkerPop framework and Gremlin language documentation.


JUNG is a software library that provides a common and extendible language for the modeling, analysis, and visualization of data that can be represented as a graph or network. It is written in Java, which allows JUNG-based applications to make use of the extensive built-in capabilities of the Java API, as well as those of other existing third-party Java libraries.

Its possible to make use of JUNG’s visualization and algorithm packages in Gremlin by means of GraphJung. GraphJung is a Blueprints class that takes any Blueprints Graph and exposes it as JUNG graph.

Gremlin and JUNG Algorithms

Before using JUNG algorithms its important to import the respective package you want to use from the JUNG API.

gremlin> import edu.uci.ics.jung.algorithms.scoring.PageRank
==>import com.tinkerpop.gremlin.*
==>import com.tinkerpop.gremlin.java.*
==>import com.tinkerpop.gremlin.pipes.*
...
==>import edu.uci.ics.jung.algorithms.scoring.PageRank

Once the respective package has been imported (e.g. PageRank) its necessary to wrap the desired Blueprints graph into a GraphJung and create and instance of the algorithm. For the examples in this section, the graph diagrammed in Defining a Property Graph is used (i.e. TinkerGraphFactory.createTinkerGraph()).

gremlin> g = new GraphJung(TinkerGraphFactory.createTinkerGraph());
==>graphjung[tinkergraph[vertices:6 edges:6]]
gremlin> pr = new PageRank<Vertex,Edge>(g, 0.15d)
==>edu.uci.ics.jung.algorithms.scoring.PageRank@34b6a6d6

Now its possible to evaluate the algorithm.

gremlin> pr.evaluate()
==>null
gremlin> g.getVertices().collect{ [it, pr.getVertexScore(it)] }
==>[v[3], 0.30472082661863664]
==>[v[2], 0.14598540145985392]
==>[v[1], 0.11375485828040566]
==>[v[6], 0.11375485828040566]
==>[v[5], 0.1757986539008436]
==>[v[4], 0.14598540145985392]

Note that all edges are treated equally. That is, with JUNG, you can not discriminate the calculation to traverse particular paths. For those familiar with PageRank, you can’t arbitrarily bias the random walker (see Grammar-Based Random Walkers in Semantics Networks). The best that we can achieve with JUNG is to bias it with edge weights and filtered edge labels. In the example below, an EdgeLabelTransformer (from Blueprints) is used to only allow the traverser to follow knows edges in the graph. As such, besides the background alpha-probability, only vertices 2 and 4 get rank as they are the head of knows edges.

gremlin> t = new EdgeLabelTransformer(['knows'] as Set, false)
==>com.tinkerpop.blueprints.pgm.oupls.jung.util.EdgeLabelTransformer@301abf87
gremlin> pr = new PageRank<Vertex,Edge>(g, t, 0.15d)
==>edu.uci.ics.jung.algorithms.scoring.PageRank@da3b359
gremlin> pr.evaluate()
==>null
gremlin> g.getVertices().collect{ [it, pr.getVertexScore(it)] }
==>[v[3], 0.05499541704857928]
==>[v[2], 0.10174152153987166]
==>[v[1], 0.05499541704857928]
==>[v[6], 0.05499541704857928]
==>[v[5], 0.05499541704857928]
==>[v[4], 0.10174152153987166]

Gremlin and JUNG Visualization

JUNG has a rich set of classes for creating Swing based visualizations. You can make use of these classes from within Gremlin. For example, to render the visualization below, use the code that follows.

import java.awt.*
import javax.swing.*
import org.apache.commons.collections15.Transformer
import edu.uci.ics.jung.algorithms.layout.CircleLayout
import edu.uci.ics.jung.visualization.BasicVisualizationServer

g = new GraphJung(TinkerGraphFactory.createTinkerGraph());
layout = new CircleLayout<Vertex, Edge>(g);
layout.setSize(new Dimension(300, 300));
viz = new BasicVisualizationServer<Vertex, Edge>(layout);
viz.setPreferredSize(new Dimension(350, 350));

vertexLabelTransformer = new Transformer<Vertex, String>() {
  public String transform(Vertex vertex) {
    return (String) vertex.getProperty("name");
  }
};

edgeLabelTransformer = new Transformer<Edge, String>() {
  public String transform(Edge edge) {
    return edge.getLabel();
  }
};

viz.getRenderContext().setEdgeLabelTransformer(edgeLabelTransformer);
viz.getRenderContext().setVertexLabelTransformer(vertexLabelTransformer);

frame = new JFrame("TinkerPop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(viz);
frame.pack();
frame.setVisible(true);