rdflib subclass problem

dieter dieter at handshake.de
Wed Mar 2 03:16:14 EST 2016


duncan smith <duncan at invalid.invalid> writes:

>       I'm just getting to grips with RDF and rdflib, and I've hit
> something I can't figure out.
>
> I have a graph with information on two people. (I haven't shown the
> imports below because they're scattered around my interactive session
> and I might reconstruct them incorrectly. Anyone familiar with rdflib
> will probably know what they are.)
>
>
>>>> G = Graph()
>>>> mark = BNode()
>>>> nat = BNode()
>>>> G.add((mark, RDF.type, FOAF.Person))
>>>> G.add((mark, FOAF.firstName, Literal('mark')))
>>>> G.add((nat, RDF.type, URIRef('Professor')))
>>>> G.add((nat, FOAF.firstName, Literal('natalie')))
>>>> G.add((URIRef('Professor'), RDFS.subClassOf, FOAF.Person))
>>>>
>
>
> So I have specified that mark is a Person, natalie is a Professor, and
> that Professor is a subclass of Person. (I know that Professor is really
> a FOAF.title, but I'm just tinkering ATM.)
>
>
>>>> qres = G.query(
>         """SELECT DISTINCT ?aname
>            WHERE {
>               ?a rdf:type foaf:Person .
>               ?a foaf:firstName ?aname .
>            }""", initNs = {"rdf": RDF,"foaf": FOAF})
>>>> for row in qres:
> 	print "%s is a person" % row
>
> 	
> mark is a person
>>>> qres = G.query(
>         """SELECT DISTINCT ?aname
>            WHERE {
>               ?a rdf:type ?prof .
>               ?a foaf:firstName ?aname .
>            }""", initNs = {"rdf": RDF,"foaf": FOAF, "prof":
> URIRef('Professor')})
>>>> for row in qres:
> 	print "%s is a Prof" % row
>
> 	
> natalie is a Prof
> mark is a Prof
>>>>
>
>
> But according to the above queries only mark is a Person, and each is a
> Professor. I would have thought that both would be Persons and only
> natalie would be a Professor. Can anyone spot where I'm going wrong
> here? Thanks.

What you observe would be consistent with "RDFS.subClassOf" working
in the other direction than the one you expect; i.e. that

   URIRef('Professor'), RDFS.subClassOf, FOAF.Person

means that "Person" is a subclass of "Professor" (not what
you expect, that "Professor" is a subclass of "Person").

Carefully check whether "A rel B" means that "B" is in relation "rel" to "A"
(I think that is the case) or that "A" is in relation "rel" to "B".




More information about the Python-list mailing list