Search in sources :

Example 11 with TupleQueryResult

use of org.eclipse.rdf4j.query.TupleQueryResult in project rdf4j by eclipse.

the class HTTPRepositoryConnection method getContextIDs.

public RepositoryResult<Resource> getContextIDs() throws RepositoryException {
    try {
        List<Resource> contextList = new ArrayList<Resource>();
        TupleQueryResult contextIDs = client.getContextIDs();
        try {
            while (contextIDs.hasNext()) {
                BindingSet bindingSet = contextIDs.next();
                Value context = bindingSet.getValue("contextID");
                if (context instanceof Resource) {
                    contextList.add((Resource) context);
                }
            }
        } finally {
            contextIDs.close();
        }
        return createRepositoryResult(contextList);
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    } catch (IOException e) {
        throw new RepositoryException(e);
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Resource(org.eclipse.rdf4j.model.Resource) ArrayList(java.util.ArrayList) Value(org.eclipse.rdf4j.model.Value) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 12 with TupleQueryResult

use of org.eclipse.rdf4j.query.TupleQueryResult in project graal by graphik-team.

the class RDF4jStore method termsByPredicatePosition.

@Override
public CloseableIterator<Term> termsByPredicatePosition(Predicate p, int position) throws AtomSetException {
    TupleQuery query = null;
    TupleQueryResult results = null;
    try {
        if (position == 0) {
            query = this.connection.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT DISTINCT ?x WHERE { ?x <" + utils.createURI(p) + "> ?y }");
        } else if (position == 1) {
            query = this.connection.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT DISTINCT ?x WHERE { ?y <" + utils.createURI(p) + "> ?x }");
        } else {
            throw new WrongArityException("Position should be 0 for subject or 1 for object.");
        }
        results = query.evaluate();
    } catch (RepositoryException e) {
        throw new AtomSetException(e);
    } catch (MalformedQueryException e) {
        throw new AtomSetException(e);
    } catch (QueryEvaluationException e) {
        throw new AtomSetException(e);
    }
    return new TermsIterator(results, "x", this.utils);
}
Also used : QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) WrongArityException(fr.lirmm.graphik.graal.api.store.WrongArityException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 13 with TupleQueryResult

use of org.eclipse.rdf4j.query.TupleQueryResult 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 14 with TupleQueryResult

use of org.eclipse.rdf4j.query.TupleQueryResult in project rdf4j by eclipse.

the class SPARQLConnection method getStatementsQuadMode.

private RepositoryResult<Statement> getStatementsQuadMode(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws MalformedQueryException, RepositoryException, QueryEvaluationException {
    TupleQueryResult qRes = null;
    RepositoryResult<Statement> result = null;
    boolean allGood = false;
    try {
        TupleQuery tupleQuery = prepareTupleQuery(SPARQL, EVERYTHING_WITH_GRAPH);
        setBindings(tupleQuery, subj, pred, obj, contexts);
        tupleQuery.setIncludeInferred(includeInferred);
        qRes = tupleQuery.evaluate();
        result = new RepositoryResult<Statement>(new ExceptionConvertingIteration<Statement, RepositoryException>(toStatementIteration(qRes, subj, pred, obj)) {

            @Override
            protected RepositoryException convert(Exception e) {
                return new RepositoryException(e);
            }
        });
        allGood = true;
        return result;
    } finally {
        if (!allGood) {
            try {
                if (result != null) {
                    result.close();
                }
            } finally {
                if (qRes != null) {
                    qRes.close();
                }
            }
        }
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) SPARQLTupleQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) 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)

Example 15 with TupleQueryResult

use of org.eclipse.rdf4j.query.TupleQueryResult in project rdf4j by eclipse.

the class SPARQLTSVCustomTest method testSES2126QuotedLiteralIntegerAsStringImplicitType.

/**
 * Only Literals with the XML Schema numeric types should be simplified.
 *
 * @throws Exception
 */
@Test
public void testSES2126QuotedLiteralIntegerAsStringImplicitType() throws Exception {
    List<String> bindingNames = Arrays.asList("test");
    TupleQueryResult tqr = new IteratingTupleQueryResult(bindingNames, Arrays.asList(new ListBindingSet(bindingNames, SimpleValueFactory.getInstance().createLiteral("1"))));
    String result = writeTupleResult(tqr);
    assertEquals("?test\n\"1\"\n", result);
}
Also used : ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) IteratingTupleQueryResult(org.eclipse.rdf4j.query.impl.IteratingTupleQueryResult) IteratingTupleQueryResult(org.eclipse.rdf4j.query.impl.IteratingTupleQueryResult) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) Test(org.junit.Test)

Aggregations

TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)15 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)10 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)9 IOException (java.io.IOException)7 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)7 BindingSet (org.eclipse.rdf4j.query.BindingSet)6 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)5 Value (org.eclipse.rdf4j.model.Value)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)2 ExceptionConvertingIteration (org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration)2 RDF4JProtocolSession (org.eclipse.rdf4j.http.client.RDF4JProtocolSession)2 Resource (org.eclipse.rdf4j.model.Resource)2 UnsupportedQueryLanguageException (org.eclipse.rdf4j.query.UnsupportedQueryLanguageException)2 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)2 IteratingTupleQueryResult (org.eclipse.rdf4j.query.impl.IteratingTupleQueryResult)2 ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)2 UnknownTransactionStateException (org.eclipse.rdf4j.repository.UnknownTransactionStateException)2 SPARQLTupleQuery (org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery)2