Java Code Examples for org.openrdf.query.TupleQuery#setBinding()
The following examples show how to use
org.openrdf.query.TupleQuery#setBinding() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RDFSContainer.java From anno4j with Apache License 2.0 | 6 votes |
private TupleQuery createBlockQuery(int b) throws RepositoryException { StringBuilder sb = new StringBuilder(); sb.append("SELECT ?pred ?value ?value_class\n"); sb.append("WHERE { $self ?pred ?value\n"); sb.append("OPTIONAL { ?value a ?value_class }\n"); sb.append("FILTER ("); for (int i = b * BSIZE, n = b * BSIZE + BSIZE; i < n; i++) { sb.append("?pred = <"); sb.append(RDF.NAMESPACE); sb.append("_"); sb.append((i + 1)); sb.append(">"); if (i + 1 < n) { sb.append(" || "); } } sb.append(")}\n"); ObjectConnection con = getObjectConnection(); try { TupleQuery query = con.prepareTupleQuery(SPARQL, sb.toString()); query.setBinding("self", getResource()); return query; } catch (MalformedQueryException e) { throw new RepositoryException(e); } }
Example 2
Source File: FieldPredicateTest.java From anno4j with Apache License 2.0 | 6 votes |
public void testReadAccess() throws Exception { ValueFactory vf = con.getValueFactory(); URI graph = vf.createURI("urn:test:graph"); con.setAddContexts(graph); Company c = new Company(); c = (Company) con.getObject(con.addObject(c)); c.setName("My Company"); assertEquals("My Company", c.getName()); TupleQuery query = con.prepareTupleQuery("SELECT ?g WHERE {GRAPH ?g {?o <urn:test:name> ?name}}"); query.setBinding("name", vf.createLiteral("My Company")); TupleQueryResult results = query.evaluate(); Value g = results.next().getValue("g"); results.close(); assertEquals(graph, g); con.setAddContexts(); assertEquals("My Company", c.getName()); query = con.prepareTupleQuery("SELECT ?g WHERE {GRAPH ?g {?o <urn:test:name> ?name}}"); query.setBinding("name", vf.createLiteral("My Company")); results = query.evaluate(); g = results.next().getValue("g"); results.close(); assertEquals(graph, g); }
Example 3
Source File: TestTicket632.java From database with GNU General Public License v2.0 | 6 votes |
private void executeQuery(final URI serviceUri, final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, RDFHandlerException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); final ValueFactory vf = conn.getValueFactory(); conn.setAutoCommit(false); try { final String query = "SELECT ?x { SERVICE <" + serviceUri.stringValue() + "> { ?x <u:1> ?bool1 } }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("bool1", vf.createLiteral(true)); final TupleQueryResult tqr = q.evaluate(); try { tqr.hasNext(); } finally { tqr.close(); } } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 4
Source File: SPARQLQueryTest.java From database with GNU General Public License v2.0 | 6 votes |
protected static String getManifestName(Repository manifestRep, RepositoryConnection con, String manifestFileURL) throws QueryEvaluationException, RepositoryException, MalformedQueryException { // Try to extract suite name from manifest file TupleQuery manifestNameQuery = con.prepareTupleQuery(QueryLanguage.SERQL, "SELECT ManifestName FROM {ManifestURL} rdfs:label {ManifestName}"); manifestNameQuery.setBinding("ManifestURL", manifestRep.getValueFactory().createURI(manifestFileURL)); TupleQueryResult manifestNames = manifestNameQuery.evaluate(); try { if (manifestNames.hasNext()) { return manifestNames.next().getValue("ManifestName").stringValue(); } } finally { manifestNames.close(); } // Derive name from manifest URL int lastSlashIdx = manifestFileURL.lastIndexOf('/'); int secLastSlashIdx = manifestFileURL.lastIndexOf('/', lastSlashIdx - 1); return manifestFileURL.substring(secLastSlashIdx + 1, lastSlashIdx); }
Example 5
Source File: SPARQLUpdateConformanceTest.java From database with GNU General Public License v2.0 | 6 votes |
protected static String getManifestName(Repository manifestRep, RepositoryConnection con, String manifestFileURL) throws QueryEvaluationException, RepositoryException, MalformedQueryException { // Try to extract suite name from manifest file TupleQuery manifestNameQuery = con.prepareTupleQuery(QueryLanguage.SERQL, "SELECT ManifestName FROM {ManifestURL} rdfs:label {ManifestName}"); manifestNameQuery.setBinding("ManifestURL", manifestRep.getValueFactory().createURI(manifestFileURL)); TupleQueryResult manifestNames = manifestNameQuery.evaluate(); try { if (manifestNames.hasNext()) { return manifestNames.next().getValue("ManifestName").stringValue(); } } finally { manifestNames.close(); } // Derive name from manifest URL int lastSlashIdx = manifestFileURL.lastIndexOf('/'); int secLastSlashIdx = manifestFileURL.lastIndexOf('/', lastSlashIdx - 1); return manifestFileURL.substring(secLastSlashIdx + 1, lastSlashIdx); }
Example 6
Source File: TestTicket4249.java From database with GNU General Public License v2.0 | 6 votes |
private void executeQuery(final RepositoryConnection conn, final Literal string, final int start, final int length, final Literal expected) throws RepositoryException, MalformedQueryException, QueryEvaluationException { final ValueFactory vf = conn.getValueFactory(); final String query = "select ?substring WHERE { BIND ( SUBSTR(?string, ?start, ?length) as ?substring ) . }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("string", string); q.setBinding("start", vf.createLiteral(start)); q.setBinding("length", vf.createLiteral(length)); final TupleQueryResult tqr = q.evaluate(); try { while (tqr.hasNext()) { final BindingSet bindings = tqr.next(); // assert expected value assertEquals(expected, bindings.getBinding("substring").getValue()); } } finally { tqr.close(); } }
Example 7
Source File: TestTicket4249.java From database with GNU General Public License v2.0 | 6 votes |
private void executeQuery(final RepositoryConnection conn, final Literal string, final int start, final Literal expected) throws RepositoryException, MalformedQueryException, QueryEvaluationException { final ValueFactory vf = conn.getValueFactory(); final String query = "select ?substring WHERE { BIND ( SUBSTR(?string, ?start) as ?substring ) . }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("string", string); q.setBinding("start", vf.createLiteral(start)); final TupleQueryResult tqr = q.evaluate(); try { while (tqr.hasNext()) { final BindingSet bindings = tqr.next(); // assert expected value assertEquals(expected, bindings.getBinding("substring").getValue()); } } finally { tqr.close(); } }
Example 8
Source File: TestTicket967.java From database with GNU General Public License v2.0 | 6 votes |
private void addDuringQueryExec(final RepositoryConnection conn, final Resource subj, final URI pred, final Value obj, final Resource... ctx) throws RepositoryException, MalformedQueryException, QueryEvaluationException { final TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, "select distinct ?s ?p ?o where{?s ?p ?t . ?t <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?o }" ); tq.setBinding("s", subj); tq.setBinding("p", pred); tq.setBinding("o", obj); final TupleQueryResult tqr = tq.evaluate(); try { if (!tqr.hasNext()) { conn.add(subj, pred, obj, ctx); } } finally { tqr.close(); } }
Example 9
Source File: TestTicket4249.java From database with GNU General Public License v2.0 | 6 votes |
private void executeQuery(final RepositoryConnection conn, final String string, final double start, final double length, final String expected) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, VisitorException { final ValueFactory vf = conn.getValueFactory(); final String query = "select ?substring WHERE { BIND ( SUBSTR(?string, ?start, ?length) as ?substring ) . }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("string", vf.createLiteral(string)); q.setBinding("start", vf.createLiteral(start)); q.setBinding("length", vf.createLiteral(length)); final TupleQueryResult tqr = q.evaluate(); try { while (tqr.hasNext()) { final BindingSet bindings = tqr.next(); // assert expected value assertEquals(expected, bindings.getBinding("substring").getValue().stringValue()); } } finally { tqr.close(); } }
Example 10
Source File: TestTicket275.java From database with GNU General Public License v2.0 | 5 votes |
private void executeQuery(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { conn.add(getClass().getResourceAsStream("TestTicket275.ttl"), "", RDFFormat.TURTLE); conn.commit(); final String query = "SELECT ?lookup WHERE { ?lookup <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <os:class/Lookup> . ?lookup <os:prop/lookup/majorType> ?majorType . OPTIONAL{?lookup <os:prop/lookup/minorType> ?minorType}. FILTER(STR(?majorType) = ?argMajorType). FILTER(!bound(?minorType))}"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("argMajorType", conn.getValueFactory() .createLiteral("majoor")); final TupleQueryResult tqr = q.evaluate(); while (tqr.hasNext()) { final Set<String> bindingNames = tqr.next().getBindingNames(); if(log.isInfoEnabled()) log.info("bindingNames="+bindingNames); } tqr.close(); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 11
Source File: TestTicket355.java From database with GNU General Public License v2.0 | 5 votes |
private void executeQuery(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, RDFHandlerException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { final ValueFactory vf = conn.getValueFactory(); conn.add(vf.createURI("os:subject"), vf.createURI("os:prop"), vf.createLiteral("value")); conn.commit(); String query = "SELECT ?subj WHERE { " + "?subj <os:prop> ?val . " + "FILTER(STR(?val) != ?arg)}"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); tq.setBinding("arg", vf.createLiteral("notValue")); TupleQueryResult tqr = tq.evaluate(); assertTrue(tqr.hasNext()); tqr.close(); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 12
Source File: TestTicket276.java From database with GNU General Public License v2.0 | 5 votes |
private void executeQuery(final SailRepository repo) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFParseException, IOException, RDFHandlerException { try { repo.initialize(); final RepositoryConnection conn = repo.getConnection(); conn.setAutoCommit(false); try { final ValueFactory vf = conn.getValueFactory(); addData(conn); conn.commit(); final String query = "SELECT ?x { ?x ?a ?t . ?x ?lookup ?l }"; final TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding( "a", vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")); q.setBinding("t", vf.createURI("os:class/Location")); q.setBinding("lookup", vf.createURI("os:prop/lookupName")); q.setBinding("l", vf.createLiteral("amsterdam")); final TupleQueryResult tqr = q.evaluate(); while (tqr.hasNext()) { final Set<String> bindingNames = tqr.next() .getBindingNames(); if (log.isInfoEnabled()) log.info("bindingNames=" + bindingNames); } tqr.close(); } finally { conn.close(); } } finally { repo.shutDown(); } }
Example 13
Source File: RepositoryConnectionTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testPreparedTupleQuery2() throws Exception { testCon.add(alice, name, nameAlice, context2); testCon.add(alice, mbox, mboxAlice, context2); testCon.add(context2, publisher, nameAlice); testCon.add(bob, name, nameBob, context1); testCon.add(bob, mbox, mboxBob, context1); testCon.add(context1, publisher, nameBob); StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" prefix foaf: <" + FOAF_NS + ">"); queryBuilder.append(" SELECT ?name ?mbox"); queryBuilder.append(" where { ?VAR foaf:name ?name . ?VAR foaf:mbox ?mbox . }"); TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SPARQL, queryBuilder.toString()); query.setBinding("VAR", bob); TupleQueryResult result = query.evaluate(); try { assertThat(result, is(notNullValue())); assertThat(result.hasNext(), is(equalTo(true))); while (result.hasNext()) { BindingSet solution = result.next(); assertThat(solution.hasBinding(NAME), is(equalTo(true))); assertThat(solution.hasBinding(MBOX), is(equalTo(true))); Value nameResult = solution.getValue(NAME); Value mboxResult = solution.getValue(MBOX); assertEquals("unexpected value for name: " + nameResult, nameBob, nameResult); assertEquals("unexpected value for mbox: " + mboxResult, mboxBob, mboxResult); } } finally { result.close(); } }
Example 14
Source File: RepositoryConnectionTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testPreparedTupleQuery() throws Exception { testCon.add(alice, name, nameAlice, context2); testCon.add(alice, mbox, mboxAlice, context2); testCon.add(context2, publisher, nameAlice); testCon.add(bob, name, nameBob, context1); testCon.add(bob, mbox, mboxBob, context1); testCon.add(context1, publisher, nameBob); StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" prefix foaf: <" + FOAF_NS + ">"); queryBuilder.append(" select ?name ?mbox "); queryBuilder.append(" where { ?s foaf:name ?name . ?s foaf:mbox ?mbox . }"); TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SPARQL, queryBuilder.toString()); query.setBinding(NAME, nameBob); TupleQueryResult result = query.evaluate(); try { assertThat(result, is(notNullValue())); assertThat(result.hasNext(), is(equalTo(true))); while (result.hasNext()) { BindingSet solution = result.next(); assertThat(solution.hasBinding(NAME), is(equalTo(true))); assertThat(solution.hasBinding(MBOX), is(equalTo(true))); Value nameResult = solution.getValue(NAME); Value mboxResult = solution.getValue(MBOX); assertEquals("unexpected value for name: " + nameResult, nameBob, nameResult); assertEquals("unexpected value for mbox: " + mboxResult, mboxBob, mboxResult); } } finally { result.close(); } }
Example 15
Source File: BigdataSailRemoteRepositoryConnectionTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testTupleQueryBindings() throws Exception { final TupleQuery tq = con.prepareTupleQuery(QueryLanguage.SPARQL, "select * where {?s ?p ?o}"); tq.setBinding("s", s); tq.setBinding("p", p); tq.setBinding("o", o); { assertEquals(s,tq.getBindings().getBinding("s").getValue()); assertEquals(p,tq.getBindings().getBinding("p").getValue()); assertEquals(o,tq.getBindings().getBinding("o").getValue()); tq.removeBinding("o"); assertFalse(tq.getBindings().hasBinding("o")); TupleQueryResult tqr = tq.evaluate(); try { assertEquals(EncodeDecodeValue.encodeValue(s),remote.data.opts.getRequestParam("$s")); assertEquals(EncodeDecodeValue.encodeValue(p),remote.data.opts.getRequestParam("$p")); assertEquals(null,remote.data.opts.getRequestParam("$o")); assertEquals(EncodeDecodeValue.encodeValue(s),remote.data.request.getParams().get("$s").getValue()); assertEquals(EncodeDecodeValue.encodeValue(p),remote.data.request.getParams().get("$p").getValue()); assertEquals(null,remote.data.request.getParams().get("$o")); } finally { tqr.close(); } tq.clearBindings(); assertEquals(0,tq.getBindings().size()); } }
Example 16
Source File: Test_Ticket_789.java From database with GNU General Public License v2.0 | 5 votes |
public void test_ticket_1325_bindings() throws Exception { populate(); BigdataSailRemoteRepository sailRepo = m_repo.getBigdataSailRemoteRepository(); BigdataSailRemoteRepositoryConnection con = sailRepo.getConnection(); final TupleQuery tq = con.prepareTupleQuery(QueryLanguage.SPARQL, "select * where {?s ?p ?o}"); tq.setBinding("s", s); tq.setBinding("o", o); { TupleQueryResult tqr = tq.evaluate(); try { int count = 0; while (tqr.hasNext()) { BindingSet bs = tqr.next(); assertEquals(s, bs.getBinding("s").getValue()); assertEquals(o, bs.getBinding("o").getValue()); count++; } // there are 2 statements with specified subject and object: (s, p, o) and (s, p1, o) assertEquals(2, count); } finally { tqr.close(); } tq.clearBindings(); assertEquals(0, tq.getBindings().size()); } }
Example 17
Source File: ObjectQueryTest.java From anno4j with Apache License 2.0 | 5 votes |
public void testTupleQueryBinding() throws Exception { Person jamie = con.addDesignation(con.getObjectFactory().createObject(), Person.class); jamie.getFoafNames().add("Jamie"); jamie.getFoafFamily_names().add("Leigh"); String q = PREFIX + "Select ?name where { ?person foaf:name ?name }"; TupleQuery query = con.prepareTupleQuery(q); query.setBinding("person", ((RDFObject)jamie).getResource()); TupleQueryResult result = query.evaluate(); assertTrue(result.hasNext()); assertEquals("Jamie", result.next().getValue("name").stringValue()); result.close(); }
Example 18
Source File: TestBigdataValueReplacer.java From database with GNU General Public License v2.0 | 4 votes |
/** * Unit test for bindings passed into a query which are not used by the * query. * * @throws RepositoryException * @throws SailException * @throws MalformedQueryException * @throws QueryEvaluationException * * @see https://sourceforge.net/apps/trac/bigdata/ticket/271 */ public void test_dropUnusedBindings() throws RepositoryException, SailException, MalformedQueryException, QueryEvaluationException { final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final BigdataSailRepositoryConnection cxn = (BigdataSailRepositoryConnection) repo .getConnection(); try { cxn.setAutoCommit(false); /* * Add a statement so the query does not get short circuited * because some of the terms in the query are undefined in the * database. */ cxn.add(new URIImpl("s:1"), new URIImpl("p:1"), new URIImpl( "s:2")); final String query = "select ?a ?b WHERE {?a <p:1> ?b}"; final TupleQuery q = cxn.prepareTupleQuery( QueryLanguage.SPARQL, query); /* * Setup some bindings. */ // bind to a term in the database. q.setBinding("a", new URIImpl("s:1")); // bind to a term in the database. q.setBinding("b", new URIImpl("s:2")); // bind to a term NOT found in the database. q.setBinding("notused", new LiteralImpl("lit")); /* * Evaluate the query and verify that the correct solution * is produced. */ final Collection<BindingSet> expected = new LinkedList<BindingSet>(); { final MapBindingSet bset = new MapBindingSet(); bset.addBinding("a", new URIImpl("s:1")); bset.addBinding("b", new URIImpl("s:2")); expected.add(bset); } final TupleQueryResult result = q.evaluate(); try { compare(result, expected); } finally { result.close(); } } finally { cxn.close(); } } finally { sail.__tearDownUnitTest(); } }
Example 19
Source File: TestOptionals.java From database with GNU General Public License v2.0 | 4 votes |
public void testOptional() throws Exception { Properties properties = getProperties(); // properties.put("com.bigdata.rdf.sail.isolatableIndices", "true"); properties.put("com.bigdata.rdf.store.AbstractTripleStore.axiomsClass", "com.bigdata.rdf.axioms.NoAxioms"); properties.put("com.bigdata.rdf.sail.truthMaintenance", "false"); properties.put("com.bigdata.rdf.store.AbstractTripleStore.vocabularyClass", "com.bigdata.rdf.vocab.NoVocabulary"); properties.put("com.bigdata.rdf.store.AbstractTripleStore.justify", "false"); final BigdataSail sail = getSail(properties); sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final BigdataSailRepositoryConnection cxn = (BigdataSailRepositoryConnection) repo.getConnection(); cxn.setAutoCommit(false); try { final ValueFactory vf = sail.getValueFactory(); cxn.add(vf.createURI("u:1"), vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createURI("u:2")); cxn.commit(); String query = "SELECT REDUCED ?subj ?subj_class ?subj_label " + "WHERE { " + " ?subj a ?subj_class . " + " OPTIONAL { ?subj <http://www.w3.org/2000/01/rdf-schema#label> ?subj_label } " + "}"; TupleQuery q = cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); q.setBinding("subj", vf.createURI("u:1")); TupleQueryResult tqr = q.evaluate(); assertTrue(tqr.hasNext()); final BindingSet tmp = tqr.next(); if (log.isInfoEnabled()) log.info(tmp); tqr.close(); } finally { cxn.close(); sail.__tearDownUnitTest(); } }
Example 20
Source File: ComplexSPARQLQueryTest.java From database with GNU General Public License v2.0 | 4 votes |
@Test public void testSameTermRepeatInUnion() throws Exception { loadTestData("/testdata-query/dataset-query.trig"); StringBuilder query = new StringBuilder(); query.append("PREFIX foaf:<http://xmlns.com/foaf/0.1/>\n"); query.append("SELECT * {\n"); query.append(" {\n"); query.append(" ?sameTerm foaf:mbox ?mbox\n"); query.append(" FILTER sameTerm(?sameTerm,$william)\n"); query.append(" } UNION {\n"); query.append(" ?x foaf:knows ?sameTerm\n"); query.append(" FILTER sameTerm(?sameTerm,$william)\n"); query.append(" }\n"); query.append("}"); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); tq.setBinding("william", conn.getValueFactory().createURI("http://example.org/william")); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); int count = 0; while (result.hasNext()) { BindingSet bs = result.next(); count++; assertNotNull(bs); System.out.println(bs); Value mbox = bs.getValue("mbox"); Value x = bs.getValue("x"); assertTrue(mbox instanceof Literal || x instanceof URI); } result.close(); assertEquals(3, count); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }