-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask07.py
More file actions
135 lines (106 loc) · 4.09 KB
/
Copy pathtask07.py
File metadata and controls
135 lines (106 loc) · 4.09 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
# -*- coding: utf-8 -*-
"""Task07.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1zvX2Bxt__j1SBu23wqpB-QcOs58Tz6_L
**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**
"""
result = []
for clase in g.subjects(RDF.type, RDFS.Class):
superclass = None
for sclass in g.objects(clase, RDFS.subClassOf):
superclass = sclass
result.append((clase, 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 person_subclass(clase):
if clase == ns.Person:
return True
for s, p, o in g.triples((clase, RDFS.subClassOf, None)):
if person_subclass(o):
return True
else:
return False
for s, p, o in g.triples((None, RDF.type, None)):
if o == ns.Person or person_subclass(o) == True:
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 .
}
"""
# 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 people: <http://oeg.fi.upm.es/def/people#>
SELECT DISTINCT ?name WHERE {
?person rdfs:label ?name .
{
?person people:hasColleague ?petowner .
?petowner people:hasColleague ?colleague .
?colleague people:ownsPet ?pet .
} UNION {
?person people:hasColleague ?petowner .
?petowner people:ownsPet ?pet .
}
}
"""
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")