Search in sources :

Example 1 with TupleQuery

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

the class RDF4jStore method match.

@Override
public CloseableIterator<Atom> match(Atom atom) throws AtomSetException {
    ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atom);
    StringWriter s = new StringWriter();
    SparqlConjunctiveQueryWriter w = new SparqlConjunctiveQueryWriter(s, this.utils.getURIzer());
    try {
        w.write(query);
        w.close();
    } catch (IOException e1) {
        throw new AtomSetException("Error while converting to SPARQL " + atom, e1);
    }
    TupleQuery sparqlQuery = this.connection.prepareTupleQuery(s.toString());
    TupleQueryResult result = sparqlQuery.evaluate();
    return new TupleQueryResultAtomIterator(result, atom, utils);
}
Also used : StringWriter(java.io.StringWriter) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) IOException(java.io.IOException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) SparqlConjunctiveQueryWriter(fr.lirmm.graphik.graal.io.sparql.SparqlConjunctiveQueryWriter)

Example 2 with TupleQuery

use of org.eclipse.rdf4j.query.TupleQuery in project nextprot-api by calipho-sib.

the class PhenotypicTTLIntegrationTest method doTupleQuery.

private TupleQueryResult doTupleQuery(String sparqlQuery) throws RepositoryException, QueryEvaluationException, MalformedQueryException {
    RepositoryConnection conn = testRepo.getConnection();
    TupleQuery query = (TupleQuery) conn.prepareQuery(QueryLanguage.SPARQL, sparqlQuery);
    TupleQueryResult result = query.evaluate();
    return result;
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 3 with TupleQuery

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

the class Repositories method tupleQueryNoTransaction.

/**
 * Performs a SPARQL Select query on the given Repository without opening a transaction and passes the
 * results to the given {@link TupleQueryResultHandler}.
 *
 * @param repository
 *        The {@link Repository} to open a connection to.
 * @param query
 *        The SPARQL Select query to execute.
 * @param handler
 *        A {@link TupleQueryResultHandler} that consumes the results.
 * @throws RepositoryException
 *         If there was an exception dealing with the Repository.
 * @throws UnknownTransactionStateException
 *         If the transaction state was not properly recognised. (Optional specific exception)
 * @throws MalformedQueryException
 *         If the supplied query is malformed
 * @throws QueryEvaluationException
 *         If there was an error evaluating the query
 */
public static void tupleQueryNoTransaction(Repository repository, String query, TupleQueryResultHandler handler) throws RepositoryException, UnknownTransactionStateException, MalformedQueryException, QueryEvaluationException {
    consumeNoTransaction(repository, conn -> {
        TupleQuery preparedQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        preparedQuery.evaluate(handler);
    });
}
Also used : TupleQuery(org.eclipse.rdf4j.query.TupleQuery)

Example 4 with TupleQuery

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

the class Repositories method tupleQuery.

/**
 * Performs a SPARQL Select query on the given Repository within a transaction and passes the results to
 * the given {@link TupleQueryResultHandler}.
 *
 * @param repository
 *        The {@link Repository} to open a connection to.
 * @param query
 *        The SPARQL Select query to execute.
 * @param handler
 *        A {@link TupleQueryResultHandler} that consumes the results.
 * @throws RepositoryException
 *         If there was an exception dealing with the Repository.
 * @throws UnknownTransactionStateException
 *         If the transaction state was not properly recognised. (Optional specific exception)
 * @throws MalformedQueryException
 *         If the supplied query is malformed
 * @throws QueryEvaluationException
 *         If there was an error evaluating the query
 */
public static void tupleQuery(Repository repository, String query, TupleQueryResultHandler handler) throws RepositoryException, UnknownTransactionStateException, MalformedQueryException, QueryEvaluationException {
    consume(repository, conn -> {
        TupleQuery preparedQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        preparedQuery.evaluate(handler);
    });
}
Also used : TupleQuery(org.eclipse.rdf4j.query.TupleQuery)

Example 5 with TupleQuery

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

the class RepositoryFederatedService method select.

/**
 * Evaluate the provided sparqlQueryString at the initialized {@link Repository} of this
 * {@link FederatedService}. Insert bindings into SELECT query and evaluate
 */
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> select(Service service, Set<String> projectionVars, BindingSet bindings, String baseUri) throws QueryEvaluationException {
    try {
        String sparqlQueryString = service.getSelectQueryString(projectionVars);
        TupleQuery query = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, sparqlQueryString, baseUri);
        Iterator<Binding> bIter = bindings.iterator();
        while (bIter.hasNext()) {
            Binding b = bIter.next();
            if (service.getServiceVars().contains(b.getName()))
                query.setBinding(b.getName(), b.getValue());
        }
        TupleQueryResult res = query.evaluate();
        // insert original bindings again
        InsertBindingSetCursor result = new InsertBindingSetCursor(res, bindings);
        if (service.isSilent())
            return new SilentIteration(result);
        else
            return result;
    } catch (MalformedQueryException e) {
        throw new QueryEvaluationException(e);
    } catch (RepositoryException e) {
        throw new QueryEvaluationException("Repository for endpoint " + rep.toString() + " could not be initialized.", e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) InsertBindingSetCursor(org.eclipse.rdf4j.repository.sparql.query.InsertBindingSetCursor) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Aggregations

TupleQuery (org.eclipse.rdf4j.query.TupleQuery)9 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)7 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)5 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)5 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)5 IOException (java.io.IOException)3 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)2 ExceptionConvertingIteration (org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration)2 BindingSet (org.eclipse.rdf4j.query.BindingSet)2 UnsupportedQueryLanguageException (org.eclipse.rdf4j.query.UnsupportedQueryLanguageException)2 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)2 UnknownTransactionStateException (org.eclipse.rdf4j.repository.UnknownTransactionStateException)2 SPARQLTupleQuery (org.eclipse.rdf4j.repository.sparql.query.SPARQLTupleQuery)2 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)2 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)2 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)1 WrongArityException (fr.lirmm.graphik.graal.api.store.WrongArityException)1 SparqlConjunctiveQueryWriter (fr.lirmm.graphik.graal.io.sparql.SparqlConjunctiveQueryWriter)1 StringWriter (java.io.StringWriter)1 HashSet (java.util.HashSet)1