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 ?

asked 15 Nov '11, 05:45

morphyn's gravatar image

morphyn
48028
accept rate: 0%

edited 17 Nov '11, 06:53

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

(15 Nov '11, 10:08) database_animal ♦ database_animal's gravatar image

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:

PREFIX ...
SELECT ...
WHERE {
...
?municipality  foaf:homepage|dbpprop:website ?someURI.
...
}
link

answered 15 Nov '11, 06:02

William%20Greenly's gravatar image

William Greenly
5.1k412
accept rate: 13%

edited 15 Nov '11, 06:07

Wow, I had no idea this existed. I'll test it right away, thanks !

(15 Nov '11, 06:53) morphyn morphyn's gravatar image

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...

(15 Nov '11, 07:02) morphyn morphyn's gravatar image

DBpedia (i.e. the release of Virtuoso that it's using) doesn't support SPARQL 1.1 yet.

(15 Nov '11, 07:38) bobdc bobdc's gravatar image

foaf:homepage|dbpprop:website will do it but return the results for dbpprop:website when foaf;homepage exists.

Something like (similar to Bob's blog but avoiding the cross product cases for more than 2 cases)

SELECT 
{
  ...
  OPTIONAL { ?municipality foaf:homepage ?website }
  OPTIONAL { ?municipality dbpprop:website ?website
             FILTER (!bound(?website)) }
  OPTIONAL { ?municipality dbpedia-owl:wikiPageExternalLink ?website
             FILTER (!bound(?website)) }
}

which makes each (after the first) OPTIONAL clause conditional on not having set ?website in the query yet.

link

answered 15 Nov '11, 06:59

AndyS's gravatar image

AndyS ♦
13.0k37
accept rate: 32%

Looks like a good alternative. The !bound seems problematic though, I'll tweak around to see why.

(15 Nov '11, 07:17) morphyn morphyn's gravatar image

@AndyS, are the !bound clauses necessary here? See @morphyn's answer below.

(16 Nov '11, 08:36) Signified ♦ Signified's gravatar image

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.

(16 Nov '11, 09:05) AndyS ♦ AndyS's gravatar image

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.

(16 Nov '11, 09:17) Signified ♦ Signified's gravatar image
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)

OPTIONAL { <municipality_x_uri> foaf:homepage ?website1 .}
OPTIONAL { <municipality_x_uri> dbprop:website ?website2 .}
OPTIONAL { <municipality_x_uri> dbowl:wikiPageExternalLink ?website3 .}
BIND(COALESCE(?website1, ?website2, ?website3) AS ?website)

or SELECT (COALESCE(?website1, ?website2, ?website3) AS ?website) ....

SDB uses SQL-COALESCE when translating SPARQL to SQL.

(16 Nov '11, 09:25) AndyS ♦ AndyS's gravatar image

Unfortunately, BIND and COALESCE are only available in SPARQL 1.1... which is not implemented in DBPedia's store.

(17 Nov '11, 06:37) morphyn morphyn's gravatar image
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 :

PREFIX ...
SELECT ...
WHERE{
    OPTIONAL { <municipality_x_uri> foaf:homepage ?website .}
    OPTIONAL { <municipality_x_uri> dbprop:website ?website .}
    OPTIONAL { <municipality_x_uri> dbowl:wikiPageExternalLink ?website .}
}

... but somehow I can't make it work...

link

answered 15 Nov '11, 07:46

morphyn's gravatar image

morphyn
48028
accept rate: 0%

This is pretty much a broken version of Andy's answer! :)

(You should check to see that ?website is not already bound.)

(15 Nov '11, 09:41) Signified ♦ Signified's gravatar image
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."

(15 Nov '11, 09:49) morphyn morphyn's gravatar image

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...

(15 Nov '11, 09:52) morphyn morphyn's gravatar image

Apologies, right yep. If ?website is bound by foaf:homepage then the latter two OPTIONALs can only be bound by the same value. But, it doesn't matter if they are bound or not since the clause is OPTIONAL.

In that case, your solution is more elegant than Andy's.

(15 Nov '11, 12:37) Signified ♦ Signified's gravatar image

BTW, this works well for the DBpedia SPARQL endpoint...

http://dbpedia.org/sparql/

...taking a silly example:

SELECT DISTINCT *
WHERE { 
   ?s a foaf:Person .
   OPTIONAL { ?s rdf:type ?typeOrName .}
   OPTIONAL { ?s foaf:name ?typeOrName .}
}
LIMIT 10

...swapping OPTIONALs has the desired effect.

(15 Nov '11, 12:41) Signified ♦ Signified's gravatar image
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 ?

(16 Nov '11, 05:37) morphyn morphyn's gravatar image

Hmm... Fooling around a bit with optionals, it seems that Virtuoso's handling of them seems a little broken. E.g.:

SELECT DISTINCT *
WHERE { 
   <http://dbpedia.org/resource/Ronnie_Jones> dbpprop:name ?hasname .
   OPTIONAL { <http://dbpedia.org/resource/Ronnie_Jones> foaf:name ?name }
   OPTIONAL { <http://dbpedia.org/resource/Ronnie_Jones> dbpprop:name ?name }
}

I cannot explain why this doesn't work (but it does when swapped).

...might ask another question (this one is cluttered).

(16 Nov '11, 08:34) Signified ♦ Signified's gravatar image
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...

(16 Nov '11, 08:48) morphyn morphyn's gravatar image
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

link

answered 14 Jan '13, 08:29

rumito's gravatar image

rumito
111
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1,251
×264
×158

Asked: 15 Nov '11, 05:45

Seen: 2,661 times

Last updated: 14 Jan '13, 08:29