Java程序shortestpath orientdb

时间:2016-11-17 15:02:33

标签: java orientdb shortest-path

我在java程序中有这个代码。我已经建立了图形,调用它,我想计算最短的路径,但我什么都没得到。我希望这个结果在java控制台中。请有人帮帮我.....

        OrientGraphFactory factory = new OrientGraphFactory("plocal:DB_PATH",  "admin", "admin"); 
    OrientGraph db = factory.getTx();
for ( int i=0; i<=Number_Nodes; i++){
        for(int j=0; j<=Number_Nodes;j++) {
            for (Vertex v : db.getVertices("vertexID", i)) {
                for (Vertex v1 : db.getVertices("vertexID", j)) {
                    for (Edge e : db.getEdges()) {
                      if (e.getVertex(Direction.IN).equals(v)) {
                          if (e.getVertex(Direction.OUT).equals(v1)){
                              String query = "select expand(shortestPath("+v+","+v1+",'OUT') ";
                            Iterable<OrientVertex> res = db.command(new OCommandSQL(query)).execute();
                                    while (res.iterator().hasNext()) {
                                        OrientVertex vf = res.iterator().next();

                                        System.out.println("Id="+vf.getProperty("vertexID"));


                           }
                       }

                    }

                }

            }
       }
    }     
}

1 个答案:

答案 0 :(得分:0)

在字符串查询中

"select expand(shortestPath("+v+","+v1+",'OUT') "

你打开2个括号,但只关闭其中一个

我试过这个结构

enter image description here

并使用此代码

for ( int i=0; i<=2; i++){
   for(int j=0; j<=2;j++) {
      for (Vertex v : g.getVertices("vertexID", i)) {
         for (Vertex v1 : g.getVertices("vertexID", j)) {
            for (Edge e : g.getEdges()) {
               if (e.getVertex(Direction.IN).equals(v)) {
                  if (e.getVertex(Direction.OUT).equals(v1)){
                    String query = "select expand(shortestPath('"+v.getId()+"','"+v1.getId()+"','BOTH'))";
                    Iterable<OrientVertex> res = g.command(new OCommandSQL(query)).execute();
                    while (res.iterator().hasNext()) {
                       OrientVertex vf = res.iterator().next();
                       System.out.println("Id="+vf.getProperty("vertexID"));
                    }
               }
            }
         }  
      }
   }
}     

}

并且有效。

enter image description here

希望它有所帮助。