/** * Converts from JSON to internal Jena format. * * @param json * A JSON representation if a SPARQL query result. * @return A Jena ResultSet. */ public static ResultSetRewindable convertJSONtoResultSet(String json) { ByteArrayInputStream bais = new ByteArrayInputStream(json .getBytes(Charset.forName("UTF-8"))); // System.out.println("JSON " + json); return ResultSetFactory.makeRewindable(ResultSetFactory.fromJSON(bais)); }
public static ResultSet create(List<String> varNames, Iterator<Binding> bindingIt) { QueryIterator queryIter = new QueryIterPlainWrapper(bindingIt); ResultSet result = ResultSetFactory.create(queryIter, varNames); return result; }
ResultSet resultSet = ResultSetFactory.fromXML(in); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); switch (resultFormat) { case CSV: ResultSetFormatter.outputAsCSV(outputStream, resultSet); break; case TEXT: ResultSetFormatter.out(outputStream, resultSet); break; case JSON: ResultSetFormatter.outputAsJSON(outputStream, resultSet); break; case XML: throw new RDFServiceException("unrecognized result format"); InputStream result = new ByteArrayInputStream( outputStream.toByteArray()); return result;
@Override public void handle(String baseIRI, HttpResponse response) throws IOException { String ct = contentType(response); ResultsFormat fmt = contentTypeToResultsFormat(ct); InputStream in = response.getEntity().getContent(); rs = ResultSetFactory.load(in, fmt); // Force reading rs = ResultSetFactory.copyResults(rs); }
public static ResultSet parseResultSet(final String resultURI) throws IOException { if (resultURI.endsWith("srx")) return ResultSetFactory.fromXML(URI.create(resultURI).toURL().openStream()); else if (resultURI.endsWith("ttl")) return ResultSetFactory.load(resultURI, ResultsFormat.FMT_RDF_TTL); else if (resultURI.endsWith("rdf")) return ResultSetFactory.load(resultURI, ResultsFormat.FMT_RDF_XML); else throw new OpenError("Unknown format."); }
@Test public void test_RS_2() { ResultSetRewindable rs1 = makeRewindable("x", org.apache.jena.graph.NodeFactory.createURI("tag:local")) ; ByteArrayOutputStream arr = new ByteArrayOutputStream() ; ResultSetFormatter.outputAsXML(arr, rs1) ; rs1.reset() ; ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray()) ; ResultSet rs2 = ResultSetFactory.fromXML(ins) ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
@Test public void test_RS_6() { ResultSetRewindable rs1 = make2Rewindable("x", org.apache.jena.graph.NodeFactory.createURI("tag:local")) ; ByteArrayOutputStream arr = new ByteArrayOutputStream() ; ResultSetFormatter.outputAsJSON(arr, rs1) ; rs1.reset() ; ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray()) ; ResultSet rs2 = ResultSetFactory.fromJSON(ins) ; // Test using the DAWG examples assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
private static ResultSetRewindable test(ResultSetRewindable resultSet) { resultSet.reset(); ByteArrayOutputStream out = new ByteArrayOutputStream() ; BinRDF.writeResultSet(out, resultSet, true) ; resultSet.reset(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()) ; ResultSet rs$ = BinRDF.readResultSet(in) ; ResultSetRewindable resultSet2 = ResultSetFactory.makeRewindable(rs$) ; // Includes bnode labels. ResultSetCompare.equalsExact(resultSet, resultSet2) ; resultSet.reset(); resultSet2.reset(); return resultSet2 ; }
@Override public InputStream sparqlSelectQuery(String query, ResultFormat resultFormat) throws RDFServiceException { ResultSet rs = ResultSetFactory.load( s.sparqlSelectQuery(query, resultFormat), RDFServiceUtils.getJenaResultSetFormat(resultFormat)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); switch (resultFormat) { case CSV: ResultSetFormatter.outputAsCSV(outputStream,resultSet); break; case TEXT: ResultSetFormatter.out(outputStream,resultSet); break; case JSON: ResultSetFormatter.outputAsJSON(outputStream, resultSet); break; case XML: throw new RDFServiceException("unrecognized result format"); return new ByteArrayInputStream(outputStream.toByteArray());
private void test_RS_fmt(ResultSet rs, ResultsFormat fmt, boolean ordered) { ResultSetRewindable rs1 = ResultSetFactory.makeRewindable(rs) ; ByteArrayOutputStream arr = new ByteArrayOutputStream() ; ResultSetFormatter.output(arr, rs1, fmt) ; byte bytes[] = arr.toByteArray() ; rs1.reset() ; ByteArrayInputStream ins = new ByteArrayInputStream(bytes) ; ResultSetRewindable rs2 = ResultSetFactory.makeRewindable(ResultSetFactory.load(ins, fmt)) ; // Ordered? Unordered? boolean b = ResultSetCompare.equalsByTerm(rs1, rs2) ; if ( ordered ) { rs1.reset() ; rs2.reset() ; b = b & ResultSetCompare.equalsByTerm(rs1, rs2) ; } if ( !b ) { System.out.println(new String(bytes)) ; rs1.reset() ; rs2.reset() ; ResultSetFormatter.out(rs1) ; ResultSetFormatter.out(rs2) ; } assertTrue(b) ; } // Test comparison
ResultFormat resultFormat) throws RDFServiceException { log.debug("sparqlSelectQuery: " + query.replaceAll("\\s+", " ")); ResultSet resultSet = ResultSetFactory.fromJSON( s.sparqlSelectQuery(query, RDFService.ResultFormat.JSON)); List<QuerySolution> solnList = getSolutionList(resultSet); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); switch (resultFormat) { case CSV: ResultSetFormatter.outputAsCSV(outputStream, filtered); break; case TEXT: ResultSetFormatter.out(outputStream, filtered); break; case JSON: ResultSetFormatter.outputAsJSON(outputStream, filtered); break; case XML: throw new RDFServiceException("unrecognized result format"); return new ByteArrayInputStream(outputStream.toByteArray());
@Test public void test_RS_2_str() { ResultSetRewindable rs1 = makeRewindable("x", org.apache.jena.graph.NodeFactory.createURI("tag:local")) ; String x = ResultSetFormatter.asXMLString(rs1) ; rs1.reset() ; InputStream in = new ByteArrayInputStream(StrUtils.asUTF8bytes(x)); ResultSet rs2 = ResultSetFactory.fromXML(in) ; assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ; }
ResultSetFactory factory = new ResultSetFactory(); InputStream in = new ByteArrayInputStream(jsonAnswer.getBytes()); ResultSet result = factory.fromJSON(in); List<String> var = result.getResultVars(); List<String> list = new ArrayList<>();
private void parseJSON(String input) { byte[] b = StrUtils.asUTF8bytes(input); ByteArrayInputStream in = new ByteArrayInputStream(b); ResultSet rs = ResultSetFactory.fromJSON(in); while (rs.hasNext()) { rs.nextBinding(); } }
@Test public void test_RS_4() { ResultSetRewindable rs1 = makeRewindable("x", org.apache.jena.graph.NodeFactory.createURI("tag:local")) ; Model model = RDFOutput.encodeAsModel(rs1) ; rs1.reset() ; ResultSetRewindable rs2 = ResultSetFactory.makeRewindable(RDFInput.fromRDF(model)) ; boolean b = ResultSetCompare.equalsByTerm(rs1, rs2) ; if ( ! b ) { rs1.reset() ; rs2.reset() ; ResultSetFormatter.out(rs1) ; ResultSetFormatter.out(rs2) ; } assertTrue(b) ; }
private static ResultSetRewindable unique(ResultSetRewindable results) { // VERY crude. Utilises the fact that bindings have value equality. List<Binding> x = new ArrayList<>() ; Set<Binding> seen = new HashSet<>() ; for ( ; results.hasNext() ; ) { Binding b = results.nextBinding() ; if ( seen.contains(b) ) continue ; seen.add(b) ; x.add(b) ; } QueryIterator qIter = new QueryIterPlainWrapper(x.iterator()) ; ResultSet rs = new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter) ; return ResultSetFactory.makeRewindable(rs) ; }
private static void same(ResultSet rs1, ResultSet rs2, boolean result) { ResultSetRewindable rsw1 = ResultSetFactory.makeRewindable(rs1) ; ResultSetRewindable rsw2 = ResultSetFactory.makeRewindable(rs2) ; boolean b = ResultSetCompare.equalsByValue(rsw1, rsw2) ; if ( b != result) { System.out.println("Different: ") ; rsw1.reset() ; rsw2.reset() ; ResultSetFormatter.out(rsw1) ; ResultSetFormatter.out(rsw2) ; System.out.println() ; } assertTrue(b == result) ; }
public static void main(String[] args) { FileManager.get().addLocatorClassLoader(ExampleARQ_02.class.getClassLoader()); Model model = FileManager.get().loadModel("data/data.ttl"); String queryString = "SELECT * { ?s ?p ?o }"; Query query = QueryFactory.create(queryString); QueryExecution qexec = QueryExecutionFactory.create(query, model); try { ResultSetRewindable results = ResultSetFactory.makeRewindable(qexec.execSelect()); System.out.println("---- XML ----"); ResultSetFormatter.outputAsXML(System.out, results); results.reset(); System.out.println("---- Text ----"); ResultSetFormatter.out(System.out, results); results.reset(); System.out.println("\n---- CSV ----"); ResultSetFormatter.outputAsCSV(System.out, results); results.reset(); System.out.println("\n---- TSV ----"); ResultSetFormatter.outputAsTSV(System.out, results); results.reset(); System.out.println("\n---- JSON ----"); ResultSetFormatter.outputAsJSON(System.out, results); results.reset(); } finally { qexec.close(); } }
ResultSetRewindable rsrw = ResultSetFactory.copyResults(rs); int size = rsrw.size(); rsrw.reset(); int i = 1; while(rsrw.hasNext()){ QuerySolution qs = rsrw.next(); if(qs.getResource("type").isURIResource()){
public static boolean isEqual(ResultSet actual, ResultSet expected) { boolean isEqual = true; ResultSetRewindable act = null; ResultSetRewindable exp = null; if (expected != null) { // TODO File in JIRA issue: This impl is bugged (look at the code, // rs1 should be rs1a) act = ResultSetFactory.makeRewindable(actual); exp = ResultSetFactory.makeRewindable(expected) ; isEqual = ResultSetCompare.equalsByTerm(act, exp); act.reset(); exp.reset(); } if(!isEqual) { String eStr = ResultSetFormatter.asText(exp); System.out.println("Expected"); System.out.println("------"); System.out.println(eStr); String aStr = ResultSetFormatter.asText(act); System.out.println("Actual"); System.out.println("------"); System.out.println(aStr); } return isEqual; }