Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding Blank Nodes from SPARQL query results

I am using RDFLib to query on the Semantic Dicom Ontology. I am querying for owl:Class in the graph constructed from the above ontology. RDFLib returns results which contain blank nodes and I wish to exclude such queries. My query -

from rdflib import Graph
g = Graph()
g.parse('dicom.owl')
q = """SELECT ?c WHERE {?c rdf:type owl:Class}"""
qres = g.query(q)

dicom.owl is the Semantic Dicom Ontology downloaded in my machine.

Some of the results that I receive - Results of owl class queries

How can I modify my query to exclude all the blank nodes?

like image 670
Anmol Kagrecha Avatar asked May 22 '17 10:05

Anmol Kagrecha


People also ask

What is optional SPARQL?

OPTIONAL is a binary operator that combines two graph patterns. The optional pattern is any group pattern and may involve any SPARQL pattern types. If the group matches, the solution is extended, if not, the original solution is given (q-opt3. rq).

What is FOAF in SPARQL?

Dataset: Friend of a Friend (FOAF) @prefix card: <http://www.w3.org/People/Berners-Lee/card#> .

Is null in SPARQL?

The term “NULL” actually does not occur in the SPARQL spec, perhaps because it doesn't really have a good rep in the database world. Instead the spec talks about bound and unbound variables.

What is construct query in SPARQL?

The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.


1 Answers

from rdflib import Graph
g = Graph()
g.parse('dicom.owl')
q = """SELECT ?c WHERE { ?c rdf:type owl:Class .
       FILTER (!isBlank(?c)) }"""
qres = g.query(q)

Take a look at this family of SPARQL functions:

  • isIRI,
  • isBlank,
  • isLiteral, isNumeric.
like image 160
Stanislav Kralin Avatar answered Sep 30 '22 18:09

Stanislav Kralin