How do I change the query formula based on whether or not a variable is bound?
I am invoking the magic property like this:
WHERE {
VALUES (?subj) {
([my bound positional parameter value goes here...])
}
?subj :myMagicProperty ?result .
}
Inside the magic property, I do a union:
?result a :Rule .
{
?result :someProp ?subj .
}
UNION
{
FILTER NOT EXISTS {
?result :someProp ?anyValue .
}
}
In other words, get me all results where :someProp
is this value or :someProp
is not defined.
Here is the tricky part. If ?subj
is unbound (i.e., I set it as UNDEF
in the VALUES
block), the above query goes wild and returns everything.
Instead, I want to check if ?subj
is unbound. If ?subj
is unbound, :myMagicProperty
should only return the following results:
FILTER NOT EXISTS {
?result ?someProp ?anyValue .
}
I have experimented with using FILTER
and the BOUND
function, but I can't figure out how to get the correct behavior. How can I drop one of UNION
clauses from my query when ?subj
is not bound?
Updates
Revised the first query to add the VALUES
block.
Added missing ?result a :Rule .
statement.
Corrected ?someProp
to :someProp
.