-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask07.py
More file actions
161 lines (115 loc) · 4.07 KB
/
Copy pathtask07.py
File metadata and controls
161 lines (115 loc) · 4.07 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
# -*- coding: utf-8 -*-
"""Task07.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1dZkadlOgWdM2gUgM8Yyy_90Bo3ACwUgB
**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
for i in g.subjects(RDF.type, RDFS.Class):
superclass = g.value(i, RDFS.subClassOf)
result.append((i, superclass))
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 is_subclass_person(c):
if c == ns.Person:
return True
for s, p, o in g.triples((c, RDFS.subClassOf, None)):
if is_subclass_person(o):
return True
return False
for s, p, o in g.triples((None, RDF.type, None)):
if o == ns.Person or is_subclass_person(o):
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 = """
PREFIX people: <http://oeg.fi.upm.es/def/people#>
SELECT DISTINCT ?ind
WHERE {
?class rdfs:subClassOf* people:Person .
?ind a ?class .
}
"""
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 people: <http://oeg.fi.upm.es/def/people#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name ?type WHERE {
?person people:knows people:Rocky .
?person rdfs:label ?name .
?person rdf:type ?type .
}
"""
# 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 people: <http://oeg.fi.upm.es/def/people#>
SELECT DISTINCT ?name WHERE {
?person rdfs:label ?name .
{
?person people:hasColleague ?colleague1 .
?colleague1 people:ownsPet ?pet1 .
} UNION {
?person people:hasColleague ?colleague1 .
?colleague1 people:hasColleague ?colleague2 .
?colleague2 people:ownsPet ?pet2 .
}
}
"""
for r in g.query(query):
print(r.name)
## Validation: Do not remove
report.validate_07_04(g,query)
report.save_report("_Task_07")