Re: [TinkerPop] Getting the Edge beween two Vertex in java

997 views
Skip to first unread message

Marko Rodriguez

unread,
Oct 12, 2012, 1:05:29 PM10/12/12
to gremli...@googlegroups.com
Hey,

> I am trying to get a labeled edge between to vertices in Java and I am not sure the best way to do this. I have the IDs of both vertices and the label of the edge between them and if they are connected I want to get back the edge.

In Gremlin:

g.v(1).outE('mylabel').inV.has('id',2).back(2)

1. g.v(1) - your start vertex
2. outE('mylabel') - get the outgoing edges with label 'mylabel'
3. inV - get the incoming/head vertex of the edge
4. has('id',2) - filter all vertices that don't have id 2 (the end vertex)
5. back(2) - go back to the edges whose paths were not filtered.

In Gremlin-Java:

Iterable<Edge> edges = new GremlinPipeline(graph.getVertex(1)).outE("mylabel").inV.has("id",2).back(2)


In Blueprints-Java:

List<Edge> edges = new ArrayList<Edge>();
for(Edge edge : graph.getVertex(1).getEdges(Direction.OUT)) {
if(edge.getVertex(Direction.IN).getId().equals(2)) {
edges.add(edge.getVertex(Direction.IN));
}
}

HTH,
Marko.

http://thinkaurelius.com

S. Aden

unread,
Oct 12, 2012, 3:46:00 PM10/12/12
to gremli...@googlegroups.com
Thank you so much.

I really appreciate the work you've done with blueprints stack stack. Great job and keep up the great work.

Cheers,

-Aden

DYH00000

unread,
Nov 13, 2017, 1:17:03 PM11/13/17
to Gremlin-users
final Graph g = TinkerGraphFactory.createTinkerGraph();
g.getVertex(1).addEdge("knows", g.getVertex(6));
final Vertex v1 = g.getVertex(2);
final Vertex v2 = g.getVertex(6);
final Set<Vertex> x = new HashSet<>(Collections.singleton(v1));
System.out.println(g.getEdges());
System.out.println(g.getVertices());
System.out.println("shortest path from "+v1+" to "+v2);
final GremlinPipeline<Object, List> pipe = new GremlinPipeline<>(v1).as("x")
.bothE().bothV().except(x).store(x).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
//while in collection not exist all graph elements->
@Override
public Boolean compute(LoopPipe.LoopBundle<Vertex> bundle) {
return !x.contains(v2);
}
}, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
//(emitted) - end condition ->
@Override
public Boolean compute(LoopPipe.LoopBundle<Vertex> bundle) {
return bundle.getObject() == v2;
}
}).path();
for (final List path : pipe) {
System.out.println(path);
}

OUTPUT:

[e[11][4-created->3], e[0][1-knows->6], e[12][6-created->3], e[7][1-knows->2], e[8][1-knows->4], e[9][1-created->3], e[10][4-created->5]]
[v[1], v[2], v[3], v[4], v[5], v[6]]
shortest path from v[2] to v[6]
[v[2], e[7][1-knows->2], v[1], e[0][1-knows->6], v[6]] 

Good luck!
Reply all
Reply to author
Forward
0 new messages