use of org.apache.clerezza.rdf.core.sparql.ResultSet 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.ResultSet 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.ResultSet 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.ResultSet 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.clerezza.rdf.core.sparql.ResultSet 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);
}
Aggregations