@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 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 + "]"); } } }
@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); }
@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()); }
qdef.setCollections("my-collections1"); qdef.setCollections("my-collections2"); qdef.setDirectory("my-Directory");
qdef.setCollections(localGraphUri); qdef.setConstrainingQueryDefinition(queryMgr.newStringDefinition().withCriteria("test1")); JsonNode jsonResults = smgr.executeSelect(qdef, new JacksonHandle()).get();