2

I am trying to create a SPARQL query using a local RDF graph. But its not working. I have included my code below is my code.

I have two classes called Student and University. The student class has two attributes (enrolledOn and studiesAt). The University class also has two attributes (UniversityLocation and UniversityRanking). Furthermore, I have entered some data (RDF triples). Both Student class and University classes have three data entities each.

My SPARQL query is at the bottom. I want to select all students who study at top 10 ranked universities. But at the moment, my SPARQL query doesn't return anything. The query should return Khalil and Ahmed.

Any help will be much appreciated. Thank you.

My code:

import rdfextras
import rdflib
from rdflib.graph import Graph, Store, URIRef, Literal
from rdflib.namespace import Namespace, RDFS
from rdflib import plugin
from SPARQLWrapper import SPARQLWrapper, JSON

rdflib.plugin.register('sparql', rdflib.query.Processor,
                       'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
                       'rdfextras.sparql.query', 'SPARQLQueryResult')


#=====================data for STUDENT class==============================
rdf_xml_Student_data = """<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:Student="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student#">

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student/Harith">
  <Student:enrolledOn>MScComputerScience</Student:enrolledOn>
  <Student:studiesAt>Queen_Mary</Student:studiesAt>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student/Khalil">
  <Student:enrolledOn>BScComputerScience</Student:enrolledOn>
  <Student:studiesAt>Oxford_University</Student:studiesAt>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student/Ahmed">
  <Student:enrolledOn>BScComputerScience</Student:enrolledOn>
  <Student:studiesAt>Oxford_University</Student:studiesAt>
</rdf:Description>

</rdf:RDF>
"""


#=====================data for UNIVERSITY class==============================
rdf_xml_University_data = """<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:University="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University#">

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University/Queen_Mary">
  <University:UniversityLocation>London</University:UniversityLocation>
  <University:UniversityRanking>36</University:UniversityRanking>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University/City_University">
  <University:UniversityLocation>London</University:UniversityLocation>
  <University:UniversityRanking>43</University:UniversityRanking>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University/Oxford_University">
  <University:UniversityLocation>Oxford</University:UniversityLocation>
  <University:UniversityRanking>2</University:UniversityRanking>
</rdf:Description>

</rdf:RDF>
"""


# -- (part1) create and RDF store in memory --
memory_store = plugin.get('IOMemory', Store)()
graph_id = URIRef(u'http://example.com/foo')
g = Graph(store=memory_store, identifier=graph_id)
g.bind('ex','http://example.com/')   

g.parse(data=rdf_xml_Student_data, format="application/rdf+xml")
g.parse(data=rdf_xml_University_data, format="application/rdf+xml")




#===========================SPARQL QUERY====================================
# QUERY - select all students who study at top 10 ranked universities
results = g.query("""PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX student: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student#>
PREFIX university: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University#>

SELECT ?stu 
WHERE { ?uni university:UniversityRanking ?UniversityRanking.
        ?stu student:studiesAt ?uni.
        FILTER ( ?UniversityRanking < 10)
}
""")



print("\n============QUERY RESULTS===============\n")
for row in results.result:
    print(row)

This is how the triples would be stored in the graph once I run the above code:

=========================STUDENT class==================================
Subject         Predicate           Object
========================================================================
Harith          enrolledOn          MScComputerScience
Harith          studiesAt           Queen_Mary
Khalil          enrolledOn          BScComputerScience
Khalil          studiesAt           Oxford_University
Ahmed           enrolledOn          BScComputerScience
Ahmed           studiesAt           Oxford_University


=============================UNIVERSITY class=======================
Subject            Predicate           Object
===============================================================
Queen_Mary             UniversityLocation      London
Queen_Mary             UniversityRanking       36
City_University        UniversityLocation      London
City_University        UniversityRanking       43
Oxford_University        UniversityLocation    Oxford
Oxford_University        UniversityRanking     2
Harith
  • 21
  • 3

3 Answers3

1

You should look at Robv's answer on this question, casting your UniversityRanking value as an integer.

Community
  • 1
  • 1
Dominique Guardiola
  • 3,431
  • 2
  • 22
  • 22
1

Not really an answer, but unless you RDFLib is more than 3-4 years old you code can be much simpler:

from rdflib import Graph

#=====================data for STUDENT class==============================
rdf_xml_Student_data = """<?xml version="1.0"?> ... <snip>"""

#=====================data for UNIVERSITY class==============================
rdf_xml_University_data = """<?xml version="1.0"?> ... <snip>"""


# -- (part1) create and RDF store in memory --
g = Graph()

g.parse(data=rdf_xml_Student_data)
g.parse(data=rdf_xml_University_data)


#===========================SPARQL QUERY====================================
# QUERY - select all students who study at top 10 ranked universities
results = g.query("""PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX student: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student#>
PREFIX university: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University#>

SELECT ?stu 
WHERE { ?uni university:UniversityRanking ?UniversityRanking.
    ?stu student:studiesAt ?uni.
    FILTER ( ?UniversityRanking < 10)
}
""")



print("\n============QUERY RESULTS===============\n")
for row in results.result:
    print(row)
gromgull
  • 1,480
  • 1
  • 10
  • 14
0
?stu student:studiesAt ?uni.

in your student data matches a literals string. In your unversity data you use URIs.

A good way to start is to print out each set of data in Turtle or N-Triples to see the true structure. RDF/XML is hard to work with.

AndyS
  • 16,345
  • 17
  • 21
  • I second looking at the data in turtle. You can easily make RDFLib serialize the graph as turtle again with ```g.serialize(format='turtle')``` – gromgull Apr 23 '13 at 06:19