org.apache.jena.query.ParameterizedSparqlString Java Examples
The following examples show how to use
org.apache.jena.query.ParameterizedSparqlString.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PatternQueryHandler.java From IGUANA with GNU Affero General Public License v3.0 | 6 votes |
protected Query convertToSelect(ParameterizedSparqlString pss, Set<String> varNames) { Query queryCpy = pss.asQuery(); queryCpy.getQueryPattern(); StringBuilder queryStr = new StringBuilder("SELECT DISTINCT "); for(String varName : varNames) { queryStr.append("?").append(varName).append(" "); } queryStr.append(queryCpy.getQueryPattern()); ParameterizedSparqlString pssSelect = new ParameterizedSparqlString(); pssSelect.setCommandText(queryStr.toString()); pssSelect.setNsPrefixes(pss.getNsPrefixMap()); pssSelect.append(" LIMIT "); pssSelect.append(this.limit); System.out.println(pssSelect); return pssSelect.asQuery(); }
Example #2
Source File: TestCaseWithTarget.java From RDFUnit with Apache License 2.0 | 6 votes |
private String injectTarget(String sparqlQuery, ShapeTarget target) { String changedQuery = sparqlQuery; if (target.getTargetType().equals(ShapeTargetType.NodeTarget)) { changedQuery = sparqlQuery .replaceFirst("[\\$\\?]this", thisVar ) .replaceAll("(?i)BOUND\\s*\\(\\s*[\\$\\?]this\\s*\\)", "BOUND\\("+ thisVar+"\\)") .replaceAll("(?i)GROUP\\s+BY\\s+[\\$\\?]this", "GROUP BY "+ thisVar); QuerySolutionMap qsm = new QuerySolutionMap(); qsm.add(CommonNames.This, target.getNode()); ParameterizedSparqlString pq = new ParameterizedSparqlString(changedQuery, qsm); changedQuery = pq.toString(); changedQuery = changedQuery .replaceFirst(thisVar, "\\$this") .replaceAll("(?i)BOUND\\("+ thisVar+"\\)", "BOUND\\(\\?this\\)") .replaceAll("(?i)GROUP BY "+ thisVar, "GROUP BY \\$this"); } return injectSparqlSnippet(changedQuery, target.getPattern()); }
Example #3
Source File: DBpediaToWikiId.java From gerbil with GNU Affero General Public License v3.0 | 5 votes |
/** * The Wikipedia Id or -1 if the Id couldn't be retrieved. * * FIXME The method throws an exception for "http://DBpedia.org/resource/Origin_of_the_name_"Empire_State"". this * might be happen because of the quotes inside the URI. * * @param dbpediaUri * @return */ @Deprecated public static int getIdFromDBpedia(String dbpediaUri) { int id = -1; ParameterizedSparqlString query = new ParameterizedSparqlString( "SELECT ?id WHERE { ?dbpedia dbo:wikiPageID ?id .}", prefixes); query.setIri("dbpedia", dbpediaUri); QueryExecution qexec = null; try { qexec = QueryExecutionFactory.create(query.asQuery(), model); } catch (QueryParseException e) { LOGGER.error("Got a bad dbpediaUri \"" + dbpediaUri + "\" which couldn't be parse inside of a SPARQL query. Returning -1.", e); return id; } ResultSet result = qexec.execSelect(); if (result.hasNext()) { id = result.next().get("id").asLiteral().getInt(); return id; } qexec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query.asQuery()); result = qexec.execSelect(); if (result.hasNext()) { id = result.next().get("id").asLiteral().getInt(); model.add(new StatementImpl(model.createResource(dbpediaUri), model .createProperty("http://dbpedia.org/ontology/wikiPageID"), model.createTypedLiteral(id))); return id; } model.add(new StatementImpl(model.createResource(dbpediaUri), model .createProperty("http://dbpedia.org/ontology/wikiPageID"), model.createTypedLiteral(id))); return id; }
Example #4
Source File: PatternQueryHandlerTest.java From IGUANA with GNU Affero General Public License v3.0 | 5 votes |
/** * Tests the convert to select method */ @Test public void testConverting() { ParameterizedSparqlString pss = new ParameterizedSparqlString(); pss.setCommandText(query); assertEquals(QueryFactory.create(expectedQuery), handler.convertToSelect(pss, varNames)); }
Example #5
Source File: ShapeTargetCore.java From RDFUnit with Apache License 2.0 | 5 votes |
static String getBindedSparql(String sparql, RDFNode node) { ParameterizedSparqlString parameterizedSparqlString = new ParameterizedSparqlString("ASK{" +sparql+ "}"); parameterizedSparqlString.setParam("target", node); String queryPattern = parameterizedSparqlString .toString() .trim() .replace("ASK{", ""); // remove last '}' return queryPattern.substring(0, queryPattern.length()-1); }
Example #6
Source File: PatternQueryHandler.java From IGUANA with GNU Affero General Public License v3.0 | 4 votes |
protected Set<String> getInstances(String queryStr) { Set<String> instances = new HashSet<String>(); //check if query is already an instance try{ QueryFactory.create(queryStr); //query is instance LOGGER.debug("[QueryHandler: {{}}] Query is already an instance: {{}}", this.getClass().getName(), queryStr); instances.add(queryStr); return instances; }catch(Exception e) { //query is pattern, nothing to do } //get vars from queryStr Set<String> varNames = new HashSet<String>(); String command = replaceVars(queryStr, varNames); //generate parameterized sparql query ParameterizedSparqlString pss = new ParameterizedSparqlString(); pss.setCommandText(command); ResultSet exchange = getInstanceVars(pss, varNames); System.out.println("instaces retrieved"); // exchange vars in PSS if(!exchange.hasNext()) { //no solution System.out.println("No solution"); LOGGER.warn("[QueryHandler: {{}}] Query has no solution, will use variables instead of var instances: {{}}", this.getClass().getName(), queryStr); instances.add(command); } while(exchange.hasNext()) { QuerySolution solution = exchange.next(); for(String var : exchange.getResultVars()) { //exchange variable with pss.clearParam(var); pss.setParam(var, solution.get(var)); } instances.add(pss.toString()); } System.out.println("Found instances "+instances); return instances; }
Example #7
Source File: PatternQueryHandler.java From IGUANA with GNU Affero General Public License v3.0 | 4 votes |
protected ResultSet getInstanceVars(ParameterizedSparqlString pss, Set<String> varNames) { QueryExecution exec = QueryExecutionFactory.createServiceRequest(service, convertToSelect(pss,varNames)); //return result set return exec.execSelect(); }
Example #8
Source File: SparqlBasedRequestProcessorForTPFs.java From Server.Java with MIT License | 4 votes |
/** * * @param subject * @param predicate * @param object * @param offset * @param limit * @return */ @Override protected ILinkedDataFragment createFragment( final ITriplePatternElement<RDFNode,String,String> subject, final ITriplePatternElement<RDFNode,String,String> predicate, final ITriplePatternElement<RDFNode,String,String> object, final long offset, final long limit ) { // FIXME: The following algorithm is incorrect for cases in which // the requested triple pattern contains a specific variable // multiple times; // e.g., (?x foaf:knows ?x ) or (_:bn foaf:knows _:bn) // see https://github.com/LinkedDataFragments/Server.Java/issues/24 QuerySolutionMap map = new QuerySolutionMap(); if ( ! subject.isVariable() ) { map.add("s", subject.asConstantTerm()); } if ( ! predicate.isVariable() ) { map.add("p", predicate.asConstantTerm()); } if ( ! object.isVariable() ) { map.add("o", object.asConstantTerm()); } query.setOffset(offset); query.setLimit(limit); Model triples = ModelFactory.createDefaultModel(); // Build the SPARQL-endpoint URIBuilder uriBuilder = new URIBuilder(endpointURI); addCredentials(uriBuilder); final String endpoint; try { endpoint = uriBuilder.build().toString(); } catch (URISyntaxException e) { throw new RuntimeException(e); } ParameterizedSparqlString queryWithParams = new ParameterizedSparqlString(query.serialize(), map); try (QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, queryWithParams.asQuery())) { qexec.execConstruct(triples); } if (triples.isEmpty()) { return createEmptyTriplePatternFragment(); } // Try to get an estimate long size = triples.size(); long estimate = -1; ParameterizedSparqlString countQueryWithParams = new ParameterizedSparqlString(countQuery.serialize(), map); try (QueryExecution qexec = QueryExecutionFactory.createServiceRequest(endpoint, countQueryWithParams.asQuery())) { ResultSet results = qexec.execSelect(); if (results.hasNext()) { QuerySolution soln = results.nextSolution() ; Literal literal = soln.getLiteral("count"); estimate = literal.getLong(); } } /*GraphStatisticsHandler stats = model.getGraph().getStatisticsHandler(); if (stats != null) { Node s = (subject != null) ? subject.asNode() : null; Node p = (predicate != null) ? predicate.asNode() : null; Node o = (object != null) ? object.asNode() : null; estimate = stats.getStatistic(s, p, o); }*/ // No estimate or incorrect if (estimate < offset + size) { estimate = (size == limit) ? offset + size + 1 : offset + size; } // create the fragment final boolean isLastPage = ( estimate < offset + limit ); return createTriplePatternFragment( triples, estimate, isLastPage ); }