Inspired by a few related questions (in particular this one), I wanted to ask a more general form of the question: how can I specify preferences for basic graph pattern matching in SPARQL (1.1) such that potentially many lower preferences are only matched if a higher preference cannot be found?
It seems there are a few potential solutions and I hope to gather some alternative patterns, some of which may work best in different concrete scenarios.
To make the general question concrete, but in a way that can easily be generalised, say I want to get the labels of each resource of type
The basic query in SPARQL would be:
How can this query be adapted to return names from predicates in the above preference order such that values for a given predicate are only returned if no value is returned for a higher-preference predicate? (I.e., if we find a
A sequence of OPTIONALs gets a sort of conditional testing but it does not allow the input from VALUES or the rest of the query.
To allow the input to be defined not by the syntax order (returns one value at the top pref level):
Given that used as a subquery, you can use it to get the ?p and all all ?person ?p is you want all values.
answered 31 Jan, 05:05
A variation on the
This may be useful if you want to provide a fallback default in the absence of any of the preferred matches being found e.g.
answered 31 Jan, 08:04
Rob Vesse ♦
We find the subjects and properties, with preference numbering. We then check to see there isn't a better match.
answered 31 Jan, 13:28