Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing the presence of a predicate in SPARQL

Tags:

rdf

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 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?

like image 860
0xFF Avatar asked May 06 '13 14:05

0xFF


People also ask

How do I query using SPARQL?

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.

What types of queries does SPARQL support?

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.

What is prefix in SPARQL?

"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.

What is an abstract SPARQL query?

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.


3 Answers

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       |
---------------------------------
like image 85
Joshua Taylor Avatar answered Oct 19 '22 11:10

Joshua Taylor


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) )
}
like image 4
AndyS Avatar answered Oct 19 '22 12:10

AndyS


That's exactly the purpose of ASK queries in SPARQL:

PREFIX : <http://test.com/#>
ASK WHERE { 
  ?graph :hasNode ?node .
  ?graph :centerNode ?node .
}
like image 1
wikier Avatar answered Oct 19 '22 12:10

wikier