Skip to content

Instantly share code, notes, and snippets.

@espeed
Created November 20, 2011 21:19
Show Gist options
  • Save espeed/1380945 to your computer and use it in GitHub Desktop.
Save espeed/1380945 to your computer and use it in GitHub Desktop.
Bulbs 0.3 gremlin.yaml
#
# Copyright 2011 James Thornton (http://jamesthornton.com)
# BSD License (see LICENSE for details)
#
# Gremlin scripts in Gremlin-Groovy v1.3
#
# Each script is a string literal of a Python string template.
#
# See the Gremlin and Blueprints docs for the full Gremlin/Blueprints API.
#
# Gremlin Wiki: https://github.com/tinkerpop/gremlin/wiki
# Gremlin Steps: https://github.com/tinkerpop/gremlin/wiki/Gremlin-Steps
# Gremlin Methods: https://github.com/tinkerpop/gremlin/wiki/Gremlin-Methods
# Blueprints Wiki: https://github.com/tinkerpop/blueprints/wiki
#
# Performance: The YAML file is only sourced once so no per-request overhead,
# and timeit showed 21ms to build a script from named params vs
# 8ms for traditional string substitutions, such as
# g.v(%s).outE(%s) % (_id, label)
#
get_vertices: |
g.getVertices()
get_edges: |
g.getEdges()
outE: |
g.v($_id).outE($label)
inE: |
g.v($_id).inE($label)
bothE: |
g.v($_id).bothE($label)
outV: |
g.v($_id).outV($label)
inV: |
g.v($_id).inV($label)
bothV: |
g.v($_id).bothV($label)
save_graphml: |
g.saveGraphML('data/graphml')
new File('data/graphml').getText()
load_graphml: |
g.loadGraphML($uri)
rebuild_vertex_index: |
index = g.getIndex('$index_name',Vertex)
AutomaticIndexHelper.reIndexElements(g, index, g.getVertices())
rebuild_edge_index: |
index = g.getIndex('$index_name',Edge)
AutomaticIndexHelper.reIndexElements(g, index, g.getEdges())
#
# Gremlin user-defined defined tree steps, inTree() and outTree().
#
# You must execute this at least once before you use the tree steps.
# For production use, this should really be defined server side.
#
# See https://groups.google.com/d/topic/gremlin-users/iCPUifiU_wk/discussion
#
# Note: $$ is a Python template escape sequence, replaced with a single "$".
#
define_tree_steps: |
tree = { vertices ->
def results = []
vertices.each() {
results << it
children = it."$$direction"().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