-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtask07.py
More file actions
169 lines (126 loc) · 4.25 KB
/
Copy pathtask07.py
File metadata and controls
169 lines (126 loc) · 4.25 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
166
167
168
169
# -*- coding: utf-8 -*-
"""Task07.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1jJ3lb2MyEekt-KEqq6de4ghh9VyOqlZM
**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
all_classes = set(g.subjects(RDF.type, RDFS.Class))
all_classes.update(g.subjects(RDFS.subClassOf, None))
for c in all_classes:
s_classes = list(g.objects(c, RDFS.subClassOf))
if s_classes:
for sc in s_classes:
result.append((c, sc))
else:
result.append((c, 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 = []
classes = {ns.Person}
changed = False
while not changed:
for s, p, o in g.triples((None, RDFS.subClassOf, None)):
if o in classes and s not in classes:
classes.add(s)
changed = True
for s, p, o in g.triples((None, RDF.type, None)):
if o in classes:
individuals.append(s)
# 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 = """
SELECT DISTINCT ?ind
WHERE {
?ind <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?class .
?class <http://www.w3.org/2000/01/rdf-schema#subClassOf>* <http://oeg.fi.upm.es/def/people#Person> .
}
"""
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**"""
from rdflib import FOAF
query = """
PREFIX ns: <http://oeg.fi.upm.es/def/people#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?name ?type
WHERE {
?s rdf:type ?type .
?s rdfs:label ?name .
?s ns:knows ns:Rocky .
}
"""
# 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#>
SELECT DISTINCT ?name
WHERE {
?p ns:ownsPet ?dog .
{
?name ns:hasColleague ?p .
}
UNION
{
?name ns:hasColleague ?c .
?c ns:hasColleague ?p .
}
}
"""
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")