Search code examples
javatraversalgremlintinkerpoptinkerpop3

TinkerPop3 updating graph during traversal


I'm using a TinkerPop3 Traversal in Java, and I want to update output vertices, edit them and then have that edited vertex not show up again while the traversal continues. Is this possible?

This code:

TinkerGraph tg = TinkerGraph.open();

/**add some vertices and edges**/

GraphTraversalSource gt = tg.traversal();
GraphTraversal<Vertex, Map<String,Object>> traversal = gt.V()
    .has("processed",false).as("initial")
    .out()
    .aggregate("vertices")
    .select("initial","vertices");

while(traversal.hasNext()){
    initial.property("processed",true);
}

Will repeatedly spit out the first vertex that it finds over and over, even though I have set the "processed" property to true.

What can I do to fix or work around this?


Solution

  • I'm not sure what's going on in your code example, but if you want to set properties within a traversal, you can do so in a single line of code. See my console session:

    gremlin> graph = TinkerGraph.open()
    ==>tinkergraph[vertices:0 edges:0]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> graph.addVertex('processed',false)
    ==>v[0]
    gremlin> graph.addVertex('processed',false)
    ==>v[2]
    gremlin> graph.addVertex('processed',false)
    ==>v[4]
    gremlin> graph.addVertex('processed',true)
    ==>v[6]
    gremlin> g.V().has('processed',false)
    ==>v[0]
    ==>v[2]
    ==>v[4]
    gremlin> g.V().has('processed',false).property('processed',true)
    ==>v[0]
    ==>v[2]
    ==>v[4]
    gremlin> g.V().has('processed',false)
    gremlin>
    

    You can see that all "processed" properties are now set to false.