Gremlin:确定在另一个顶点的所有连接中的顶点

Gremlin: Determine vertices that are in all connections of another vertex

我正在使用 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

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

另一种表达方式是“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”)

非常感谢任何帮助!

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

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]