@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 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 testNamedGraphUris() { SPARQLQueryDefinition qdef = smgr .newQueryDefinition("SELECT ?s where { GRAPH ?g { ?s a ?o } }"); qdef.setIncludeDefaultRulesets(false); qdef.setNamedGraphUris("http://marklogic.com/SPARQLQDefTest/g3"); ArrayNode bindings = executeAndExtractBindings(qdef); assertEquals("From named 0 result assertions", 0, bindings.size()); qdef.setNamedGraphUris("http://marklogic.com/SPARQLQDefTest/g4"); bindings = executeAndExtractBindings(qdef); assertEquals("From named 1 result assertions", 1, bindings.size()); qdef.setNamedGraphUris("http://marklogic.com/SPARQLQDefTest/g4", "http://marklogic.com/SPARQLQDefTest/g2"); bindings = executeAndExtractBindings(qdef); assertEquals("From named 1 result assertions", 2, bindings.size()); }
qdef.setIncludeDefaultRulesets(false); qdef.setIncludeDefaultRulesets(true); qdef.setSparql(sparqlInferQuery.toString()); assertEquals("One Ruleset should have been returned from testInferenceAndRuleSet method ", 1, qdef.getRulesets().length); qdef.setIncludeDefaultRulesets(true); qdef.setSparql(sparqlInferQuery.toString()); assertEquals("Two Rulesets should have been returned from testInferenceAndRuleSet method ", 2, qdef.getRulesets().length);
@Test public void testDefaultURI() { // verify default graph String defGraphQuery = "SELECT ?s WHERE { ?s a ?o }"; SPARQLQueryDefinition qdef = smgr.newQueryDefinition(defGraphQuery); qdef.setIncludeDefaultRulesets(false); qdef.setOptimizeLevel(1); // this only tests whether optimize level is set, not sent to server // I'm not sure how to test that assertEquals(1, qdef.getOptimizeLevel()); ArrayNode bindings = executeAndExtractBindings(qdef); qdef.setDefaultGraphUris("http://marklogic.com/SPARQLQDefTest/g4"); bindings = executeAndExtractBindings(qdef); assertEquals("Single graphs has one assertion", 1, bindings.size()); qdef.setDefaultGraphUris("http://marklogic.com/SPARQLQDefTest/g4", "http://marklogic.com/SPARQLQDefTest/g2"); bindings = executeAndExtractBindings(qdef); assertEquals("Union two default graphs has two assertions", 2, bindings.size()); }
qdef.setIncludeDefaultRulesets(false); qdef.setCollections(localGraphUri); qdef.setConstrainingQueryDefinition(queryMgr.newStringDefinition().withCriteria("test1"));
@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"); }