if (start > 1) params.add("start", Long.toString(start)); if (pageLength >= 0) params.add("pageLength", Long.toString(pageLength)); if (qdef.getOptimizeLevel() >= 0) { params.add("optimize", Integer.toString(qdef.getOptimizeLevel())); if (qdef.getCollections() != null ) { for ( String collection : qdef.getCollections() ) { params.add("collection", collection); addPermsParams(params, qdef.getUpdatePermissions()); String sparql = qdef.getSparql(); SPARQLBindings bindings = qdef.getBindings(); for ( Map.Entry<String,List<SPARQLBinding>> entry : bindings.entrySet() ) { String paramName = "bind:" + entry.getKey(); QueryDefinition constrainingQuery = qdef.getConstrainingQueryDefinition(); StructureWriteHandle input; if ( constrainingQuery != null ) { if (qdef.getOptionsName()!= null && qdef.getOptionsName().length() > 0) { params.add("options", qdef.getOptionsName()); if (qdef.getBaseUri() != null) { params.add("base", qdef.getBaseUri()); if (qdef.getDefaultGraphUris() != null) { for (String defaultGraphUri : qdef.getDefaultGraphUris()) { params.add("default-graph-uri", defaultGraphUri); if (qdef.getNamedGraphUris() != null) {
private SPARQLQueryDefinition prepareQueryDefinition(Query query) { JenaDatabaseClient client = markLogicDatasetGraph.getDatabaseClient(); SPARQLQueryDefinition qdef = client.newQueryDefinition(query.toString()); if (query.getBaseURI() != null) { qdef.setBaseUri(query.getBaseURI()); } else { query.setBaseURI((String) null); } if (markLogicDatasetGraph.getRulesets() != null) { qdef.setRulesets(markLogicDatasetGraph.getRulesets()); } bindVariables(qdef, this.initial, markLogicDatasetGraph); QueryDefinition constrainingQueryDefinition = markLogicDatasetGraph.getConstrainingQueryDefinition(); qdef.setConstrainingQueryDefinition(constrainingQueryDefinition); return qdef; }
@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()); }
sb.append("SELECT ?s ?p ?o where { ?s ?p ?o ."); if (s2 != Node.ANY) { qdef.withBinding("a", s2.getURI()); sb.append("FILTER (?s = ?a) "); qdef.withBinding("b", p2.getURI()); sb.append("FILTER (?p = ?b) "); qdef.setSparql(sb.toString()); qdef.setDefaultGraphUris(graphName); InputStreamHandle results = client.executeSelect(qdef, new InputStreamHandle());
sb.append("SELECT ?g ?s ?p ?o where {GRAPH ?g { ?s ?p ?o }"); if (s1 != Node.ANY) { qdef.withBinding("a", s1.getURI()); sb.append("FILTER (?s = ?a) "); qdef.withBinding("b", p1.getURI()); sb.append("FILTER (?p = ?b) "); qdef.setSparql(sb.toString()); InputStreamHandle results = client.executeSelect(qdef, new InputStreamHandle());
qdef.setSparql(sparqlCreateQuery.toString()); qdef.setCollections("my-collections1"); qdef.setCollections("my-collections2"); qdef.setDirectory("my-Directory"); qdef.setUpdatePermissions(graphPermissions); for (String collections : qdef.getCollections()) assertTrue("QueryDefinition Collections incorrectlty set ", collections.contains("my-collections1") || collections.contains("my-collections2")); assertEquals("QueryDefinition Directory incorrectlty set ", "my-Directory", qdef.getDirectory()); assertNull("QueryDefinition DefaultRulesets incorrectlty set. Should be Null.", qdef.getIncludeDefaultRulesets()); assertTrue("QueryDefinition Bindings incorrectlty set. Should be Empty.", qdef.getBindings().isEmpty()); assertNull("QueryDefinition Options incorrectlty set. Should be Null.", qdef.getOptionsName()); qdef.setDirectory("TestMarkLogic"); qdef = sparqlQmgr.newQueryDefinition(); strWriteHandle.with("COPY <OriginalGraph> TO <CopiedGraph>"); qdef.setSparql(strWriteHandle); sparqlQmgr.executeUpdate(qdef);
SPARQLBindings bindings = qdef1.getBindings(); bindings.bind("firstname", "Ling", RDFTypes.STRING); qdef1.setBindings(bindings); assertTrue("Bindings Map do not have expected key ", qdef1.getBindings().containsKey("firstname"));
qdef1.withBinding("firstname", "Ling", RDFTypes.STRING); assertTrue("Bindings Map do not have expected key ", qdef1.getBindings().containsKey("firstname"));
strquerydef.setCriteria("Foo AND bar"); SPARQLBindings bindings = qdef.getBindings(); qdef.setBindings(bindings); qdef.setConstrainingQueryDefinition(null); assertEquals("Result lastname from testConstrainingQueryNull is incorrect", "Pei", jsonBindings.get(0).path("lastname").path("value").asText()); SPARQLQueryDefinition qdef1 = sparqlQmgr.newQueryDefinition(queryStr.toString()).withConstrainingQuery(strquerydef);
SPARQLBindings bindings = qdef.getBindings(); bindings.bind("playerid", "30", RDFTypes.INTEGER); qdef.setBindings(bindings); SPARQLQueryDefinition qdef1 = sparqlQmgr.newQueryDefinition().withSparql("select ?s ?p ?o from <BindingsGraph> where {?s ?p ?o. }"); JsonNode jsonBindingsNodes = sparqlQmgr.executeSelect(qdef1, new JacksonHandle()).get().path("results").path("bindings").get(0); SPARQLBindings bindings2 = qdef2.getBindings(); bindings2.bind("playerid", "30", RDFTypes.INTEGER); qdef2.setBindings(bindings2); qdef2.setBaseUri(baseuri2); SPARQLQueryDefinition qdefValid = sparqlQmgr.newQueryDefinition().withSparql(q21); JsonNode jsonBindingsNodes21 = sparqlQmgr.executeSelect(qdefValid, new JacksonHandle()).get().path("results").path("bindings").get(0); SPARQLBindings bindingsNull = qdefNull.getBindings(); bindingsNull.bind("playerid", "30", RDFTypes.INTEGER); qdefNull.setBindings(bindingsNull); qdefNull.setBaseUri(baseuriNull); SPARQLQueryDefinition qdefNullExeSel = sparqlQmgr.newQueryDefinition().withSparql(qNull); JsonNode jsonBindingsNodesNull = sparqlQmgr.executeSelect(qdefNullExeSel, new JacksonHandle()).get().path("results").path("bindings").get(0); SPARQLBindings bindingsEmpty = qdefEmpty.getBindings(); bindingsEmpty.bind("playerid", "30", RDFTypes.INTEGER); qdefEmpty.setBindings(bindingsEmpty);
SPARQLBindings bindings = qdef.getBindings(); qdef.setBindings(bindings); querydef.setCriteria("Meng AND 8888"); qdef.setConstrainingQueryDefinition(querydef);
sparqlInferQuery.append(newline); qdef.setSparql(sparqlInferQuery.toString()); qdef.setIncludeDefaultRulesets(false); qdef.setRulesets(SPARQLRuleset.SUBCLASS_OF); qdef.setIncludeDefaultRulesets(true); qdef.setSparql(sparqlInferQuery.toString()); assertEquals("One Ruleset should have been returned from testInferenceAndRuleSet method ", 1, qdef.getRulesets().length); SPARQLRuleset[] rulesets = qdef.getRulesets(); assertEquals("Ruleset name returned from testInferenceAndRuleSet method is incorrect", "subClassOf.rules", rulesets[0].getName()); qdef.setRulesets(SPARQLRuleset.SUBCLASS_OF, SPARQLRuleset.SUBPROPERTY_OF); qdef.setIncludeDefaultRulesets(true); qdef.setSparql(sparqlInferQuery.toString()); assertEquals("Two Rulesets should have been returned from testInferenceAndRuleSet method ", 2, qdef.getRulesets().length); Collection<SPARQLRuleset> list = Arrays.asList(qdef.getRulesets());
@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()); }
int bindNumber = 1; SPARQLQueryDefinition qdef = client.newQueryDefinition("TMP"); SPARQLBindings bindings = qdef.getBindings(); StringBuffer entireQuery = new StringBuffer(); entireQuery.append("DELETE DATA { "); qdef.setSparql(entireQuery.toString());
qdef.setIncludeDefaultRulesets(false); qdef.setCollections(localGraphUri); qdef.setConstrainingQueryDefinition(queryMgr.newStringDefinition().withCriteria("test1")); JsonNode jsonResults = smgr.executeSelect(qdef, new JacksonHandle()).get(); JsonNode tuples = jsonResults.path("results").path("bindings"); sqb.value(sqb.element("test2"), "testValue") ); qdef.setConstrainingQueryDefinition(sqdef); jsonResults = smgr.executeSelect(qdef, new JacksonHandle()).get(); tuples = jsonResults.path("results").path("bindings"); StringHandle handle = new StringHandle(rawXMLStructuredQuery).withFormat(Format.XML); RawStructuredQueryDefinition rawStructuredQDef = queryMgr.newRawStructuredQueryDefinition(handle); qdef.setConstrainingQueryDefinition(rawStructuredQDef); jsonResults = smgr.executeSelect(qdef, new JacksonHandle()).get(); tuples = jsonResults.path("results").path("bindings"); handle = new StringHandle(rawJSONStructuredQuery).withFormat(Format.JSON); rawStructuredQDef = queryMgr.newRawStructuredQueryDefinition(handle); qdef.setConstrainingQueryDefinition(rawStructuredQDef); jsonResults = smgr.executeSelect(qdef, new JacksonHandle()).get(); tuples = jsonResults.path("results").path("bindings"); handle = new StringHandle(rawCombinedQuery).withFormat(Format.XML); RawCombinedQueryDefinition rawCombinedQDef = queryMgr.newRawCombinedQueryDefinition(handle); qdef.setConstrainingQueryDefinition(rawCombinedQDef); jsonResults = smgr.executeSelect(qdef, new JacksonHandle()).get(); tuples = jsonResults.path("results").path("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(); } }
private void exec(Update update) { SPARQLQueryDefinition qdef = client.newQueryDefinition(update .toString()); if (markLogicDatasetGraph.getRulesets() != null) { qdef.setRulesets(markLogicDatasetGraph.getRulesets()); } MarkLogicQueryEngine.bindVariables(qdef, this.initial, markLogicDatasetGraph); if (markLogicDatasetGraph.getSPARQLUpdatePermissions() != null) { qdef.setUpdatePermissions(markLogicDatasetGraph.getSPARQLUpdatePermissions()); } client.executeUpdate(qdef); }
@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"); }
SPARQLBindings bindings = qdef1.getBindings();
@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()); }