简体   繁体   English

Gremlin-遍历树图中的叶节点

[英]Gremlin - Traverse to leaf nodes in tree graph

I have tree data structure in graph as shown in below diagram. 我在图中有树数据结构,如下图所示。 Each color represents node with different labels with relation like employee -> app -> project -> pv -> scan). 每种颜色代表带有不同标签的节点,并具有诸如雇员->应用程序->项目-> PV->扫描的关系。

Question #1: 问题1:

I want to find all leaf nodes (ones in green) of top node 0. 我想找到顶部节点0的所有叶节点(绿色的节点)。

I tried below code with loop which returns all nodes with label employee. 我用循环下面的代码尝试返回所有标签为employee的节点。 Not just leaf nodes. 不只是叶子节点。

g.V().has('person', 'id', '0').repeat(__.in('reportsTo')).emit().values('id')

Sample graph can be found in gremlinbin . 示例图可以在gremlinbin中找到。

How do I find all green leaf nodes? 如何找到所有绿叶节点?

Update #1: 更新#1:

As mentioned in comments, I tried tree pattern. 如评论中所述,我尝试了树模式。 But it doesn't let me call getLeafObjects() on tree. 但这不允许我在树上调用getLeafObjects()。 Not sure what's missing. 不知道丢失了什么。 Also, again I am able to create tree of employee nodes only. 同样,我再次只能创建雇员节点树。 How to traverse to scan nodes? 如何遍历扫描节点?

> tree = g.V().has('person', 'id', '0').repeat(__.in('reportsTo')).emit().tree()
>  tree.getLeafObjects()
No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.getLeafObjects() is applicable for argument types: () values: []

Question #2: 问题2:

How do I retrieve a child vertex amongst children under each parent based on max(id)? 如何基于max(id)在每个父级下的子级中检索子顶点? So in my sample graph, each black vertex can have one or more green child vertex. 因此,在我的示例图中,每个黑色顶点可以具有一个或多个绿色子顶点。 I want to find the green vertices with max(property) under each black vertices. 我想在每个黑色顶点下找到具有max(property)的绿色顶点。

在此处输入图片说明

I think you just need to modify your emit() . 我认为您只需要修改您的emit() Without an argument, that's saying to emit everything from the repeat() . 没有参数,就是说要从repeat()发出所有内容。 If you only want leaf vertices, then include something like: not(outE()) which basically says only emit if there are no outgoing edges on the vertex which would mean it's a leaf vertex. 如果只需要叶顶点,则包括类似: not(outE()) ,它基本上表示仅在顶点上没有出线的边时才发出,这意味着它是叶顶点。 You might need to make your specific emit() predicate a bit smarter as it looks like your schema is such that different types of vertices have different rules for what might make it a leaf. 您可能需要使特定的emit()谓词更聪明,因为您的架构看起来像是这样,不同类型的顶点有不同的规则来定义叶子。

Given the sample graph you had in GremlinBin, I did this to get all the green vertices at the bottom of your picture above: 给定您在GremlinBin中使用的示例图,我这样做是为了在您的图片上方获得所有绿色顶点:

g.V().has('employee','id',1).
  repeat(__.in('reportsTo')).emit().
  repeat(out('has')).emit(__.not(outE('has')))

In answer to your second question you could extend the above to: 为了回答第二个问题,您可以将以上内容扩展到:

g.V().has('employee','id',1).
  repeat(__.in('reportsTo')).emit().
  repeat(out('has')).emit(__.not(outE('has'))).
  group().
    by(__.in('has')).
  select(values).
  unfold().
  order(local).
    by('id',decr).
  local(unfold().limit(1))

Basically group the leaf vertices back on their parent vertex, then pop off the values which is the list of leaves per parent. 基本上将叶顶点重新分组到其父顶点上,然后弹出值(每个父叶的列表)。 Flatten those with unfold() and order them each by the property you care about (in this case "id") and then choose the first item in that ordered list. 使用unfold()平它们,并通过您关心的属性(在本例中为“ id”)对它们进行排序,然后在该有序列表中选择第一项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM