public InputStreamHandle executeConstruct(SPARQLQueryDefinition qdef, InputStreamHandle handle) { return this.sparqlQueryManager.executeConstruct(qdef, handle, currentTransaction); }
@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()); }
@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]); }
JsonNode jsonResults = sparqlQmgr.executeConstruct(qdef, jh).get(); String s = jsonResults.fieldNames().next(); String p = jsonResults.get(s).fieldNames().next(); Document xmlDoc = sparqlQmgr.executeConstruct(qdef, handle).get(); Node description = xmlDoc.getFirstChild().getFirstChild(); NamedNodeMap attrs = description.getFirstChild().getAttributes();
@Test public void testDescribe() { // verify base has expected effect String relativeConstruct = "DESCRIBE <http://example.org/s1>"; SPARQLQueryDefinition qdef = smgr.newQueryDefinition(relativeConstruct); Document rdf = smgr.executeConstruct(qdef, new DOMHandle()).get(); Node description = rdf.getFirstChild().getFirstChild(); assertNotNull(description.getAttributes()); assertEquals("subject", "http://example.org/s1", description.getAttributes().item(0).getTextContent()); assertNotNull(description.getFirstChild()); assertEquals("predicate", "p1", description.getFirstChild().getNodeName()); assertEquals("predicate namespace", "http://example.org/", description.getFirstChild().getNamespaceURI()); NamedNodeMap attrs = description.getFirstChild().getAttributes(); assertNotNull(attrs); assertNotNull(attrs.getNamedItemNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "resource")); assertEquals("object", "http://example.org/o1", attrs.getNamedItemNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "resource").getTextContent()); }
String[] jsonResults = sparqlQmgr.executeConstruct(qdef, new StringHandle().withMimetype(RDFMimeTypes.NTRIPLES), t).get().split(" "); String strRDFJSON = sparqlQmgr.executeConstruct(qdef, new StringHandle().withMimetype(RDFMimeTypes.RDFJSON), t).get().toString(); System.out.println("\n RDFJSON format is " + strRDFJSON); assertEquals("RDFJSON value output is incorrect ", "1bfbfb8:ff2d706919:-7fa9", strValue); String strRDFXML = sparqlQmgr.executeConstruct(qdef, new StringHandle().withMimetype(RDFMimeTypes.RDFXML), t).get().toString(); assertTrue("RDFXML object value output is incorrect ", strRDFXML.contains("knows rdf:resource=\"1bfbfb8:ff2d706919:-7fa9\"")); String strTRIPLEXML = sparqlQmgr.executeConstruct(qdef, new StringHandle().withMimetype(RDFMimeTypes.TRIPLEXML), t).get().toString(); System.out.println("\n TRIPLEXML format is " + strTRIPLEXML); assertTrue("TRIPLEXML subject value output is incorrect ", strTRIPLEXML.contains("<sem:subject>http://www.ucsb.edu/random-alum</sem:subject>")); assertTrue("TRIPLEXML object value output is incorrect ", strTRIPLEXML.contains("<sem:object>1bfbfb8:ff2d706919:-7fa9</sem:object>")); String strTURTLE = sparqlQmgr.executeConstruct(qdef, new StringHandle().withMimetype(RDFMimeTypes.TURTLE), t).get().toString(); System.out.println("\n TURTLE format is " + strTURTLE); assertTrue("TURTLE subject value output is incorrect ", strTURTLE.contains("<http://www.ucsb.edu/random-alum>")); String strNTriples = sparqlQmgr.executeConstruct(qdef, new StringHandle().withMimetype(RDFMimeTypes.NTRIPLES), t).get().toString(); System.out.println("\n NTRIPLES format is " + strNTriples); assertTrue("NTRIPLES subject value output is incorrect ", strNTriples.contains("<http://www.ucsb.edu/random-alum>"));
@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()); }