Search in sources :

Example 76 with Edge

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

the class PropertyGraphSailConnection method getStatements_SPx.

private CloseableIteration<Statement, SailException> getStatements_SPx(final Resource subject, final URI predicate) throws SailException {
    if (subject instanceof URI) {
        if (predicate.equals(RDF.TYPE)) {
            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> s = new Source<Edge>(new SingleItemIterator<Edge>(e), edgeTypes);
                    return new StatementIteration(s);
                }
            } else {
                Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexTypes);
                return new StatementIteration(s);
            }
        } else if (predicate.equals(PropertyGraphSail.ID)) {
            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> s = new Source<Edge>(new SingleItemIterator<Edge>(e), edgeIds);
                    return new StatementIteration(s);
                }
            } else {
                Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexIds);
                return new StatementIteration(s);
            }
        } else if (predicate.equals(PropertyGraphSail.LABEL)) {
            if (!firstClassEdges) {
                return new StatementIteration();
            }
            Edge e = edgeForURI((URI) subject);
            if (null == e) {
                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();
            }
            Edge e = edgeForURI((URI) subject);
            if (null == e) {
                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();
            }
            Edge e = edgeForURI((URI) subject);
            if (null == e) {
                return new StatementIteration();
            } else {
                Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), tails);
                return new StatementIteration(s);
            }
        } else if (isPropertyPredicate(predicate)) {
            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> s = new Source<Edge>(new SingleItemIterator<Edge>(e), edgePropertiesWithKey(key, predicate));
                    return new StatementIteration(s);
                }
            } else {
                Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexPropertiesWithKey(key, predicate));
                return new StatementIteration(s);
            }
        } else if (isRelationPredicate(predicate)) {
            Vertex v = vertexForURI((URI) subject);
            if (null != v) {
                String label = labelForRelationPredicate(predicate);
                Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), new RelationGenerator(label, false));
                return new StatementIteration(s);
            } else {
                return new StatementIteration();
            }
        } else {
            return new StatementIteration();
        }
    } else {
        return new StatementIteration();
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) URI(org.openrdf.model.URI) Edge(com.tinkerpop.blueprints.Edge) SailConnectionTripleSource(net.fortytwo.sesametools.SailConnectionTripleSource) TripleSource(org.openrdf.query.algebra.evaluation.TripleSource)

Example 77 with Edge

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

the class GraphSailConnection method removeStatementsInternal.

private void removeStatementsInternal(final boolean inferred, final Resource subject, final URI predicate, final Value object, final Resource... contexts) throws SailException {
    if (!canWrite()) {
        WriteAction a = new WriteAction(ActionType.REMOVE);
        a.inferred = inferred;
        a.subject = subject;
        a.predicate = predicate;
        a.object = object;
        a.contexts = contexts;
        queueUpdate(a);
        return;
    }
    Collection<Edge> edgesToRemove = new LinkedList<Edge>();
    int index = 0;
    if (null != subject) {
        index |= 0x1;
    }
    if (null != predicate) {
        index |= 0x2;
    }
    if (null != object) {
        index |= 0x4;
    }
    if (0 == contexts.length) {
        Iterable<Edge> i = store.matchers[index].match(subject, predicate, object, null, inferred);
        for (Edge anI : i) {
            edgesToRemove.add(anI);
        }
    } else {
        // TODO: as an optimization, filter on multiple contexts simultaneously (when context is not used in the matcher), rather than trying each context consecutively.
        for (Resource context : contexts) {
            index |= 0x8;
            Iterable<Edge> i = store.matchers[index].match(subject, predicate, object, context, inferred);
            for (Edge e : i) {
                Boolean b = e.getProperty(GraphSail.INFERRED);
                if ((!inferred && null == b) || (inferred && null != b && b)) {
                    edgesToRemove.add(e);
                }
            }
        }
    }
    for (Edge e : edgesToRemove) {
        SimpleStatement s;
        if (hasConnectionListeners()) {
            s = new SimpleStatement();
            fillStatement(s, e);
        } else {
            s = null;
        }
        removeEdge(e);
        if (null != s) {
            notifyStatementRemoved(s);
        }
    }
    if (0 < edgesToRemove.size()) {
        statementsRemoved = true;
        prevSubject = null;
        prevOutVertex = null;
    }
}
Also used : Resource(org.openrdf.model.Resource) Edge(com.tinkerpop.blueprints.Edge) LinkedList(java.util.LinkedList)

Example 78 with Edge

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

the class GraphSailConnection method deleteEdgesInIterator.

private void deleteEdgesInIterator(final boolean inferred, final Iterable<Edge> i) {
    Iterator<Edge> iter = i.iterator();
    while (iter.hasNext()) {
        Edge e = iter.next();
        Boolean b = e.getProperty(GraphSail.INFERRED);
        if ((!inferred && null == b) || (inferred && null != b && b)) {
            SimpleStatement s;
            if (hasConnectionListeners()) {
                s = new SimpleStatement();
                fillStatement(s, e);
            } else {
                s = null;
            }
            try {
                iter.remove();
            } catch (UnsupportedOperationException x) {
            // TODO: it so happens that Neo4jGraph, the only IndexableGraph implementation so far tested whose
            // iterators don't support remove(), does *not* throw ConcurrentModificationExceptions when you
            // delete an edge in an iterator currently being traversed.  So for now, just ignore the
            // UnsupportedOperationException and proceed to delete the edge from the graph.
            }
            removeEdge(e);
            if (null != s) {
                notifyStatementRemoved(s);
            }
            statementsRemoved = true;
            prevSubject = null;
            prevOutVertex = null;
        }
    }
}
Also used : Edge(com.tinkerpop.blueprints.Edge)

Example 79 with Edge

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

the class GraphSailConnection method addStatementInternal.

private void addStatementInternal(final boolean inferred, final Resource subject, final URI predicate, final Value object, final Resource... contexts) throws SailException {
    if (!canWrite()) {
        WriteAction a = new WriteAction(ActionType.ADD);
        a.inferred = inferred;
        a.subject = subject;
        a.predicate = predicate;
        a.object = object;
        a.contexts = contexts;
        queueUpdate(a);
        return;
    }
    if (null == subject || null == predicate || null == object) {
        throw new IllegalArgumentException("null part-of-speech for to-be-added statement");
    }
    for (Resource context : ((0 == contexts.length) ? NULL_CONTEXT_ARRAY : contexts)) {
        String contextStr = null == context ? GraphSail.NULL_CONTEXT_NATIVE : store.resourceToNative(context);
        // Track the subject since data will often list relations for the same subject consecutively.
        Vertex out = subject.equals(prevSubject) ? prevOutVertex : (prevOutVertex = getOrCreateVertex(prevSubject = subject));
        // object-level identity of subject and object facilitates creation of self-loop edges in some Graph implementations
        Vertex in = subject.equals(object) ? out : getOrCreateVertex(object);
        // if enforcing uniqueness of statements, check for an edge identical to the one we are about to add
        if (store.uniqueStatements) {
            Iterator<Edge> prevEdges = out.query().direction(Direction.OUT).has(StringFactory.LABEL, predicate.stringValue()).has(GraphSail.CONTEXT_PROP, contextStr).edges().iterator();
            boolean found = false;
            Object objectId = in.getId();
            while (prevEdges.hasNext()) {
                if (prevEdges.next().getVertex(Direction.IN).getId().equals(objectId)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                continue;
            }
        }
        Edge edge = store.graph.addEdge(null, out, in, predicate.stringValue());
        if (inferred) {
            edge.setProperty(GraphSail.INFERRED, true);
        }
        for (IndexingMatcher m : (Collection<IndexingMatcher>) store.indexers) {
            m.indexStatement(edge, subject, predicate, object, contextStr);
        }
        // Hack to encode graph context even if the "c" index is disabled
        if (null == edge.getProperty(GraphSail.CONTEXT_PROP)) {
            edge.setProperty(GraphSail.CONTEXT_PROP, contextStr);
        }
        if (hasConnectionListeners()) {
            Statement s = store.valueFactory.createStatement(subject, predicate, object, context);
            notifyStatementAdded(s);
        }
        statementsAdded = true;
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) Collection(java.util.Collection) Edge(com.tinkerpop.blueprints.Edge)

Example 80 with Edge

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

the class GraphJungTest method testTinkerGraph.

public void testTinkerGraph() {
    GraphJung<TinkerGraph> jung = new GraphJung<TinkerGraph>(TinkerGraphFactory.createTinkerGraph());
    assertEquals(jung.getVertices().size(), 6);
    assertEquals(jung.getEdges().size(), 6);
    assertEquals(jung.getVertexCount(), 6);
    assertEquals(jung.getEdgeCount(), 6);
    Vertex marko = null;
    Vertex josh = null;
    Vertex vadas = null;
    for (Vertex vertex : jung.getVertices()) {
        assertTrue(jung.containsVertex(vertex));
        for (Edge edge : jung.getOutEdges(vertex)) {
            assertEquals(jung.getSource(edge), vertex);
        }
        for (Edge edge : jung.getInEdges(vertex)) {
            assertEquals(jung.getDest(edge), vertex);
        }
        if (vertex.getId().equals("1")) {
            marko = vertex;
            assertEquals(jung.getOutEdges(vertex).size(), 3);
            assertEquals(jung.getInEdges(vertex).size(), 0);
            assertEquals(jung.getNeighborCount(vertex), 3);
            int count = 0;
            for (Vertex vertex2 : jung.getNeighbors(vertex)) {
                if (vertex2.getId().equals("2"))
                    count++;
                else if (vertex2.getId().equals("4"))
                    count++;
                else if (vertex2.getId().equals("3"))
                    count++;
                else
                    assertTrue(false);
            }
            assertEquals(count, 3);
            assertEquals(jung.getSuccessorCount(vertex), 3);
            count = 0;
            for (Vertex vertex2 : jung.getSuccessors(vertex)) {
                if (vertex2.getId().equals("2"))
                    count++;
                else if (vertex2.getId().equals("4"))
                    count++;
                else if (vertex2.getId().equals("3"))
                    count++;
                else
                    assertTrue(false);
            }
            assertEquals(jung.getPredecessorCount(vertex), 0);
        } else if (vertex.getId().equals("2")) {
            vadas = vertex;
            assertEquals(jung.getOutEdges(vertex).size(), 0);
            assertEquals(jung.getInEdges(vertex).size(), 1);
            assertEquals(jung.getNeighborCount(vertex), 1);
            int count = 0;
            for (Vertex vertex2 : jung.getNeighbors(vertex)) {
                if (vertex2.getId().equals("1"))
                    count++;
                else
                    assertTrue(false);
            }
            assertEquals(count, 1);
            assertEquals(jung.getSuccessorCount(vertex), 0);
            assertEquals(jung.getPredecessorCount(vertex), 1);
            count = 0;
            for (Vertex vertex2 : jung.getPredecessors(vertex)) {
                if (vertex2.getId().equals("1"))
                    count++;
                else
                    assertTrue(false);
            }
            assertEquals(count, 1);
        } else if (vertex.getId().equals("4")) {
            josh = vertex;
            assertEquals(jung.getOutEdges(vertex).size(), 2);
            assertEquals(jung.getInEdges(vertex).size(), 1);
            assertEquals(jung.getNeighborCount(vertex), 3);
            int count = 0;
            for (Vertex vertex2 : jung.getNeighbors(vertex)) {
                if (vertex2.getId().equals("1"))
                    count++;
                else if (vertex2.getId().equals("3"))
                    count++;
                else if (vertex2.getId().equals("5"))
                    count++;
                else
                    assertTrue(false);
            }
            assertEquals(count, 3);
            assertEquals(jung.getSuccessorCount(vertex), 2);
            count = 0;
            for (Vertex vertex2 : jung.getSuccessors(vertex)) {
                if (vertex2.getId().equals("3"))
                    count++;
                else if (vertex2.getId().equals("5"))
                    count++;
                else
                    assertTrue(false);
            }
            assertEquals(count, 2);
            assertEquals(jung.getPredecessorCount(vertex), 1);
            count = 0;
            for (Vertex vertex2 : jung.getPredecessors(vertex)) {
                if (vertex2.getId().equals("1"))
                    count++;
                else
                    assertTrue(false);
            }
            assertEquals(count, 1);
        }
    }
    assertTrue(null != marko);
    assertTrue(null != vadas);
    assertTrue(null != josh);
    assertEquals(jung.findEdgeSet(marko, josh).size(), 1);
    assertTrue(jung.findEdgeSet(marko, josh).contains(jung.findEdge(marko, josh)));
    assertEquals(jung.getDefaultEdgeType(), EdgeType.DIRECTED);
    for (Edge edge : jung.getEdges()) {
        assertTrue(jung.containsEdge(edge));
        assertEquals(jung.getEdgeType(edge), EdgeType.DIRECTED);
        assertEquals(jung.getIncidentCount(edge), 2);
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) GraphJung(com.tinkerpop.blueprints.oupls.jung.GraphJung) 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