-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask07.py
More file actions
165 lines (123 loc) · 4.26 KB
/
Copy pathtask07.py
File metadata and controls
165 lines (123 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""Task07_2025.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/13zP7djRTBeG5tsTNq-mV-TGtIeDmqV7W
**Task 07: Querying RDF(s)**
"""
#!pip install rdflib
import urllib.request
url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'
urllib.request.urlretrieve(url, 'validation.py')
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials"
from validation import Report
"""First let's read the RDF file"""
from rdflib import Graph, Namespace, Literal
from rdflib.namespace import RDF, RDFS
# Do not change the name of the variables
g = Graph()
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
g.parse(github_storage+"/rdf/data06.ttl", format="TTL")
report = Report()
"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**
**Do the exercise in RDFLib returning a list of Tuples: (class, superclass) called "result". If a class does not have a super class, then return None as the superclass**
"""
# TO DO
# Visualize the results
result = [] #list of tuples
result = [
(cls, sup)
for cls in g.subjects(RDF.type, RDFS.Class)
for sup in (list(g.objects(cls, RDFS.subClassOf)) or [None])
]
for r in result:
print(r)
## Validation: Do not remove
report.validate_07_1a(result)
"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**"""
query = """SELECT ?c ?sc WHERE {
?c a rdfs:Class .
OPTIONAL { ?c rdfs:subClassOf ?sc }
} """
for r in g.query(query):
print(r.c, r.sc)
## Validation: Do not remove
report.validate_07_1b(query,g)
"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"**
"""
ns = Namespace("http://oeg.fi.upm.es/def/people#")
# variable to return
individuals = []
def get_all_subclasses(graph, base_class):
subclasses = {base_class}
to_visit = [base_class]
while to_visit:
current = to_visit.pop()
for subclass in graph.subjects(RDFS.subClassOf, current):
if subclass not in subclasses:
subclasses.add(subclass)
to_visit.append(subclass)
return subclasses
all_classes = get_all_subclasses(g, ns.Person)
for cls in all_classes:
for individual in g.subjects(RDF.type, cls):
individuals.append(individual)
# visualize results
for i in individuals:
print(i)
# validation. Do not remove
report.validate_07_02a(individuals)
"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**"""
query = """
PREFIX ns: <http://oeg.fi.upm.es/def/people#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?ind
WHERE {
?c rdfs:subClassOf* ns:Person .
?ind a ?c .
}
"""
for r in g.query(query):
print(r.ind)
# Visualize the results
## Validation: Do not remove
report.validate_07_02b(g, query)
"""**TASK 7.3: List the name and type of those who know Rocky (in SPARQL only). Use name and type as variables in the query**"""
query = """
PREFIX ns: <http://oeg.fi.upm.es/def/people#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?name ?type WHERE {
?name ns:knows ns:Rocky .
?name rdf:type ?type .
}
"""
# TO DO
# Visualize the results
for r in g.query(query):
print(r.name, r.type)
## Validation: Do not remove
report.validate_07_03(g, query)
"""**Task 7.4: List the name of those entities who have a colleague with a dog, or that have a collegue who has a colleague who has a dog (in SPARQL). Return the results in a variable called name**"""
query = """
PREFIX ns: <http://oeg.fi.upm.es/def/people#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?name WHERE{
?p ns:ownsPet ?m .
?m rdf:type ns:Animal .
{
?name ns:hasColleague ?p .
}
UNION
{
?p2 ns:hasColleague ?p .
?name ns:hasColleague ?p2 .
}
}
"""
for r in g.query(query):
print(r.name)
# TO DO
# Visualize the results
## Validation: Do not remove
report.validate_07_04(g,query)
report.save_report("_Task_07")