SPARQL is a graph pattern matching language, The matching can be semantic (basic graph patterns are entailed by the data in order to match) or through the algebra that works over the top of basic graph pattern matching.
Complex queries make use of UNION
and OPTIONAL
,
including the idiom with
OPTIONAL
and !BOUND
, in all sorts of creative ways.
And sometimes they want the answers to indicate which way the query matched
to get a particular solution.
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT ?label { { ?concept skos:prefLabel ?label } UNION { ?concept skos:altLabel ?label } }
Which matched? the prefLabel
or altLabel
?
The application can assign the different to branches to different
variables although that might be inconvenient in a larger query
if ?label
is used elsewhere in the query.
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT ?label_pref ?label_alt { { ?concept skos:prefLabel ?label_pref } UNION { ?concept skos:altLabel ?label_alt } }
ARQ's property functions provide a way of adding different ways to match a triple pattern. Uses include calling out to custom code to perform some calculation, or calling out to use a external index, such as Lucene.
Property functions can assign to variables in the solution - unlike
FILTER
functions which must not have side effects. Property
functions can't change values but they can bind an unbound variable or check
an existing binding is compatible.
PREFIX apf: <java:com.hp.hpl.jena.query.pfunction.library.> SELECT ?x { ?x apf:assign "Hello World" }
----------------- | x | ================= | "Hello World" | -----------------
It works both ways round: { "Hello World" apf:assign ?x }
gives the same results.
If subject and object are constants or already bound,
apf:assign
checks to see if they are the same. If they are,
the tripe pattern
matches with no change to the solution, otherwise it does not match and that solution
is rejected. The "sameness" is Jena's
sameValueAs
,
just like the rest of graph matching.
If both variables are unbound, apf:assign
complains
and the query is broken.
No comments:
Post a Comment