org.eclipse.rdf4j.query.BooleanQuery Java Examples
The following examples show how to use
org.eclipse.rdf4j.query.BooleanQuery.
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: SAILFederatedService.java From CostFed with GNU Affero General Public License v3.0 | 6 votes |
@Override public boolean ask(Service service, BindingSet bindings, String baseUri) throws QueryEvaluationException { RepositoryConnection conn = endpoint.getConn(); try { BooleanQuery query = conn.prepareBooleanQuery(QueryLanguage.SPARQL, service.getAskQueryString(), baseUri); Iterator<Binding> bIter = bindings.iterator(); while (bIter.hasNext()) { Binding b = bIter.next(); if (service.getServiceVars().contains(b.getName())) query.setBinding(b.getName(), b.getValue()); } return query.evaluate(); } catch(Throwable e) { throw new QueryEvaluationException(e); } finally { conn.close(); } }
Example #2
Source File: RepositoryConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testPreparedBooleanQuery() 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(ASK); queryBuilder.append("{ ?p foaf:name ?name }"); BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString()); query.setBinding(NAME, nameBob); assertThat(query.evaluate()).isTrue(); }
Example #3
Source File: BasicBenchmarks.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Benchmark public void cantInvokeOldReasoner() throws IOException { NotifyingSail baseSail = new MemoryStore(); DedupingInferencer deduper = new DedupingInferencer(baseSail); NotifyingSail rdfsInferencer = new ForwardChainingRDFSInferencer(deduper); SpinSail spinSail = new SpinSail(rdfsInferencer); SailRepository repo = new SailRepository(spinSail); repo.init(); try (SailRepositoryConnection conn = repo.getConnection()) { loadRDF("/schema/spif.ttl", conn); BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix spif: <http://spinrdf.org/spif#> " + "ask where {filter(spif:canInvoke(spif:indexOf, 'foobar', 2))}"); assertFalse(bq.evaluate()); } repo.init(); }
Example #4
Source File: BasicBenchmarks.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Benchmark public void cantInvoke() throws IOException { NotifyingSail baseSail = new MemoryStore(); DedupingInferencer deduper = new DedupingInferencer(baseSail); NotifyingSail rdfsInferencer = new SchemaCachingRDFSInferencer(deduper); SpinSail spinSail = new SpinSail(rdfsInferencer); SailRepository repo = new SailRepository(spinSail); repo.init(); try (SailRepositoryConnection conn = repo.getConnection()) { loadRDF("/schema/spif.ttl", conn); BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix spif: <http://spinrdf.org/spif#> " + "ask where {filter(spif:canInvoke(spif:indexOf, 'foobar', 2))}"); assertFalse(bq.evaluate()); } repo.shutDown(); }
Example #5
Source File: RepositoryModelSet.java From semweb4j with BSD 2-Clause "Simplified" License | 5 votes |
@Override public boolean sparqlAsk(String queryString) throws ModelRuntimeException { this.assertModel(); BooleanQuery booleanQuery; try { booleanQuery = this.connection.prepareBooleanQuery(QueryLanguage.SPARQL, queryString); boolean result = booleanQuery.evaluate(); return result; } catch(RDF4JException e) { throw new ModelRuntimeException(e); } }
Example #6
Source File: FedXSailRepositoryConnection.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
@Override public SailQuery prepareQuery(QueryLanguage ql, String queryString, String baseURI) { SailQuery q = super.prepareQuery(ql, queryString, baseURI); if (q instanceof TupleQuery) insertOriginalQueryString(q, queryString, QueryType.SELECT); else if (q instanceof GraphQuery) insertOriginalQueryString(q, queryString, QueryType.CONSTRUCT); else if (q instanceof BooleanQuery) insertOriginalQueryString(q, queryString, QueryType.ASK); return q; }
Example #7
Source File: RepositoryModel.java From semweb4j with BSD 2-Clause "Simplified" License | 5 votes |
@Override public boolean sparqlAsk(String query) throws ModelRuntimeException { assertModel(); try { BooleanQuery prepared = this.connection.prepareBooleanQuery(QueryLanguage.SPARQL, query); return prepared.evaluate(); } catch (MalformedQueryException | RepositoryException | UnsupportedQueryLanguageException | QueryEvaluationException e) { throw new ModelRuntimeException(e); } }
Example #8
Source File: BooleanQueryResultView.java From semagrow with Apache License 2.0 | 5 votes |
protected void renderInternal(Map model, HttpServletRequest request, HttpServletResponse response) throws IOException { BooleanQueryResultWriterFactory brWriterFactory = (BooleanQueryResultWriterFactory)model.get("factory"); BooleanQueryResultFormat brFormat = brWriterFactory.getBooleanQueryResultFormat(); response.setStatus(200); this.setContentType(response, brFormat); this.setContentDisposition(model, response, brFormat); boolean headersOnly = ((Boolean)model.get("headersOnly")).booleanValue(); if(!headersOnly) { ServletOutputStream out = response.getOutputStream(); try { BooleanQueryResultWriter e = brWriterFactory.getWriter(out); BooleanQuery query = ((BooleanQuery)model.get("queryResult")); boolean value = query.evaluate(); e.handleBoolean(value); } catch (QueryResultHandlerException var13) { if(var13.getCause() != null && var13.getCause() instanceof IOException) { throw (IOException)var13.getCause(); } throw new IOException(var13); } finally { out.close(); } } this.logEndOfRequest(request); }
Example #9
Source File: ContextAwareConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public BooleanQuery prepareBooleanQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException { if (baseURI == null) { baseURI = getBaseURI(); } return initQuery(super.prepareBooleanQuery(ql, query, baseURI)); }
Example #10
Source File: SPARQLConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public BooleanQuery prepareBooleanQuery(QueryLanguage ql, String query, String base) throws RepositoryException, MalformedQueryException { if (SPARQL.equals(ql)) { return new SPARQLBooleanQuery(client, base, query); } throw new UnsupportedQueryLanguageException("Unsupported query language " + ql); }
Example #11
Source File: SPARQLConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public boolean hasStatement(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws RepositoryException { try { BooleanQuery query = prepareBooleanQuery(SPARQL, SOMETHING, ""); setBindings(query, subj, pred, obj, contexts); return query.evaluate(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
Example #12
Source File: SPARQLConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public boolean isEmpty() throws RepositoryException { try { BooleanQuery query; if (isQuadMode()) { query = prepareBooleanQuery(SPARQL, SOMETHING_WITH_GRAPH); } else { query = prepareBooleanQuery(SPARQL, SOMETHING); } return !query.evaluate(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
Example #13
Source File: SailRepositoryConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testPrepareBooleanQuery_bypassed() throws Exception { TupleExpr expr = mock(TupleExpr.class); Optional<TupleExpr> response = Optional.of(expr); when(sailConnection.prepareQuery(any(), eq(Query.QueryType.BOOLEAN), any(), any())).thenReturn(response); when(sailConnection.evaluate(eq(expr), any(), any(), anyBoolean())).thenReturn(new EmptyIteration<>()); BooleanQuery query = subject.prepareBooleanQuery("ASK WHERE { ?s ?p ?o }"); query.evaluate(); // check that the TupleExpr implementation created by the underlying sail was passed to the evaluation verify(sailConnection).evaluate(eq(expr), any(), any(), anyBoolean()); }
Example #14
Source File: SailRepositoryConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testPrepareBooleanQuery_not_bypassed() throws Exception { Optional<TupleExpr> response = Optional.empty(); when(sailConnection.prepareQuery(any(), eq(Query.QueryType.BOOLEAN), any(), any())).thenReturn(response); when(sailConnection.evaluate(any(), any(), any(), anyBoolean())).thenReturn(new EmptyIteration<>()); BooleanQuery query = subject.prepareBooleanQuery("ASK WHERE { ?s ?p ?o }"); query.evaluate(); // check that evaluation is still called, and not with an empty TupleExpr verify(sailConnection).evaluate(any(TupleExpr.class), any(), any(), anyBoolean()); }
Example #15
Source File: SpifSailTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testCantInvoke() throws Exception { loadRDF("/schema/spif.ttl"); BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix spif: <http://spinrdf.org/spif#> " + "ask where {filter(spif:canInvoke(spif:indexOf, 'foobar', 2))}"); assertFalse(bq.evaluate()); }
Example #16
Source File: SpifSailTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testCanInvoke() throws Exception { loadRDF("/schema/spif.ttl"); BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix spif: <http://spinrdf.org/spif#> " + "ask where {filter(spif:canInvoke(spif:indexOf, 'foobar', 'b'))}"); assertTrue(bq.evaluate()); }
Example #17
Source File: TripleSourceBase.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
@Override public CloseableIteration<BindingSet, QueryEvaluationException> getStatements( String preparedQuery, RepositoryConnection conn, QueryType queryType) throws RepositoryException, MalformedQueryException, QueryEvaluationException { switch (queryType) { case SELECT: monitorRemoteRequest(); TupleQuery tQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, preparedQuery); disableInference(tQuery); return tQuery.evaluate(); case CONSTRUCT: monitorRemoteRequest(); GraphQuery gQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, preparedQuery); disableInference(gQuery); return new GraphToBindingSetConversionIteration(gQuery.evaluate()); case ASK: monitorRemoteRequest(); BooleanQuery bQuery = conn.prepareBooleanQuery(QueryLanguage.SPARQL, preparedQuery); disableInference(bQuery); return booleanToBindingSetIteration(bQuery.evaluate()); default: throw new UnsupportedOperationException( "Operation not supported for query type " + queryType); } }
Example #18
Source File: BasicTests.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testSingleSource_SetBinding() throws Exception { /* test a single source select query where we set a binding */ prepareTest(Arrays.asList("/tests/basic/data01endpoint1.ttl", "/tests/basic/data01endpoint2.ttl")); try (RepositoryConnection conn = fedxRule.getRepository().getConnection()) { // SELECT query TupleQuery tq = conn .prepareTupleQuery("SELECT ?person WHERE { ?person <http://xmlns.com/foaf/0.1/name> ?name }"); tq.setBinding("name", l("Alan")); TupleQueryResult tqr = tq.evaluate(); List<BindingSet> res = Iterations.asList(tqr); assertContainsAll(res, "person", Sets.newHashSet(iri("http://example.org/", "a"))); // CONSTRUCT query GraphQuery gq = conn.prepareGraphQuery( "CONSTRUCT { ?person <http://xmlns.com/foaf/0.1/name> ?name } WHERE { ?person <http://xmlns.com/foaf/0.1/name> ?name }"); gq.setBinding("name", l("Alan")); GraphQueryResult gqr = gq.evaluate(); List<Statement> stmts = Iterations.asList(gqr); Assertions.assertEquals(1, stmts.size()); Assertions.assertEquals(iri("http://example.org/", "a"), stmts.get(0).getSubject()); // BOOLEAN query BooleanQuery bq = conn.prepareBooleanQuery("ASK { ?person <http://xmlns.com/foaf/0.1/name> ?name }"); bq.setBinding("name", l("non-existing-name")); Assertions.assertEquals(false, bq.evaluate()); } }
Example #19
Source File: QueryEvaluator.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void evaluate(final TupleResultBuilder builder, final OutputStream out, final String xslPath, final WorkbenchRequest req, HttpServletResponse resp, CookieHandler cookies, final Query query, boolean writeCookie, boolean paged, int offset, int limit) throws RDF4JException, BadRequestException { if (query instanceof TupleQuery) { this.evaluateTupleQuery(builder, xslPath, req, resp, cookies, (TupleQuery) query, writeCookie, paged, offset, limit); } else { final RDFFormat format = req.isParameterPresent(ACCEPT) ? Rio.getWriterFormatForMIMEType(req.getParameter(ACCEPT)).orElse(null) : null; if (query instanceof GraphQuery) { GraphQuery graphQuery = (GraphQuery) query; if (null == format) { this.evaluateGraphQuery(builder, xslPath, req, resp, cookies, graphQuery, writeCookie, paged, offset, limit); } else { this.evaluateGraphQuery(Rio.createWriter(format, out), graphQuery); } } else if (query instanceof BooleanQuery) { builder.transform(xslPath, "boolean.xsl"); builder.startBoolean(); this.evaluateBooleanQuery(builder, (BooleanQuery) query); builder.endBoolean(); } else { throw new BadRequestException("Unknown query type: " + query.getClass().getSimpleName()); } } }
Example #20
Source File: TripleSourceBase.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public boolean hasStatements(ExclusiveTupleExpr group, BindingSet bindings) throws RepositoryException, MalformedQueryException, QueryEvaluationException { monitorRemoteRequest(); String preparedAskQuery = QueryStringUtil.askQueryString(group, bindings, group.getQueryInfo().getDataset()); try (RepositoryConnection conn = endpoint.getConnection()) { BooleanQuery query = conn.prepareBooleanQuery(QueryLanguage.SPARQL, preparedAskQuery); configureInference(query, group.getQueryInfo()); applyMaxExecutionTimeUpperBound(query); return query.evaluate(); } }
Example #21
Source File: SpifSailTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testCast() throws Exception { BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "prefix spif: <http://spinrdf.org/spif#> " + "ask where {filter(spif:cast(3.14, xsd:integer) = 3)}"); assertTrue(bq.evaluate()); }
Example #22
Source File: RepositoryConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void testDataset() 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(ASK); queryBuilder.append("{ ?p foaf:name ?name }"); BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString()); query.setBinding(NAME, nameBob); assertThat(query.evaluate()).isTrue(); SimpleDataset dataset = new SimpleDataset(); // default graph: {context1} dataset.addDefaultGraph(context1); query.setDataset(dataset); assertThat(query.evaluate()).isTrue(); // default graph: {context1, context2} dataset.addDefaultGraph(context2); query.setDataset(dataset); assertThat(query.evaluate()).isTrue(); // default graph: {context2} dataset.removeDefaultGraph(context1); query.setDataset(dataset); assertThat(query.evaluate()).isFalse(); queryBuilder.setLength(0); queryBuilder.append(PREFIX_FOAF + FOAF_NS + "> "); queryBuilder.append(ASK); queryBuilder.append("{ GRAPH ?g { ?p foaf:name ?name } }"); query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString()); query.setBinding(NAME, nameBob); // default graph: {context2}; named graph: {} query.setDataset(dataset); assertThat(query.evaluate()).isFalse(); // default graph: {context1, context2}; named graph: {context2} dataset.addDefaultGraph(context1); dataset.addNamedGraph(context2); query.setDataset(dataset); assertThat(query.evaluate()).isFalse(); // default graph: {context1, context2}; named graph: {context1, // context2} dataset.addNamedGraph(context1); query.setDataset(dataset); assertThat(query.evaluate()).isTrue(); }
Example #23
Source File: ContextAwareConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public BooleanQuery prepareBooleanQuery(QueryLanguage ql, String query) throws MalformedQueryException, RepositoryException { return prepareBooleanQuery(ql, query, getBaseURI()); }
Example #24
Source File: HTTPRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public BooleanQuery prepareBooleanQuery(QueryLanguage ql, String queryString, String baseURI) { return new HTTPBooleanQuery(this, ql, queryString, baseURI); }
Example #25
Source File: RepositoryConnectionWrapper.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public BooleanQuery prepareBooleanQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException { return getDelegate().prepareBooleanQuery(ql, query, baseURI); }
Example #26
Source File: AbstractRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public BooleanQuery prepareBooleanQuery(QueryLanguage ql, String query) throws MalformedQueryException, RepositoryException { return prepareBooleanQuery(ql, query, null); }
Example #27
Source File: QueryEvaluator.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
private void evaluateBooleanQuery(final TupleResultBuilder builder, final BooleanQuery query) throws QueryEvaluationException, QueryResultHandlerException { final boolean result = query.evaluate(); builder.link(Arrays.asList(INFO)); builder.bool(result); }
Example #28
Source File: TripleSourceBase.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public CloseableIteration<BindingSet, QueryEvaluationException> getStatements( String preparedQuery, BindingSet queryBindings, QueryType queryType, QueryInfo queryInfo) throws RepositoryException, MalformedQueryException, QueryEvaluationException { return withConnection((conn, resultHolder) -> { final String baseURI = queryInfo.getBaseURI(); switch (queryType) { case SELECT: monitorRemoteRequest(); TupleQuery tQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, preparedQuery, baseURI); applyBindings(tQuery, queryBindings); applyMaxExecutionTimeUpperBound(tQuery); configureInference(tQuery, queryInfo); resultHolder.set(tQuery.evaluate()); return; case CONSTRUCT: monitorRemoteRequest(); GraphQuery gQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, preparedQuery, baseURI); applyBindings(gQuery, queryBindings); applyMaxExecutionTimeUpperBound(gQuery); configureInference(gQuery, queryInfo); resultHolder.set(new GraphToBindingSetConversionIteration(gQuery.evaluate())); return; case ASK: monitorRemoteRequest(); boolean hasResults = false; try (RepositoryConnection _conn = conn) { BooleanQuery bQuery = _conn.prepareBooleanQuery(QueryLanguage.SPARQL, preparedQuery, baseURI); applyBindings(bQuery, queryBindings); applyMaxExecutionTimeUpperBound(bQuery); configureInference(bQuery, queryInfo); hasResults = bQuery.evaluate(); } resultHolder.set(booleanToBindingSetIteration(hasResults)); return; default: throw new UnsupportedOperationException("Operation not supported for query type " + queryType); } }); }
Example #29
Source File: SailQueryPreparer.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public BooleanQuery prepare(ParsedBooleanQuery askQuery) { BooleanQuery query = new SailBooleanQuery(askQuery, con); query.setIncludeInferred(includeInferred); return query; }
Example #30
Source File: DatasetRepositoryConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 4 votes |
private BooleanQuery wrap(final SailBooleanQuery q) { return new DatasetBooleanQuery(this, q); }