Brenden
Brenden

Reputation: 185

Gremlin 3 stop repeat at specified depth (java)

I want a query that traverses downwards on a certain edge type until it hits a vertex with a given property OR it reaches a given distance from the starting vertex.

I can get this traversal to stop when it hits a certain vertex type, but I can't make it stop at a given depth / distance. There must be something wrong with the way I'm using the "simplePath" and "count" steps, but I'm not sure what it is.

Here's the Java code:

GraphTraversal<Vertex, TinkerGraph> traversal = g.V(start)
    .repeat(__.outE("type").subgraph("subGraph").inV())
    .until(
            __.or(
                //this line works just fine
                __.has("type", "one"),   
                //this line doesn't seem to do what I expect,
                //stop when the size of the path from "start" gets too long
                __.simplePath().count().is(P.gt(3))
            )
    )
    .cap("subGraph");

So what do I need to do to make this traversal stop when the size of the path from the "start" vertex to the current vertex is greater than 3?

Upvotes: 1

Views: 3345

Answers (1)

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

If you're using the latest version, then you can do this:

g.V(start).repeat(...)
          .until(has("type", "one").or().loops().is(gt(3)))
          .cap("subGraph")

Examples:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).repeat(both().simplePath()).until(has("name","peter").or().loops().is(1)).path().by("name")
==>[marko, lop]
==>[marko, vadas]
==>[marko, josh]
gremlin> g.V(1).repeat(both().simplePath()).until(has("name","peter").or().loops().is(2)).path().by("name")
==>[marko, lop, josh]
==>[marko, lop, peter]
==>[marko, josh, ripple]
==>[marko, josh, lop]
gremlin> g.V(1).repeat(both().simplePath()).until(has("name","peter").or().loops().is(3)).path().by("name")
==>[marko, lop, peter]
==>[marko, lop, josh, ripple]
==>[marko, josh, lop, peter]

Upvotes: 6

Related Questions