Search in sources :

Example 1 with Resource

use of org.openrdf.model.Resource in project blueprints by tinkerpop.

the class SailTest method testRemoveStatements.

@Test
public void testRemoveStatements() throws Exception {
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        Resource[] contexts = { uriA, null };
        int count;
        // Remove from all contexts.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        // Remove from one partition context.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        Resource[] oneContext = { uriA };
        sc.removeStatements(uriA, null, null, oneContext);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(1, count);
        // Remove from the null context.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        Resource[] nullContext = { null };
        sc.removeStatements(uriA, null, null, nullContext);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(1, count);
        // Remove from more than one context.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false, contexts);
        assertEquals(0, count);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 2 with Resource

use of org.openrdf.model.Resource in project blueprints by tinkerpop.

the class SailVertex method setProperty.

public void setProperty(final String key, final Object value) {
    if (this.rawVertex instanceof Resource) {
        throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
    } else {
        boolean update = false;
        final Literal oldLiteral = (Literal) this.rawVertex;
        if (key.equals(SailTokens.DATATYPE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), new URIImpl(this.graph.expandPrefix(value.toString())));
            update = true;
        } else if (key.equals(SailTokens.LANGUAGE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), value.toString());
            update = true;
        }
        if (update) {
            this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
        }
    }
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Literal(org.openrdf.model.Literal) Resource(org.openrdf.model.Resource) URIImpl(org.openrdf.model.impl.URIImpl)

Example 3 with Resource

use of org.openrdf.model.Resource in project blueprints by tinkerpop.

the class SailVertex method removeProperty.

public <T> T removeProperty(final String key) {
    if (this.rawVertex instanceof Resource) {
        throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
    } else {
        final Literal oldLiteral = (Literal) this.rawVertex;
        if (key.equals(SailTokens.DATATYPE) || key.equals(SailTokens.LANGUAGE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel());
            this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
        }
        if (key.equals(SailTokens.DATATYPE)) {
            return (T) oldLiteral.getDatatype().toString();
        } else if (key.equals(SailTokens.LANGUAGE)) {
            return (T) oldLiteral.getLanguage();
        }
    }
    return null;
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Literal(org.openrdf.model.Literal) Resource(org.openrdf.model.Resource)

Example 4 with Resource

use of org.openrdf.model.Resource 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 5 with Resource

use of org.openrdf.model.Resource 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)

Aggregations

Resource (org.openrdf.model.Resource)16 URI (org.openrdf.model.URI)7 Statement (org.openrdf.model.Statement)6 SailConnection (org.openrdf.sail.SailConnection)5 SailException (org.openrdf.sail.SailException)5 Test (org.junit.Test)4 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)4 Value (org.openrdf.model.Value)3 Edge (com.tinkerpop.blueprints.Edge)2 LinkedList (java.util.LinkedList)2 Literal (org.openrdf.model.Literal)2 LiteralImpl (org.openrdf.model.impl.LiteralImpl)2 StatementImpl (org.openrdf.model.impl.StatementImpl)2 URIImpl (org.openrdf.model.impl.URIImpl)2 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2 RepositoryConnection (org.openrdf.repository.RepositoryConnection)2 Vertex (com.tinkerpop.blueprints.Vertex)1 CloseableIteration (info.aduna.iteration.CloseableIteration)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1