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?
A SPARQL query may specify the dataset to be used for matching by using the FROM clause and the FROM NAMED clause to describe the RDF dataset. If a query provides such a dataset description, then it is used in place of any dataset that the query service would use if no dataset description is provided in a query.
SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports aggregation, subqueries, negation, creating values by expressions, extensible value testing, and constraining queries by source RDF graph.
"PREFIX", however (without the "@"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.
Abstract. RDF is a directed, labeled graph data format for representing information in the Web. This specification defines the syntax and semantics of the SPARQL query language for RDF.
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 |
---------------------------------
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) )
}
That's exactly the purpose of ASK queries in SPARQL:
PREFIX : <http://test.com/#>
ASK WHERE {
?graph :hasNode ?node .
?graph :centerNode ?node .
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With