|
Hi, I'm importing data from DBPedia and as you know, the vocabulary used to describe the information is not very consistent and/or complete. I'm trying to figure out a way to specify in my SPARQL query a "fallback" property in case the property I'm interested in is not set. Example : I want to retrieve the website of a list of municipalities (one per municipality). Most of them have a property called foaf:homepage, but sometimes they don't. In this case, I want to use the property dbpprop:website instead, if it exists, and if it doesn't, as a last resort dbpedia-owl:wikiPageExternalLink often contains what I need. The only "documentation" I could find on this matter is this blog post : Fallback with SPARQL. The proposed solution seems to be working quite well but it's, in my sense, very verbose for such a common problem. If I want to retrieve several properties, my query will be 100 lines long and I would very much like to avoid that. Does anyone know anything about a proper way to solve this problem ? (edit) DBPedia case For other endpoints, good solutions can be found in the answers below, but since SPARQL 1.1 is not implemented in DBPedia's Virtuoso, BIND, COALESCE, MINUS, NOT EXISTS or property paths are not available. In addition, its interpretation of OPTIONAL seems to be counter-intuitive (addressed in this question), meaning that the last solution doesn't work. Can we conclude that there is no way to use fallback properties on DBPedia's endpoint ? |
|
This is possible in SPARQL 1.1 property paths http://www.w3.org/TR/sparql11-query/#propertypaths I am not sure if Virtuoso 6 supports these, but one way of finding out is as follows:
Wow, I had no idea this existed. I'll test it right away, thanks ! Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Invalid character in SPARQL expression at '|' It is sad, because this is exactly what I was looking for... DBpedia (i.e. the release of Virtuoso that it's using) doesn't support SPARQL 1.1 yet. |
|
Something like (similar to Bob's blog but avoiding the cross product cases for more than 2 cases)
which makes each (after the first) OPTIONAL clause conditional on not having set Looks like a good alternative. The !bound seems problematic though, I'll tweak around to see why. No - not necessary in this case (one defaulting variable). If there were other variables that could contribute to cardinality of the OPTIONAL when it matched, then, yes, the FILER(!bound) makes a difference. Could you see the clarification question here: http://answers.semanticweb.com/questions/12710/sparql-fallback-property The solution still doesn't seem to work for the DBpedia SPARQL endpoint. 1
Virtuoso has been reported to set unbounds to NULL (like SQL), not leave unbound. It does not execute the dependent OPTIONALs with ?website unbound. Try using COALESCE (not checked on DBpedia)
or SELECT (COALESCE(?website1, ?website2, ?website3) AS ?website) .... SDB uses SQL-COALESCE when translating SPARQL to SQL. Unfortunately, BIND and COALESCE are only available in SPARQL 1.1... which is not implemented in DBPedia's store.
showing 5 of 6
show 1 more comments
|
|
After reading the comments of the above mentioned blog post (why didn't I read them before ?), it turns out there is a simple solution "taking advantage of the fact that the OPTIONAL keyword is left-associative to express an ordered preference between predicates" (src1, src2). The query would look like this :
... but somehow I can't make it work... This is pretty much a broken version of Andy's answer! :) (You should check to see that 1
This is based on my src2 link, which states : "This idiom takes advantage of the fact that the OPTIONAL keyword is left-associative to express an ordered preference between predicates within our SPARQL query1. That is: OPTIONAL { ?who foaf:name ?name } OPTIONAL { ?who rdfs:label ?name } can be read as (given that ?who is already bound by the first (non-optional) triple pattern in the query): Bind ?name to the object of either the foaf:name or rdfs:label predicates; but if both such bindings exist, we prefer the object of foaf:name." Basically, it should avoid the burden of checking if ?website is already bound. Anyway, I could not make any of these two versions (with and without the binding check) work on dbpedia... Apologies, right yep. If In that case, your solution is more elegant than Andy's. BTW, this works well for the DBpedia SPARQL endpoint... ...taking a silly example:
...swapping 1
I swapped foaf:name and rdf:type and tried it: the ones that have no foaf:name return nothing (they should return the type instead). Did I understand something wrong ? Hmm... Fooling around a bit with optionals, it seems that Virtuoso's handling of them seems a little broken. E.g.:
I cannot explain why this doesn't work (but it does when swapped). ...might ask another question (this one is cluttered). 1
Just to be sure, I tried this kind of query on my repository (Sesame+OWLIM) and it works just fine. So there's definitely something wrong with Virtuoso...
showing 5 of 8
show 3 more comments
|
|
Hi, Virtuoso does support the features from above. Here is list of example collections re. Virtuoso SPARQL 1.1. support:
Other links: Best Regards, Rumi Kocis |


in general i still find myself struggling with defaults in RDF-based systems