0

I have java application from which I'm calling a groovy class to compute the shortest path between 2 vertices in a graph.

Java:

public class Test {
   public static void main(String[] args) {
      TinkerGraph g = new TinkerGraph();
      Vertex src = null;
      src = g.addVertex(null);
      src.setProperty("name",1); 
      src.setProperty("Type", "switch");
      Vertex src1 = null;
      src1 = g.addVertex(null);
      src1.setProperty("name",2);
      src1.setProperty("Type", "switch");
      Vertex src2 = null;
      src2 = g.addVertex(null);
      src2.setProperty("name",3);
      src2.setProperty("Type", "switch");
      Vertex src3 = null;
      src3 = g.addVertex(null);
      src3.setProperty("name",4);
      src3.setProperty("Type", "switch");
      Edge e=null;
      e=g.addEdge(null, src, src1, "connects");
      Edge e1=null;
      e1= g.addEdge(null, src1, src2, "connects");
      Edge e2=null;
      e2= g.addEdge(null, src2, src3, "connects");
      System.out.println(GetRoute.getPathToHost(g));
   }
}

Groovy:

class GetRoute {
   static {
      Gremlin.load()
   }
   public static Map<Vertex, Integer> getPathToHost(TinkerGraph g) {
      g1.V[["name":1]].both.loop(2){!it.object.equals("name":4)}.Paths >> 1
   }
}

My question is:

  1. First of all the query is wrong. I've computed a function in gremlinpipe+java but it's pretty huge and I'm try to find a easier way with groovy. How do I refine this query?

  2. Lets say my query prints all the vertices in the path between 2 vertices then how do I store it in let say an Map array or how do I print it onto the console?

Any help would be highly appreciated.

Regards

Opal
  • 81,889
  • 28
  • 189
  • 210

1 Answers1

0

Here's my Gremlin session:

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> src = g.addVertex(null)
==>v[0]
gremlin> src.setProperty("name",1) 
==>null
gremlin> src.setProperty("Type", "switch")
==>null
gremlin> src1 = g.addVertex(null)
==>v[1]
gremlin> src1.setProperty("name",2)
==>null
gremlin> src1.setProperty("Type", "switch")
==>null
gremlin> src2 = g.addVertex(null)
==>v[2]
gremlin> src2.setProperty("name",3)
==>null
gremlin> src2.setProperty("Type", "switch")
==>null
gremlin> src3 = g.addVertex(null)
==>v[3]
gremlin> src3.setProperty("name",4)
==>null
gremlin> src3.setProperty("Type", "switch")
==>null
gremlin> e=g.addEdge(null, src, src1, "connects")
==>e[4][0-connects->1]
gremlin> e1= g.addEdge(null, src1, src2, "connects")
==>e[5][1-connects->2]
gremlin> e2= g.addEdge(null, src2, src3, "connects")
==>e[6][2-connects->3]
gremlin> g.V.has('name',1).out.loop(1){it.object.name != 4 && it.loops < 5}{true}.path.filter{it.last().name == 4}
==>[v[0], v[1], v[2], v[3]]

The last line there calculates the path between 1 and 4. You can read more about shortest path calculations with Gremlin in gremlindocs: http://gremlindocs.com/#recipes/shortest-path (please pardon the formatting...some dns issues are plaguing it atm)

I'm not sure how you would want that Map you asked for. The List format seems good to me for a path, but I suppose if you wanted it keyed on the first vertex in the path, I guess you could just issue another tranform:

gremlin> g.V.has('name',1).out.loop(1){it.object.name != 4 && it.loops < 5}{true}.path.filter{it.last().name == 4}.transform{[(it.first()):it]}
==>{v[0]=[v[0], v[1], v[2], v[3]]}
stephen mallette
  • 45,298
  • 5
  • 67
  • 135