我的数据存储在 Titan 图形数据库中。我试图找到两个顶点(v1 和 v2)之间的最短路径。目前我有以下代码:
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();
它返回所有路径。我有以下问题:
- 这是找到最短路径的最快方法吗?
- 我怎样才能得到所有这些路径中最短的?
编辑:我正在尝试使用 GremlinGroovyScriptEngine 做同样的工作。我有以下代码:
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);
但我收到以下错误:
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
对任何这些问题的任何建议都会很棒。