use of org.apache.clerezza.rdf.core.sparql.query.Query 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);
}
use of org.apache.clerezza.rdf.core.sparql.query.Query in project stanbol by apache.
the class ClerezzaRuleStore method getRecipe.
@Override
public Recipe getRecipe(IRI recipeID) throws NoSuchRecipeException, RecipeConstructionException {
log.info("Called get recipe for id: " + recipeID);
Graph recipeGraph = null;
/**
* Throw a NoSuchRecipeException in case of the TcManager throws a NoSuchEntityException with respect
* to IRI representing the recipe.
*/
try {
recipeGraph = tcManager.getGraph(recipeID);
} catch (NoSuchEntityException e) {
throw new NoSuchRecipeException(recipeID.toString());
}
Iterator<Triple> descriptions = recipeGraph.filter(null, Symbols.description, null);
String recipeDescription = null;
if (descriptions != null && descriptions.hasNext()) {
recipeDescription = descriptions.next().getObject().toString();
}
String query = "SELECT ?rule ?ruleName ?ruleBody ?ruleHead " + "WHERE { " + " " + recipeID.toString() + " " + Symbols.hasRule.toString() + " ?rule . " + " ?rule " + Symbols.ruleName.toString() + " ?ruleName . " + " ?rule " + Symbols.ruleBody.toString() + " ?ruleBody . " + " ?rule " + Symbols.ruleHead.toString() + " ?ruleHead . " + "}";
Query sparql;
try {
sparql = QueryParser.getInstance().parse(query);
ResultSet resultSet = tcManager.executeSparqlQuery((SelectQuery) sparql, recipeGraph);
StringBuilder stanbolRulesBuilder = new StringBuilder();
boolean firstIteration = true;
while (resultSet.hasNext()) {
SolutionMapping solutionMapping = resultSet.next();
RDFTerm nameResource = solutionMapping.get("ruleName");
RDFTerm bodyResource = solutionMapping.get("ruleBody");
RDFTerm headResource = solutionMapping.get("ruleHead");
StringBuilder stanbolRuleBuilder = new StringBuilder();
stanbolRuleBuilder.append(((Literal) nameResource).getLexicalForm());
stanbolRuleBuilder.append("[");
stanbolRuleBuilder.append(((Literal) bodyResource).getLexicalForm());
stanbolRuleBuilder.append(" -> ");
stanbolRuleBuilder.append(((Literal) headResource).getLexicalForm());
stanbolRuleBuilder.append("]");
if (!firstIteration) {
stanbolRulesBuilder.append(" . ");
} else {
firstIteration = false;
}
String stanbolSyntax = stanbolRuleBuilder.toString();
log.info("Rule content {}", stanbolSyntax);
stanbolRulesBuilder.append(stanbolSyntax);
}
String stanbolSyntax = stanbolRulesBuilder.toString();
RuleList ruleList = null;
if (!stanbolSyntax.isEmpty()) {
String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
ruleList = RuleParserImpl.parse(namespace, stanbolSyntax).getRuleList();
}
return new RecipeImpl(recipeID, recipeDescription, ruleList);
} catch (ParseException e) {
throw new RecipeConstructionException(e);
}
}
Aggregations