Search in sources :

Example 41 with Edge

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

the class GraphFunctionsTest method testInE.

@Test
public void testInE() {
    int found;
    // V1
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( inE() ) from " + v1.getId())).execute()) found++;
    Assert.assertEquals(found, 0);
    // V2
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( inE() ) from " + v2.getId())).execute()) found++;
    Assert.assertEquals(found, 1);
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( inE('SubEdge') ) from " + v2.getId())).execute()) found++;
    Assert.assertEquals(found, 1);
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( inE('dddd') ) from " + v2.getId())).execute()) found++;
    Assert.assertEquals(found, 0);
    // V3
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( inE() ) from " + v3.getId())).execute()) found++;
    Assert.assertEquals(found, 1);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 42 with Edge

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

the class GraphFunctionsTest method testOutE.

@Test
public void testOutE() {
    int found;
    // V1
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( outE() ) from " + v1.getId())).execute()) found++;
    Assert.assertEquals(found, 2);
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( outE('SubEdge') ) from " + v1.getId())).execute()) found++;
    Assert.assertEquals(found, 1);
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( outE('dddd') ) from " + v1.getId())).execute()) found++;
    Assert.assertEquals(found, 0);
    // V2
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( outE() ) from " + v2.getId())).execute()) found++;
    Assert.assertEquals(found, 0);
    // V3
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( outE() ) from " + v3.getId())).execute()) found++;
    Assert.assertEquals(found, 0);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 43 with Edge

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

the class GraphFunctionsTest method testOutEPolymorphic.

@Test
public void testOutEPolymorphic() {
    int found;
    // V1
    found = 0;
    for (Edge v : (Iterable<Edge>) graph.command(new OCommandSQL("select expand( outE('E') ) from " + v1.getId())).execute()) found++;
    Assert.assertEquals(found, 2);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 44 with Edge

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

the class OHttpGraphResponse method writeRecords.

public void writeRecords(final Object iRecords, final String iFetchPlan, String iFormat, final String accept, final Map<String, Object> iAdditionalProperties, final String mode) throws IOException {
    if (iRecords == null) {
        send(OHttpUtils.STATUS_OK_NOCONTENT_CODE, "", OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
        return;
    }
    if (!mode.equalsIgnoreCase("graph")) {
        super.writeRecords(iRecords, iFetchPlan, iFormat, accept, iAdditionalProperties, mode);
        return;
    }
    if (accept != null && accept.contains("text/csv"))
        throw new IllegalArgumentException("Graph mode cannot accept '" + accept + "'");
    final OrientGraphNoTx graph = (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) ODatabaseRecordThreadLocal.INSTANCE.get());
    try {
        // all the edges in the result-set
        final Set<OrientEdge> rsEdges = new HashSet<OrientEdge>();
        // all the vertices sent
        final Set<OrientVertex> vertices = new HashSet<OrientVertex>();
        final Iterator<Object> iIterator = OMultiValue.getMultiValueIterator(iRecords);
        while (iIterator.hasNext()) {
            Object entry = iIterator.next();
            if (entry == null || !(entry instanceof OIdentifiable))
                // IGNORE IT
                continue;
            entry = ((OIdentifiable) entry).getRecord();
            if (entry == null || !(entry instanceof OIdentifiable))
                // IGNORE IT
                continue;
            if (entry instanceof ODocument) {
                OClass schemaClass = ((ODocument) entry).getSchemaClass();
                if (schemaClass != null && schemaClass.isVertexType())
                    vertices.add(graph.getVertex(entry));
                else if (schemaClass != null && schemaClass.isEdgeType()) {
                    OrientEdge edge = graph.getEdge(entry);
                    rsEdges.add(edge);
                    vertices.add(graph.getVertex(edge.getVertex(Direction.IN)));
                    vertices.add(graph.getVertex(edge.getVertex(Direction.OUT)));
                } else
                    // IGNORE IT
                    continue;
            }
        }
        final StringWriter buffer = new StringWriter();
        final OJSONWriter json = new OJSONWriter(buffer, "");
        json.beginObject();
        json.beginObject("graph");
        // WRITE VERTICES
        json.beginCollection("vertices");
        for (OrientVertex vertex : vertices) {
            json.beginObject();
            json.writeAttribute("@rid", vertex.getIdentity());
            json.writeAttribute("@class", vertex.getRecord().getClassName());
            // ADD ALL THE PROPERTIES
            for (String field : vertex.getPropertyKeys()) {
                final Object v = vertex.getProperty(field);
                if (v != null)
                    json.writeAttribute(field, v);
            }
            json.endObject();
        }
        json.endCollection();
        // WRITE EDGES
        json.beginCollection("edges");
        if (rsEdges.isEmpty()) {
            // no explicit edges in the result-set, let's calculate them from vertices
            Set<ORID> edgeRids = new HashSet<ORID>();
            for (OrientVertex vertex : vertices) {
                for (Edge e : vertex.getEdges(Direction.BOTH)) {
                    OrientEdge edge = (OrientEdge) e;
                    if (edgeRids.contains(((OrientEdge) e).getIdentity())) {
                        continue;
                    }
                    if (!vertices.contains(edge.getVertex(Direction.OUT)) || !vertices.contains(edge.getVertex(Direction.IN)))
                        // ONE OF THE 2 VERTICES ARE NOT PART OF THE RESULT SET: DISCARD IT
                        continue;
                    edgeRids.add(edge.getIdentity());
                    writeEdge(edge, json);
                }
            }
        } else {
            // edges are explicitly in the RS, only send them
            for (OrientEdge edge : rsEdges) {
                if (!vertices.contains(edge.getVertex(Direction.OUT)) || !vertices.contains(edge.getVertex(Direction.IN)))
                    // ONE OF THE 2 VERTICES ARE NOT PART OF THE RESULT SET: DISCARD IT
                    continue;
                writeEdge(edge, json);
            }
        }
        json.endCollection();
        if (iAdditionalProperties != null) {
            for (Map.Entry<String, Object> entry : iAdditionalProperties.entrySet()) {
                final Object v = entry.getValue();
                if (OMultiValue.isMultiValue(v)) {
                    json.beginCollection(-1, true, entry.getKey());
                    formatMultiValue(OMultiValue.getMultiValueIterator(v), buffer, null);
                    json.endCollection(-1, true);
                } else
                    json.writeAttribute(entry.getKey(), v);
                if (Thread.currentThread().isInterrupted())
                    break;
            }
        }
        json.endObject();
        json.endObject();
        send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
    } finally {
        graph.shutdown();
    }
}
Also used : OJSONWriter(com.orientechnologies.orient.core.serialization.serializer.OJSONWriter) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) StringWriter(java.io.StringWriter) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) ORID(com.orientechnologies.orient.core.id.ORID) Edge(com.tinkerpop.blueprints.Edge) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Map(java.util.Map) HashSet(java.util.HashSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 45 with Edge

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

the class DatabaseConflictStrategyAutoMergeTest method dbClient1.

private void dbClient1() {
    sleep(500);
    synchronized (LOCK) {
        OrientBaseGraph graph = new OrientGraph(CLIENT_ORIENT_URL_MAIN);
        try {
            // Create 2 parent vertices.
            OrientVertex parentV1 = graph.addVertex("vertextype1", (String) null);
            graph.commit();
            assertEquals(1, parentV1.getRecord().getVersion());
            parentV1Id = parentV1.getId();
            OrientVertex parentV2 = graph.addVertex("vertextype2", (String) null);
            graph.commit();
            assertEquals(1, parentV2.getRecord().getVersion());
            parentV2Id = parentV2.getId();
            // Create vertices.
            for (int i = 0; i < NUM_OF_LOOP_ITERATIONS; i++) {
                pause();
                if (exceptionInThread != null)
                    break;
                OrientVertex vertex = graph.addVertex("vertextype3", (String) null);
                graph.commit();
                assertEquals(1, vertex.getRecord().getVersion());
                vertex.setProperty("num", i);
                graph.commit();
                assertEquals(2, vertex.getRecord().getVersion());
                parentV1.addEdge("edgetype1", vertex);
                graph.commit();
                assertNotNull(parentV1.getProperty("cnt"), "record " + parentV1.getIdentity() + " has no 'cnt' property");
                boolean edge1Exists = false;
                for (Edge e : parentV1.getEdges(Direction.OUT, "edgetype1")) {
                    if (e.getVertex(Direction.IN).equals(vertex)) {
                        edge1Exists = true;
                        break;
                    }
                }
                assertTrue(edge1Exists);
                boolean edge2Exists = false;
                for (Edge e : vertex.getEdges(Direction.IN, "edgetype1")) {
                    if (e.getVertex(Direction.OUT).equals(parentV1)) {
                        edge2Exists = true;
                        break;
                    }
                }
                assertTrue(edge2Exists);
                assertNotNull(vertex.getProperty("num"));
                parentV2.addEdge("edgetype2", vertex);
                graph.commit();
                assertNotNull(parentV2.getProperty("cnt"));
                edge1Exists = false;
                for (Edge e : parentV2.getEdges(Direction.OUT, "edgetype2")) {
                    if (e.getVertex(Direction.IN).equals(vertex)) {
                        edge1Exists = true;
                        break;
                    }
                }
                assertTrue(edge1Exists);
                edge2Exists = false;
                for (Edge e : vertex.getEdges(Direction.IN, "edgetype2")) {
                    if (e.getVertex(Direction.OUT).equals(parentV2)) {
                        edge2Exists = true;
                        break;
                    }
                }
                assertTrue(edge2Exists);
                assertNotNull(vertex.getProperty("num"));
            }
        } catch (Throwable e) {
            if (exceptionInThread == null) {
                exceptionInThread = e;
            }
        } finally {
            graph.shutdown();
            LOCK.notifyAll();
        }
    }
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) 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