@Test public void testUsingURI() { // verify default graph String defGraphQuery = "INSERT { GRAPH <http://marklogic.com/SPARQLQDefTest/g3> " + "{ <http://marklogic.com/SPARQLQDefTest/r3> " + "<http://marklogic.com/SPARQLQDefTest/p3> " + "<http://marklogic.com/SPARQLQDefTest/o3> } } " + "WHERE { <http://marklogic.com/SPARQLQDefTest/r1> <http://marklogic.com/SPARQLQDefTest/p3> ?o }"; String defCheckQuery = "ASK WHERE { <http://marklogic.com/SPARQLQDefTest/r3> <http://marklogic.com/SPARQLQDefTest/p3> <http://marklogic.com/SPARQLQDefTest/o3> }"; SPARQLQueryDefinition qdef = smgr.newQueryDefinition(defGraphQuery); qdef.setUsingGraphUris("http://marklogic.com/SPARQLQDefTest/g1"); smgr.executeUpdate(qdef); SPARQLQueryDefinition checkDef = smgr.newQueryDefinition(defCheckQuery); checkDef.setDefaultGraphUris("http://marklogic.com/SPARQLQDefTest/g3"); assertTrue(smgr.executeAsk(checkDef)); // clean up smgr.executeUpdate(smgr.newQueryDefinition("DROP GRAPH <http://marklogic.com/SPARQLQDefTest/g3>")); assertFalse(smgr.executeAsk(checkDef)); }
public InputStreamHandle executeConstruct(SPARQLQueryDefinition qdef, InputStreamHandle handle) { return this.sparqlQueryManager.executeConstruct(qdef, handle, currentTransaction); }
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); } }
public synchronized void executeUpdate(SPARQLQueryDefinition qdef) { this.sparqlQueryManager.clearPageLength(); this.sparqlQueryManager.executeUpdate(qdef, currentTransaction); }
@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); }
SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlCreateGraphQuery.toString()); sparqlQmgr.executeUpdate(qdef); qdef = null; qdef = sparqlQmgr.newQueryDefinition(sparqlInsertData.toString()); sparqlQmgr.executeUpdate(qdef); sparqlExists.append(newline); sparqlExists.append("}"); qdef = sparqlQmgr.newQueryDefinition(sparqlExists.toString()); JsonNode jsonResults = sparqlQmgr.executeSelect(qdef, new JacksonHandle()).get(); JsonNode jsonBindingsNodes = jsonResults.path("results").path("bindings"); qdef = sparqlQmgr.newQueryDefinition(sparqlCreateGraphQuery.toString()); sparqlQmgr.executeUpdate(qdef); } catch (Exception e) { exception = e.toString(); qdef = sparqlQmgr.newQueryDefinition(sparqlCreateGraphSilentQuery.toString()); String expectedSilentException = ""; String exceptionSilent = ""; try { sparqlQmgr.executeUpdate(qdef); } catch (Exception e) {
@Test public void testBaseUri() { // verify base has expected effect String relativeConstruct = "CONSTRUCT { <relative1> <relative2> <relative3> } \n" + "WHERE { ?s ?p ?o . } LIMIT 1"; SPARQLQueryDefinition qdef = smgr.newQueryDefinition(relativeConstruct); qdef.setBaseUri("http://marklogic.com/SPARQLQDefTest/"); JsonNode rdf = smgr.executeConstruct(qdef, new JacksonHandle()).get(); String subject = rdf.fieldNames().next(); assertEquals("base uri plus relative subject uri", "http://marklogic.com/SPARQLQDefTest/relative1", subject); String predicate = rdf.get(subject).fieldNames().next(); assertEquals("base uri plus relative predicate uri", "http://marklogic.com/SPARQLQDefTest/relative2", predicate); JsonNode objects = rdf.get(subject).get(predicate); assertEquals(1, objects.size()); assertEquals("base uri plus relative uri", "http://marklogic.com/SPARQLQDefTest/relative3", objects.path(0).path("value").asText()); }
sparqlQueryTrue.append(newline); sparqlQueryTrue.append("ASK { ?alum foaf:schoolHomepage <http://www.ucsb.edu/> }"); SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQueryTrue.toString()); boolean bAskTrue = sparqlQmgr.executeAsk(qdef); sparqlQueryfalse.append(newline); sparqlQueryfalse.append("ASK { ?alum foaf:schoolHomepage <http://www.blahblah.edu/> }"); SPARQLQueryDefinition qdef1 = sparqlQmgr.newQueryDefinition(sparqlQueryfalse.toString()); boolean bAskFalse = sparqlQmgr.executeAsk(qdef1); sparqlQueryEmpty.append(newline); sparqlQueryEmpty.append("ASK { }"); SPARQLQueryDefinition qdefEmpty = sparqlQmgr.newQueryDefinition(sparqlQueryEmpty.toString()); boolean bAskEmpty = sparqlQmgr.executeAsk(qdefEmpty);
SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString()); sparqlQmgr.setPageLength(1); String jsonStrResults = sparqlQmgr.executeSelect(qdef, new StringHandle(), 1, t).get(); ObjectMapper mapper = new ObjectMapper(); sparqlQmgr.setPageLength(1); String jsonStrResults2 = sparqlQmgr.executeSelect(qdef, new StringHandle(), 2, t).get(); sparqlQmgr.setPageLength(3); String jsonStrResults3 = sparqlQmgr.executeSelect(qdef, new StringHandle(), 2, t).get(); assertEquals("Result returned from testPaginationInTransaction Page Length incorrect", 3, sparqlQmgr.getPageLength()); sparqlQmgr.clearPageLength(); assertEquals("Result returned from testPaginationInTransaction Page Length incorrect", -1, sparqlQmgr.getPageLength()); sparqlQmgr.setPageLength(2); String jsonStrResultsNeg1 = sparqlQmgr.executeSelect(qdef, new StringHandle(), 21, t).get(); sparqlQmgr.setPageLength(100); String jsonStrResultsNeg2 = sparqlQmgr.executeSelect(qdef, new StringHandle(), 100, t).get(); sparqlQmgr.setPageLength(-1); String jsonStrResultsNeg3 = sparqlQmgr.executeSelect(qdef, new StringHandle(), -1, t).get(); } catch (Exception e) { exception = e.toString();
sparqldropQuery.append(newline); SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqldropQuery.toString()); try { sparqlQmgr.executeUpdate(qdef); } catch (Exception e) { exception = e.toString(); sparqlCreateQuery.append(newline); qdef = sparqlQmgr.newQueryDefinition(sparqlCreateQuery.toString()); sparqlQmgr.executeUpdate(qdef); qdef = sparqlQmgr.newQueryDefinition(sparqldropQuery.toString()); String expectedNamedException = ""; String exceptionNamed = ""; try { sparqlQmgr.executeUpdate(qdef); } catch (Exception e) { exception = e.toString();
@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()); }
SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString()); String jsonResults = sparqlQmgr.executeDescribe(qdef, new StringHandle(), t).get(); SPARQLQueryDefinition qdefInvalid = sparqlQmgr.newQueryDefinition(sparqlInvalidQuery.toString()); String jsonInvalidResults = sparqlQmgr.executeDescribe(qdefInvalid, new StringHandle(), t).get(); ObjectMapper mapper = new ObjectMapper(); String expectedException = "FailedRequestException"; String exception = ""; SPARQLQueryDefinition qdefInvalid1 = sparqlQmgr.newQueryDefinition(sparqlInvalidQuery1.toString()); try { sparqlQmgr.executeDescribe(qdefInvalid1, new StringHandle(), t).get(); } catch (Exception e) { exception = e.toString();
/** * Create a new {@link com.marklogic.client.semantics.SPARQLQueryDefinition} * from a query String. You can use the resulting object to configure * various aspects of the query or set binding variables. * * @param queryString * A SPARQL Query or SPARQL Update. * @return A new * {@link com.marklogic.client.semantics.SPARQLQueryDefinition} */ public SPARQLQueryDefinition newQueryDefinition(String queryString) { return this.sparqlQueryManager.newQueryDefinition(queryString); }
@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)); }
public boolean executeAsk(SPARQLQueryDefinition qdef) { return this.sparqlQueryManager.executeAsk(qdef, currentTransaction); }
public InputStreamHandle executeDescribe(SPARQLQueryDefinition qdef, InputStreamHandle handle) { return this.sparqlQueryManager.executeDescribe(qdef, handle, currentTransaction); }
@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)); }
try { tx = Common.client.openTransaction(); smgr.executeUpdate(smgr.newQueryDefinition(q1), tx); .executeSelect( sparqlManagerReader .newQueryDefinition("select ?o where { <s1> <p1> ?o }"), new StringHandle()); assertEquals("Empty result outside transaction", "{\"head\":{\"vars\":[]},\"results\":{\"bindings\":[]}}", handle.get()); .executeSelect( sparqlManagerReader .newQueryDefinition("select ?o where { <s1> <p1> ?o }"), new StringHandle(), tx); assertEquals("writer must see effects within transaction.", "{\"head\":{\"vars\":[\"o\"]},\"results\":{\"bindings\":[{\"o\":{\"type\":\"uri\",\"value\":\"o1\"}}]}}", handle.get()); .executeSelect( sparqlManagerReader .newQueryDefinition("select ?o where { <s1> <p1> ?o }"), new StringHandle()); assertEquals("Empty result after rollback", "{\"head\":{\"vars\":[]},\"results\":{\"bindings\":[]}}", handle.get()); smgr.executeUpdate(smgr.newQueryDefinition(q1), tx); smgr.executeUpdate(smgr.newQueryDefinition(q2), tx); .executeSelect( sparqlManagerReader .newQueryDefinition("select ?o where { <s1> <p1> ?o }"), new StringHandle());
@Test public void testExecuteConstruct() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException { System.out.println("In SPARQL Query Manager Test testExecuteConstruct method"); SPARQLQueryManager sparqlQmgr = readclient.newSPARQLQueryManager(); StringBuffer sparqlQuery = new StringBuffer().append(" PREFIX foaf: <http://xmlns.com/foaf/0.1/>"); sparqlQuery.append(newline); sparqlQuery.append("CONSTRUCT {<http://www.ucsb.edu/random-alum> foaf:knows ?alum }"); sparqlQuery.append(newline); sparqlQuery.append("where"); sparqlQuery.append(newline); sparqlQuery.append("{ ?alum foaf:schoolHomepage <http://www.ucsb.edu/> }"); SPARQLQueryDefinition qdef = sparqlQmgr.newQueryDefinition(sparqlQuery.toString()); // Execute Construct query String[] jsonResults = sparqlQmgr.executeConstruct(qdef, new StringHandle()).get().split(" "); // Account for the dot at the end of the triple. Hence size is 4. assertEquals("Method testExecuteConstruct in-memory Triple part size is incorrect", 4, jsonResults.length); assertEquals("Method testExecuteConstruct in-memory subject is incorrect", "<http://www.ucsb.edu/random-alum>", jsonResults[0]); assertEquals("Method testExecuteConstruct in-memory predicate is incorrect", "<http://xmlns.com/foaf/0.1/knows>", jsonResults[1]); assertEquals("Method testExecuteConstruct in-memory object is incorrect", "<1bfbfb8:ff2d706919:-7fa9>", jsonResults[2]); }