Search code examples
gremlindirected-acyclic-graphs

Adding the incoming edges of a vertex to a set property during a traversal


What I would like to do is a depth first traversal, where on each step I add the incoming edge (actually the vertex) to a set. Ultimately I want each node in the step to have a list of incoming vertices that were traversed. Initially I was adding one to a simple property for each incoming edge :

g.V().has('Name', 'startnode').repeat(__.in()).emit().property('degree', union(values('degree'), constant(1)).sum())

I end up with a property degree which holds a count of incoming edges. I now want a set of incoming edges rather than just a count. something similar to :

g.V().has('Name', 'R1\\B').repeat(__.in()).emit().property(set, 'incoming', XXX)

It's that XXX. What do I need to set it to? i.e. the current incoming vertex in the traversal.


Solution

  • In order to get access to the edges, you need to traverse them explicitely.

    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.V().hasLabel('software').
               repeat(__.inE().as('e').outV()).
                 emit().
               property(set, 'incoming', select(last, 'e')).iterate()
    gremlin> g.V().valueMap()
    ==>[incoming:[e[9][1-created->3],e[8][1-knows->4]],name:[marko],age:[29]]
    ==>[name:[vadas],age:[27]]
    ==>[name:[lop],lang:[java]]
    ==>[incoming:[e[11][4-created->3],e[10][4-created->5]],name:[josh],age:[32]]
    ==>[name:[ripple],lang:[java]]
    ==>[incoming:[e[12][6-created->3]],name:[peter],age:[35]]
    

    I wouldn't recommend storing whole edges though; edge ids might be fine.

    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.V().hasLabel('software').
               repeat(__.inE().as('e').outV()).
                 emit().
               property(set, 'incoming', select(last, 'e').by(id)).iterate()
    gremlin> g.V().valueMap()
    ==>[incoming:[9,8],name:[marko],age:[29]]
    ==>[name:[vadas],age:[27]]
    ==>[name:[lop],lang:[java]]
    ==>[incoming:[11,10],name:[josh],age:[32]]
    ==>[name:[ripple],lang:[java]]
    ==>[incoming:[12],name:[peter],age:[35]]
    

    UPDATE

    To collect the adjacent vertex ids, it would be:

    gremlin> g.V().hasLabel('software').
               repeat(__.as('v').in()).
                 emit().
               property(set, 'incoming', select(last, 'v').by(id)).iterate()
    gremlin> g.V().valueMap()
    ==>[incoming:[3,4],name:[marko],age:[29]]
    ==>[name:[vadas],age:[27]]
    ==>[name:[lop],lang:[java]]
    ==>[incoming:[3,5],name:[josh],age:[32]]
    ==>[name:[ripple],lang:[java]]
    ==>[incoming:[3],name:[peter],age:[35]]