如何从传递边构建子图?

时间:2017-10-26 09:02:24

标签: gremlin tinkerpop3

我有一个带有具体信息关系的图表,它包含有用的信息,但出于可视化目的,我需要创建一个没有这些中间节点的子图。

示例:

[A:Person] <--AFFILIATE-- [B:Affiliation] --COMPANY--> [C:Org]

我想制作一个这样的子图:

[A:Person] --AFFILIATED_TO--> [C:Org]

有没有简单的方法可以用Gremlin来获得它?

1 个答案:

答案 0 :(得分:5)

我认为您最好的选择可能是使用subgraph()步骤,因为您通常可能会提取边缘引发的子图,然后在该子图上执行一些Gremlin来引入可视化边缘并删除您不需要的东西&# 39;不想。

我可以使用TinkerPop打包的现代玩具图表进行演示:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> sg = g.V().outE('created').subgraph('sg').cap('sg').next() // subgraph creation
==>tinkergraph[vertices:5 edges:4]
gremlin> g = sg.traversal()
==>graphtraversalsource[tinkergraph[vertices:5 edges:4], standard]
gremlin> g.V().as('a').                                     // add special subgraph edge
......1>   out('created').as('software').
......2>   in('created').where(neq('a')).
......3>   addE('co-developer').from('a').
......4>     property('project',select('software').by('name')) 
==>e[0][1-co-developer->4]
==>e[1][1-co-developer->6]
==>e[2][4-co-developer->1]
==>e[3][4-co-developer->6]
==>e[4][6-co-developer->1]
==>e[5][6-co-developer->4]
gremlin> g.V().hasLabel('software').drop() //remove junk from subgraph
gremlin> g.E()
==>e[0][1-co-developer->4]
==>e[1][1-co-developer->6]
==>e[2][4-co-developer->1]
==>e[3][4-co-developer->6]
==>e[4][6-co-developer->1]
==>e[5][6-co-developer->4]
gremlin> g.V().has('name','marko').outE('co-developer').valueMap(true)
==>[label:co-developer,project:lop,id:0]
==>[label:co-developer,project:lop,id:1]