Search in sources :

Example 1 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery in project stanbol by apache.

the class ClerezzaYard method findRepresentation.

@Override
public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    final ResultSet result = executeSparqlFieldQuery(query);
    //Note: An other possibility would be to first iterate over all results and add it to
    //      a list and create this Iterator than based on the List. This would
    //      be the preferenced way if changes in the graph could affect the
    //     Iteration over the SPARQL query results.
    Iterator<Representation> representationIterator = new AdaptingIterator<SolutionMapping, Representation>(result, new AdaptingIterator.Adapter<SolutionMapping, Representation>() {

        /**
                     * Adapter that gets the rootVariable of the Query (selecting the ID)
                     * and creates a Representation for it.
                     * @param solution a solution of the query
                     * @param type the type (no generics here)
                     * @return the representation or <code>null</code> if result is
                     * not an IRI or there is no Representation for the result.
                     */
        @Override
        public Representation adapt(SolutionMapping solution, Class<Representation> type) {
            RDFTerm resource = solution.get(query.getRootVariableName());
            if (resource instanceof IRI) {
                try {
                    return getRepresentation((IRI) resource, false);
                } catch (IllegalArgumentException e) {
                    log.warn("Unable to create Representation for ID " + resource + "! -> ignore query result");
                    return null;
                }
            } else {
                return null;
            }
        }
    }, Representation.class);
    //      created before the method returns.
    return new QueryResultListImpl<Representation>(query, representationIterator, Representation.class);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)

Example 2 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery in project stanbol by apache.

the class ClerezzaYard method findReferences.

@Override
public QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    final ResultSet result = executeSparqlFieldQuery(query);
    //A little bit complex construct ...
    // first we use the adaptingIterator to convert reseource to string
    // to get the resources we have to retrieve the root-variable of the
    // Iterator<SolutionMapping> provided by the ResultSet of the SPARQL query
    Iterator<String> representationIdIterator = new AdaptingIterator<RDFTerm, String>(new Iterator<RDFTerm>() {

        @Override
        public void remove() {
            result.remove();
        }

        @Override
        public RDFTerm next() {
            return result.next().get(query.getRootVariableName());
        }

        @Override
        public boolean hasNext() {
            return result.hasNext();
        }
    }, new Resource2StringAdapter<RDFTerm>(), String.class);
    return new QueryResultListImpl<String>(query, representationIdIterator, String.class);
}
Also used : SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)

Example 3 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery in project stanbol by apache.

the class ClerezzaYard method find.

@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
    Query sparqlQuery;
    //NOTE:
    // - use the endpoint type standard, because we do not know what type of
    //   SPARQL implementation is configured for Clerezza via OSGI
    String sparqlQueryString = SparqlQueryUtils.createSparqlConstructQuery(query, limit, EndpointTypeEnum.Standard);
    try {
        sparqlQuery = QueryParser.getInstance().parse(sparqlQueryString);
    } catch (ParseException e) {
        log.error("ParseException for SPARQL Query in findRepresentation");
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
    }
    Object resultObject = tcManager.executeSparqlQuery(sparqlQuery, graph);
    final Graph resultGraph;
    if (resultObject instanceof Graph) {
        resultGraph = (Graph) resultObject;
    } else if (resultObject instanceof ImmutableGraph) {
        resultGraph = new IndexedGraph();
        resultGraph.addAll((ImmutableGraph) resultObject);
    } else {
        log.error("Unable to create " + Graph.class + " instance for query reults of type " + resultObject.getClass() + " (this indicates that the used SPARQL Query was not of type CONSTRUCT)");
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to process results of Query");
    }
    return new RdfQueryResultList(query, resultGraph);
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Query(org.apache.clerezza.rdf.core.sparql.query.Query) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Aggregations

SparqlFieldQuery (org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery)3 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)2 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)2 AdaptingIterator (org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)2 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)1 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)1 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)1 Query (org.apache.clerezza.rdf.core.sparql.query.Query)1 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)1 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)1 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)1 RdfQueryResultList (org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)1 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)1