Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 2.56 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #
  2. # Copyright 2011 James Thornton (http://jamesthornton.com)
  3. # BSD License (see LICENSE for details)
  4. #
  5. # Gremlin scripts in Gremlin-Groovy v1.3
  6. #
  7. # Each script is a string literal of a Python string template.
  8. #
  9. # See the Gremlin and Blueprints docs for the full Gremlin/Blueprints API.
  10. #
  11. # Gremlin Wiki:     https://github.com/tinkerpop/gremlin/wiki
  12. # Gremlin Steps:    https://github.com/tinkerpop/gremlin/wiki/Gremlin-Steps
  13. # Gremlin Methods:  https://github.com/tinkerpop/gremlin/wiki/Gremlin-Methods
  14. # Blueprints Wiki:  https://github.com/tinkerpop/blueprints/wiki
  15. #
  16. # Performance: The YAML file is only sourced once so no per-request overhead,
  17. #              and timeit showed 21ms to build a script from named params vs
  18. #              8ms for traditional string substitutions, such as
  19. #              g.v(%s).outE(%s) % (_id, label)
  20. #
  21.  
  22.  
  23. get_vertices: |
  24.   g.getVertices()
  25.  
  26. get_edges: |
  27.   g.getEdges()
  28.  
  29. outE: |
  30.   g.v($_id).outE($label)
  31.  
  32. inE: |
  33.   g.v($_id).inE($label)
  34.  
  35. bothE: |
  36.   g.v($_id).bothE($label)
  37.  
  38. outV: |
  39.   g.v($_id).outV($label)
  40.  
  41. inV: |
  42.   g.v($_id).inV($label)
  43.  
  44. bothV: |
  45.   g.v($_id).bothV($label)
  46.  
  47. save_graphml: |
  48.   g.saveGraphML('data/graphml')
  49.   new File('data/graphml').getText()
  50.  
  51. load_graphml: |
  52.   g.loadGraphML($uri)
  53.  
  54. rebuild_vertex_index: |
  55.   index = g.getIndex('$index_name',Vertex)
  56.   AutomaticIndexHelper.reIndexElements(g, index, g.getVertices())
  57.  
  58. rebuild_edge_index: |
  59.   index = g.getIndex('$index_name',Edge)
  60.   AutomaticIndexHelper.reIndexElements(g, index, g.getEdges())
  61.  
  62.  
  63. #
  64. # Gremlin user-defined defined tree steps, inTree() and outTree().
  65. #
  66. # You must execute this at least once before you use the tree steps.
  67. # For production use, this should really be defined server side.
  68. #
  69. # See https://groups.google.com/d/topic/gremlin-users/iCPUifiU_wk/discussion
  70. #
  71. # Note: $$ is a Python template escape sequence, replaced with a single "$".
  72. #
  73. define_tree_steps: |
  74.   tree = { vertices ->
  75.  
  76.     def results = []
  77.  
  78.     vertices.each() {
  79.       results << it
  80.       children = it."$$direction"().toList()
  81.       if (children) {
  82.         child_tree = tree(children)
  83.         results << child_tree
  84.       }
  85.     }
  86.     results
  87.   }
  88.  
  89.   inClosure = {final Object... params ->
  90.     try { label = params[0] }
  91.     catch(e){ label = null }
  92.     results = []
  93.     direction = "in"
  94.     _().transform{ tree(it) }
  95.   }    
  96.  
  97.   outClosure = {final Object... params ->
  98.     try { label = params[0] }
  99.     catch(e){ label = null }
  100.     results = []
  101.     direction = "out"
  102.     _().transform{ tree(it) }
  103.   }  
  104.  
  105.   Gremlin.defineStep("inTree", [Vertex,Pipe], inClosure)
  106.   Gremlin.defineStep("outTree", [Vertex,Pipe], outClosure)