How to remove edge between two vertices?
Asked Answered
L

1

11

I want to remove edge between two vertices, so my code in java tinkerpop3 as below

private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
        if(g.V(toV).inE(edgeLabel).bothV().hasId(fromV.id()).hasNext()){
            List<Edge> edgeList = g.V(toV).inE(edgeLabel).toList();
            for (Edge edge:edgeList){
                if(edge.outVertex().id().equals(fromV.id())) {
                    TitanGraph().tx();
                    edge.remove();                    
                    TitanGraph().tx().commit();
                    return;//Remove edge ok, now return.
                }
            }
        }
    }

Is there a simpler way to remove edge between two vertices by a direct query to that edge and remove it? Thank for your help.

Llanes answered 4/1, 2016 at 10:36 Comment(0)
C
23

Here's an example of how to drop edges between two vertices (where you just have the ids of those vertices:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]

For purpose of the example, let's say we want to drop edges between vertex 1 and vertex 2. We could find those with:

gremlin> g.V(1).bothE().where(otherV().hasId(2))
==>e[7][1-knows->2]

and then remove it with:

gremlin> g.V(1).bothE().where(otherV().hasId(2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]

If you have the actual vertices, then you could just do:

gremlin> g.V(v1).bothE().where(otherV().is(v2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]

You could re-write your function as:

private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
    g.V(fromV).bothE().hasLabel(edgeLabel).where(__.otherV().is(toV)).drop().iterate();
    g.tx().commit();    
}
Cryptoanalysis answered 4/1, 2016 at 12:32 Comment(7)
Thank you. This is very helpful for me. Tinkerpop3 is new and not too many examples. If possible, can you give me more resources,links or references where i can find more examples? I need to learn more gremlin from examples to optimize code. Thank you again.Llanes
More documentation is under development, but as of right now you only have the reference documentation where there are examples of nearly all the available steps.Cryptoanalysis
it works when i change to g.V(fromV).bothE(edgeLabel).where(__.otherV().is(toV)).drop().iterate(); g.tx().commit();Llanes
I guess drop() doesn't auto-iterate - i thought it was a terminating step - sorry about that.Cryptoanalysis
Exactly, the problem in this language is that things do not happen immediately. Up to drop() is just a pipeline that it is executed only when you call iterate() or next() They show it in the console, which automatically consumes the command, so is like there is an hidden step in all their instructionsDittman
I would like to hide the edge relation instead of removing or destroying the connection so that I can retrieve the relation at a later stage. Could you please explain how to do this? Thanks.Edmondedmonda
"hide"? i guess you just mean you would add a property of "hidden" to the edge? if so, instead of drop() you would just do property('hiddent',true). not sure if that answers your question. if not, perhaps you should create a new question here on SO.Cryptoanalysis

© 2022 - 2024 — McMap. All rights reserved.