Search in sources :

Example 6 with TupleQuery

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

the class RepositoryFederatedService method evaluateInternal.

/**
 * Evaluate the SPARQL query that can be constructed from the SERVICE node at the initialized
 * {@link Repository} of this {@link FederatedService}. Use specified bindings as constraints to the
 * query. Try to evaluate using BINDINGS clause, if this yields an exception fall back to the naive
 * implementation. This method deals with SILENT SERVICEs.
 */
protected CloseableIteration<BindingSet, QueryEvaluationException> evaluateInternal(Service service, CloseableIteration<BindingSet, QueryEvaluationException> bindings, String baseUri) throws QueryEvaluationException {
    // materialize all bindings (to allow for fallback in case of errors)
    // note that this may be blocking depending on the underlying iterator
    List<BindingSet> allBindings = new LinkedList<BindingSet>();
    while (bindings.hasNext()) {
        allBindings.add(bindings.next());
    }
    if (allBindings.size() == 0) {
        return new EmptyIteration<BindingSet, QueryEvaluationException>();
    }
    // projection vars
    Set<String> projectionVars = new HashSet<String>(service.getServiceVars());
    projectionVars.removeAll(allBindings.get(0).getBindingNames());
    // below we need to take care for SILENT services
    CloseableIteration<BindingSet, QueryEvaluationException> result = null;
    try {
        // fallback to simple evaluation (just a single binding)
        if (allBindings.size() == 1) {
            result = select(service, projectionVars, allBindings.get(0), baseUri);
            result = service.isSilent() ? new SilentIteration(result) : result;
            return result;
        }
        // To be able to insert the input bindings again later on, we need some
        // means to identify the row of each binding. hence, we use an
        // additional
        // projection variable, which is also passed in the BINDINGS clause
        // with the value of the actual row. The value corresponds to the index
        // of the binding in the index list
        projectionVars.add("__rowIdx");
        String queryString = service.getSelectQueryString(projectionVars);
        List<String> relevantBindingNames = getRelevantBindingNames(allBindings, service.getServiceVars());
        if (relevantBindingNames.size() != 0) {
            // append the VALUES clause to the query
            queryString += buildVALUESClause(allBindings, relevantBindingNames);
        }
        TupleQuery query = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString, baseUri);
        TupleQueryResult res = null;
        // TODO how to retrieve max query value
        query.setMaxQueryTime(60);
        // from actual setting?
        res = query.evaluate();
        if (relevantBindingNames.size() == 0)
            // cross
            result = new SPARQLCrossProductIteration(res, allBindings);
        else
            // product
            // common
            result = new ServiceJoinConversionIteration(res, allBindings);
        // join
        result = service.isSilent() ? new SilentIteration(result) : result;
        return result;
    } catch (RepositoryException e) {
        Iterations.closeCloseable(result);
        if (service.isSilent())
            return new CollectionIteration<BindingSet, QueryEvaluationException>(allBindings);
        throw new QueryEvaluationException("Repository for endpoint " + rep.toString() + " could not be initialized.", e);
    } catch (MalformedQueryException e) {
        // this exception must not be silenced, bug in our code
        throw new QueryEvaluationException(e);
    } catch (QueryEvaluationException e) {
        Iterations.closeCloseable(result);
        if (service.isSilent())
            return new CollectionIteration<BindingSet, QueryEvaluationException>(allBindings);
        throw e;
    } catch (RuntimeException e) {
        Iterations.closeCloseable(result);
        // QueryEval) if silent
        if (service.isSilent())
            return new CollectionIteration<BindingSet, QueryEvaluationException>(allBindings);
        throw e;
    }
}
Also used : EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) EmptyIteration(org.eclipse.rdf4j.common.iteration.EmptyIteration) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) LinkedList(java.util.LinkedList) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) HashSet(java.util.HashSet)

Example 7 with TupleQuery

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

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

use of org.eclipse.rdf4j.query.TupleQuery 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)

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