public synchronized InputStreamHandle executeSelect(SPARQLQueryDefinition qdef, InputStreamHandle handle, Long offset, Long limit) { if (limit == null) { this.sparqlQueryManager.clearPageLength(); } else { this.sparqlQueryManager.setPageLength(limit); } if (offset != null) { return this.sparqlQueryManager.executeSelect(qdef, handle, offset, currentTransaction); } else { return this.sparqlQueryManager.executeSelect(qdef, handle, currentTransaction); } }
SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString()); String jsonStrResults = sparqlQmgr.executeSelect(qdef, new StringHandle()).get(); ObjectMapper mapper = new ObjectMapper();
SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString()); String jsonStrResults = sparqlQmgr.executeSelect(qdef, new StringHandle()).get(); ObjectMapper mapper = new ObjectMapper();
@Test public void testSPARQLWithLimit() throws Exception { SPARQLQueryDefinition qdef1 = smgr.newQueryDefinition("select ?s ?p ?o { ?s ?p ?o } limit 1"); qdef1.setIncludeDefaultRulesets(false); qdef1.setCollections(graphUri); JsonNode jsonResults = smgr.executeSelect(qdef1, new JacksonHandle()).get(); int numResults = jsonResults.path("results").path("bindings").size(); // because we said 'limit 1' we should only get one result assertEquals(1, numResults); JsonNode firstResult = jsonResults.path("results").path("bindings").path(0); assertEquals(mapper.readTree(expectedFirstResult), firstResult); }
@Test public void testExecuteEmptySelectQuery() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException { System.out.println("In SPARQL Query Manager Test testExecuteEmptySelectQuery method"); SPARQLQueryManager sparqlQmgr = readclient.newSPARQLQueryManager(); StringBuffer sparqlQuery = new StringBuffer().append(""); SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString()); String expectedException = "FailedRequestException"; String exception = ""; try { sparqlQmgr.executeSelect(qdef, new JacksonHandle()).get(); } catch (Exception e) { exception = e.toString(); } System.out.println("Exception thrown from testExecuteEmptySelectQuery is \n" + exception); assertTrue("Test testExecuteEmptySelectQuery method exception is not thrown", exception.contains(expectedException)); }
@Test public void testExecuteSelectQueryNullQDEF() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException { System.out.println("In SPARQL Query Manager Test testExecuteSelectQueryNullQDEF method"); SPARQLQueryManager sparqlQmgr = readclient.newSPARQLQueryManager(); String expectedException = "IllegalArgumentException"; String exception = ""; try { sparqlQmgr.executeSelect(null, new JacksonHandle()).get(); } catch (Exception e) { exception = e.toString(); } System.out.println("Exception thrown from testExecuteSelectQueryNullQDEF is \n" + exception); assertTrue(" Test testExecuteSelectQueryNullQDEF method exception is not thrown", exception.contains(expectedException)); }
JacksonHandle jacksonHandle = new JacksonHandle(); jacksonHandle.setMimetype("application/json"); JsonNode jsonResults = sparqlQmgr.executeSelect(qdef, jacksonHandle).get();
JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get(); System.out.println(jsonStrResults);
JsonNode jsonResults = sparqlQmgr.executeSelect(qdef, new JacksonHandle()).get(); JsonNode jsonBindingsNodes = jsonResults.path("results").path("bindings");
JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get(); System.out.println(jsonStrResults);
String exception = ""; try { JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get(); } catch (Exception e) { exception = e.toString();
qdef1.withBinding("firstname", mbSearchStr, cnLocale); JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get(); System.out.println(jsonStrResults);
qdef1.setBindings(bindings); JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get(); System.out.println(jsonStrResults);
@Test public void testSPARQLWithTwoResults() throws Exception { SPARQLQueryDefinition qdef2 = smgr.newQueryDefinition("select ?s ?p ?o { ?s ?p ?o } limit 100"); qdef2.setIncludeDefaultRulesets(false); qdef2.setCollections(graphUri); JsonNode jsonResults = smgr.executeSelect(qdef2, new JacksonHandle()).get(); JsonNode tuples = jsonResults.path("results").path("bindings"); // loop through the "bindings" array (we would call each row a tuple) for ( int i=0; i < tuples.size(); i++ ) { JsonNode tuple = tuples.get(i); String s = tuple.path("s").path("value").asText(); String p = tuple.path("p").path("value").asText(); String o = tuple.path("o").path("value").asText(); if ( "http://example.org/s1".equals(s) ) { assertEquals("http://example.org/p1", p); assertEquals("http://example.org/o1", o); } else if ( "http://example.org/s2".equals(s) ) { assertEquals("http://example.org/p2", p); assertEquals("http://example.org/o2", o); } else { fail("Unexpected value for s:[" + s + "]"); } } }
qdef1.setBindings(bindings); JsonNode jsonStrResults = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get(); System.out.println(jsonStrResults);
@Test public void testPagination() throws Exception { SPARQLQueryDefinition qdef1 = smgr.newQueryDefinition( "SELECT ?s ?p ?o FROM <" + graphUri + "> { ?s ?p ?o }"); qdef1.setIncludeDefaultRulesets(false); qdef1.setCollections(graphUri); long start = 1; smgr.setPageLength(1); JacksonHandle handle = new JacksonHandle(); handle.setMimetype(SPARQLMimeTypes.SPARQL_JSON); JsonNode results = smgr.executeSelect(qdef1, handle, start).get(); JsonNode bindings = results.path("results").path("bindings"); // because we set pageLength to 1 we should only get one result assertEquals(1, bindings.size()); String uri1 = bindings.get(0).get("s").get("value").asText(); smgr.setPageLength(2); results = smgr.executeSelect(qdef1, new JacksonHandle(), start).get(); // because we set pageLength to 2 we should get two results assertEquals(2, results.path("results").path("bindings").size()); start = 2; results = smgr.executeSelect(qdef1, new JacksonHandle(), start).get(); bindings = results.path("results").path("bindings"); // because we skipped the first result (by setting start=2) there are not enough // results for a full page, so size() only returns 1 assertEquals(1, bindings.size()); String uri2 = bindings.get(0).get("s").get("value").asText(); assertNotEquals(uri1, uri2); }
private ArrayNode executeAndExtractBindings(SPARQLQueryDefinition qdef) { JacksonHandle handle = smgr.executeSelect(qdef, new JacksonHandle()); JsonNode results = handle.get(); ArrayNode bindings = (ArrayNode) results.findPath("results").findPath( "bindings"); return bindings; }
@Test public void testIncludeDefaultInference() { // install default inference. // TODO installDefaultInference("rdfs.rules"); // check query with and without // uninstall default inference. SPARQLQueryDefinition qdef = smgr .newQueryDefinition("select ?o where {?s a ?o . filter (?s = <http://marklogic.com/SPARQLQDefTest/r4> )}"); qdef.setIncludeDefaultRulesets(false); assertFalse(qdef.getIncludeDefaultRulesets()); JacksonHandle handle = smgr.executeSelect(qdef, new JacksonHandle()); JsonNode results = handle.get(); assertEquals("Size of results with no inference", 1, results.get("results").get("bindings").size()); qdef.setIncludeDefaultRulesets(true); handle = smgr.executeSelect(qdef, new JacksonHandle()); results = handle.get(); assertEquals("Size of results with default inference", 3, results.get("results").get("bindings").size()); qdef = smgr .newQueryDefinition( "select ?o where {?s a ?o . filter (?s = <http://marklogic.com/SPARQLQDefTest/r4> )}") .withIncludeDefaultRulesets(false); // TODO removeDefaultInference(); } }
@Test public void testSPARQLWithBindings() throws Exception { SPARQLQueryDefinition qdef3 = smgr.newQueryDefinition("construct { ?s ?p ?o } where { <subjectExample0> ?p ?o } "); qdef3.setIncludeDefaultRulesets(false); qdef3.setCollections(graphUri); StringHandle results1 = smgr.executeConstruct(qdef3, new StringHandle()); SPARQLQueryDefinition qdef4 = smgr.newQueryDefinition("select ?s ?p ?o { ?s ?p ?o . filter (?s = ?b) }"); qdef4.setCollections(graphUri); SPARQLBindings bindings = qdef4.getBindings(); bindings.bind("b", "http://example.org/s1"); qdef4.setBindings(bindings); // or use a builder qdef4 = qdef4.withBinding("c", "http://example.org/o2").withBinding("d", "http://example.org/o3"); DOMHandle handle = new DOMHandle(); handle.setMimetype(SPARQLMimeTypes.SPARQL_XML); Document jsonResults2 = smgr.executeSelect(qdef4, handle).get(); NodeList results = jsonResults2.getDocumentElement().getLastChild().getChildNodes(); // the number of children of the element "bindings" int numResults2 = results.getLength(); // because we said 'filter (?s = ?b)' we should only get one result assertEquals(1, numResults2); Node s = results.item(0).getChildNodes().item(0); Node o = results.item(0).getChildNodes().item(1); Node p = results.item(0).getChildNodes().item(2); assertEquals("http://example.org/s1", s.getTextContent()); assertEquals("http://example.org/p1", o.getTextContent()); assertEquals("http://example.org/o1", p.getTextContent()); }
@Test public void testInference() throws Exception { gmgr.write("/ontology", new StringHandle(ontology).withMimetype("application/n-triples")); SPARQLQueryDefinition qdef = smgr.newQueryDefinition( "SELECT ?s { ?s a <http://example.org/C1> }"); qdef.setIncludeDefaultRulesets(false); StringHandle handle = new StringHandle().withMimetype(SPARQLMimeTypes.SPARQL_CSV); String results = smgr.executeSelect(qdef, handle).get(); assertEquals("%0D%0A", URLEncoder.encode(results, "utf8")); qdef.setRulesets(SPARQLRuleset.RANGE); results = smgr.executeSelect(qdef, handle).get(); assertEquals(1, countLines(parseCsv(results))); qdef.setRulesets(SPARQLRuleset.RANGE, SPARQLRuleset.DOMAIN); results = smgr.executeSelect(qdef, handle).get(); MappingIterator<Map<String,String>> csvRows = parseCsv(results); assertTrue(csvRows.hasNext()); Map<String,String> row = csvRows.next(); assertEquals("http://example.org/o1", row.get("s")); assertTrue(csvRows.hasNext()); row = csvRows.next(); assertEquals("http://example.org/s2", row.get("s")); assertFalse(csvRows.hasNext()); gmgr.delete("/ontology"); }