Search in sources :

Example 61 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class SPARQLConnection method createDeletePatternCommand.

private String createDeletePatternCommand(Resource subject, IRI predicate, Value object, Resource[] contexts) {
    StringBuilder qb = new StringBuilder();
    qb.append("DELETE WHERE \n");
    qb.append("{ \n");
    if (contexts.length > 0) {
        for (Resource context : contexts) {
            if (context != null) {
                String namedGraph = context.stringValue();
                if (context instanceof BNode) {
                    // SPARQL does not allow blank nodes as named graph
                    // identifiers, so we need to skolemize
                    // the blank node id.
                    namedGraph = "urn:nodeid:" + context.stringValue();
                }
                qb.append("    GRAPH <" + namedGraph + "> { \n");
            }
            createBGP(qb, subject, predicate, object);
            if (context != null && context instanceof IRI) {
                qb.append(" } \n");
            }
        }
    } else {
        createBGP(qb, subject, predicate, object);
    }
    qb.append("}");
    return qb.toString();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Resource(org.eclipse.rdf4j.model.Resource)

Example 62 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class SPARQLConnection method toStatementIteration.

/**
 * Converts a {@link TupleQueryResult} resulting from the {@link #EVERYTHING_WITH_GRAPH} to a statement by
 * using the respective values from the {@link BindingSet} or (if provided) the ones from the arguments.
 *
 * @param iter
 *        the {@link TupleQueryResult}
 * @param subj
 *        the subject {@link Resource} used as input or <code>null</code> if wildcard was used
 * @param pred
 *        the predicate {@link IRI} used as input or <code>null</code> if wildcard was used
 * @param obj
 *        the object {@link Value} used as input or <code>null</code> if wildcard was used
 * @return the converted iteration
 */
protected Iteration<Statement, QueryEvaluationException> toStatementIteration(TupleQueryResult iter, final Resource subj, final IRI pred, final Value obj) {
    return new ConvertingIteration<BindingSet, Statement, QueryEvaluationException>(iter) {

        @Override
        protected Statement convert(BindingSet b) throws QueryEvaluationException {
            Resource s = subj == null ? (Resource) b.getValue("s") : subj;
            IRI p = pred == null ? (IRI) b.getValue("p") : pred;
            Value o = obj == null ? b.getValue("o") : obj;
            Resource ctx = (Resource) b.getValue("ctx");
            return SimpleValueFactory.getInstance().createStatement(s, p, o, ctx);
        }
    };
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) ConvertingIteration(org.eclipse.rdf4j.common.iteration.ConvertingIteration) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value)

Example 63 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class SPARQLConnection method getContextIDs.

public RepositoryResult<Resource> getContextIDs() throws RepositoryException {
    TupleQueryResult iter = null;
    RepositoryResult<Resource> result = null;
    boolean allGood = false;
    try {
        TupleQuery query = prepareTupleQuery(SPARQL, NAMEDGRAPHS, "");
        iter = query.evaluate();
        result = new RepositoryResult<Resource>(new ExceptionConvertingIteration<Resource, RepositoryException>(new ConvertingIteration<BindingSet, Resource, QueryEvaluationException>(iter) {

            @Override
            protected Resource convert(BindingSet bindings) throws QueryEvaluationException {
                return (Resource) bindings.getValue("_");
            }
        }) {

            @Override
            protected RepositoryException convert(Exception e) {
                return new RepositoryException(e);
            }
        });
        allGood = true;
        return result;
    } catch (MalformedQueryException e) {
        throw new RepositoryException(e);
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    } finally {
        if (!allGood) {
            try {
                if (result != null) {
                    result.close();
                }
            } finally {
                if (iter != null) {
                    iter.close();
                }
            }
        }
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) Resource(org.eclipse.rdf4j.model.Resource) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) SPARQLTupleQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) UnknownTransactionStateException(org.eclipse.rdf4j.repository.UnknownTransactionStateException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 64 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class NotifyingTest method testRemove.

@Test
public void testRemove() throws Exception {
    ValueFactory vf = SimpleValueFactory.getInstance();
    final IRI uri = vf.createIRI("http://example.com/");
    final RepositoryConnection stub = new RepositoryConnectionStub() {

        protected void removeWithoutCommit(Resource subject, IRI predicate, Value object, Resource... contexts) throws RepositoryException {
        }
    };
    Repository repo = stub.getRepository();
    NotifyingRepositoryConnection con = new NotifyingRepositoryConnectionWrapper(repo, stub);
    con.addRepositoryConnectionListener(new RepositoryConnectionListenerAdapter() {

        public void remove(RepositoryConnection conn, Resource subject, IRI predicate, Value object, Resource... contexts) {
            assertEquals(stub, conn);
            assertEquals(uri, subject);
            assertEquals(uri, predicate);
            assertEquals(uri, object);
            assertEquals(0, contexts.length);
        }
    });
    con.remove(con.getValueFactory().createStatement(uri, uri, uri));
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Repository(org.eclipse.rdf4j.repository.Repository) NotifyingRepositoryConnectionWrapper(org.eclipse.rdf4j.repository.event.base.NotifyingRepositoryConnectionWrapper) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) RepositoryConnectionListenerAdapter(org.eclipse.rdf4j.repository.event.base.RepositoryConnectionListenerAdapter) Test(org.junit.Test)

Example 65 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class InterceptorTest method testRemove.

@Test
public void testRemove() throws Exception {
    ValueFactory vf = SimpleValueFactory.getInstance();
    final IRI uri = vf.createIRI("http://example.com/");
    final RepositoryConnection stub = new RepositoryConnectionStub() {

        protected void removeWithoutCommit(Resource subject, IRI predicate, Value object, Resource... contexts) throws RepositoryException {
            fail();
        }
    };
    Repository repo = stub.getRepository();
    InterceptingRepositoryConnection con = new InterceptingRepositoryConnectionWrapper(repo, stub);
    con.addRepositoryConnectionInterceptor(new RepositoryConnectionInterceptorAdapter() {

        public boolean remove(RepositoryConnection conn, Resource subject, IRI predicate, Value object, Resource... contexts) {
            assertEquals(stub, conn);
            assertEquals(uri, subject);
            assertEquals(uri, predicate);
            assertEquals(uri, object);
            assertEquals(0, contexts.length);
            return true;
        }
    });
    con.remove(con.getValueFactory().createStatement(uri, uri, uri));
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Repository(org.eclipse.rdf4j.repository.Repository) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) RepositoryConnectionInterceptorAdapter(org.eclipse.rdf4j.repository.event.base.RepositoryConnectionInterceptorAdapter) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) InterceptingRepositoryConnectionWrapper(org.eclipse.rdf4j.repository.event.base.InterceptingRepositoryConnectionWrapper) Test(org.junit.Test)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4