Skip to content

Instantly share code, notes, and snippets.

@infojunkie
Created September 6, 2011 02:36
Show Gist options
  • Save infojunkie/1196430 to your computer and use it in GitHub Desktop.
Save infojunkie/1196430 to your computer and use it in GitHub Desktop.
Gremlin steps for tree traversal.
// 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