Reputation: 1198
This is my data
@prefix : <http://example.org/rs#>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
:user1 :hasFirstName "user1First".
:user1 :hasLastName "user2Last".
:user1 :hasMobileNumber "013434334".
:user2 :hasFirstName "ania".
:user2 :hasLastName "ania".
This is my query (to get all people without a phone number)
PREFIX : <http://example.org/rs#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?firstName ?lastName ?mobileNumber
{
?user :hasFirstName ?firstName.
?user :hasLastName ?lastName.
minus {?user :hasMobileNumber ?mobileNumber}
}
it works very good. However, it i added an optional like this:
PREFIX : <http://example.org/rs#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?firstName ?lastName ?mobileNumber
{
?user :hasFirstName ?firstName.
?user :hasLastName ?lastName.
optional {?user :hasMobileNumber ?mobileNumber}
minus {?user :hasMobileNumber ?mobileNumber}
}
the reuslt becomes empty though it was just optional, and the optional will not afect the result of the query since both users will be results (with and without optional)
so why this is happening plase ?
Upvotes: 1
Views: 162
Reputation: 85813
If you look at the documentation, there's a description of what minus does, and even an example.
8.2 Removing Possible Solutions
The other style of negation provided in SPARQL is MINUS which evaluates both its arguments, then calculates solutions in the left-hand side that are not compatible with the solutions on the right-hand side.
In your case, the left hand side, once you add the optional, you always have a binding for ?user, and sometimes have a binding for ?mobileNumber. The results with the optional, but without the minus, look like this:
------------------------------------------------------
| user | firstName | lastName | mobileNumber |
======================================================
| :user1 | "user1First" | "user2Last" | "013434334" |
| :user2 | "ania" | "ania" | |
------------------------------------------------------
Now, when the minus and optional are both present, you get no results. I assume you understand why user1 is excluded. But why is user2 excluded?
The "left hand" part includes the bindings
------------------------------------------------------
| user | firstName | lastName | mobileNumber |
======================================================
| :user2 | "ania" | "ania" | |
------------------------------------------------------
and the right hand part (the part in the minus) doesn't have any matches. That means that all the solutions in the left hand part are compatible with it. That means that when you get the
solutions in the left-hand side that are not compatible with the solutions on the right-hand side.
you get nothing, because all the solutions in the left-hand side are not "not compatible" with the solutions on the right-hand side.
Upvotes: 3