【问题标题】:Gremlin: Determine vertices that are in all connections of another vertexGremlin:确定在另一个顶点的所有连接中的顶点
【发布时间】:2018-07-14 06:12:20
【问题描述】:

我正在使用 TinkerPop3 Gremlin Console 3.3.1 来分析图形数据库。我想确定哪些顶点的连接与相同标签的其他顶点的所有相似连接重叠。例如,为了清楚起见,使用带有附加“软件”顶点和两个“创建”边的 TinkerFactory Modern 图:

graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
g = graph.traversal() 
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
graph.addVertex(T.label, "software", T.id, 13, "name", “sw")
==>v[13]
g.V("4").addE("created").to(V("13"))
==>e[14][4-created->13]
g.V("6").addE("created").to(V("5"))
==>e[15][6-created->5]

请看下图我修改的现代图。我把我感兴趣的箭头放在橙色上。

Modified TinkerFactory Modern graph visual

通过这个示例,我想确定哪些人创建了包含其他人创建的所有软件的软件。没有必要知道是哪个软件。所以这个例子的结果是:

  • Josh (V(4)) 共同创建了 Marko (V(1)) 创建的所有软件
  • Josh (V(4)) 与 Peter (V(6)) 共同创建了所有软件
  • Peter (V(6)) 共同创建了 Marko (V(1)) 创建的所有软件

另一种说法是“Marko 创建的所有软件也都有 Josh 作为创建者”,等等。

下面的代码是我所能得到的。它的目的是通过检查每个人和“a”之间共享的软件数量是否等于“a”创建的软件总量来找到重叠连接。不幸的是,它没有给出结果。

gremlin> 
g.V().has(label,"person").as("a").
    both().has(label,"software").aggregate("swA").
    both().has(label,"person").where(neq("a")).dedup().
    where(both().has(label,"software").
       where(within("swA")).count().
           where(is(eq(select("swA").unfold().count())
           )
        )
    ).as("b").select("a","b").by(“name”)

非常感谢任何帮助!

【问题讨论】:

    标签: graph-databases gremlin tinkerpop tinkerpop3


    【解决方案1】:

    首先找到所有共同创造了至少一个产品的人。

    g.V().hasLabel('person').as('p1').
      out('created').in('created').
      where(neq('p1')).as('p2').
      dedup('p1','p2').
      select('p1','p2').
        by('name')
    

    您可以从那里添加一些模式匹配,以验证人员 p1 创建的产品数与从这些产品到人员 p2 的连接数匹配。

    g.V().hasLabel('person').as('p1').
      out('created').in('created').
      where(neq('p1')).as('p2').
      dedup('p1','p2').
      match(__.as('p1').out('created').fold().as('x'),
            __.as('x').count(local).as('c'),
            __.as('x').unfold().in('created').where(eq('p2')).count().as('c')).
      select('p1','p2').
        by('name')
    

    结果:

    gremlin> g.V().hasLabel('person').as('p1').
    ......1>   out('created').in('created').
    ......2>   where(neq('p1')).as('p2').
    ......3>   dedup('p1','p2').
    ......4>   match(__.as('p1').out('created').fold().as('x'),
    ......5>         __.as('x').count(local).as('c'),
    ......6>         __.as('x').unfold().in('created').where(eq('p2')).count().as('c')).
    ......7>   select('p1','p2').
    ......8>     by('name')
    ==>[p1:marko,p2:josh]
    ==>[p1:marko,p2:peter]
    ==>[p1:peter,p2:josh]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      相关资源
      最近更新 更多