使用本地RDF的SPARQL查询

2024-09-30 20:21:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用本地RDF图创建SPARQL查询。但它不起作用。我已经包括我的代码下面是我的代码。在

我有两个班,叫学生班和大学班。student类有两个属性(entrolledon和studiesAt)。大学类还有两个属性(UniversityLocation和UniversityRanking)。此外,我还输入了一些数据(RDF三元组)。学生班级和大学班级都有三个数据实体。在

我的SPARQL查询在底部。我想选择在排名前十的大学学习的所有学生。但目前,我的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)

这是运行上述代码后,三元组将如何存储在图形中:

^{pr2}$

Tags: orghttpwwwrdfdescriptionowlstudentrdflib
3条回答
?stu student:studiesAt ?uni.

在你的学生资料中匹配一个字面值字串。在你的大学数据中使用uri。在

一个好的方法是将每组数据打印成Turtle格式或N-Triples格式,以查看真正的结构。RDF/XML很难使用。在

这不是一个真正的答案,但除非您的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)

您应该查看Robv在this question上的答案,将您的UniversityRanking值转换为一个整数。在

相关问题 更多 >