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);
}
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);
}
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);
}
Aggregations