如何从数据库查询中构建Groovy中的树?

时间:2011-08-18 09:29:51

标签: groovy recursion tree

看起来它返回结构,但值不存在......

g = TinkerGraphFactory.createTinkerGraph()
root = g.v(1)

def tree
def results = []
tree = { vertices ->
  vertices.each() {
    children = it.out().toList()
    if (children) 
      results << tree(children)
  }
  results.toList()
}

println tree(root)

以下是结果......

$ ./gremlin.sh -e treeTest.groovy
[[], [[]]]

注意:我通常使用Python而不是Groovy,所以我可能错过了一些明显的东西。

1 个答案:

答案 0 :(得分:0)

tree = { vertices ->

  def results = []

  vertices.each() {
    results << it
    children = it."$direction"().toList()
    if (children) {
      child_tree = tree(children)
      results << child_tree
    }
  }
  results
}