ruby-rdf / sparql

Ruby SPARQL library
http://rubygems.org/gems/sparql
The Unlicense
88 stars 14 forks source link

Executing a SPARQL 1.1 construct query gives Argument error #9

Closed mwkuster closed 11 years ago

mwkuster commented 11 years ago

Executing a SPARQL 1.1 construct query gives a SPARQL::MalformedQuery: ArgumentError: wrong number of arguments exception

Query: https://github.com/mwkuster/eli-budabe/blob/master/sparql/eli_md.rq Sample data for the query: https://github.com/mwkuster/eli-budabe/tree/master/samples

The query executes correctly against ARQ and validates using http://www.sparql.org/query-validator.html

irb(main):011:0> g2 = SPARQL.execute(query, e.repo) SPARQL::MalformedQuery: ArgumentError: wrong number of arguments (5 for 3) at line 53, production = :BuiltInCall ArgumentError: wrong number of arguments (2 for 3) at line 53, production = :BuiltInCall ArgumentError: wrong number of arguments (0 for 1) at line 53, production = :BuiltInCall ArgumentError: wrong number of arguments (0 for 1) at line 53, production = :BuiltInCall NoMethodError: undefined method unshift' for nil:NilClass at line 53, production = :Bind from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql.rb:87:inrescue in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql.rb:66:in execute' from (irb):11 from /opt/local/bin/irb1.9:12:in

'

mwkuster commented 11 years ago

Version of the libraries used: sparql (1.0.7) sparql-client (1.0.1) (the latest one available via Ruby-Gem).

gkellogg commented 11 years ago

Congratulations for finding a corner case not present in the test cases. Complex as it is, everything in here should be supported. I'll try to simplify this to get at the root problem.

mwkuster commented 11 years ago

Promise, didn't do it on purpose :-)

Please le me know if I can be of any assistance

gkellogg commented 11 years ago

It ended up being fairly straightforward (presuming that the query actually does what you want). CONCAT was defined to take just two arguments, not a variable number of arguments. Simple update to use Enumerable#inject to get results.

mwkuster commented 11 years ago

Thanks a lot! Technically it works now (I've just locally replaced in the Gem concat.rb) with the new version

However, the end result is very different from the one using ARQ (Jena version 2.10.0) on the same query, and I would like to think that ARQ's results are the good ones:

Source: https://github.com/mwkuster/eli-budabe/blob/master/sparql/source.ttl

ARQ's results: https://github.com/mwkuster/eli-budabe/blob/master/sparql/sparql_arq.ttl

The library's results: https://github.com/mwkuster/eli-budabe/blob/master/sparql/sparql_ruby.ttl

Query is unchanged: https://github.com/mwkuster/eli-budabe/blob/master/sparql/eli_md.rq

gkellogg commented 11 years ago

I new it wouldn't be that easy :(. I'll need to be sure I'm generating the same algebra; if so, I should also get the same results.

mwkuster commented 11 years ago

Just to alert you that I've committed a slightly modified version of the original query (strictly no impact on the results with ARQ and the library, though)

mwkuster commented 11 years ago

Sorry to slightly highjack this ticket for a related question, but is there a defined interface to register specific new custom extension functions? If so, must they also be derived from Operator with the Evaluatable mixin?

gkellogg commented 11 years ago

There isn't yet a way to define custom functions; if there were, it would be by extending evaluatable, as with others such functions. It would also need a way to register the classes, so they could be dispatched. I'd certainly consider submissions to handle this.

The current issue seems to relate to a difference in generating the algebra for the query; I'm tied up at the moment, but will get to it. It's also possible to directly execute an SSE query, which could be an alternative for you in the mean time.

mwkuster commented 11 years ago

OK, can have a go at the custom functions, probably not before the weekend though

gkellogg commented 11 years ago

@mwkuster, I found an error in how the algebra was being generated, which could have accounted for the differences in output you were seeing. Could you check your results again with the top of the master branch?

mwkuster commented 11 years ago

thanks, but unfortunately no luck yet (Gem updated based on git clone https://github.com/ruby-rdf/sparql.git, then gem1.9 build sparql.gemspec ):

irb(main):003:0> g = SPARQL.execute(e.legal_resource_query, e.repo) NoMethodError: undefined method join' for nil:NilClass from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/iri.rb:34:inapply' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/evaluatable.rb:16:in evaluate' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/extend.rb:40:inblock (2 levels) in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/extend.rb:38:in each' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/extend.rb:38:inblock in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/extend.rb:36:in each' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/extend.rb:36:inexecute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/join.rb:39:in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/left_join.rb:38:inexecute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/extend.rb:35:in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/construct.rb:41:inexecute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/prefix.rb:30:in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql.rb:85:inexecute' from (irb):3 from /opt/local/bin/irb1.9:12:in `

' irb(main):004:0> e.legal_resource_query => "PREFIX cdm: http://publications.europa.eu/ontology/cdm#\nPREFIX eli: http://eurlex.europa.eu/eli#\nPREFIX xsd: http://www.w3.org/2001/XMLSchema#\nPREFIX owl: http://www.w3.org/2002/07/owl#\n\nCONSTRUCT {\n ?eli eli:id_document ?eli_str ;\n eli:type_document http://publications.europa.eu/resource/authority/resource-type/dir ;\n eli:agent_document ?agent_document ;\n eli:id_local ?celex ;\n eli:is_about ?eurovoc ;\n eli:date_document ?date_document ;\n eli:date_entry-in-force ?date_entry_in_force ;\n eli:date_no-longer-in-force ?date_no_longer_in_force ;\n eli:cites ?cites ;\n eli:transposes ?transposes ;\n eli:based_on ?based_on ;\n eli:basis_for ?basis_for ;\n eli:changes ?changes ;\n eli:changed_by ?changed_by .\n ?expr_eli eli:expression_title ?title ;\n eli:language_expression ?lang ;\n eli:belongs_to ?eli .\n ?manif_eli eli:manifests ?expr_eli ;\n eli:link_manifestation ?item ;\n eli:publisher \"Publications Office\"@en .\n} WHERE {\n ?subj cdm:work_created_by_agent ?agent_document ;\n cdm:resource_legal_id_celex ?celex ;\n cdm:work_date_document ?date_document .\n OPTIONAL { ?subj cdm:work_is_about_concept_eurovoc ?eurovoc }\n OPTIONAL {?subj cdm:resource_legal_date_entry-into-force ?date_entry_in_force }\n OPTIONAL {?subj cdm:resource_legal_date_end-of-validity ?date_no_longer_in_force }\n OPTIONAL { ?subj cdm:work_cites_work ?cites }\n OPTIONAL { ?subj cdm:resource_legal_based_on_resource_legal ?based_on }\n OPTIONAL { ?subj cdm:resource_legal_implemented_by_resource_legal ?transposes }\n OPTIONAL { ?subj cdm:directive_implemented_by_measure_national_implementing ?transposes }\n OPTIONAL { ?subj cdm:resource_legal_basis_for_resource_legal ?basis_for }\n OPTIONAL { ?subj cdm:resource_legal_amends_resource_legal ?changes } #and many more variants\n OPTIONAL { ?subj cdm:resource_legal_amended_by_resource_legal ?changed_by }\n BIND (http://eli.budabe.eu/eli/dir/2010/24/oj AS ?eli)\n BIND (str(?eli) AS ?eli_str)\n\n ?expr cdm:expression_title ?title .\n ?expr cdm:expression_useslanguage ?lang .\n BIND ( IRI(lcase(replace(str(?expr), \"^.\.([A-Z]{3})$\", concat(?eli_str, \"/\", \"$1\")))) AS ?expr_eli)\n\n ?manif cdm:manifestation_manifests_expression ?expr .\n OPTIONAL { \n ?manif_cellar_id owl:sameAs ?manif .\n ?item cdm:item_belongs_to_manifestation ?manif_cellarid . \n } #Print manifestations have no link to a file\n BIND ( IRI(lcase(replace(str(?manif), \"^.\.([A-Z]{3})\.([a-z0-9]+)$\", concat(?eli_str, \"/\", \"$1\", \"/\", \"$2\")))) AS ?manif_eli)\n}" irb(main):005:0>

gkellogg commented 11 years ago

That problem was due to running the query without a base URI; I corrected this in fc13d9e2cc444ae18c6460adb6f066d78b5c851c. Can you verify that you have that version, with the updated iri.rb?

gkellogg commented 11 years ago

I verified running your example with https://github.com/ruby-rdf/sparql/blob/master/examples/issue9.rb, but didn't exhaustively check the actual results; the algebra which is created now should be identical to that from ARQ.

mwkuster commented 11 years ago

Did another git pull and rebuilt the gem. However, get now another error (same query as before):

e.legal_resource_query: as before e.repo: Repository based on https://github.com/mwkuster/eli-budabe/blob/master/sparql/source.ttl

irb(main):003:0> g = SPARQL.execute(e.legal_resource_query, e.repo) NoMethodError: undefined method execute' for #<Array:0x007fb2ce6df758> from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/extend.rb:35:inexecute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/project.rb:30:in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/distinct.rb:32:inexecute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql/algebra/operator/prefix.rb:30:in execute' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/sparql-1.0.7/lib/sparql.rb:85:inexecute' from /Users/marcwilhelmkuster/sandbox/OP/eli-budabe/eli.rb:60:in eli' from /Users/marcwilhelmkuster/sandbox/OP/eli-budabe/eli.rb:76:inlegal_resource_query' from (irb):3 from /opt/local/bin/irb1.9:12:in `

' irb(main):004:0>

mwkuster commented 11 years ago

Sorry, wrong alarm. This is an entirely unrelated issue. Having tested the result with the new version I can confirm that everything looks great now. Thanks a lot!

mwkuster commented 11 years ago

However, the library does fail on a different query with said error message. Since it seems a different problem, I've opened a new ticket for it

gkellogg commented 11 years ago

Cool.