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) ");
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) ");
qdef1.withBinding("firstname", "Ling", RDFTypes.STRING);
qdef1.withBinding("bdate", "1983-08-21", RDFTypes.DATE);
qdef1.withBinding("firstname", mbSearchStr, cnLocale);
@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()); }
askQuery.withBinding("o", "1", RDFTypes.BOOLEAN); assertTrue(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "0", RDFTypes.BOOLEAN); assertFalse(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "1", RDFTypes.DECIMAL); assertTrue(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "2", RDFTypes.DECIMAL); assertFalse(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "1", RDFTypes.INTEGER); assertTrue(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "2", RDFTypes.INTEGER); assertFalse(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "1", RDFTypes.DOUBLE); assertTrue(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "2", RDFTypes.DOUBLE); assertFalse(smgr.executeAsk(askQuery)); bindings.clear(); askQuery.withBinding("o", "1", RDFTypes.FLOAT);