Search in sources :

Example 31 with Edge

use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.

the class OGraphSONUtility method edgeFromJson.

/**
 * Creates an edge from GraphSON using settings supplied in the constructor.
 */
public Edge edgeFromJson(final JsonNode json, final Vertex out, final Vertex in) throws IOException {
    final Map<String, Object> props = OGraphSONUtility.readProperties(json, true, this.hasEmbeddedTypes);
    final Object edgeId = getTypedValueFromJsonNode(json.get(GraphSONTokens._ID));
    final JsonNode nodeLabel = json.get(GraphSONTokens._LABEL);
    // assigned an empty string edge label in cases where one does not exist. this gets around the requirement
    // that blueprints graphs have a non-null label while ensuring that GraphSON can stay flexible in parsing
    // partial bits from the JSON. Not sure if there is any gotchas developing out of this.
    final String label = nodeLabel == null ? EMPTY_STRING : nodeLabel.textValue();
    final Edge e = factory.createEdge(edgeId, out, in, label);
    for (Map.Entry<String, Object> entry : props.entrySet()) {
        // if (this.edgePropertyKeys == null || this.edgePropertyKeys.contains(entry.getKey())) {
        if (includeKey(entry.getKey(), edgePropertyKeys, this.edgePropertiesRule)) {
            e.setProperty(entry.getKey(), entry.getValue());
        }
    }
    return e;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) Edge(com.tinkerpop.blueprints.Edge)

Example 32 with Edge

use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.

the class GraphDatabaseTest method sqlNestedQueries.

@Test
public void sqlNestedQueries() {
    Vertex vertex1 = database.addVertex(null, "driver", "John");
    Vertex vertex2 = database.addVertex(null, "car", "ford");
    Vertex targetVertex = database.addVertex(null, "car", "audi");
    Edge edge = database.addEdge(null, vertex1, vertex2, "E");
    edge.setProperty("color", "red");
    edge.setProperty("action", "owns");
    edge = database.addEdge(null, vertex1, targetVertex, "E");
    edge.setProperty("color", "red");
    edge.setProperty("action", "wants");
    database.commit();
    String query1 = "select driver from V where out().car contains 'ford'";
    List<ODocument> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query1));
    Assert.assertEquals(result.size(), 1);
    String query2 = "select driver from V where outE()[color='red'].inV().car contains 'ford'";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query2));
    Assert.assertEquals(result.size(), 1);
    // TODO these tests are broken, they should test "contains" instead of "="
    String query3 = "select driver from V where outE()[action='owns'].inV().car = 'ford'";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query3));
    Assert.assertEquals(result.size(), 1);
    String query4 = "select driver from V where outE()[color='red'][action='owns'].inV().car = 'ford'";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query4));
    Assert.assertEquals(result.size(), 1);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Edge(com.tinkerpop.blueprints.Edge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 33 with Edge

use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.

the class GraphDatabaseTest method testDeleteOfVerticesAndEdgesWithDeleteCommandAndUnsafe.

public void testDeleteOfVerticesAndEdgesWithDeleteCommandAndUnsafe() {
    Iterable<OIdentifiable> deletedVertices = database.command(new OCommandSQL("delete from GraphVehicle return before limit 1 unsafe")).execute();
    Assert.assertTrue(deletedVertices.iterator().hasNext());
    OrientVertex v = (OrientVertex) deletedVertices.iterator().next();
    Integer confirmDeleted = database.command(new OCommandSQL("delete from " + v.getIdentity() + " unsafe")).execute();
    Assert.assertFalse(deletedVertices.iterator().hasNext());
    Assert.assertEquals(confirmDeleted.intValue(), 0);
    Iterable<Edge> edges = v.getEdges(Direction.BOTH);
    for (Edge e : edges) {
        Integer deletedEdges = database.command(new OCommandSQL("delete from " + ((OrientEdge) e).getIdentity() + " unsafe")).execute();
        Assert.assertEquals(deletedEdges.intValue(), 1);
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Edge(com.tinkerpop.blueprints.Edge)

Example 34 with Edge

use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.

the class GraphDatabaseTest method populate.

@Test
public void populate() {
    OClass vehicleClass = database.createVertexType("GraphVehicle");
    database.createVertexType("GraphCar", vehicleClass);
    database.createVertexType("GraphMotocycle", "GraphVehicle");
    ODocument carNode = database.addVertex("class:GraphCar", "brand", "Hyundai", "model", "Coupe", "year", 2003).getRecord();
    ODocument motoNode = database.addVertex("class:GraphMotocycle", "brand", "Yamaha", "model", "X-City 250", "year", 2009).getRecord();
    database.commit();
    database.addEdge(null, database.getVertex(carNode), database.getVertex(motoNode), "E").save();
    List<ODocument> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>("select from GraphVehicle"));
    Assert.assertEquals(result.size(), 2);
    for (ODocument v : result) {
        Assert.assertTrue(v.getSchemaClass().isSubClassOf(vehicleClass));
    }
    database.shutdown();
    database = new OrientGraph(url);
    database.setUseLightweightEdges(false);
    database.getRawGraph().getMetadata().getSchema().reload();
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>("select from GraphVehicle"));
    Assert.assertEquals(result.size(), 2);
    Edge edge1 = null;
    Edge edge2 = null;
    for (ODocument v : result) {
        Assert.assertTrue(v.getSchemaClass().isSubClassOf("GraphVehicle"));
        if (v.getClassName().equals("GraphCar")) {
            Assert.assertEquals(database.getVertex(v).countEdges(Direction.OUT), 1);
            edge1 = database.getVertex(v).getEdges(Direction.OUT).iterator().next();
        } else {
            Assert.assertEquals(database.getVertex(v).countEdges(Direction.IN), 1);
            edge2 = database.getVertex(v).getEdges(Direction.IN).iterator().next();
        }
    }
    Assert.assertEquals(edge1, edge2);
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Edge(com.tinkerpop.blueprints.Edge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 35 with Edge

use of com.tinkerpop.blueprints.Edge in project orientdb by orientechnologies.

the class ServerClusterAsyncGraphTest method executeTest.

@Override
protected void executeTest() throws Exception {
    {
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server0/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            g.createVertexType("Post");
            g.createVertexType("User");
            g.createEdgeType("Own");
            g.addVertex("class:User");
            g.command(new OCommandSQL("insert into Post (content, timestamp) values('test', 1)")).execute();
        } finally {
            g.shutdown();
        }
    }
    // CHECK VERTEX CREATION ON ALL THE SERVERS
    for (int s = 0; s < SERVERS; ++s) {
        OrientGraphFactory factory2 = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g2 = factory2.getNoTx();
        try {
            Iterable<OrientVertex> result = g2.command(new OCommandSQL("select from Post")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            Assert.assertNotNull(result.iterator().next());
        } finally {
            g2.shutdown();
        }
    }
    {
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server0/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            g.command(new OCommandSQL("create edge Own from (select from User) to (select from Post)").onAsyncReplicationError(new OAsyncReplicationError() {

                @Override
                public ACTION onAsyncReplicationError(Throwable iException, int iRetry) {
                    return iException instanceof ONeedRetryException && iRetry <= 3 ? ACTION.RETRY : ACTION.IGNORE;
                }
            })).execute();
        } finally {
            g.shutdown();
        }
    }
    Thread.sleep(1000);
    // CHECK VERTEX CREATION ON ALL THE SERVERS
    for (int s = 0; s < SERVERS; ++s) {
        OrientGraphFactory factory2 = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g2 = factory2.getNoTx();
        try {
            Iterable<OrientVertex> result = g2.command(new OCommandSQL("select from Own")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            Assert.assertNotNull(result.iterator().next());
            result = g2.command(new OCommandSQL("select from Post")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            final OrientVertex v = result.iterator().next();
            Assert.assertNotNull(v);
            final Iterable<Edge> inEdges = v.getEdges(Direction.IN);
            Assert.assertTrue(inEdges.iterator().hasNext());
            Assert.assertNotNull(inEdges.iterator().next());
            result = g2.command(new OCommandSQL("select from User")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            final OrientVertex v2 = result.iterator().next();
            Assert.assertNotNull(v2);
            final Iterable<Edge> outEdges = v2.getEdges(Direction.OUT);
            Assert.assertTrue(outEdges.iterator().hasNext());
            Assert.assertNotNull(outEdges.iterator().next());
        } finally {
            g2.shutdown();
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAsyncReplicationError(com.orientechnologies.orient.core.replication.OAsyncReplicationError) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) Edge(com.tinkerpop.blueprints.Edge)

Aggregations

Edge (com.tinkerpop.blueprints.Edge)214 Vertex (com.tinkerpop.blueprints.Vertex)141 Test (org.junit.Test)53 Graph (com.tinkerpop.blueprints.Graph)49 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)49 HashSet (java.util.HashSet)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)13 ArrayList (java.util.ArrayList)13 Collection (java.util.Collection)11 JSONObject (org.codehaus.jettison.json.JSONObject)11 HashMap (java.util.HashMap)10 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)8 Map (java.util.Map)8 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)6 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)6 URI (org.openrdf.model.URI)6