Created
September 6, 2011 10:06
-
-
Save espeed/1197179 to your computer and use it in GitHub Desktop.
Gremlin Trees
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
// Gremlin user-defined defined tree steps | |
// inTree() and outTree() | |
// by James Thornton, http://jamesthornton.com | |
// see https://groups.google.com/d/topic/gremlin-users/iCPUifiU_wk/discussion | |
// closure can't have the same name as the defined step | |
tree = { vertices -> | |
def results = [] | |
vertices.each() { | |
results << it | |
if (label == null) { | |
children = it."$direction"().toList() | |
} else { | |
children = it."$direction"(label).toList() | |
} | |
if (children) { | |
child_tree = tree(children) | |
results << child_tree | |
} | |
} | |
results | |
} | |
inClosure = {final Object... params -> | |
try { label = params[0] } | |
catch(e){ label = null } | |
results = [] | |
direction = "in" | |
_().transform{ tree(it) } | |
} | |
outClosure = {final Object... params -> | |
try { label = params[0] } | |
catch(e){ label = null } | |
results = [] | |
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