Search in sources :

Example 71 with Edge

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

the class ElementHelperTest method testTypecastProperty.

public void testTypecastProperty() {
    Graph graph = TinkerGraphFactory.createTinkerGraph();
    for (Edge e : graph.getEdges()) {
        assertTrue(e.getProperty("weight") instanceof Float);
    }
    ElementHelper.typecastProperty("weight", Double.class, (Iterable) graph.getEdges());
    for (Edge e : graph.getEdges()) {
        assertTrue(e.getProperty("weight") instanceof Double);
    }
}
Also used : TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) Edge(com.tinkerpop.blueprints.Edge)

Example 72 with Edge

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

the class GraphSONReaderTestSuite method testTinkerGraphEdges.

public void testTinkerGraphEdges() throws Exception {
    Graph graph = this.graphTest.generateGraph();
    if (graph.getFeatures().supportsEdgeIteration) {
        this.stopWatch();
        new GraphSONReader(graph).inputGraph(GraphSONReader.class.getResourceAsStream("graph-example-1.json"));
        printPerformance(graph.toString(), null, "graph-example-1 loaded", this.stopWatch());
        Set<String> edgeIds = new HashSet<String>();
        Set<String> edgeKeys = new HashSet<String>();
        Set<String> edgeValues = new HashSet<String>();
        int count = 0;
        for (Edge e : graph.getEdges()) {
            count++;
            edgeIds.add(e.getId().toString());
            for (String key : e.getPropertyKeys()) {
                edgeKeys.add(key);
                edgeValues.add(e.getProperty(key).toString());
            }
        }
        assertEquals(count, 6);
        assertEquals(edgeIds.size(), 6);
        assertEquals(edgeKeys.size(), 1);
        assertEquals(edgeValues.size(), 4);
    }
    graph.shutdown();
}
Also used : Graph(com.tinkerpop.blueprints.Graph) Edge(com.tinkerpop.blueprints.Edge) HashSet(java.util.HashSet)

Example 73 with Edge

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

the class GraphSONReaderTestSuite method testTinkerGraphVertexAndEdges.

public void testTinkerGraphVertexAndEdges() throws Exception {
    Graph graph = this.graphTest.generateGraph();
    if (graph.getFeatures().supportsVertexIteration) {
        this.stopWatch();
        new GraphSONReader(graph).inputGraph(GraphSONReader.class.getResourceAsStream("graph-example-1.json"));
        printPerformance(graph.toString(), null, "graph-example-1 loaded", this.stopWatch());
        Vertex marko = null;
        Vertex peter = null;
        Vertex josh = null;
        Vertex vadas = null;
        Vertex lop = null;
        Vertex ripple = null;
        int count = 0;
        for (Vertex v : graph.getVertices()) {
            count++;
            String name = v.getProperty("name").toString();
            if (name.equals("marko")) {
                marko = v;
            } else if (name.equals("peter")) {
                peter = v;
            } else if (name.equals("josh")) {
                josh = v;
            } else if (name.equals("vadas")) {
                vadas = v;
            } else if (name.equals("lop")) {
                lop = v;
            } else if (name.equals("ripple")) {
                ripple = v;
            } else {
                assertTrue(false);
            }
        }
        assertEquals(count, 6);
        assertTrue(null != marko);
        assertTrue(null != peter);
        assertTrue(null != josh);
        assertTrue(null != vadas);
        assertTrue(null != lop);
        assertTrue(null != ripple);
        if (graph.getFeatures().supportsEdgeIteration) {
            assertEquals(count(graph.getEdges()), 6);
        }
        // test marko
        Set<Vertex> vertices = new HashSet<Vertex>();
        assertEquals(marko.getProperty("name"), "marko");
        assertEquals(((Number) marko.getProperty("age")).intValue(), 29);
        assertEquals(marko.getPropertyKeys().size(), 2);
        assertEquals(count(marko.getEdges(Direction.OUT)), 3);
        assertEquals(count(marko.getEdges(Direction.IN)), 0);
        for (Edge e : marko.getEdges(Direction.OUT)) {
            vertices.add(e.getVertex(Direction.IN));
        }
        assertEquals(vertices.size(), 3);
        assertTrue(vertices.contains(lop));
        assertTrue(vertices.contains(josh));
        assertTrue(vertices.contains(vadas));
        // test peter
        vertices = new HashSet<Vertex>();
        assertEquals(peter.getProperty("name"), "peter");
        assertEquals(((Number) peter.getProperty("age")).intValue(), 35);
        assertEquals(peter.getPropertyKeys().size(), 2);
        assertEquals(count(peter.getEdges(Direction.OUT)), 1);
        assertEquals(count(peter.getEdges(Direction.IN)), 0);
        for (Edge e : peter.getEdges(Direction.OUT)) {
            vertices.add(e.getVertex(Direction.IN));
        }
        assertEquals(vertices.size(), 1);
        assertTrue(vertices.contains(lop));
        // test josh
        vertices = new HashSet<Vertex>();
        assertEquals(josh.getProperty("name"), "josh");
        assertEquals(((Number) josh.getProperty("age")).intValue(), 32);
        assertEquals(josh.getPropertyKeys().size(), 2);
        assertEquals(count(josh.getEdges(Direction.OUT)), 2);
        assertEquals(count(josh.getEdges(Direction.IN)), 1);
        for (Edge e : josh.getEdges(Direction.OUT)) {
            vertices.add(e.getVertex(Direction.IN));
        }
        assertEquals(vertices.size(), 2);
        assertTrue(vertices.contains(lop));
        assertTrue(vertices.contains(ripple));
        vertices = new HashSet<Vertex>();
        for (Edge e : josh.getEdges(Direction.IN)) {
            vertices.add(e.getVertex(Direction.OUT));
        }
        assertEquals(vertices.size(), 1);
        assertTrue(vertices.contains(marko));
        // test vadas
        vertices = new HashSet<Vertex>();
        assertEquals(vadas.getProperty("name"), "vadas");
        assertEquals(((Number) vadas.getProperty("age")).intValue(), 27);
        assertEquals(vadas.getPropertyKeys().size(), 2);
        assertEquals(count(vadas.getEdges(Direction.OUT)), 0);
        assertEquals(count(vadas.getEdges(Direction.IN)), 1);
        for (Edge e : vadas.getEdges(Direction.IN)) {
            vertices.add(e.getVertex(Direction.OUT));
        }
        assertEquals(vertices.size(), 1);
        assertTrue(vertices.contains(marko));
        // test lop
        vertices = new HashSet<Vertex>();
        assertEquals(lop.getProperty("name"), "lop");
        assertEquals(lop.getProperty("lang"), "java");
        assertEquals(lop.getPropertyKeys().size(), 2);
        assertEquals(count(lop.getEdges(Direction.OUT)), 0);
        assertEquals(count(lop.getEdges(Direction.IN)), 3);
        for (Edge e : lop.getEdges(Direction.IN)) {
            vertices.add(e.getVertex(Direction.OUT));
        }
        assertEquals(vertices.size(), 3);
        assertTrue(vertices.contains(marko));
        assertTrue(vertices.contains(josh));
        assertTrue(vertices.contains(peter));
        // test ripple
        vertices = new HashSet<Vertex>();
        assertEquals(ripple.getProperty("name"), "ripple");
        assertEquals(ripple.getProperty("lang"), "java");
        assertEquals(ripple.getPropertyKeys().size(), 2);
        assertEquals(count(ripple.getEdges(Direction.OUT)), 0);
        assertEquals(count(ripple.getEdges(Direction.IN)), 1);
        for (Edge e : ripple.getEdges(Direction.IN)) {
            vertices.add(e.getVertex(Direction.OUT));
        }
        assertEquals(vertices.size(), 1);
        assertTrue(vertices.contains(josh));
    }
    graph.shutdown();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Graph(com.tinkerpop.blueprints.Graph) Edge(com.tinkerpop.blueprints.Edge) HashSet(java.util.HashSet)

Example 74 with Edge

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

the class PropertyGraphSailConnection method getStatements_SPO.

private CloseableIteration<Statement, SailException> getStatements_SPO(final Resource subject, final URI predicate, final Value object) throws SailException {
    if (predicate.equals(RDF.TYPE)) {
        if (subject instanceof URI) {
            Vertex v = vertexForURI((URI) subject);
            if (null == v) {
                if (!firstClassEdges) {
                    return new StatementIteration();
                }
                Edge e = edgeForURI((URI) subject);
                if (null == e) {
                    return new StatementIteration();
                } else {
                    if (object.equals(PropertyGraphSail.EDGE)) {
                        Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), edgeTypes);
                        return new StatementIteration(s);
                    } else {
                        return new StatementIteration();
                    }
                }
            } else {
                if (object.equals(PropertyGraphSail.VERTEX)) {
                    Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexTypes);
                    return new StatementIteration(s);
                } else {
                    return new StatementIteration();
                }
            }
        } else {
            return new StatementIteration();
        }
    } else if (predicate.equals(PropertyGraphSail.ID)) {
        Object id = literalToObject(object);
        if (null == id || !(subject instanceof URI)) {
            return new StatementIteration();
        } else {
            Vertex v = vertexForURI((URI) subject);
            if (null == v) {
                if (!firstClassEdges) {
                    return new StatementIteration();
                }
                Edge e = edgeForURI((URI) subject);
                if (null == e) {
                    return new StatementIteration();
                } else {
                    if (e.getId().equals(id)) {
                        Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), edgeIds);
                        return new StatementIteration(s);
                    } else {
                        return new StatementIteration();
                    }
                }
            } else {
                if (v.getId().equals(id)) {
                    Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexIds);
                    return new StatementIteration(s);
                } else {
                    return new StatementIteration();
                }
            }
        }
    } else if (predicate.equals(PropertyGraphSail.LABEL)) {
        if (!firstClassEdges) {
            return new StatementIteration();
        }
        Object label = literalToObject(object);
        if (null == label || !(label instanceof String) || !(subject instanceof URI)) {
            return new StatementIteration();
        } else {
            Edge e = edgeForURI((URI) subject);
            if (null == e || !e.getLabel().equals(label)) {
                return new StatementIteration();
            } else {
                Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), labels);
                return new StatementIteration(s);
            }
        }
    } else if (predicate.equals(PropertyGraphSail.HEAD)) {
        if (!firstClassEdges) {
            return new StatementIteration();
        }
        if (!(subject instanceof URI) || !(object instanceof URI)) {
            return new StatementIteration();
        } else {
            Edge e = edgeForURI((URI) subject);
            Vertex v = vertexForURI((URI) object);
            if (null == e || null == v || !e.getVertex(Direction.IN).equals(v)) {
                return new StatementIteration();
            } else {
                Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), heads);
                return new StatementIteration(s);
            }
        }
    } else if (predicate.equals(PropertyGraphSail.TAIL)) {
        if (!firstClassEdges) {
            return new StatementIteration();
        }
        if (!(subject instanceof URI) || !(object instanceof URI)) {
            return new StatementIteration();
        } else {
            Edge e = edgeForURI((URI) subject);
            Vertex v = vertexForURI((URI) object);
            if (null == e || null == v || !e.getVertex(Direction.OUT).equals(v)) {
                return new StatementIteration();
            } else {
                Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), tails);
                return new StatementIteration(s);
            }
        }
    } else if (isPropertyPredicate(predicate)) {
        Object val = literalToObject(object);
        if (null == val || !(subject instanceof URI)) {
            return new StatementIteration();
        } else {
            String key = keyFromPredicate(predicate);
            Vertex v = vertexForURI((URI) subject);
            if (null == v) {
                if (!firstClassEdges) {
                    return new StatementIteration();
                }
                Edge e = edgeForURI((URI) subject);
                if (null == e) {
                    return new StatementIteration();
                } else {
                    Source<Edge> edges = new Source<Edge>(new SingleItemIterator<Edge>(e), edgePropertiesWithKeyAndValue(key, predicate, val, (Literal) object));
                    return new StatementIteration(edges);
                }
            } else {
                Source<Vertex> vertices = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexPropertiesWithKeyAndValue(key, predicate, val, (Literal) object));
                return new StatementIteration(vertices);
            }
        }
    } else if (isRelationPredicate(predicate)) {
        if (!(subject instanceof URI) || !(object instanceof URI)) {
            return new StatementIteration();
        } else {
            String label = labelForRelationPredicate(predicate);
            Vertex vSubj = vertexForURI((URI) subject);
            Vertex vObj = vertexForURI((URI) object);
            if (null != vSubj && null != vObj) {
                Collection<Edge> edges = new LinkedList<Edge>();
                for (Edge ev : vSubj.getEdges(Direction.OUT, label)) {
                    if (ev.getVertex(Direction.IN).equals(vObj)) {
                        edges.add(ev);
                    }
                }
                if (edges.size() > 0) {
                    return new StatementIteration(new Source<Edge>(edges.iterator(), allEdgeStatements));
                } else {
                    return new StatementIteration();
                }
            } else {
                return new StatementIteration();
            }
        }
    } else {
        return new StatementIteration();
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) URI(org.openrdf.model.URI) SailConnectionTripleSource(net.fortytwo.sesametools.SailConnectionTripleSource) TripleSource(org.openrdf.query.algebra.evaluation.TripleSource) LinkedList(java.util.LinkedList) Edge(com.tinkerpop.blueprints.Edge)

Example 75 with Edge

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

the class PropertyGraphSailConnection method edgePropertiesWithValue.

private StatementGenerator<Edge> edgePropertiesWithValue(final Object value, final Literal object) {
    return new StatementGenerator<Edge>() {

        public void generate(Edge source, Collection<Statement> results) {
            for (String key : source.getPropertyKeys()) {
                Object v = source.getProperty(key);
                if (null != v && v.equals(value)) {
                    URI predicate = predicateForPropertyKey(key);
                    Statement s = context.valueFactory.createStatement(uriForEdge(source), predicate, object);
                    results.add(s);
                }
            }
        }
    };
}
Also used : Statement(org.openrdf.model.Statement) Collection(java.util.Collection) Edge(com.tinkerpop.blueprints.Edge) URI(org.openrdf.model.URI)

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