2013-04-20 59 views
1

我正在嘗試使用本地RDF圖創建SPARQL查詢。但它不工作。我已經包含了我的代碼,下面是我的代碼。使用本地RDF存儲的SPARQL查詢

我有兩個類叫學生和大學。學生班有兩個屬性(enrolledOn和studiesAt)。大學課程還有兩個屬性(UniversityLocation和UniversityRanking)。此外,我輸入了一些數據(RDF三元組)。學生班級和大學班級均有三個數據實體。

我的SPARQL查詢位於底部。我想選擇所有在排名前10的大學學習的所有學生。但目前,我的SPARQL查詢不會返回任何內容。查詢應該返回Khalil和Ahmed。

任何幫助將不勝感激。謝謝。

我的代碼:

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) 

這是三元將如何被存儲在圖形一旦我運行上面的代碼:在您的學生數據

=========================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 

回答

0
?stu student:studiesAt ?uni. 

一個文字字符串相匹配。在你的數據中使用URI。

一個好的開始方法是打印出Turtle或N-Triples中的每一組數據以查看真正的結構。 RDF/XML很難使用。

+0

我第二次看着龜數據。你可以很容易地讓RDFLib用'''g.serialize(format ='turtle')再次將圖形序列化爲烏龜''' – gromgull 2013-04-23 06:19:23

1

你應該看看Robv在this question上的回答,將你的UniversityRanking值作爲一個整數。

+0

我嘗試了Robv的答案,但不幸的是它沒有奏效。順便謝謝你的回答。 – Harith 2013-04-25 18:52:57

1

不是一個真正的答案,但除非你RDFLib超過3-4歲,你的代碼可以變得更簡單:

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)