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.
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.