Search in sources :

Example 1 with AdaptingIterator

use of org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator 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 AdaptingIterator

use of org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator 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 AdaptingIterator

use of org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator in project stanbol by apache.

the class SiteManagerRootResource method executeLDPathQuery.

/**
 * Execute a Query that uses LDPath to process results.
 * @param query the query
 * @param mediaType the mediaType for the response
 * @param headers the http headers of the request
 * @return the response
 */
private Response executeLDPathQuery(SiteManager manager, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
    QueryResultList<Representation> result;
    ValueFactory vf = new RdfValueFactory(new IndexedGraph());
    SiteManagerBackend backend = new SiteManagerBackend(manager);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    // copy the selected fields, because we might need to delete some during
    // the preparation phase
    Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
    // first prepare (only execute the query if the parameters are valid)
    Program<Object> program;
    try {
        program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
    } catch (LDPathParseException e) {
        log.warn("Unable to parse LDPath program used as select for a Query to the '/sites' endpoint:");
        log.warn("FieldQuery: \n {}", query);
        log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
        log.warn("Exception:", e);
        return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
    } catch (IllegalStateException e) {
        log.warn("parsed LDPath program is not compatible with the Query " + "parsed to the '/sites' endpoint!", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    // 2. execute the query
    // we need to adapt from Entity to Representation
    // TODO: should we add the metadata to the result?
    Iterator<Representation> resultIt = new AdaptingIterator<Entity, Representation>(manager.findEntities(query).iterator(), new AdaptingIterator.Adapter<Entity, Representation>() {

        @Override
        public Representation adapt(Entity value, Class<Representation> type) {
            return value.getRepresentation();
        }
    }, Representation.class);
    // process the results
    Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
    result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
    ResponseBuilder rb = Response.ok(result);
    rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SiteManagerBackend(org.apache.stanbol.entityhub.ldpath.backend.SiteManagerBackend) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Aggregations

AdaptingIterator (org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)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 SparqlFieldQuery (org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery)2 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)2 HashSet (java.util.HashSet)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)1 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)1 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)1 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)1 SiteManagerBackend (org.apache.stanbol.entityhub.ldpath.backend.SiteManagerBackend)1 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)1 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)1 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)1 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)1 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)1