2

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.

kaqqao
  • 12,984
  • 10
  • 64
  • 118
  • Are you trying to add extra steps after `findMarko` or is it failing exactly as you show it. For me it works fine using the steps you have shown. However, if you are trying to do something like `where(findMarko.someOtherSteps())` then you need to clone the traversal once the traversal has already been iterated once. – Kelvin Lawrence Jul 14 '21 at 19:41
  • @KelvinLawrence I don't add extra steps, but my traversal is more complex than the example. I have has(...).has(...).out().has(...).has(...). Maybe something there is stateful... Oh, btw, is the code I posted even the correct way to check whether a vertex exists? – kaqqao Jul 14 '21 at 20:01
  • It's one way. Another is to just do something like `...id().toList()` if nothing exists the list will be empty. – Kelvin Lawrence Jul 14 '21 at 20:59

1 Answers1

0

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?
kaqqao
  • 12,984
  • 10
  • 64
  • 118