Search code examples
gremlintinkerpop3

How to build a subgraph from transitive edges?


I have a graph with reified relations, which hold useful information, but for visualization purpose I need to create a subgraph without these intermediary nodes.

Example :

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

And I want to produce a subgraph like this :

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

Is there any simple way to get that with Gremlin ?


Solution

  • I think that your best option might be to use subgraph() step as you normally might to extract the edge-induced subgraph and then execute some Gremlin on that subgraph to introduce the visualization edges and remove the stuff you don't want.

    I can demonstrate with the modern toy graph packaged with 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]