/** * Executes any sparql query. The type of the result object will vary * depending on the type of the query. If the defaultGraph is available * in this TcManages executeSparqlQuery(String, UriRef) should be used instead. * * @param query the sparql query to execute * @param defaultGraphName the ImmutableGraph to be used as default ImmutableGraph in the Sparql ImmutableGraph Store * @return the resulting ResultSet, ImmutableGraph or Boolean value */ public Object executeSparqlQuery(String query, IRI defaultGraphName) throws ParseException { return executeSparqlQuery(query, defaultGraphName, false); }
/** * Executes any sparql query. The type of the result object will vary * depending on the type of the query. If the defaultGraph is available * in this TcManages executeSparqlQuery(String, UriRef) should be used instead. * * @param query the sparql query to execute * @param defaultGraphName the ImmutableGraph to be used as default ImmutableGraph in the Sparql ImmutableGraph Store * @return the resulting ResultSet, ImmutableGraph or Boolean value */ public Object executeSparqlQuery(String query, IRI defaultGraphName) throws ParseException { return executeSparqlQuery(query, defaultGraphName, false); }
private ImmutableGraph sparqlConstruct(ConstructQuery constructQuery, Graph tripleCollection) { return tcManager.executeSparqlQuery(constructQuery, tripleCollection); }
/** * Executes a sparql ASK query. * * @param query the sparql ASK query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the boolean value this query evaluates to * @deprecated Query is discontinued */ @Deprecated public boolean executeSparqlQuery(AskQuery query, Graph defaultGraph) { return (Boolean) executeSparqlQuery((Query) query, defaultGraph); }
/** * Executes a sparql DESCRIBE query. * * @param query the sparql DESCRIBE query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the resulting ImmutableGraph * @deprecated Query is discontinued */ @Deprecated public ImmutableGraph executeSparqlQuery(DescribeQuery query, Graph defaultGraph) { return (ImmutableGraph) executeSparqlQuery((Query) query, defaultGraph); }
/** * Executes a sparql CONSTRUCT query. * * @param query the sparql CONSTRUCT query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the resulting ImmutableGraph * @deprecated Query is discontinued */ @Deprecated public ImmutableGraph executeSparqlQuery(ConstructQuery query, Graph defaultGraph) { return (ImmutableGraph) executeSparqlQuery((Query) query, defaultGraph); }
/** * Executes a sparql SELECT query. * * @param query the sparql SELECT query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the resulting ResultSet * @deprecated Query is discontinued */ @Deprecated public ResultSet executeSparqlQuery(SelectQuery query, Graph defaultGraph) { return (ResultSet) executeSparqlQuery((Query) query, defaultGraph); }
/** * Executes a sparql DESCRIBE query. * * @param query the sparql DESCRIBE query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the resulting ImmutableGraph * @deprecated Query is discontinued */ @Deprecated public ImmutableGraph executeSparqlQuery(DescribeQuery query, Graph defaultGraph) { return (ImmutableGraph) executeSparqlQuery((Query) query, defaultGraph); }
/** * Executes a sparql ASK query. * * @param query the sparql ASK query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the boolean value this query evaluates to * @deprecated Query is discontinued */ @Deprecated public boolean executeSparqlQuery(AskQuery query, Graph defaultGraph) { return (Boolean) executeSparqlQuery((Query) query, defaultGraph); }
/** * Executes a sparql SELECT query. * * @param query the sparql SELECT query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the resulting ResultSet * @deprecated Query is discontinued */ @Deprecated public ResultSet executeSparqlQuery(SelectQuery query, Graph defaultGraph) { return (ResultSet) executeSparqlQuery((Query) query, defaultGraph); }
/** * Executes a sparql CONSTRUCT query. * * @param query the sparql CONSTRUCT query to execute * @param defaultGraph the default ImmutableGraph against which to execute the query * if not FROM clause is present * @return the resulting ImmutableGraph * @deprecated Query is discontinued */ @Deprecated public ImmutableGraph executeSparqlQuery(ConstructQuery query, Graph defaultGraph) { return (ImmutableGraph) executeSparqlQuery((Query) query, defaultGraph); }
private InternetAddress getUserAddress(String user) throws MessagingException { MGraph systemGraph = tcManager.getMGraph(systemGraphUri); final String queryString = "SELECT ?email WHERE { " + "?x " + FOAF.mbox + " ?email . " + "?x " + PLATFORM.userName + " \"" + user + "\" . " + "}"; try { SelectQuery selectQuery = (SelectQuery) QueryParser.getInstance().parse(queryString); ResultSet result = tcManager.executeSparqlQuery(selectQuery, systemGraph); if (result.hasNext()) { Resource email = result.next().get("email"); String emailString = ((UriRef) email).getUnicodeString(); //TODO should add personal name (if available) as well return new InternetAddress(emailString.substring("mailto:".length())); } } catch (ParseException ex) { throw new RuntimeException(ex); } catch (AddressException ex) { throw new RuntimeException(ex); } throw new MessagingException("No address found for user " + user); } }
@Override public Graph retrieveConcepts(String searchTerm) { QueryParser queryParser = QueryParser.getInstance(); String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> " + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "PREFIX skos: <http://www.w3.org/2008/05/skos#> " + "CONSTRUCT {?concept a skos:Concept; skos:prefLabel ?prefLabel; " + "rdfs:comment ?comment; owl:sameAs ?sameConcept .} " + "WHERE {?concept skos:inScheme " + selectedScheme.toString() + "; skos:prefLabel ?prefLabel . " + "OPTIONAL { ?concept skos:altLabel ?altLabel .} " + "OPTIONAL { ?concept rdfs:comment ?comment .} " + "OPTIONAL { ?concept owl:sameAs ?sameConcept .} " + "FILTER (REGEX(STR(?prefLabel), '" + searchTerm + "', 'i') || REGEX(STR(?altLabel), '" + searchTerm + "', 'i'))}"; ConstructQuery constructQuery; try { constructQuery = (ConstructQuery) queryParser.parse(query); } catch (ParseException ex) { throw new RuntimeException(ex); } MGraph defaultGraph = contentGraphProvider.getContentGraph(); return tcManager.executeSparqlQuery(constructQuery, defaultGraph); } }
private void removeEnhancements(String id) throws StoreException { LockableMGraph enhancementGraph = getEnhancementGraph(); String enhancementQuery = QueryGenerator.getEnhancementsOfContent(id); SelectQuery selectQuery = null; try { selectQuery = (SelectQuery) QueryParser.getInstance().parse(enhancementQuery); } catch (ParseException e) { String msg = "Cannot parse the SPARQL while trying to delete the enhancements of the ContentItem"; log.error(msg, e); throw new StoreException(msg, e); } List<Triple> willBeRemoved = new ArrayList<Triple>(); Lock l = enhancementGraph.getLock().writeLock(); l.lock(); try { ResultSet resultSet = tcManager.executeSparqlQuery(selectQuery, enhancementGraph); while (resultSet.hasNext()) { SolutionMapping mapping = resultSet.next(); UriRef ref = (UriRef) mapping.get("enhID"); Iterator<Triple> tripleItr = enhancementGraph.filter(ref, null, null); while (tripleItr.hasNext()) { Triple triple = tripleItr.next(); willBeRemoved.add(triple); } } enhancementGraph.removeAll(willBeRemoved); } finally { l.unlock(); } }
/** * 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); } @Override
ResultSet result = tcManager.executeSparqlQuery(query, ci.getMetadata()); List<String> values = new ArrayList<String>(); while (result.hasNext()) {
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){
@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; }
@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; }
ResultSet resultSet = tcManager.executeSparqlQuery(query, unionGraph);