RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.16k stars 555 forks source link

SPARQL query does not recognize rdf:type #416

Closed scharrenbach closed 10 years ago

scharrenbach commented 10 years ago

Affected version 4.1.2

When loading data or adding data manually, querying for rdf:type returns no results. It does not matter whether data was added from a URL resource or manually.

When querying for a variable subject or iterating over the graph, the statements with a predicate of rdf:type are returned correctly.

from rdflib import Graph, URIRef
from rdflib.namespace import RDF

class MyGraph(Graph):
    def __init__(self):
        super(MyGraph, self).__init__()

def print_results(g, q):
    print('- - - - - - - - - - - - - - -')
    for x in g.query(q):
        print(x)
    print('- - - - - - - - - - - - - - -')

g = Graph()

subject = URIRef('http://example.com/test/')
my_type = URIRef('http://example.com/ontology/MyType')
g.load('http://dbpedia.org/resource/Semantic_Web')

#g.add((subject, URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), my_type))
#g.add((subject, RDF.type, my_type))

# returns predivates with rdf:type
query = '' \
         'SELECT * \n' \
         'WHERE {\n' \
         '  ?s ?p ?o' \
         '}'

print_results(g, query)

# no results although graph contains statements with the predicate rdf:type
query = '' \
         'SELECT * \n' \
         'WHERE {\n' \
         '  ?s a ?o' \
         '}'

print_results(g, query)

# no results although graph contains statements with the predicate rdf:type
query = '' \
         'SELECT * \n' \
         'WHERE {\n' \
         '  ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?o' \
         '}'

print_results(g, query)
joernhees commented 10 years ago

i just tried to reproduce this but everything seems to work for me:

In [18]: print_results(g, query)
- - - - - - - - - - - - - - -
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Buzzwords'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Service100577525'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Nonsense106607339'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://www.w3.org/2002/07/owl#Thing'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Communication100033020'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/PsychologicalFeature100023100'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/ontology/TopicalConcept'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Message106598915'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Work100575741'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/YagoPermanentlyLocatedEntity'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Act100030358'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Buzzword106608277'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/WebServices'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Event100029378'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/ontology/Genre'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/ontology/MusicGenre'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Abstraction100002137'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
(rdflib.term.URIRef(u'http://dbpedia.org/class/yago/Activity100407535'), rdflib.term.URIRef(u'http://dbpedia.org/resource/Semantic_Web'))
- - - - - - - - - - - - - - -

also i get the same results for a or rdf:type...

@scharrenbach can you try again? maybe the DBpedia endpoint was down? also there's a MyGraph in your snippet which i don't see any use for, could you retry the following simplistic snippet:

from rdflib import Graph, URIRef
from rdflib.namespace import RDF
subject = URIRef('http://example.com/test/')
my_type = URIRef('http://example.com/ontology/MyType')

g = Graph()
g.add((subject, URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), my_type))
print list(g.query('select * where { ?s a ?o }'))

this returns

[(rdflib.term.URIRef(u'http://example.com/ontology/MyType'), rdflib.term.URIRef(u'http://example.com/test/'))]

for me

scharrenbach commented 10 years ago

After two clean re-installs and testing it in several environments (both Python 2.7 and 3.4), I was no longer able to reproduce the error. Sorry for the trouble. However, what puzzled me was that queries worked for every predicate except rdf:type.

scharrenbach commented 10 years ago

I finally found the issue. It seems that I was calling the code accidentally including a module which defines the extension point for BGP matching. Works like a charm and the problem was rather located between my ears :-)

joernhees commented 10 years ago

;) no worries, happens to all of us