gremlin-graphviz
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -7,3 +7,2 @@ // lib/index.js | ||
var _ = require('lodash'); | ||
var assert = require('assert'); | ||
@@ -14,2 +13,5 @@ var graphviz = require('graphviz'); | ||
// Construct a Graphviz graph from a Gremlin graph. | ||
// *opts* is an optional object allowing overriding default behavior: | ||
// * *graphName*: Name of the Graphviz graph [default: 'G'] | ||
// * *vertexId*: Function that returns vertex ID string from a Gremlin vertex [default: util.getVertexId] | ||
var factory = module.exports = Q.promised(function (gremlinGraph, opts) { | ||
@@ -24,4 +26,3 @@ var graphvizGraph = new GraphvizGraph(gremlinGraph, opts); | ||
then(function () { | ||
var Direction = gremlin.java.import('com.tinkerpop.gremlin.structure.Direction'); | ||
return iterate(edgeTraversal, function (edge) { addEdge(graphvizGraph, Direction, edge); }); | ||
return iterate(edgeTraversal, function (edge) { addEdge(graphvizGraph, edge); }); | ||
}). | ||
@@ -34,4 +35,4 @@ then(function () { | ||
var GraphvizGraph = function (gremlinGraph, opts) { | ||
var opts = opts || {}; | ||
var graphName = opts.graphName || 'G'; | ||
this.opts = opts || {}; | ||
var graphName = this.opts.graphName || 'G'; | ||
this.impl = graphviz.digraph(graphName); | ||
@@ -48,3 +49,4 @@ }; | ||
// ## Expose certain immutable properties of the graphviz graph. | ||
// ## Immutable properties | ||
// Expose certain immutable properties of the graphviz graph. | ||
['id', | ||
@@ -67,3 +69,4 @@ 'type' | ||
// ## Expose certain mutable properties of the graphviz graph. | ||
// ## Mutable properties | ||
// Expose certain mutable properties of the graphviz graph. | ||
['use' | ||
@@ -80,3 +83,4 @@ ].map(haulMutableProperty); | ||
// All of the useful read-only methods are exported. | ||
// ## Non-mutatingmethods | ||
// All of the useful non-mutating methods are exported. | ||
['getNode', | ||
@@ -89,22 +93,7 @@ 'edgeCount', | ||
// Iterate through a Gremlin traversal | ||
var iterate = function (traversal, process) { | ||
return traversal.hasNext(). | ||
then(function (hasNext) { | ||
if (hasNext) { | ||
return traversal.next(); | ||
} | ||
}). | ||
then(function (element) { | ||
if (element) { | ||
process(element); | ||
return iterate(traversal, process); | ||
} | ||
}); | ||
return traversal.forEach(process); | ||
}; | ||
// Check whether we have a valid vertex ID. | ||
var assertVertexId = function (id) { | ||
assert(_.isNumber(id)); | ||
}; | ||
// Add a vertex (synchronously) to a Graphviz graph based on a Gremlin vertex. | ||
@@ -115,23 +104,41 @@ var addVertex = function (graphvizGraph, gremlinVertex) { | ||
assert(gremlinVertex); | ||
var id = gremlinVertex.getId(); | ||
assertVertexId(id); | ||
graphvizGraph.impl.addNode(id.toString()); | ||
var vertexId = graphvizGraph.opts.vertexId || util.getVertexId; | ||
graphvizGraph.impl.addNode(vertexId(gremlinVertex)); | ||
}; | ||
// Add an edge (synchronously) to a Graphviz graph based on a Gremlin edge. | ||
var addEdge = function (graphvizGraph, Direction, gremlinEdge) { | ||
var addEdge = function (graphvizGraph, gremlinEdge) { | ||
assert(graphvizGraph); | ||
assert(graphvizGraph.impl); | ||
assert(gremlinEdge); | ||
// Pull the Direction enumeration from the embedded Gremlin object. | ||
var gremlin = gremlinEdge.gremlin; | ||
assert(gremlin); | ||
var Direction = gremlin.Direction; | ||
assert(Direction); | ||
// TODO: Switch this from a Java edge object to something from gremlin-v3. | ||
var vertexId = graphvizGraph.opts.vertexId || util.getVertexId; | ||
var iterators = gremlinEdge.unwrap().iteratorsSync(); | ||
var inV = iterators.vertexIteratorSync(Direction.IN).nextSync(); | ||
assert(inV); | ||
var inId = inV.idSync(); | ||
assertVertexId(inId); | ||
var inId = vertexId(gremlin.wrapVertex(inV)); | ||
var outV = iterators.vertexIteratorSync(Direction.OUT).nextSync(); | ||
assert(outV); | ||
var outId = outV.idSync(); | ||
assertVertexId(outId); | ||
graphvizGraph.impl.addEdge(outId.toString(), inId.toString()); | ||
var outId = vertexId(gremlin.wrapVertex(outV)); | ||
var graphvizEdge = graphvizGraph.impl.addEdge(outId.toString(), inId.toString()); | ||
// See if we have to label the edge. | ||
var edgeLabeler = graphvizGraph.opts.edgeLabel; | ||
if (edgeLabeler) { | ||
var edgeLabel = edgeLabeler(gremlinEdge); | ||
if (edgeLabel) { | ||
graphvizEdge.set('label', edgeLabel); | ||
} | ||
}; | ||
}; | ||
// Import the utilities module. | ||
var util = module.exports.util = require('./util.js'); |
{ | ||
"name": "gremlin-graphviz", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Use Graphviz to visualize Gremlin graphs.", | ||
@@ -10,2 +10,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"doc": "docco lib/*.js test/*.js", | ||
"lint": "jshint lib test", | ||
@@ -35,3 +36,4 @@ "test": "mocha" | ||
"chai": "^1.10.0", | ||
"gremlin-v3": "0.0.4", | ||
"docco": "^0.6.3", | ||
"gremlin-v3": "0.0.5", | ||
"heredoc": "^1.2.0", | ||
@@ -38,0 +40,0 @@ "jshint": "^2.5.10", |
@@ -165,2 +165,52 @@ // test/test.js | ||
it ('allows specifying alternate vertex ID', function (done) { | ||
var expected = heredoc(function () {/* | ||
digraph G { | ||
"marko"; | ||
"vadas"; | ||
"lop"; | ||
"josh"; | ||
"ripple"; | ||
"peter"; | ||
"marko" -> "vadas"; | ||
"marko" -> "josh"; | ||
"marko" -> "lop"; | ||
"josh" -> "ripple"; | ||
"josh" -> "lop"; | ||
"peter" -> "lop"; | ||
} | ||
*/}); | ||
gremlinGraphviz(graph, { vertexId: gremlinGraphviz.util.vertexAttributeGetter('name') }) | ||
.then(function (g) { | ||
expect(g.to_dot()).to.equal(expected); | ||
}) | ||
.done(done); | ||
}); | ||
it ('allows specifying alternate edge label', function (done) { | ||
var expected = heredoc(function () {/* | ||
digraph G { | ||
"1"; | ||
"2"; | ||
"3"; | ||
"4"; | ||
"5"; | ||
"6"; | ||
"1" -> "2" [ label = "knows" ]; | ||
"1" -> "4" [ label = "knows" ]; | ||
"1" -> "3" [ label = "created" ]; | ||
"4" -> "5" [ label = "created" ]; | ||
"4" -> "3" [ label = "created" ]; | ||
"6" -> "3" [ label = "created" ]; | ||
} | ||
*/}); | ||
gremlinGraphviz(graph, { edgeLabel: gremlinGraphviz.util.getEdgeLabel }) | ||
.then(function (g) { | ||
expect(g.to_dot()).to.equal(expected); | ||
}) | ||
.done(done); | ||
}); | ||
it ('can be rendered as force-directed graph in SVG', function (done) { | ||
@@ -167,0 +217,0 @@ gremlinGraphviz(graph) |
Sorry, the diff of this file is not supported yet
16083
9
352
9