Search code examples
javagraph-databasesgremlintinkerpop3

Type-Filter Step in Gremlin 3?


I would like to know how to apply a type filter in a Gremlin 3.x GraphTraversal. For example, let's consider the Graph of the Gods as an example graph.

Here, we can do the following:

Set<Element> set = graph.V().has("name", "jupiter").toSet()

... and get a Set<Element>. What I would like to have is an Iterable<Vertex> instead (or a subclass of Iterable). From the traversal declaration, the result can only consist of vertices, because of the .V() operator. Due to the Java type system, it is needlessly generalized to Element. Is there a way to type filter this? What I would like to do is something along these lines...

Set<Vertex> vertices = graph.V().has("name", "jupiter").cast(Vertex.class).toSet();

... but there is no cast operator. Ultimately, the reason why I want to do this is because I need to iterate over the resulting vertices in a foreach-loop, and having to down-cast the loop variable as the first statement in the loop is annoying.

I'm open for alternatives, I'm still new to Gremlin 3 and there may be many things I'm not aware of yet.


Solution

  • Against Tinkerpop3 M6/ titan0.9 M1, the following should work:

    Set<Vertex> vertices = graph.V().<Vertex>has("name", "jupiter").toSet();
    

    Most of the M6 type issues can be resolved in a similar manner (parameterizing the method that produces the element by the expected type).