use of org.apache.clerezza.rdf.core.sparql.query.SelectQuery in project stanbol by apache.
the class ClerezzaRuleStore method findRecipesByDescription.
@Override
public RecipeList findRecipesByDescription(String term) {
String sparql = "SELECT ?recipe " + "WHERE { ?recipe a " + Symbols.Recipe.toString() + " . " + "?recipe " + Symbols.description + " ?description . " + "FILTER (regex(?description, \"" + term + "\", \"i\"))" + "}";
Graph tripleCollection = tcManager.getGraph(new IRI(recipeIndexLocation));
RecipeList matchingRecipes = new RecipeList();
try {
SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
ResultSet resultSet = tcManager.executeSparqlQuery(query, tripleCollection);
while (resultSet.hasNext()) {
SolutionMapping solutionMapping = resultSet.next();
IRI recipeID = (IRI) solutionMapping.get("recipe");
try {
Recipe recipe = getRecipe(recipeID);
log.info("Found recipe {}.", recipeID.toString());
matchingRecipes.add(recipe);
log.info("Found {} matching recipes.", matchingRecipes.size());
} catch (NoSuchRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (RecipeConstructionException e) {
// in this case go on in the iteration by fetching other matching recipes
}
}
} catch (ParseException e) {
log.error("The sparql query contains errors: ", e);
}
return matchingRecipes;
}
use of org.apache.clerezza.rdf.core.sparql.query.SelectQuery in project stanbol by apache.
the class ClerezzaRuleStore method findRulesByName.
@Override
public RuleList findRulesByName(String term) {
String sparql = "SELECT ?recipe ?rule ?description " + "WHERE { " + "?recipe " + Symbols.hasRule + " ?rule . " + "?rule " + Symbols.ruleName + " ?name . " + "?rule " + Symbols.description + " ?description . " + "FILTER (regex(?name, \"" + term + "\", \"i\"))" + "}";
List<IRI> recipeIDs = listRecipeIDs();
Graph[] tripleCollections = new Graph[recipeIDs.size()];
for (int i = 0; i < tripleCollections.length; i++) {
tripleCollections[i] = tcManager.getGraph(recipeIDs.get(i));
}
UnionGraph unionGraph = new UnionGraph(tripleCollections);
RuleList matchingRules = new RuleList();
try {
SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
ResultSet resultSet = tcManager.executeSparqlQuery(query, unionGraph);
while (resultSet.hasNext()) {
SolutionMapping solutionMapping = resultSet.next();
IRI recipeID = (IRI) solutionMapping.get("recipe");
IRI ruleID = (IRI) solutionMapping.get("rule");
Literal description = (Literal) solutionMapping.get("description");
try {
Recipe recipe = getRecipe(recipeID);
Rule rule = new RecipeRule(recipe, getRule(recipe, ruleID));
if (description != null) {
rule.setDescription(description.getLexicalForm());
}
matchingRules.add(rule);
} catch (NoSuchRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (RecipeConstructionException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (NoSuchRuleInRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
}
}
} catch (ParseException e) {
log.error("The sparql query contains errors: ", e);
}
return matchingRules;
}
use of org.apache.clerezza.rdf.core.sparql.query.SelectQuery in project stanbol by apache.
the class OpenCalaisEngine method queryModel.
/**
* Extracts the relevant entity information from the Calais RDF data.
* The entities and the relted information is extracted by a Sparql query.
*
* @param model the Graph representing the Calais data
*
* @return a Collection of entity information
* @throws EngineException on a {@link ParseException} while processing the
* Sparql query.
*/
public Collection<CalaisEntityOccurrence> queryModel(Graph model) throws EngineException {
//TODO extract also Geo info (latitude/longitude)?
String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " + "PREFIX p: <http://s.opencalais.com/1/pred/> " + "PREFIX t: <http://s.opencalais.com/1/type/em/e/> " + "SELECT DISTINCT ?id ?did ?name ?type ?dtype ?offset ?length ?exact ?context ?score WHERE { " + "?id p:name ?name ." + "?id rdf:type ?type ." + "?y p:subject ?id ." + "?y p:offset ?offset ." + "?y p:length ?length ." + "?y p:exact ?exact ." + "?y p:detection ?context ." + " OPTIONAL { ?z p:subject ?id . ?z p:relevance ?score . } " + // get disambiguated entity references if available
" OPTIONAL { ?did p:subject ?id . ?did p:name ?name . ?did rdf:type ?dtype . } " + "FILTER (" + "?type = t:Person || " + "?type = t:City || " + "?type = t:Continent || " + "?type = t:Country || " + "?type = t:ProvinceOrState || " + "?type = t:Region || " + "?type = t:Company || " + "?type = t:Facility || " + "?type = t:Organization " + ")" + "} ";
Collection<CalaisEntityOccurrence> result = new ArrayList<CalaisEntityOccurrence>();
try {
SelectQuery sQuery = (SelectQuery) QueryParser.getInstance().parse(query);
ResultSet rs = tcManager.executeSparqlQuery(sQuery, model);
while (rs.hasNext()) {
SolutionMapping row = rs.next();
CalaisEntityOccurrence occ = new CalaisEntityOccurrence();
RDFTerm disambiguated = row.get("did");
occ.id = (disambiguated == null ? row.get("id") : disambiguated);
if (onlyNERMode) {
occ.type = row.get("type");
} else {
occ.type = (disambiguated == null ? row.get("type") : row.get("dtype"));
}
if (calaisTypeMap != null) {
IRI mappedType = calaisTypeMap.get(occ.type);
if (mappedType != null) {
occ.type = mappedType;
}
}
occ.name = ((Literal) row.get("name")).getLexicalForm();
occ.exact = ((Literal) row.get("exact")).getLexicalForm();
//TODO for html the offsets might not be those of the original document but refer to a cleaned up version?
occ.offset = Integer.valueOf(((Literal) row.get("offset")).getLexicalForm());
// remove brackets
occ.context = ((Literal) row.get("context")).getLexicalForm().replaceAll("[\\[\\]]", "");
occ.length = Integer.valueOf(((Literal) row.get("length")).getLexicalForm());
if (row.get("score") != null) {
occ.relevance = Double.valueOf(((Literal) row.get("score")).getLexicalForm());
}
result.add(occ);
}
} catch (ParseException e) {
throw new EngineException("Unable to parse SPARQL query for processing OpenCalais results", e);
}
log.info("Found {} occurences", result.size());
return result;
}
use of org.apache.clerezza.rdf.core.sparql.query.SelectQuery in project stanbol by apache.
the class ClerezzaYard method executeSparqlFieldQuery.
/**
* Returns the SPARQL result set for a given {@link SparqlFieldQuery} that
* was executed on this yard
* @param query the SparqlFieldQuery instance
* @return the results of the SPARQL query in the yard
* @throws YardException in case the generated SPARQL query could not be parsed
* or the generated Query is not an SPARQL SELECT query.
*/
private ResultSet executeSparqlFieldQuery(final SparqlFieldQuery query) throws YardException {
int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
SelectQuery sparqlQuery;
String sparqlQueryString = SparqlQueryUtils.createSparqlSelectQuery(query, false, limit, EndpointTypeEnum.Standard);
try {
sparqlQuery = (SelectQuery) 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);
} catch (ClassCastException e) {
log.error("ClassCastExeption because parsed SPARQL Query is not of Type " + SelectQuery.class);
log.error("FieldQuery: " + query);
log.error("SPARQL Query: " + sparqlQueryString);
throw new YardException("Unable to parse SPARQL SELECT query generated for the parse FieldQuery", e);
}
return tcManager.executeSparqlQuery(sparqlQuery, graph);
}
use of org.apache.clerezza.rdf.core.sparql.query.SelectQuery in project stanbol by apache.
the class ClerezzaRuleStore method findRulesByDescription.
@Override
public RuleList findRulesByDescription(String term) {
String sparql = "SELECT ?recipe ?rule ?description " + "WHERE { " + "?recipe " + Symbols.hasRule + " ?rule . " + "?rule " + Symbols.description + " ?description . " + "FILTER (regex(?description, \"" + term + "\", \"i\"))" + "}";
List<IRI> recipeIDs = listRecipeIDs();
Graph[] tripleCollections = new Graph[recipeIDs.size()];
for (int i = 0; i < tripleCollections.length; i++) {
tripleCollections[i] = tcManager.getGraph(recipeIDs.get(i));
}
UnionGraph unionGraph = new UnionGraph(tripleCollections);
RuleList matchingRules = new RuleList();
try {
SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
ResultSet resultSet = tcManager.executeSparqlQuery(query, unionGraph);
while (resultSet.hasNext()) {
SolutionMapping solutionMapping = resultSet.next();
IRI recipeID = (IRI) solutionMapping.get("recipe");
IRI ruleID = (IRI) solutionMapping.get("rule");
Literal description = (Literal) solutionMapping.get("description");
try {
Recipe recipe = getRecipe(recipeID);
Rule rule = new RecipeRule(recipe, getRule(recipe, ruleID));
if (description != null) {
rule.setDescription(description.getLexicalForm());
}
matchingRules.add(rule);
} catch (NoSuchRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (RecipeConstructionException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (NoSuchRuleInRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
}
}
} catch (ParseException e) {
log.error("The sparql query contains errors: ", e);
}
return matchingRules;
}
Aggregations