-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask07.py
More file actions
154 lines (123 loc) · 4.33 KB
/
Copy pathtask07.py
File metadata and controls
154 lines (123 loc) · 4.33 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
# -*- coding: utf-8 -*-
"""task07.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1-XjrbfW5Nf7zWDzAUhOXRjW0RQQ6x-TI
**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
result = [] #list of tuples
classes = set(g.subjects(RDF.type, RDFS.Class))
classes.update(g.subjects(RDFS.subClassOf, None))
for c in classes:
superclasses = list(g.objects(c, RDFS.subClassOf))
if not superclasses:
result.append((c, None))
else:
for sc in superclasses:
result.append((c, sc))
# Visualize the results
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 = """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?c ?sc WHERE {
?c rdf:type 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_person_subclass(cls):
if cls == ns.Person:
return True
for s, p, o in g.triples((cls, RDFS.subClassOf, None)):
if is_person_subclass(o):
return True
return False
for s, p, o in g.triples((None, RDF.type, None)):
if o == ns.Person or is_person_subclass(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 ppl: <http://oeg.fi.upm.es/def/people#>
SELECT DISTINCT ?ind
WHERE {
?p rdfs:subClassOf* ppl:Person .
?ind a ?p .
}"""
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**"""
# TO DO
query = """
PREFIX ppl: <http://oeg.fi.upm.es/def/people#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name ?type WHERE {
?p ppl:knows ppl:Rocky .
?p rdfs:label ?name .
?p 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 ppl: <http://oeg.fi.upm.es/def/people#>
SELECT DISTINCT ?name WHERE {
?p rdfs:label ?name .
{
?p ppl:hasColleague ?colleague1 .
?colleague1 ppl:ownsPet ?pet1 .
} UNION {
?p ppl:hasColleague ?colleague1 .
?colleague1 ppl:hasColleague ?colleague2 .
?colleague2 ppl:ownsPet ?pet2 .
}
}
"""
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")