private List<String> getResourcesOfResultSet(final ResultSet resultSet) { final List<QuerySolution> querySolutions = ResultSetFormatter.toList(resultSet); final List<String> resources = new ArrayList<>(); for (final QuerySolution querySolution : querySolutions) { final String resultUri = querySolution.toString(); String resourceResult = resultUri.substring(resultUri.indexOf(" <") + 1, resultUri.indexOf("> ")); resourceResult = resourceResult.substring(OntConfig.NAMESPACE.length() + 1); resources.add(resourceResult); } return resources; }
public static SortedSet<String> getStringSetForVariableFromResultSet( ResultSetRewindable rs, String variable) { final SortedSet<String> result = new TreeSet<>(); @SuppressWarnings("unchecked") final List<QuerySolution> l = ResultSetFormatter.toList(rs); for (QuerySolution resultBinding : l) { result.add(resultBinding.get(variable).toString()); } rs.reset(); return result; }
@SuppressWarnings("unchecked") public SortedSet<RDFNodeTuple> queryAsRDFNodeTuple(String sparqlQueryString, String var1, String var2) { ResultSetRewindable rsw = null; SortedSet<RDFNodeTuple> returnSet = new TreeSet<>(); try { String jsonString = query(sparqlQueryString); rsw = SparqlQuery.convertJSONtoResultSet(jsonString); List<QuerySolution> l = ResultSetFormatter.toList(rsw); for (QuerySolution resultBinding : l) { returnSet.add(new RDFNodeTuple(resultBinding.get(var1),resultBinding.get(var2))); } rsw.reset(); } catch (Exception e) { logger.info("ignoring (see log for details): Exception caught in SPARQLTasks, passing empty result: "+e.getMessage()); } return returnSet; }
@SuppressWarnings("unchecked") public List<Count> countInstances(String property, String namespace) { List<Count> ret = new ArrayList<>(); String query = "SELECT ?class count(?instance) as ?count {" + "?instance <"+property+"> ?class ." + "FILTER (?class LIKE <"+namespace+"%>) " + "}" + "GROUP BY ?class " + "ORDER by DESC(?count)" + ""; ResultSetRewindable rsw = t.queryAsResultSet(query); final List<QuerySolution> l = ResultSetFormatter.toList(rsw); for (QuerySolution resultBinding : l) { String res = resultBinding.get("class").toString(); Literal lit = (Literal) resultBinding.get("count"); ret.add(new Count(res, lit.getInt())); } System.out.println("retrieved: "+ret.size()); return ret; } }
private static SortedSet<StringTuple> getTuplesFromResultSet( ResultSetRewindable rs, String predicate, String object) { final SortedSet<StringTuple> returnSet = new TreeSet<>(); //SimpleClock sc = new SimpleClock(); @SuppressWarnings("unchecked") final List<QuerySolution> l = ResultSetFormatter.toList(rs); for (QuerySolution resultBinding : l) { returnSet.add(new StringTuple(resultBinding.get(predicate).toString(),resultBinding.get(object).toString())); } //sc.printAndSet("allTuples"); rs.reset(); //sc.printAndSet("reset"); return returnSet; }
@SuppressWarnings("unchecked") public List<Count> countProperties() { List<Count> ret = new ArrayList<>(); String query = "SELECT ?p count(?p) as ?count {" + "?s ?p ?o" + "} GROUP BY ?p " + "ORDER by DESC(?count)" + ""; ResultSetRewindable rsw = t.queryAsResultSet(query); final List<QuerySolution> l = ResultSetFormatter.toList(rsw); for (QuerySolution resultBinding : l) { Resource res = (Resource)resultBinding.get("p"); Literal lit = (Literal) resultBinding.get("count"); ret.add(new Count(res.getURI(), lit.getInt())); } return ret; }
private final ResultSetRewindable realResultsHandler(final QueryResult bindings) { final ResultSetRewindable real = ResultSetFactory.makeRewindable(new SparqlDLResultSet(bindings, _model.getRawModel())); final List<?> realList = ResultSetFormatter.toList(real); if (realList.size() > 10) { if (_logger.isLoggable(Level.INFO)) _logger.log(Level.INFO, "Real=" + realList.subList(0, 9) + " ... " + realList.size()); } else if (_logger.isLoggable(Level.INFO)) _logger.info("Real=" + realList); real.reset(); return real; }
public static void main(String[] args) { String p1 = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "PREFIX mo: <http://purl.org/ontology/mo/> "; String sparqlQueryString = p1+ "SELECT ?artist ?name ?image ?homepage WHERE {?artist a mo:MusicArtist .?artist foaf:name \"Allison Crowe\" .?artist foaf:name ?name .?artist foaf:img ?image . ?artist foaf:homepage ?homepage . }LIMIT 10"; System.out.println("SparqlQuery: "); System.out.println(sparqlQueryString); System.out.println("wget -S -O test.txt "+"'http://dbtune.org:2105/sparql/?query="+sparqlQueryString+"'"); ResultSet rs = ResultSetFactory.fromXML(xml); @SuppressWarnings("unchecked") List<QuerySolution> l = ResultSetFormatter.toList(rs); for (QuerySolution binding : l) { System.out.println(binding.toString()); } System.out.println("Executing query"); rs = null; //String service = "http://dbtune.org:2105/sparql/"; //QueryEngineHTTP queryExecution = new QueryEngineHTTP(service, sparqlQueryString); try{ SparqlQuery s = new SparqlQuery(sparqlQueryString, new SparqlEndpoint(new URL("http://dbtune.org:2105/sparql/"))); s.send(); //rs = queryExecution.execSelect(); //ByteArrayOutputStream baos = new ByteArrayOutputStream(); //ResultSetFormatter.outputAsJSON(baos, rs); //System.out.println( baos.toString("UTF-8")); } catch (Exception e) { // should never happen as UTF-8 is supported e.printStackTrace(); } }
public static void testJenaAsList(int howOften, String queryString){ SparqlEndpoint sse = SparqlEndpoint.getEndpointDBpedia(); SparqlQuery sqJena = new SparqlQuery(queryString, sse); // first query is not counted //sqJena.getAsList(); sqJena.send(); long now = System.currentTimeMillis(); long tmp = now; for (int i = 0; i < howOften; i++) { // sqJena.getAsList(); ResultSet rs = sqJena.send(); ResultSetFormatter.toList(rs); p("Jena as List needed: " + (System.currentTimeMillis() - tmp)); tmp = System.currentTimeMillis(); } long total=System.currentTimeMillis() - now; System.out.println("Jena as List total: " + total + " ms , average: "+ (total/howOften) ); }
QueryExecution qexec1 = QueryExecutionFactory.create(query1, model) ; ResultSet rs1 = qexec1.execSelect() ; List<QuerySolution> results = ResultSetFormatter.toList(rs1) ; if ( results.size() == 0 ) { QueryExecution qexec2 = QueryExecutionFactory.create(query2, model, qsol1) ; ResultSet rs2 = qexec2.execSelect() ; List<QuerySolution> mapEntries = ResultSetFormatter.toList(rs2) ; QueryExecution qexec3 = QueryExecutionFactory.create(query3, model, qsol1) ; ResultSet rs3 = qexec3.execSelect() ; mapEntries = ResultSetFormatter.toList(rs3) ; QueryExecution qexec4 = QueryExecutionFactory.create(query4, model, qsol1) ; ResultSet rs4 = qexec4.execSelect() ; List<QuerySolution> results4 = ResultSetFormatter.toList(rs4) ; if (results4.size() ==0){ return docDef;
QueryExecution qexec1 = QueryExecutionFactory.create(query1, model) ; ResultSet rs1 = qexec1.execSelect() ; List<QuerySolution> results = ResultSetFormatter.toList(rs1) ; if ( results.size() == 0 ) { QueryExecution qexec2 = QueryExecutionFactory.create(query2, model, qsol1) ; ResultSet rs2 = qexec2.execSelect() ; List<QuerySolution> mapEntries = ResultSetFormatter.toList(rs2) ; QueryExecution qexec3 = QueryExecutionFactory.create(query3, model, qsol1) ; ResultSet rs3 = qexec3.execSelect() ; mapEntries = ResultSetFormatter.toList(rs3) ; QueryExecution qexec4 = QueryExecutionFactory.create(query4, model, qsol1) ; ResultSet rs4 = qexec4.execSelect() ; List<QuerySolution> results4 = ResultSetFormatter.toList(rs4) ; if (results4.size() ==0){ return docDef;
List<QuerySolution> l = ResultSetFormatter.toList(rsw); rsw.reset();
final List<?> expectedList = ResultSetFormatter.toList(expected); if (expected.size() > 10)
QueryExecution qexec1 = QueryExecutionFactory.create(query1, model) ; ResultSet rs1 = qexec1.execSelect() ; List<QuerySolution> results = ResultSetFormatter.toList(rs1) ; if ( results.size() == 0 ) { Log.warn(this, "Failed to find a valid EntityMap for : "+root) ;
QueryExecution qexec1 = QueryExecutionFactory.create(query1, model) ; ResultSet rs1 = qexec1.execSelect() ; List<QuerySolution> results = ResultSetFormatter.toList(rs1) ; if ( results.size() == 0 ) { Log.warn(this, "Failed to find a valid EntityMap for : "+root) ;