I have the following RDF model:
@prefix : <http://test.com/#> .
:graph1 :hasNode :node1 ;
:centerNode :node1 .
:graph1 :hasNode :node2 .
:graph1 :hasNode :node3 .
I want to run a SPARQL query in which if a :nodeX
is related to a :graphX
with a predicate :centerNode
I return true
(or some other indication) otherwise false
; The result would look something like the following:
?n ?g ?centered
-----------------------------
:node1 :graph1 true
:node2 :graph1 false
:node3 :graph1 false
Is there a way to do this in SPARQL 1.0? if not, can it be done with in SPARQL 1.1?
In SPARQL 1.0,
SELECT * {
?graph :hasNode ?node .
OPTIONAL{ ?graph :centerNode ?node1 FILTER sameterm(?node, ?node1) }
}
and ?node1 will be bound or not bound in the answers. Cardinality is messy though.
OPTIONAL/!BOUND can do a sort of NOT EXISTS:
SELECT * {
?graph :hasNode ?node .
OPTIONAL{ ?graph :centerNode ?node1 FILTER sameterm(?node, ?node1) }
FILTER( !bound(?node1) )
}
You can combine EXISTS
and BIND
as follows in SPARQL 1.1:
PREFIX : <http://test.com/#>
SELECT * WHERE {
?graph :hasNode ?node .
BIND( EXISTS { ?graph :centerNode ?node } as ?isCentered )
}
Using Jena's ARQ, I get these results:
$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
--data predicate.n3 \
--query predicate.sparql
---------------------------------
| graph | node | isCentered |
=================================
| :graph1 | :node3 | false |
| :graph1 | :node2 | false |
| :graph1 | :node1 | true |
---------------------------------
That's exactly the purpose of ASK queries in SPARQL:
PREFIX : <http://test.com/#>
ASK WHERE {
?graph :hasNode ?node .
?graph :centerNode ?node .
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.