Identifying if 2 Vertices are connected with edge using Orientdb 3 and tinkerpop 3

101 Views Asked by At

I have been trying to look through documentation. I am just trying to find a simple example to identify as mentioned in the title if 2 vertices are connected to each other. This is for OrientDB 3 using the tinkerpop API blueprints 2.6. Ex: Vertex v1; Vertex v2;

boolean connected = false;

connected = <somefunction returns true/false if v1 and v2 connected by an edge>;

Can someone please provide an example if they have seen something like this?

2

There are 2 best solutions below

1
On

try this:

String db_name = "dbname";
String path = "remote:localhost/" + db_name;

OrientDB orientDB = new OrientDB("remote:localhost", "username", "password", OrientDBConfig.defaultConfig());

try(ODatabaseSession db = orientDB.open(db_name,"username","password")) 
{
    ORID theEdge = new ORecordId("edge_rid");
    OEdge e = db.load(theEdge);

    ORID theVertex = new ORecordId("v1_rid");
    OVertex v1 = db.load(theVertex);

    ORID theVertex2 = new ORecordId("v2_rid");
    OVertex v2 = db.load(theVertex2);

    boolean connected = false;
    OVertex from = e.getFrom();
    OVertex to = e.getTo();

    if(from.getIdentity().equals(v1) && to.getIdentity().equals(v2))
    {
        connected = true;
    }

    if(connected)
    {
        System.out.println("Vertex v1: " + from.getIdentity());
        System.out.println("Vertex v2: " + to.getIdentity());
    }
}



Hope it helps

Regards

1
On

You need iterate one vertex(v1) edges and check if it s connecting to the other vertex(v2):

OrientVertex v1,v2; 
for (Edge e : (Iterable<Edge>)() ->  v1.edges(Direction.OUT)) 
    if (e.vertices(Direction.IN).next().id().equals(v2.id())) {
    }