1

I've been trying to query an OWL data using RDFlib (SPARQL), but I didn't get why it doesn't work. I tested the same query in Protege (SPARQL query) and it works perfectly! This is my code:

import rdflib
from rdflib import plugin
from rdflib.graph import Graph

g = Graph()
g.parse("/localPath/a.owl")

from rdflib.namespace import Namespace
ns = Namespace("http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#")

plugin.register(
    'sparql', rdflib.query.Processor,
    'rdfextras.sparql.processor', 'Processor')
plugin.register(
    'sparql', rdflib.query.Result,
    'rdfextras.sparql.query', 'SPARQLQueryResult')
#
qres = g.query(
                """
                    SELECT  DISTINCT ?varClass ?varSubClass ?varSubClassComment ?varProperty ?varPropComment
                        WHERE {
                                {
                                    ?varClass rdf:type owl:Class .
                                    ?varProperty rdf:type owl:ObjectProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment} .
                                    OPTIONAL{?varSubClass rdfs:subClassOf ?varClass ; rdfs:comment ?varSubClassComment} .
                        }
                    UNION
                        {
                                    ?varClass rdf:type owl:Class .
                                    ?varProperty rdf:type owl:DatatypeProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment}.
                        }
                             }
                """
                , initNs=dict(
                                ns=Namespace("http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#")
                              )
               )

for row in qres.result:
    #print ("%s %s %s %s %s" % row) # %s represent the fields selected in the query
    print row

print (len(qres.result))

My result is nothing. There's no error, but the length of the result file is 0. What am I doing wrong? Does anyone can help me?

Marcelo
  • 438
  • 5
  • 16
  • What are the contents of `"/localPath/a.owl"`? It's hard to diagnose why you're not getting any results if we can't see the data that you're querying against. – Joshua Taylor Nov 08 '13 at 16:14
  • What version of rdflib are you using? I'm not much of a Pythonista, but I just installed version 4.0.1 and version 0.4 of rdfextras, and when I run your code (changing the pathname to "a.owl", with the "a.owl" that you provided in the current directory), I get lots of results – Joshua Taylor Nov 08 '13 at 19:08
  • Thanks Joshua! Yes, the problem is not with data, but in the rdflib and the rdfextras outdated! Both packages were the cause of the error. I was using python from Canopy (rdflib v3.0.1-1 and rdfextras 0.1-1). I've updated both packages in the built-in python on mac osx and tested the same code. Now, it works. – Marcelo Nov 08 '13 at 20:33

1 Answers1

1

When I run this query (with prefixes defined) at sparql.org's query processor, I get a bunch of results:

PREFIX ns: <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

SELECT  DISTINCT ?varClass ?varSubClass ?varSubClassComment ?varProperty ?varPropComment
FROM <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf>
WHERE {
  {
    ?varClass rdf:type owl:Class .
    ?varProperty rdf:type owl:ObjectProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment} .
    OPTIONAL{?varSubClass rdfs:subClassOf ?varClass ; rdfs:comment ?varSubClassComment} .
  }
  UNION
  {
    ?varClass rdf:type owl:Class .
    ?varProperty rdf:type owl:DatatypeProperty ; rdfs:domain ?varClass . OPTIONAL{?varProperty rdfs:comment ?varPropComment}.
  }
}

I'd note that you can significantly simplify this query with values since you're using union just to specify owl:ObjectProperty and owl:DatatypeProperty:

PREFIX ns: <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

SELECT DISTINCT ?varClass ?varSubClass ?varSubClassComment ?varProperty ?varPropComment
FROM <http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf>
WHERE {
  VALUES ?propertyType { owl:ObjectProperty owl:DatatypeProperty }

  ?varClass rdf:type owl:Class .
  ?varProperty rdf:type ?propertyType ;
               rdfs:domain ?varClass .
  OPTIONAL{ ?varProperty rdfs:comment ?varPropComment }
  OPTIONAL{ ?varSubClass rdfs:subClassOf ?varClass ;
                         rdfs:comment ?varSubClassComment }
}

I don't see any reason that you need to be defining any prefixes for http://oaei.ontologymatching.org/2011/benchmarks/101/onto.rdf in your query, since you don't use any such namespace in your query. I'm assuming that that's the dataset that you're trying to query against, or perhaps that you expect the results to begin with that prefix (so perhaps defining the namespace makes their printed output nicer).

This strongly suggests that the problem is that the graph you're actually querying, from /localPath/a.owl isn't the same as that dataset, or that you're running some outdated versions of RDFlib, rdfextras, or both. I was able to run your Python code locally with RDFlib version 4.0.1 and rdfextras 0.4.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks Joshua, I am still learning sparql and I'm making lots of mistake. As I mentioned, if I run the same query in the sparql of protege, it gives the result I'm expecting. Do you have any clue, why it gives error when I run using RDFlib in Python? The file a.owl is a local file and I don't know how to share here. – Marcelo Nov 08 '13 at 18:19
  • You said in your question that "There's no error". The fact that your query returns results in Protégé and is recognized as valid by sparql.org suggests that it's not a problem with your query, but with the data that your query is being run against. As I asked for clarification in a comment on the main question, **what's the data that you're running this against. I.e., what's the content of `/localPath/a.owl`**? – Joshua Taylor Nov 08 '13 at 18:23
  • This is the link with the a.owl file ( https://www.dropbox.com/sh/cwlxa0o1tcbmfta/2coOveRXYK ). Maybe my code is a messy, but I want from this file all classes and properties with their respective labels and comments. That's what I'm trying to code. – Marcelo Nov 08 '13 at 18:48