Created
September 6, 2011 02:36
-
-
Save infojunkie/1196430 to your computer and use it in GitHub Desktop.
Gremlin steps for tree traversal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Taken from: https://gist.github.com/1170000 | |
// Gremlin user-defined defined tree() steps | |
// inTree() and outTree() | |
// closure can't have the same name as the defined step | |
_tree = { vertices -> | |
def results = [] | |
vertices.each() { | |
results << it | |
if (label) | |
children = it."$direction"(label).toList() | |
else | |
children = it."$direction"().toList() | |
if (children) | |
results << _tree(children) | |
} | |
results | |
} | |
inClosure = {final Object... params -> | |
try { label = params[0] } | |
catch(e){ label = null } | |
direction = "in" | |
_().transform{ _tree(it) } | |
} | |
outClosure = {final Object... params -> | |
try { label = params[0] } | |
catch(e){ label = null } | |
direction = "out" | |
_().transform{ _tree(it) } | |
} | |
Gremlin.defineStep("inTree", [Vertex,Pipe], inClosure) | |
Gremlin.defineStep("outTree", [Vertex,Pipe], outClosure) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment