Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FILTER inside OPTIONAL Clause #75

Closed
mdorf opened this issue Oct 31, 2016 · 2 comments
Closed

FILTER inside OPTIONAL Clause #75

mdorf opened this issue Oct 31, 2016 · 2 comments
Labels
enhancement New feature or request

Comments

@mdorf
Copy link

mdorf commented Oct 31, 2016

We have a use case for writing a query where the FILTER statement resides inside an OPTIONAL clause. The query was given to us by our triple store support engineers (AllegroGraph). The query looks like this:

SELECT DISTINCT ?id ?prefLabel ?synonym 
FROM <http://data.bioontology.org/ontologies/CSV_TEST_BRO/submissions/1> 
WHERE { 
	?id a <http://www.w3.org/2002/07/owl#Class> . 
	OPTIONAL { 
		?id ?rewrite0 ?prefLabel . 
		FILTER(?rewrite0 = <http://data.bioontology.org/metadata/def/prefLabel> || 
			?rewrite0 = <http://www.w3.org/2004/02/skos/core#prefLabel>)
	} 
	OPTIONAL { 
		?id ?rewrite1 ?synonym . 
		FILTER(?rewrite1 = <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> || 
			?rewrite1 = <http://purl.obolibrary.org/obo/synonym> || 
			?rewrite1 = <http://www.geneontology.org/formats/oboInOwl#hasBroadSynonym>) 	
	} 
	FILTER(?id = <http://bioontology.org/ontologies/Activity.owl#Activity> || 
		?id = <http://bioontology.org/ontologies/Activity.owl#Biospecimen_Management> || 
		?id = <http://bioontology.org/ontologies/Activity.owl#Community_Engagement> || 
		?id = <http://bioontology.org/ontologies/Activity.owl#Deprecated_Activity>)
}

I've reviewed the sparql-client API and didn't find an obvious way to embed a FILTER statement inside an OPTIONAL block. I wanted to check with you to make sure I'm not missing anything obvious. Is it possible to construct such a query without customizing the sparql-client?

Thank you!

@gkellogg
Copy link
Member

gkellogg commented Nov 1, 2016

This will require that optional take a block; there may be other operators we want to do this for too (arguably, most things that return a result set could take a block). A query construction might look like the following:

subject.select(:id, :prefLabel, :synonym).
  from("http://data.bioontology.org/ontologies/CSV_TEST_BRO/submissions/1").
  where([:id, RDF.type, RDF::OWL.Class]).optional {|q|
    q << [:id, :rewrite0, :prefLabel]
    q.filter(%(?rewrite0 = <http://data.bioontology.org/metadata/def/prefLabel> || 
            ?rewrite0 = <http://www.w3.org/2004/02/skos/core#prefLabel>))
  }.optional {
    self << [:id, :rewrite1, :synonym]
    filter(%(?rewrite1 = <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> || 
            ?rewrite1 = <http://purl.obolibrary.org/obo/synonym> || 
            ?rewrite1 = <http://www.geneontology.org/formats/oboInOwl#hasBroadSynonym>))
  }.filter(%(?id = <http://bioontology.org/ontologies/Activity.owl#Activity> || 
        ?id = <http://bioontology.org/ontologies/Activity.owl#Biospecimen_Management> || 
        ?id = <http://bioontology.org/ontologies/Activity.owl#Community_Engagement> || 
        ?id = <http://bioontology.org/ontologies/Activity.owl#Deprecated_Activity>))

Filter could probably use more DSL elements so that it simply didn't take a string, but that's a separate matter.

This pattern is similar to that used in RDF::Query.

@gkellogg gkellogg added the enhancement New feature or request label Nov 1, 2016
@gkellogg
Copy link
Member

gkellogg commented Nov 1, 2016

Revised syntax:

SPARQL::Client::Query.select(:id, :prefLabel, :synonym).
  from(RDF::URI("http://data.bioontology.org/ontologies/CSV_TEST_BRO/submissions/1")).
  where([:id, RDF.type, RDF::OWL.Class]).optional([:id, :rewrite0, :prefLabel]) {|q|
    q.filter(%(?rewrite0 = <http://data.bioontology.org/metadata/def/prefLabel> || 
            ?rewrite0 = <http://www.w3.org/2004/02/skos/core#prefLabel>))
  }.optional([:id, :rewrite1, :synonym]) {
    filter(%(?rewrite1 = <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> || 
            ?rewrite1 = <http://purl.obolibrary.org/obo/synonym> || 
            ?rewrite1 = <http://www.geneontology.org/formats/oboInOwl#hasBroadSynonym>))
  }.filter(%(?id = <http://bioontology.org/ontologies/Activity.owl#Activity> || 
        ?id = <http://bioontology.org/ontologies/Activity.owl#Biospecimen_Management> || 
        ?id = <http://bioontology.org/ontologies/Activity.owl#Community_Engagement> || 
        ?id = <http://bioontology.org/ontologies/Activity.owl#Deprecated_Activity>)).to_s

syphax-bouazzouni added a commit to ontoportal-lirmm/sparql-client that referenced this issue May 22, 2024
* Update dependencies for RDF 1.1. Use json gem instead of json_pure, as json is built into all rubies now.

* Update to RSpec expect syntax and use webmock for remote resources.

* Update travis to run unrestricted version of JRuby

* Add rdf-aggregate-repo to Gemfile.

* Revert use of Enumerator for solutions.

* Update Travis test matrix to use rbx instead of rbx-mode19

* Use Nokogiri instead of REXML for parsing results.

Nokogiri is significantly faster, and now has a pure-java backend for
use on JRuby.

This patch potentially breaks backwards compatibility, as
parse_xml_bindings no longer accepts REXML elements.

* Add Nokogiri dep to README.

* Add Nokogiri as development dependency.

* Use Nokogiri instead of REXML for parsing results.

Nokogiri is significantly faster, and now has a pure-java backend for
use on JRuby.

This patch potentially breaks backwards compatibility, as
parse_xml_bindings no longer accepts REXML elements.

* Add Nokogiri dep to README.

* Add Nokogiri as development dependency.

* Add tests for XML results parsing.

* Simplified xml tests slightly.

* Update node hash when parsing json.

* Add tests for json parsing.

* Add csv parsing test.

* Minor fixes to TSV parsing.

- save blank nodes in nodes table.
- Don't crash when columns are empty.

* Add test for TSV parsing.

* Make nokogiri really be a soft dependency and fallback to REXML if it doesn't load.

* add read_timeout support for requests

* Test both with and without nokogiri.

* Version 1.1.1.

* Remove json dependency

* Add Ruby 2.1.0 to Travis CI mix.

* Use develop version of sxp.

* Add rubinius dependencies to Gemfile.

* Require json gem for rbx.

* Move .gemspec to sparql-client.gemspec and update Gemfile.

* Move .gemspec to sparql-client.gemspec and update Gemfile.

* distinct should work with the "*" select form
`&&` and `and` are not identical
`a = true and false` results in `a` = `true`
`a = true && false` results in `a` = `false` as intended
in order to achieve the same results with `and` you must indicate
the precedence, e.g. `a = (true and false)` message for your changes. Lines starting

* Adding support for property paths. See spec for DSL syntax and examples.

* Add more specs for query builder.

* Update Travis Ruby versions.

* Finish WritableRepository

* Integrated @curoverse on a Repository using SPARQL::Client.
* Use Dydra repository for testing.
  * Some tests involving matching doubles don't pass.

This closes issue ruby-rdf#45

* Version 1.1.2.

* Added :endpoint option for #update method to specify an alternative update endpoint

* Be more descriminating on Accept headers sent based on different queries. Queries build using the DSL use either RDF content types, or SPARQL Results content types, not both.

Those using RDF content types:

 * CONSTRUCT, DESCRIBE, DELETE DATA, LOAD, CREATE

Those using SPARQL Results content types:

  * ASK, SELECT, INSERT DATA, CLEAR, DROP

Hopefully, this makes issues such as come up in ruby-rdf#51 less likely to happen.

* Ensured that SPARQL 1.1 JSON typed literals are parsed correctly.

The parsing code now supports both SPARQL 1.0 and 1.1 JSON results:

    {"type": "literal", "value": "S", "datatype": "D"}        # SPARQL 1.1 JSON
    {"type": "typed-literal", "value": "S", "datatype": "D"}  # SPARQL 1.0 JSON

See: http://www.w3.org/TR/sparql11-results-json/#select-encode-terms
See: http://www.w3.org/TR/rdf-sparql-json-res/#variable-binding-results

* Update code setting appropriate Accept header, and also add */*;q=0.1 to every request as a fallback.

* Only use DELETE DATA for #delete_statements if the statement is both constant, and contains no BNodes, otherwise, it falls back to DELETE/INSERT.

* Check error response outputs query.
When doing updates, change BNodes to Variables.

* Add around block with response delegation to capture query and report on queries run in this example if the example fails. (Not perfect, but still useful).

* Remove debug point.

* Version 1.1.3.

* Follow redirects when querying sparql

Some SPARQL servers redirect requests. This allows the library to follow
redirects, and raise an error in case of a large number of redirects.

* Update dependency on net-http-persistent to ~> 2.9.
Disable Repository tests, as Dydra is really just too slow for remote testing, and WebMock continues to be enabled, causing errors.
This relates to issue ruby-rdf#18.

* disable webmock for rdf-spec repo tests and reenable repo tests

* Version 1.1.3.1.

* Update build matrix to run both with and without nokogiri.

* Gemfile-pure, not Gemfile.pure

* Update README.md

Change account for Christoph Badura from @b4d to @bad

* Option for separate update_endpoint for Client::Repositories

* Add a mechanism to override the HTTP verb
Marmotta 3.3.0 requires GET for DELETE requests, but can
accept POST for INSERT

This enables rdf-marmotta to override less of #request. See
jcoyne/rdf-marmotta@9b1cf62

* Change Client#method to Client#request_method, as #method replaces Object#method, which is over-broad and broke the repository specs.

This fixes ruby-rdf#57.

* Version 1.1.4.

* When using the SPARQL gem as an update endpoint, use the `:update` option to invoke the proper parser path and catch malformed queries.

* Updates to allow a native RDF::Repository instance to be used for SPARQL::Client::Repository URL, which will use the SPARQL gem for doing updates.

* Version 1.1.5.

* Added support for specifying the sort order in SPARQL::Client::Query.order(*variables).

* Update documentation. Add support for `#asc` and `#desc` modifiers.

* Version 1.1.6.

* Add link to coveralls coverage report.

* Improve code coverage.

* Use mri 2.2.1, instead of 2.2; this defaults to 2.2.0p0, because of old version of rvm on Travis-CI.

* When calling @http.request, use ::URI, not ::RDF::URI.
Fixes ruby-rdf#29.

* Add that Repository does not support graph_name in addition to context for RDF.rb 2.0.

* Don't insert incomplete statement in `Repository::insert_statement(s)`.

* Minor updates for RDF.rb 2.0.

* Handle `Enumerable` values on `Repository#delete`

A test for `RDF::Enumerable` inputs was added by
ruby-rdf/rdf-spec#39. This adds conformance, and
allows `SPARQL::Client::Repository` to use the new
`Mutable#delete_insert` interface. It does not yet implement an
effecient SPARQL `#delete_insert`.

It may be possible to refactor `#delete_statements` in response to these
changes, to remove the code smells called out in the comment in that
method.

* Update Ruby versions.

* Updates for keyword arguments.

* Update required ruby version >= 2.0.

* Change gemspec dependencies to '>= 1.99', '< 3' in prep for 2.0.0.beta release.

* Change calling sequence to Repository to use `uri` named parameter instead of fixed `endpoint` parameter.

@no-reply, you might look at the Repository failures, as they related to Transaction changes.

* Change pattern of uri named parameter for backwards compatibility with earlier versions of Ruby 2.x

* Set version to 2.0.0.beta1 and change gemspec dependencies to '>= 2.0.0.beta', '< 3' until 2.0.0 is released.

* Don't run coverage unless the gem is loaded.

* Update gemspec. Remove README symlink.

* Allow configuration of keep-alive

* Add CONTRIBUTING.md.

* Fix CONTRIBUTING typos.

* Updates for release 2.0.0.

* Update Gemfile dependencies.

* content_type is always passed to RDF::Reader.for

* raise error when no suitable rdf reader is found

Currently sparql-client will silently fail with a nil if no suitable rdf reader is found. This can lead to the awkward situation where a nil is returned for the query and the user has to find out what is causing it. This commit tries to quicken this debugging proces by at least indicating the point of failure by raising a descriptive error.

* set appropriate content types in mocks

because an error is now raised when no rdf reader is found, the content
type in the mocked responses needs to be correct

* call original when rdf reader is mocked

* require rdf/turtle to have a reader for turtle

* Fixed quality values in generated Accept headers (closes ruby-rdf#69).

* Added pre/post HTTP request hooks for monitoring/debugging.

* Bumped the version to 2.0.1.

* Update client_spec mock request expectations to expaect "q=.." instead of "p=..".

* Update minimum ruby version, and change sxp repo location in Gemfiles.

* Remove wirble from Gemfile, as dependency-ci objects that it has no license and it's not really neccessary.

* Change Travis JRuby to default and allow failures.

* Handle empty response body on update queries

Update responses are implementation defined; some servers return an
empty body and no content type. This handles that case.

* Implements tests for update alternative endpoint

* Fixed problems with the alternative endpoint

The alternative endpoint is set optionally in the update method if an
endpoint is provided in the options. If the next call doesn't provide an
alternative endpoint, it should use the instance's configured endpoint.

Also: a call to the query method may use the make_post_request method.
We should make sure to not keep any previous alternative endpoint.

Note: this commit not only fix the current issues with the alternative
endpoints, it also implements a new functionality: the possibility to
override the configured endpoint when calling the query method.

* Improvement: don't parse update response

The result of an update is not accessible to the caller. Therefore,
there is no point of parsing it and it's a risk that the whole call
fails if an error occurs during the parsing.

Fixes ruby-rdf#71

* Skip depencency checking on rdf-isomorphic.

* Version 2.1.0.

* Remove require of 'sparql/client' from Rakefile.

* Add block forms for `#where` and `#optional`, this allows sub-queries to be run within the block, and filters to be added to the OPTIONAL block.
Fixes ruby-rdf#75.

* Add support for UNION, either with triple patterns, subquery, or block form.
Fixes ruby-rdf#32.

* Add support for MINUS, pretty much equivalent to UNION support.
Fixes ruby-rdf#65.

* Add documentation for select `:count` option. Not quite what was requested in ruby-rdf#27, but probably good enough.

* Add `Repository#each_statement` using a copy from `Enumerable`. This is because the `Dataset#each_statement` uses an internal instance variable which is not set generically. (We may want to think about this, as `Repository` subclasses `Dataset`, and we either need to give guidance to developers on what methods need to be implemented, or fallback to the correct behavior if `@statements` doesn't exist. `SPARQL::Client::Repository#initialize` calls super (`RDF::Repository#initialize`), which doesn't call `RDF::Dataset#initialize`, thus the `@statements` instance variable is never set.

* Add supports `literal_equality` so that spec count passes.

* Add support for net-http-persistent 3.x

* Update webmock to ~> 2.3.
Use `Solution#to_h` instead of `#to_hash`.

* Use Travis "trusty" build and wildcard Ruby versions.

* Fix broken Markdown headings

* Remove rubyforge reference.

* Fix comment

Looks like the comment from the previous block was copy-and-pasted.

* Fix example code

[insert | delete]_data happen in the context of the sparql client.

* Relax dependencies for 3.0 release.

* Fix Gemfile.

* Version 2.2.0.

* Version 2.2.1.

* Update yard ~> 0.9.12 due to vulnerability.

* Allow VALUES to be specified using `Query#values`.

* Update dependencies.

* Version 3.0.0

* Improved error message

* Remove gemspec deprecations.

* use ruby syntax highlighting for readme
add links to rubydocs
add prefix example

* remove prefix example, add as separate pr

* add support for RDF::URI prefixes
favor class Module::Class over module Module; class Class

* match code style in spec
ignore rbenv ruby-version file

* support prefix hashes
update tests
leave docs alone since hash format is now supported

* added a default graph option

* support multiple default graphs

* Added Tests for the default-graph feature

* Version 3.0.1.

* Update travis config to deal with rubygems not supporting ruby < 2.3 any longer.

See rubygems/rubygems#2534.

* Add 2.6 to travis RVM matrix.

* Update Gemfile.

* Add default for User-Agent HTTP header, and fix code that sets default headers when creating the client.

Fixes ruby-rdf#94.

* Add `Client#close` to shutdown any HTTP connection and object finalizer to do the same.

Fixes ruby-rdf#86.

* Run 2.2.2 on Travis.

* Remove jruby-openssl from Gemfiles.

* Updates for 3.1 release and Ruby 2.7 calling sequences.

* Update URLs to use HTTPS, where possible.

* Update doap:license to https://unlicense.org/.

* Update doap:license.

* Use `each_statement.count` instead of `each_statement {count+=1}`. as Enumerable#count will handle this case.

* Use `optimize: true` for queries to the sparql gem.

* Update PDD info in the README.

* Update gem dependencies.

* CI on GitHub.

* Fix README badges.

* Fix bug in values when value is not a literal.

Fixes ruby-rdf#96

* Explicitlly require 'delegate' and add example for ruby-rdf#92.

* Version 3.1.1.

* Update net-http-dependency to >= 4.0.1 and enable CI on Ruby 3.0.

* Change finalizer for closing HTTP connections to a class method.

See https://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/

* Version 3.1.2.

* Add .coveralls.yml.

* Change Nokogiri dependency in Gemfile to ~> 1.10, as 1.11 no requires Ruby ~> 2.5.

* Add triple forms parsing to XML and JSON results for RDF-star.

* Fixes for REXML used in Gemfile-pure.

* Update CI for coveralls.

* Update CI.

* Update documentation, dependencies, and version sync for 3.2.

* CI on Ruby 3.1.

* Force CI on Ruby "3.0".

* Run client finalizer in a Thread (if possible) for emergent Ruby 3.1 issue with net-http-persistent.

* * Force running REXML in addition to Nokogiri by adding `library` keyword argument to `parse_xml_bindings`.
* Put documents on GH-pages.
* Improve TSV parsing and CSV tests.

* Set variable_names in solutions, which might not be the same as the actual variables used in the solutions.

* Since cc9812c the interface to specify options has changed

* Add test for correct hand over of the graph parameter

* remove focus tag

* Add WebMock around test for default query specifying default graph.

* Version 3.2.1.

* Tolerate an empty binding in XML.

* CI on 3.2.

* Update badges

* Adds support for Federated Service with SERVICE keyword.

Fixes ruby-rdf#99.

* Update dependencies.

* Version 3.2.2.

* make it work with ruby <= 3.0

* add pry gem to use binding.pry

* use INSERT { GRAPH ...} instead of INSERT GRAPH {...} to work for virtuoso

* put again NCBO custom code

* handle the insert data bug of Virtuoso

* put again the fix of the string datatype for 4store and Virtuoso

---------

Co-authored-by: Gregg Kellogg <gregg@kellogg-assoc.com>
Co-authored-by: Tom Nixon <tom@tomn.co.uk>
Co-authored-by: Christophe Desclaux <descl@zouig.org>
Co-authored-by: Danny Tran <dannybtran@gmail.com>
Co-authored-by: Gregg Kellogg <gregg@greggkellogg.net>
Co-authored-by: Ben Peters <ben@bencpeters.com>
Co-authored-by: Arto Bendiken <arto@bendiken.net>
Co-authored-by: Tom Johnson <thomas.johnson@oregonstate.edu>
Co-authored-by: Nick Gottlieb <ngottlieb@gmail.com>
Co-authored-by: Marcel Otto <marcelotto@gmx.de>
Co-authored-by: Justin Coyne <justin@curationexperts.com>
Co-authored-by: Christoph Kindl <mail@ckristo.net>
Co-authored-by: Tom Johnson <tom@dp.la>
Co-authored-by: nielsv <niels.vandekeybus@tenforce.com>
Co-authored-by: Cecile Tonglet <cecile.tonglet@gmail.com>
Co-authored-by: Chris Beer <chris@cbeer.info>
Co-authored-by: Santiago Castro <santi.1410@hotmail.com>
Co-authored-by: David Rupp <david@ruppworks.com>
Co-authored-by: Richard Degenne <richdeg2@gmail.com>
Co-authored-by: conors_nli <conor.sheehan.2@ucdconnect.ie>
Co-authored-by: Nime <sebastianz541@googlemail.com>
Co-authored-by: Natanael Arndt <arndtn@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants