Checking for a predicate in SPARQL

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 associated with :graphX with the predicate :centerNode , I return true (or some other indication) otherwise false ; The result will look something like this:

 ?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 this be done in SPARQL 1.1?

+4
source share
3 answers

You can combine EXISTS and BIND in SPARQL 1.1:

 PREFIX : <http://test.com/#> SELECT * WHERE { ?graph :hasNode ?node . BIND( EXISTS { ?graph :centerNode ?node } as ?isCentered ) } 

Using Jena ARQ, I get the following 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 | --------------------------------- 
+2
source

In SPARQL 1.0,

 SELECT * { ?graph :hasNode ?node . OPTIONAL{ ?graph :centerNode ?node1 FILTER sameterm(?node, ?node1) } } 

and? node1 will be connected or not connected in the answers. However, the cardinality is dirty.

ADDITIONALLY/! BOUND can perform NEEDS:

 SELECT * { ?graph :hasNode ?node . OPTIONAL{ ?graph :centerNode ?node1 FILTER sameterm(?node, ?node1) } FILTER( !bound(?node1) ) } 
+3
source

That is the purpose of ASK queries in SPARQL:

 PREFIX : <http://test.com/#> ASK WHERE { ?graph :hasNode ?node . ?graph :centerNode ?node . } 
+1
source

Source: https://habr.com/ru/post/1479370/


All Articles