generated from FacultadInformatica-LinkedData/Template-Curso
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtask07.py
189 lines (145 loc) · 4.94 KB
/
task07.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
"""Task07.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1EJwdGA6qVXJ-TQ4mJe4VKX6mfwv84ukz
**Task 07: Querying RDF(s)**
"""
!pip install rdflib
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials"
"""First let's read the RDF file"""
from rdflib import Graph, Namespace, Literal
from rdflib.namespace import RDF, RDFS
g = Graph()
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False)
g.parse(github_storage+"/rdf/example6.rdf", format="xml")
"""**TASK 7.1: List all subclasses of "LivingThing" with RDFLib and SPARQL**"""
# TO DO
query = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subclass WHERE {
?subclass rdfs:subClassOf <http://somewhere#LivingThing>.
}
"""
# Execute the SPARQL query
results = g.query(query)
# Print the results
for row in results:
print(row.subclass)
"""**TASK 7.2: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)**
"""
# TO DO
person_uri = "http://somewhere#Person"
# Step 1: Find all subclasses of Person
subclass_query = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subclass WHERE {
?subclass rdfs:subClassOf <http://somewhere#Person>.
}
"""
subclasses = g.query(subclass_query)
# Create a list of subclasses URIs
subclass_list = [str(row.subclass) for row in subclasses]
# Include the Person class itself in the list
subclass_list.append(person_uri)
# Step 2: Find all individuals of the Person class and its subclasses
individuals_query = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?individual WHERE {{
?individual rdf:type ?type .
FILTER (?type IN ({', '.join(['<'+subclass+'> ' for subclass in subclass_list])}))
}}
"""
# Execute the SPARQL query to get individuals
individuals = g.query(individuals_query)
# Print the results
for row in individuals:
print(row.individual)
"""**TASK 7.3: List all individuals of just "Person" or "Animal". You do not need to list the individuals of the subclasses of person (in SPARQL only)**
"""
# TO DO
animal_uri = "http://somewhere#Animal"
individuals_query = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?individual WHERE {{
{{ ?individual rdf:type <{person_uri}> }}
UNION
{{ ?individual rdf:type <{animal_uri}> }}
}}
"""
# Execute the SPARQL query to get individuals
individuals = g.query(individuals_query)
# Print the results
for row in individuals:
print(row.individual)
"""**TASK 7.4: List the name of the persons who know Rocky (in SPARQL only)**"""
# TO DO
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name WHERE {
{
?person rdf:type <http://somewhere#Person> .
}
UNION
{
?person rdf:type ?subclass .
?subclass rdfs:subClassOf <http://somewhere#Person> .
}
?person foaf:knows <http://somewhere#RockySmith> .
?person vcard:FN ?name .
}
"""
# Execute the SPARQL query to get names
names = g.query(query)
# Print the results
for row in names:
print(row.name)
"""**Task 7.5: List the name of those animals who know at least another animal in the graph (in SPARQL only)**"""
# TO DO
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>
SELECT DISTINCT ?animalName WHERE {
?animal rdf:type <http://somewhere#Animal> .
?animal foaf:knows ?otherAnimal .
?otherAnimal rdf:type <http://somewhere#Animal> .
?animal vcard:Given ?animalName .
}
"""
# Execute the SPARQL query to get animal names
animal_names = g.query(query)
# Print the results
for row in animal_names:
print(row.animalName)
"""**Task 7.6: List the age of all living things in descending order (in SPARQL only)**"""
# TO DO
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0/>
SELECT ?livingThing ?age
WHERE {
{ ?livingThing rdf:type <http://somewhere#Person> .
?livingThing foaf:age ?age .}
UNION
{ ?livingThing rdf:type <http://somewhere#Animal> .
?livingThing foaf:age ?age .}
UNION
{ ?livingThing rdf:type <http://somewhere#Researcher> .
?livingThing foaf:age ?age .}
UNION
{ ?livingThing rdf:type <http://somewhere#Professor> .
?livingThing foaf:age ?age .}
}
ORDER BY DESC(?age)
"""
# Execute the SPARQL query to get living things and their ages
living_thing_ages = g.query(query)
# Print the results
for row in living_thing_ages:
print(f"{row.livingThing}: {row.age}")