How to remove the border between two vertices?

I want to remove the face between two vertices, so my code in java tinkerpop3 is 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 an easier way to remove the border between two vertices by directly querying this edge and removing it? Thank you for your help.

+4
source share
1 answer

Here is an example of how to drop edges between two vertices (where you only have the id of these 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 example, suppose we want to drop the edges between vertices 1 and vertex 2. We can find those for which:

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

and then delete 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 actual vertices, you can simply 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 can rewrite 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();    
}
+10
source

Source: https://habr.com/ru/post/1622717/


All Articles