Search in sources :

Example 41 with FieldQuery

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

FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)41 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)18 Test (org.junit.Test)13 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)12 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)10 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)7 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)6 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)5 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)5 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)5 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)4 FieldQueryFactory (org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory)4 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)4 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)3 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)3 JSONException (org.codehaus.jettison.json.JSONException)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 IOException (java.io.IOException)2 PrivilegedActionException (java.security.PrivilegedActionException)2