Reputation: 1089
My data is stored in a Titan graph database. I am trying to find the shortest path between two vertices (v1 and v2). Currently I have the following code:
final Vertex v1 = titanGraph.getVertices("nodeId", "110969224").iterator().next();
final Vertex v2 = titanGraph.getVertices("nodeId", "141396276").iterator().next();
System.out.println(v2);
final GremlinPipeline<String, List> pipe = new GremlinPipeline<String, List>(v1)
.as("similar")
.both("similar")
.loop("similar", new PipeFunction<LoopBundle<Vertex>, Boolean>() {
@Override
public Boolean compute(LoopBundle<Vertex> bundle) {
return bundle.getLoops() < 4 && bundle.getObject() != v2;
}
})
.path();
which returns alls the paths. I have the following questions:
EDIT: I am trying to do the same work but with GremlinGroovyScriptEngine. I have the following code:
List results = new ArrayList();
Bindings bindings = engine.createBindings();
bindings.put("v1", v1);
bindings.put("v2", v2);
bindings.put("results", results);
engine.eval("v1.both.filter{it.nodeId!='nodeId'}.loop('similar'){!it.object.equals(v2) && it.loop < 5}.paths.fill(results)", bindings);
but I get the following error:
Exception in thread "main" javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
at com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:94)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
at TitanQuery.findShortestPath(TitanQuery.java:89)
at TitanQuery.main(TitanQuery.java:40)
Caused by: javax.script.ScriptException: java.lang.NullPointerException
at com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:221)
at com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:90)
... 3 more
Caused by: java.lang.NullPointerException
at com.tinkerpop.pipes.branch.LoopPipe.getLoops(LoopPipe.java:75)
at com.tinkerpop.pipes.branch.LoopPipe.processNextStart(LoopPipe.java:49)
at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
at com.tinkerpop.pipes.transform.PropertyPipe.processNextStart(PropertyPipe.java:29)
at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
at com.tinkerpop.pipes.util.Pipeline.next(Pipeline.java:115)
at com.tinkerpop.pipes.util.PipeHelper.fillCollection(PipeHelper.java:52)
at com.tinkerpop.gremlin.java.GremlinPipeline.fill(GremlinPipeline.java:1575)
at com.tinkerpop.gremlin.java.GremlinFluentPipeline$fill.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script1.run(Script1.groovy:1)
at com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:219)
... 4 more
Any advice for any of these issues would be great.
Upvotes: 1
Views: 2918
Reputation: 46206
Your code looks to be somewhat similar to the the shortest path recipe in GremlinDocs:
http://gremlindocs.com/#recipes/shortest-path
You might want to read that section in full as you are evaluating both
directions of a vertex which has consequences and has been shown to be better handled with the store/except pattern.
Once you have all the paths need to just select the shortest one from the returned list. In pure Java that's a bit more work than with Groovy, but it basically boils down to a sort on the path length and then choosing the shortest one. In groovy that would be something like:
gremlin> g.v(1).out.loop(1){it.object.id != "3" && it.loops < 6}.path.sort{a,b->a.size()<=>b.size()}
==>[v[1], v[3]]
==>[v[1], v[4], v[3]]
Looking at that made me wonder if you could always just pop-off the first item in the pipeline as it would be the earliest path detected and hence the shortest:
gremlin> g.v(1).out.loop(1){it.object.id != "3" && it.loops < 6}.path[0]
==>[v[1], v[3]]
You might want to experiment with that a bit, but it sounds like a promising theory that would allow you to short-circuit the pipeline if you just needed the first shortest path detected.
Upvotes: 3