SPARQL { public static Results executeSelect(final String query, final String endpoint) { QueryExecutionFactory qef = new QueryExecutionFactoryHttp(endpoint); QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); Results res = new Results(); res.header.addAll(rs.getResultVars()); while(rs.hasNext()) { QuerySolution sol = rs.nextSolution(); res.table.add(new ArrayList<String>()); for(String head: res.header) { String answer = ""; if(sol.get(head).isResource()) { answer = sol.getResource(head).toString(); } else { String temp = sol.get(head).toString(); if(temp.contains("@")) { answer = "\"" + temp.substring(0, temp.indexOf("@")) + "\"" + temp.substring(temp.indexOf("@")); } else if (temp.contains("^^")){ answer = "\"" + temp.substring(0, temp.indexOf("^")) + "\"^^<" + temp.substring(temp.indexOf("^")+2) + ">"; } else { answer = temp; } } res.table.get(res.table.size()-1).add(answer); } } closeExecFactory(qef); return res; } SPARQL();  SPARQL(final String endpoint); Set<RDFNode> sparql(final String query); static Set<String> extractAnswerStrings(final Set<RDFNode> answers); static Results executeSelect(final String query, final String endpoint); static Boolean executeAsk(final String query, final String endpoint); long getCacheTimeToLive(); void setCacheTimeToLive(final long timeToLive); static boolean isValidSparqlQuery(final String sparql); static boolean isEndpointAlive(final String endpoint); public QueryExecutionFactory qef; }
@Test public void testResults() { Results res = SPARQL.executeSelect("SELECT ?s ?o FROM <http: assertTrue(res.header.size() == 2); assertTrue(res.header.contains("s")); assertTrue(res.header.contains("o")); List<String> row = res.table.get(0); assertTrue(row.get(res.header.indexOf("o")).equals("http: assertTrue(row.get(res.header.indexOf("s")).equals("http: row = res.table.get(1); assertTrue(row.get(res.header.indexOf("o")).equals("http: assertTrue(row.get(res.header.indexOf("s")).equals("http: }
@Test public void testSelect() { Set<String> resources = SPARQL.executeSelect("SELECT ?s FROM <http: Object[] actual = resources.toArray(); Arrays.sort(actual); assertArrayEquals(getGoldenArray1(), actual); resources = SPARQL.executeSelect("SELECT ?o FROM <http: actual = resources.toArray(); Arrays.sort(actual); assertArrayEquals(getGoldenArray2(), actual); }
@Test public void testResultsSet() { Results res = SPARQL.executeSelect("PREFIX foaf: <http: String[] expecteds = new String[4]; expecteds[0] = "http: expecteds[1] = "\"Academy Award\"@en"; expecteds[2] = "http: expecteds[3] = "\"Aggregation\"@en"; Object[] actuals = res.getStringSet().toArray(); Arrays.sort(expecteds); Arrays.sort(actuals); assertArrayEquals(expecteds, actuals); }