private TupleQueryResult query ( RepositoryConnection rc, String qs ) throws RepositoryException, MalformedQueryException, QueryEvaluationException { return rc.prepareTupleQuery ( QueryLanguage.SPARQL, qs ).evaluate () ; }
private boolean evaluateSparql(String qry) throws RepositoryException, MalformedQueryException, QueryEvaluationException { TupleQuery query = con.prepareTupleQuery(QueryLanguage.SPARQL, qry); TupleQueryResult evaluate = query.evaluate(); try { return evaluate.hasNext(); } finally { evaluate.close(); } } }
protected long countSolutions(final String query) throws QueryEvaluationException, RepositoryException, MalformedQueryException { TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate(); try { long n = 0; while (result.hasNext()) { final BindingSet bset = result.next(); n++; if (logger.isInfoEnabled()) logger.info(bset.toString()); } return n; } finally { result.close(); } }
@Test public void testSES1991NOWEvaluation() throws Exception { loadTestData("/testdata-query/defaultgraph.ttl"); String query = "SELECT ?d WHERE {?s ?p ?o . BIND(NOW() as ?d) } LIMIT 2"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); Literal d1 = (Literal)result.next().getValue("d"); Literal d2 = (Literal)result.next().getValue("d"); assertNotNull(d1); assertEquals(d1, d2); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
private void checkResults(final BigdataSailRemoteRepositoryConnection conn, final int count) throws QueryEvaluationException, RepositoryException, MalformedQueryException { final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, QUERY).evaluate(); Collection<Value> subjects = new ArrayList<>(); try { while(result.hasNext()) { BindingSet bs = result.next(); subjects.add(bs.getBinding("s").getValue()); } } finally { result.close(); } if(log.isInfoEnabled()) log.info(subjects); assertEquals(count, subjects.size()); }
public interface Factory { SPARQLSyntaxTest createSPARQLSyntaxTest(String testURI, String testName, String testAction, boolean positiveTest); }
protected double checkIfSimilar(RepositoryConnection conn, String contribA, String contribB) throws RepositoryException, MalformedQueryException, QueryEvaluationException { String zeroQueryString = "" + "Select distinct jour \n" + "from \n" + "{doc} <"+RelConstants.RL_HAS_CONTRIBUTOR+"> {<"+contribA+">}, \n" + "{doc} <"+RelConstants.RL_JOURNAL+"> {jour}, \n" + "{doc2} <"+RelConstants.RL_HAS_CONTRIBUTOR+"> {<"+contribB+">}, \n" + "{doc2} <"+RelConstants.RL_JOURNAL+"> {jour} \n" + ""; TupleQuery query = null; TupleQueryResult res = null; query = conn.prepareTupleQuery(QueryLanguage.SERQL, zeroQueryString); res = query.evaluate(); if(res.hasNext()){ res.close(); return 1; } return 0; } }
@Test public void testSES1991NOWEvaluation() throws Exception { loadTestData("/testdata-query/defaultgraph.ttl"); String query = "SELECT ?d WHERE {?s ?p ?o . BIND(NOW() as ?d) } LIMIT 2"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); Literal d1 = (Literal)result.next().getValue("d"); Literal d2 = (Literal)result.next().getValue("d"); assertNotNull(d1); assertEquals(d1, d2); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
private void assignOrphansToTheirOntology(ContextAwareConnection conn, Map<String, Ontology> ontologies) throws MalformedQueryException, RepositoryException, QueryEvaluationException { TupleQuery query; query = conn.prepareTupleQuery(FIND_ORPHANS_ONTOLOGY); TupleQueryResult result = query.evaluate(); try { while (result.hasNext()) { BindingSet tuple = result.next(); URI uri = (URI) tuple.getValue("bean"); if (!conn.hasStatement(uri, RDFS.ISDEFINEDBY, null)) { URI ontology = (URI) tuple.getValue("ontology"); logger.debug("assigning {} {}", uri, ontology); conn.add(uri, RDFS.ISDEFINEDBY, ontology); } } } finally { result.close(); } }
public RepositoryQueryResultTable(String queryString, QueryLanguage language, RepositoryConnection connection) throws ModelRuntimeException { try { this.query = connection.prepareTupleQuery(language, queryString); this.queryResult = this.query.evaluate(); } catch(OpenRDFException e) { throw new ModelRuntimeException(e); } }
// open a connection to the Sesame repository containing your statements RepositoryConnection conn = repository.getConnection(); try { // create a prepared query object from your query string TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); // evaluate the query on the repository TupleQueryResult result = query.evaluate(); // iterate over the results and do something with each result while (result.hasNext()) { BindingSet s = result.next(); Value subject = s.getValue("subject"); Value object = s.getValue("object"); System.out.println("value of ?subject: " + subject); System.out.println("value of ?object: " + object); } } finally { conn.close(); }
@Test public void testInComparison2() throws Exception { loadTestData("/testdata-query/dataset-ses1913.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/>\n"); query.append(" SELECT ?y WHERE { :a :p ?y. FILTER(?y in (:c, :d, 1/0)) } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertFalse(result.hasNext()); }
@Test public void testSES1991STRUUIDEvaluation() throws Exception { loadTestData("/testdata-query/defaultgraph.ttl"); String query = "SELECT ?uid WHERE {?s ?p ?o . BIND(STRUUID() as ?uid) } LIMIT 2"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); Literal uid1 = (Literal)result.next().getValue("uid"); Literal uid2 = (Literal)result.next().getValue("uid"); assertNotNull(uid1); assertFalse(uid1.equals(uid2)); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * Test supposed to check if constants in BIND expressions will be resolved */ public void test_ticket_1717() throws Exception { { // Clear DB per task description (Executing the query over the empty database) m_repo.remove(new RemoveOp(null, null, null)); String query = "SELECT ?z ?s1 { ?s ?p ?o . BIND(?o+1 AS ?z) BIND(?z+1 AS ?z2) }"; final TupleQuery tq = m_repo.getBigdataSailRemoteRepository().getConnection().prepareTupleQuery(QueryLanguage.SPARQL, query, null); final TupleQueryResult tqr = tq.evaluate(); try { int count = 0; while (tqr.hasNext()) { System.out.println(tqr.next()); count++; } assertEquals(0,count); // asserting successful execution of the query, as it was failing while parsing } finally { tqr.close(); } } }
String queryString = "SELECT ..."; RepositoryConnection conn = repo.getConnection(); try { TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult result = tq.evaluate(); } finally { conn.close(); }
protected long debugPrintSolutions(final String query) throws QueryEvaluationException, RepositoryException, MalformedQueryException { TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate(); try { long n = 0; while (result.hasNext()) { System.err.println("==> NEXT SOLUTION"); final BindingSet bset = result.next(); for (final String bindingName : bset.getBindingNames()) { final Binding b = bset.getBinding(bindingName); System.err.println(bindingName + " -> " + b); } n++; } return n; } finally { result.close(); } } }
@Test public void testInComparison2() throws Exception { loadTestData("/testdata-query/dataset-ses1913.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/>\n"); query.append(" SELECT ?y WHERE { :a :p ?y. FILTER(?y in (:c, :d, 1/0)) } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertFalse(result.hasNext()); }
@Test public void testSES1991STRUUIDEvaluation() throws Exception { loadTestData("/testdata-query/defaultgraph.ttl"); String query = "SELECT ?uid WHERE {?s ?p ?o . BIND(STRUUID() as ?uid) } LIMIT 2"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); Literal uid1 = (Literal)result.next().getValue("uid"); Literal uid2 = (Literal)result.next().getValue("uid"); assertNotNull(uid1); assertFalse(uid1.equals(uid2)); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
private void checkResults(final BigdataSailRemoteRepositoryConnection conn, final int count) throws QueryEvaluationException, RepositoryException, MalformedQueryException { final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, QUERY).evaluate(); Collection<Value> subjects = new ArrayList<>(); try { while(result.hasNext()) { BindingSet bs = result.next(); subjects.add(bs.getBinding("s").getValue()); } } finally { result.close(); } if(log.isInfoEnabled()) log.info(subjects); assertEquals(count, subjects.size()); }
public RepositoryQueryResultTable(String queryString, QueryLanguage language, RepositoryConnection connection) throws ModelRuntimeException { try { queryResult = connection.prepareTupleQuery(language, queryString).evaluate(); } catch (OpenRDFException e) { throw new ModelRuntimeException(e); } }