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(); } } }
public ObjectArrayCursor(ObjectConnection manager, TupleQueryResult result, List<String> bindings, Class<?> componentType) throws QueryEvaluationException { this.bindings = bindings; this.result = result; this.next = result.hasNext() ? result.next() : null; this.manager = manager; this.of = manager.getObjectFactory(); this.componentType = componentType; }
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(); } }
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; } }
public ObjectArrayCursor(ObjectConnection manager, TupleQueryResult result, List<String> bindings, Class<?> componentType) throws QueryEvaluationException { this.bindings = bindings; this.result = result; this.next = result.hasNext() ? result.next() : null; this.manager = manager; this.of = manager.getObjectFactory(); this.componentType = componentType; }
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(); } }
// 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()); }
/** * Returns a single element from the query result. The QueryResult is * automatically closed by this method. * * @since 2.7.0 * @return a single query result element. * @throws QueryEvaluationException */ public static BindingSet singleResult(TupleQueryResult result) throws QueryEvaluationException { BindingSet singleResult = null; if (result.hasNext()) { singleResult = result.next(); } result.close(); return singleResult; }
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()); }
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()); }
private List<BindingSet> readProperties(Value... values) throws QueryEvaluationException { List<BindingSet> properties = new ArrayList<BindingSet>(); while (next != null) { for (int i = 0; i < values.length; i++) { if (!equals(values[i], next.getValue(bindings.get(i)))) return properties; } properties.add(next); next = result.hasNext() ? result.next() : null; } return properties; }
/** * 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(); } } }
@Test public void testSES1685propPathSameVar() throws Exception { final String queryStr = "PREFIX : <urn:> SELECT ?x WHERE {?x :p+ ?x}"; conn.add(new StringReader("@prefix : <urn:> . :a :p :b . :b :p :a ."), "", RDFFormat.TURTLE); TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryStr); TupleQueryResult result = query.evaluate(); assertNotNull(result); int count = 0; while (result.hasNext()) { result.next(); count++; } // result should be both a and b. assertEquals(2, count); }
protected void assertNoQueryResult(String literal) throws Exception { // fire a query for all subjects with a given term String queryString = "SELECT Resource " + "FROM {Resource} <" + MATCHES + "> {} " + " <" + QUERY + "> {\"" + literal + "\"} "; TupleQuery query = connection.prepareTupleQuery(QueryLanguage.SERQL, queryString); TupleQueryResult result = query.evaluate(); try { // check the result assertFalse("query for literal '" + literal + " did return results, which was not expected.", result.hasNext()); } finally { result.close(); } }
private List<BindingSet> readProperties(Value... values) throws QueryEvaluationException { List<BindingSet> properties = new ArrayList<BindingSet>(); while (next != null) { for (int i = 0; i < values.length; i++) { if (!equals(values[i], next.getValue(bindings.get(i)))) return properties; } properties.add(next); next = result.hasNext() ? result.next() : null; } return properties; }
@SuppressWarnings("unchecked") private void testQuery(final BigdataSailRepositoryConnection conn, final String query, final int expectedIVs) throws MalformedQueryException, RepositoryException, QueryEvaluationException { TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); TupleQueryResult tqr = tq.evaluate(); try { int cnt = 0; while (tqr.hasNext()) { tqr.next(); cnt++; } // assertEquals("Expected 1 row in resultset", 1, cnt); QueryRoot queryRoot = ((BigdataSailTupleQuery)tq).getASTContainer().getOriginalAST(); cnt = 0; for (Object filterNode: queryRoot.getWhereClause().getChildren(FilterNode.class)) { cnt += checkNode((BOp)filterNode); } assertEquals("Expected inlined IV for date literal", expectedIVs, cnt); } finally { tqr.close(); } }