Search code examples
javagremlintinkerpop3tinkergraph

Is there way to decouple a traversal from a concrete graph and apply it to multiple graphs?


I want to have an abstract description of traversal steps that I can freely apply to multiple graphs but the traversal instances seem stateful, and not reusable.

I'm looking for something like:

GraphTraversal findMarko = __.has("name", "marko"); //Does Marko exist?
GraphTraversalSource g1 = ...;
GraphTraversalSource g2 = ...;
g1.V().where(findMarko).hasNext(); //Does Marko exist in g1?
g2.V().where(findMarko).hasNext(); //Does Marko exist in g2?

But this results in:

java.lang.IllegalStateException: The traversal strategies are complete and the traversal can no longer be modulated

Is this somehow doable? Does it even make sense?

The reason I need this is that I'm working with thousands of small graphs that I query in the exact same way. I transform abstract rules from some DSL to Gremlin, and would like to skip redoing this step for each graph. Having a reusable (or cloneable) traversal instance would solve that.


Solution

  • The method I was looking for is called match. It provides a way to query the graph declaratively. It takes an anonymous traversal (Useful keyword! This is Gremlin's name for what I called abstract description of traversal steps) and looks for matches starting from the given point:

    GraphTraversal findMarko = __.has("name", "marko"); //Does Marko exist?
    GraphTraversalSource g1 = ...;
    GraphTraversalSource g2 = ...;
    g1.V().match(findMarko).hasNext(); //Does Marko exist in g1?
    g2.V().match(findMarko).hasNext(); //Does Marko exist in g2?