Skip to content

Commit 76f6389

Browse files
authored
Merge pull request #214 from abraham-pozuelo/master
Assigment4_AbrahamPozuelo_Corregido
2 parents bc4e7ae + 3e9f424 commit 76f6389

3 files changed

Lines changed: 586 additions & 0 deletions

File tree

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# -*- coding: utf-8 -*-
2+
"""Task06.ipynb
3+
4+
Automatically generated by Colab.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1zlcSInjsG6haAulzk8AT3u8nCAbF-qMf
8+
9+
**Task 06: Modifying RDF(s)**
10+
"""
11+
12+
#!pip install rdflib
13+
import urllib.request
14+
url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'
15+
urllib.request.urlretrieve(url, 'validation.py')
16+
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials"
17+
18+
"""Import RDFLib main methods"""
19+
20+
from rdflib import Graph, Namespace, Literal, XSD
21+
from rdflib.namespace import RDF, RDFS
22+
from validation import Report
23+
g = Graph()
24+
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
25+
r = Report()
26+
27+
"""Create a new class named Researcher"""
28+
29+
ns = Namespace("http://mydomain.org#")
30+
g.add((ns.Researcher, RDF.type, RDFS.Class))
31+
for s, p, o in g:
32+
print(s,p,o)
33+
34+
"""**Task 6.0: Create new prefixes for "ontology" and "person" as shown in slide 14 of the Slidedeck 01a.RDF(s)-SPARQL shown in class.**"""
35+
36+
ontology = Namespace("http://oeg.fi.upm.es/def/ontology#")
37+
person = Namespace("http://oeg.fi.upm.es/def/people#")
38+
g.bind("ontology", ontology)
39+
g.bind("person", person)
40+
41+
"""**TASK 6.1: Reproduce the taxonomy of classes shown in slide 34 in class (all the classes under "Vocabulario", Slidedeck: 01a.RDF(s)-SPARQL). Add labels for each of them as they are in the diagram (exactly) with no language tags. Remember adding the correct datatype (xsd:String) when appropriate**
42+
43+
"""
44+
45+
# TO DO
46+
47+
48+
49+
g.add((person.Person, RDF.type, RDFS.Class))
50+
g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string)))
51+
52+
g.add((person.Professor, RDF.type, RDFS.Class))
53+
g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string)))
54+
g.add((person.Professor, RDFS.subClassOf, person.Person))
55+
56+
57+
g.add((person.FullProfessor, RDF.type, RDFS.Class))
58+
g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string)))
59+
g.add((person.FullProfessor, RDFS.subClassOf, person.Professor))
60+
61+
g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class))
62+
g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string)))
63+
g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor))
64+
65+
66+
g.add((person.AssociateProfessor, RDF.type, RDFS.Class))
67+
g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string)))
68+
g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor))
69+
70+
71+
72+
73+
for s, p, o in g:
74+
print(s, p, o)
75+
76+
# Validation. Do not remove
77+
r.validate_task_06_01(g)
78+
79+
"""**TASK 6.2: Add the 3 properties shown in slide 36. Add labels for each of them (exactly as they are in the slide, with no language tags), and their corresponding domains and ranges using RDFS. Remember adding the correct datatype (xsd:String) when appropriate. If a property has no range, make it a literal (string)**"""
80+
81+
# TO DO
82+
83+
#hasColleague#
84+
g.add((person.hasColleague, RDF.type, RDF.Property))
85+
g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
86+
g.add((person.hasColleague, RDFS.domain, person.Person))
87+
g.add((person.hasColleague, RDFS.range, person.Person))
88+
89+
#hasName#
90+
g.add((person.hasName, RDF.type, RDF.Property))
91+
g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
92+
g.add((person.hasName, RDFS.domain, person.Person))
93+
g.add((person.hasName, RDFS.range, RDFS.Literal))
94+
95+
#hasHomePage#
96+
g.add((person.hasHomePage, RDF.type, RDF.Property))
97+
g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
98+
g.add((person.hasHomePage, RDFS.domain, person.FullProfessor))
99+
g.add((person.hasHomePage, RDFS.range, RDFS.Literal))
100+
101+
102+
103+
# Visualize the results
104+
for s, p, o in g:
105+
print(s,p,o)
106+
107+
# Validation. Do not remove
108+
r.validate_task_06_02(g)
109+
110+
"""**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""
111+
112+
# TO DO
113+
114+
115+
DATA = Namespace("http://oeg.fi.upm.es/resource/person/")
116+
117+
#Oscar#
118+
g.add((DATA.Oscar, RDF.type, person.AssociateProfessor))
119+
g.add((DATA.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
120+
g.add((DATA.Oscar, person.hasColleague, DATA.Asun))
121+
g.add((DATA.Oscar, person.hasName, Literal("Oscar Corcho García", datatype=XSD.string)))
122+
123+
124+
#Asun#
125+
g.add((DATA.Asun, RDF.type, person.FullProfessor))
126+
g.add((DATA.Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
127+
g.add((DATA.Asun, person.hasColleague, DATA.Raul))
128+
g.add((DATA.Asun, person.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string)))
129+
130+
#Raul#
131+
g.add((DATA.Raul, RDF.type, person.InterimAssociateProfessor))
132+
g.add((DATA.Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
133+
134+
135+
136+
137+
# Visualize the results
138+
for s, p, o in g:
139+
print(s,p,o)
140+
141+
r.validate_task_06_03(g)
142+
143+
"""**TASK 6.4: Add to the individual person:Oscar the email address, given and family names. Use the properties already included in example 4 to describe Jane and John (https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials/rdf/example4.rdf). Do not import the namespaces, add them manually**
144+
145+
"""
146+
147+
# TO DO
148+
149+
150+
VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
151+
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
152+
153+
154+
155+
g.add((DATA.Oscar, VCARD.Given, Literal("Oscar", datatype=XSD.string)))
156+
g.add((DATA.Oscar, VCARD.Family, Literal("Corcho", datatype=XSD.string)))
157+
g.add((DATA.Oscar, FOAF.email, Literal("oscarcorcho@upm.es", datatype=XSD.string)))
158+
159+
160+
161+
# Visualize the results
162+
for s, p, o in g:
163+
print(s,p,o)
164+
165+
# Validation. Do not remove
166+
r.validate_task_06_04(g)
167+
r.save_report("_Task_06")
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# -*- coding: utf-8 -*-
2+
"""Task07.ipynb
3+
4+
Automatically generated by Colab.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1dZkadlOgWdM2gUgM8Yyy_90Bo3ACwUgB
8+
9+
**Task 07: Querying RDF(s)**
10+
"""
11+
12+
#!pip install rdflib
13+
import urllib.request
14+
url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'
15+
urllib.request.urlretrieve(url, 'validation.py')
16+
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials"
17+
18+
from validation import Report
19+
20+
"""First let's read the RDF file"""
21+
22+
from rdflib import Graph, Namespace, Literal
23+
from rdflib.namespace import RDF, RDFS
24+
# Do not change the name of the variables
25+
g = Graph()
26+
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
27+
g.parse(github_storage+"/rdf/data06.ttl", format="TTL")
28+
report = Report()
29+
30+
"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**
31+
**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**
32+
"""
33+
34+
# TO DO
35+
# Visualize the results
36+
result = [] #list of tuples
37+
38+
for i in g.subjects(RDF.type, RDFS.Class):
39+
superclass = g.value(i, RDFS.subClassOf)
40+
result.append((i, superclass))
41+
42+
43+
for r in result:
44+
print(r)
45+
46+
## Validation: Do not remove
47+
report.validate_07_1a(result)
48+
49+
"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**"""
50+
51+
query = """
52+
SELECT ?c ?sc WHERE{
53+
?c a rdfs:Class.
54+
OPTIONAL{
55+
?c rdfs:subClassOf ?sc
56+
}.
57+
} """
58+
59+
60+
61+
for r in g.query(query):
62+
print(r.c, r.sc)
63+
64+
## Validation: Do not remove
65+
report.validate_07_1b(query,g)
66+
67+
"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"**
68+
69+
"""
70+
71+
ns = Namespace("http://oeg.fi.upm.es/def/people#")
72+
73+
# variable to return
74+
individuals = []
75+
76+
77+
def is_subclass_person(c):
78+
if c == ns.Person:
79+
return True
80+
81+
for s, p, o in g.triples((c, RDFS.subClassOf, None)):
82+
if is_subclass_person(o):
83+
return True
84+
85+
return False
86+
87+
88+
89+
for s, p, o in g.triples((None, RDF.type, None)):
90+
if o == ns.Person or is_subclass_person(o):
91+
individuals.append(s)
92+
93+
94+
# visualize results
95+
for i in individuals:
96+
print(i)
97+
98+
# validation. Do not remove
99+
report.validate_07_02a(individuals)
100+
101+
"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**"""
102+
103+
query = """
104+
PREFIX people: <http://oeg.fi.upm.es/def/people#>
105+
SELECT DISTINCT ?ind
106+
WHERE {
107+
?class rdfs:subClassOf* people:Person .
108+
?ind a ?class .
109+
}
110+
"""
111+
112+
for r in g.query(query):
113+
print(r.ind)
114+
# Visualize the results
115+
116+
## Validation: Do not remove
117+
report.validate_07_02b(g, query)
118+
119+
"""**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**"""
120+
121+
query = """
122+
PREFIX people: <http://oeg.fi.upm.es/def/people#>
123+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
124+
SELECT ?name ?type WHERE {
125+
?person people:knows people:Rocky .
126+
?person rdfs:label ?name .
127+
?person rdf:type ?type .
128+
}
129+
"""
130+
131+
132+
# Visualize the results
133+
for r in g.query(query):
134+
print(r.name, r.type)
135+
136+
## Validation: Do not remove
137+
report.validate_07_03(g, query)
138+
139+
"""**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**"""
140+
141+
query = """
142+
PREFIX people: <http://oeg.fi.upm.es/def/people#>
143+
SELECT DISTINCT ?name WHERE {
144+
?person rdfs:label ?name .
145+
{
146+
?person people:hasColleague ?colleague1 .
147+
?colleague1 people:ownsPet ?pet1 .
148+
} UNION {
149+
?person people:hasColleague ?colleague1 .
150+
?colleague1 people:hasColleague ?colleague2 .
151+
?colleague2 people:ownsPet ?pet2 .
152+
}
153+
}
154+
"""
155+
156+
for r in g.query(query):
157+
print(r.name)
158+
159+
## Validation: Do not remove
160+
report.validate_07_04(g,query)
161+
report.save_report("_Task_07")

0 commit comments

Comments
 (0)