public String executeQuery(String queryString) { /* //System.out.println(queryString); Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); ResultSetFormatter.out(ostream, results, query); //ResultSetFormatter.out(System.out, results, query); String r = ""; try{ r = new String(ostream.toByteArray(), "UTF-8"); //System.out.println(r); } catch(Exception e){ System.out.println(e.getMessage()); } qe.close(); return r; */ //// new added for test JSON output try { Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); ResultSetFormatter.outputAsJSON(ostream, results); // ResultSetFormatter.out(ostream, results, query); // ResultSetFormatter.out(System.out, results, query); String r = ""; try { r = new String(ostream.toByteArray(), "UTF-8"); // System.out.println(r); } catch (Exception e) { System.out.println(e.getMessage()); } qe.close(); return r; } catch (Exception e) { System.out.println(e.toString()); return ""; } }
public ArrayList<QuerySolution> executeQueryRaw(String queryString) { // System.out.println(queryString); try { Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); /* ByteArrayOutputStream ostream = new ByteArrayOutputStream(); ResultSetFormatter.out(ostream, results, query); //ResultSetFormatter.out(System.out, results, query); String r = ""; try{ r = new String(ostream.toByteArray(), "UTF-8"); System.out.println(r); } catch(Exception e){ System.out.println(e.getMessage()); } */ /* ArrayList<QuerySolution> resList = new ArrayList<QuerySolution>(); if(results.hasNext()) { QuerySolution qs = results.next(); resList.add(qs); //double x = qs.getLiteral("x").getFloat(); //Literal y = qs.getLiteral("y"); //Literal theta = qs.getLiteral("theta"); } */ ArrayList<QuerySolution> resList = (ArrayList) ResultSetFormatter.toList(results); qe.close(); return resList; // results; } catch (Exception e) { System.out.println(e.toString()); return new ArrayList<QuerySolution>(); } }