简体   繁体   中英

Can I somehow reuse Gremlin GraphTraversals code when assigned to a variable?

I have GraphTraversals that look like that (attrGroup:

GraphTraversal<Vertex,Edge> attrGroupTraversal = some code generates it; 
List<Vertex> attributeVs = attrGroupTraversal.inV().out(EdgeLabels.ContainsAttribute).toList();
List<Vertex> sourceVs = attrGroupTraversal.inV().out(EdgeLabels.ContainsSource).toList();
List<Vertex> targetVs = attrGroupTraversal.out(EdgeLabels.ContainsTarget).toList();

Is there a way to reuse attrGroupTraversal so I don't need to rerun the code that created it in the first place?

I am getting:

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

at org.apache.tinkerpop.gremlin.process.traversal.Traversal$Exceptions.traversalIsLocked(Traversal.java:245) at org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal.addStep(DefaultTraversal.java:274) at org.apache.tinkerpop.gremlin.process.traversal.Traversal$Admin.addStep(Traversal.java:301) at org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal$Admin.addStep(GraphTraversal.java:170) at org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal.inV(GraphTraversal.java:384)

I suggest you .clone() your original traversal. The following should work:

GraphTraversal<Vertex,Edge> attrGroupTraversal = some code generates it; 
List<Vertex> attributeVs = attrGroupTraversal.clone().inV().out(EdgeLabels.ContainsAttribute).toList();
List<Vertex> sourceVs = attrGroupTraversal.clone().inV().out(EdgeLabels.ContainsSource).toList();
List<Vertex> targetVs = attrGroupTraversal.clone().out(EdgeLabels.ContainsTarget).toList();

See also: Traversal component reuse from the TinkerPop documentation recipes.

I am assuming that you are getting the exception in the second invocation of "inV(...)". The exception is telling you that is finished traversing.

If several gremlin invocations become expensive (which I am assuming is what you are trying to avoid) you could bypass gremlin (if the underlying graph allows you to do such a thing) and work directly with the java api.

That way you can pick and choose as many v and e as you need

Try this:

GraphTraversal t = graph.traversal().V().has("type", "COMPONENT");

GraphTraversal newT = t.asAdmin().clone();

This saved my day.

Credit: https://groups.google.com/forum/#!msg/gremlin-users/ltEYV-ShTYg/xf9s8k8SCQAJ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM