Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an edge and add a new edge between two vertices?

I'm trying to drop an edge and add a new edge between two vertices. How do I do that in Tinkerpop3?

def user = g.V().has("userId", 'iamuser42').has("tenantId", 'testtenant').hasLabel('User'); 
user.outE("is_invited_to_join").where(otherV().has("groupId", 'test123')).drop();
def group = g.V().has("groupId", 'test123').has("tenantId", 'testtenant').hasLabel('Group').next();
user.addEdge("is_member_of", group);

This is the error I get on gremlin shell:

No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.addEdge() is applicable for argument types: (java.lang.String, com.thinkaurelius.titan.graphdb.vertices.CacheVertex) values: [is_member_of, v[8224]]

Thank you.

like image 411
Vibgy Avatar asked Nov 17 '16 14:11

Vibgy


People also ask

How do you add an edge between two vertices in Java?

Inserting an edge: To insert an edge between two vertices suppose i and j, set the corresponding values in the adjacency matrix equal to 1, i.e. g[i][j]=1 and g[j][i]=1 if both the vertices i and j exists.

How do you add edges to a directed graph?

Add an edge between u and v. The nodes u and v will be automatically added if they are not already in the graph. Edge attributes can be specified with keywords or by directly accessing the edge's attribute dictionary.


1 Answers

The Gremlin Console tutorial discusses this issue a bit. You are not iterating your traversal. Consider the following:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> person = g.V().has('name','marko')
==>v[1]

Great! The person Vertex is stored in the "person" variable...or is it?

gremlin> person.class
==>class org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal

Apparently it is not a Vertex. "person" is a Traversal, the console sees it as such and iterates it for you which is why you get "v1" in the output. Note that the traversal is now "done":

gremlin> person.hasNext()
==>false

You need to iterate the Traversal into your variable - in this case, using next():

gremlin> person = g.V().has('name','marko').next()
==>v[1]
gremlin> person.class
==>class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex

Note that you will have further problems down the line in your script because you are using what will now be a Vertex as a Traversal, so this line:

user.outE("is_invited_to_join").where(otherV().has("groupId", 'test123')).drop();

will now fail with a similar error because Vertex will not have outE(). You would need to do something like:

g.V(user).outE("is_invited_to_join").where(otherV().has("groupId", 'test123')).drop();

If you would like to add an edge and drop the old one in the same traversal, you could do something like this:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).outE('knows')
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.V(1).as('a').outE().as('edge').inV().hasId(2).as('b').addE('knew').from('a').to('b').select('edge').drop()
gremlin> g.V(1).outE('knew')
==>e[12][1-knew->2]
gremlin> g.V(1).outE('knows')
==>e[8][1-knows->4]

Arguably, that might not be as readable as breaking it into two or more separate traversals, but it can be done.

like image 180
stephen mallette Avatar answered Oct 16 '22 05:10

stephen mallette