Skip to content

Commit b3cc40d

Browse files
authored
Merge pull request #77 from raulguijarroc-474/master
Pull request Raul Guijarro Colomar
2 parents 4e4e95e + 4eca70c commit b3cc40d

3 files changed

Lines changed: 562 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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/1_Mlq7c51j9uDOD1mMhzj55qCIM7CeNbd
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+
# this task is validated in the next step
37+
ontologia= Namespace("http://www.oeg-upm.net/Ontology#")
38+
Persona= Namespace("http://oeg.fi.upm.es/def/people#")
39+
40+
"""**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**
41+
42+
"""
43+
44+
# TO DO
45+
46+
g.add((Persona.Person, RDF.type, RDFS.Class))
47+
g.add((Persona.Person, RDFS.label, Literal("Person", datatype=XSD.string)))
48+
49+
50+
g.add((Persona.Professor, RDF.type, RDFS.Class))
51+
g.add((Persona.Professor, RDFS.subClassOf, Persona.Person))
52+
g.add((Persona.Professor, RDFS.label, Literal("Professor", datatype=XSD.string)))
53+
54+
55+
g.add((Persona.AssociateProfessor, RDF.type, RDFS.Class))
56+
g.add((Persona.AssociateProfessor, RDFS.subClassOf, Persona.Professor))
57+
g.add((Persona.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string)))
58+
59+
60+
g.add((Persona.FullProfessor, RDF.type, RDFS.Class))
61+
g.add((Persona.FullProfessor, RDFS.subClassOf, Persona.Professor))
62+
g.add((Persona.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string)))
63+
64+
65+
g.add((Persona.InterimAssociateProfessor, RDF.type, RDFS.Class))
66+
g.add((Persona.InterimAssociateProfessor, RDFS.subClassOf, Persona.AssociateProfessor))
67+
g.add((Persona.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string)))
68+
69+
70+
# Visualize the results
71+
for s, p, o in g:
72+
print(s,p,o)
73+
74+
# Validation. Do not remove
75+
r.validate_task_06_01(g)
76+
77+
""":**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)**"""
78+
79+
# TO DO
80+
# primero declaro las propiedas
81+
g.add((Persona.hasName, RDF.type, RDF.Property))
82+
g.add((Persona.hasColleague, RDF.type, RDF.Property))
83+
g.add((Persona.hasHomePage, RDF.type, RDF.Property))
84+
85+
# dominios
86+
g.add((Persona.hasName, RDFS.domain, Persona.Person))
87+
g.add((Persona.hasColleague, RDFS.domain, Persona.Person))
88+
g.add((Persona.hasHomePage, RDFS.domain, Persona.FullProfessor))
89+
90+
# rangos
91+
g.add((Persona.hasName, RDFS.range, RDFS.Literal))
92+
g.add((Persona.hasColleague, RDFS.range, Persona.Person))
93+
g.add((Persona.hasHomePage, RDFS.range, RDFS.Literal))
94+
95+
# etiquetas
96+
g.add((Persona.hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
97+
g.add((Persona.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
98+
g.add((Persona.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
99+
100+
# Visualize the results
101+
for s, p, o in g:
102+
print(s,p,o)
103+
104+
# Validation. Do not remove
105+
r.validate_task_06_02(g)
106+
107+
"""**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""
108+
109+
# TO DO
110+
datos=Namespace("http://oeg.fi.upm.es/resource/person/")
111+
112+
g.add((datos.Asun, RDF.type, Persona.FullProfessor))
113+
g.add((datos.Oscar, RDF.type, Persona.AssociateProfessor))
114+
g.add((datos.Raul, RDF.type, Persona.InterimAssociateProfessor))
115+
116+
g.add((datos.Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
117+
118+
g.add((datos.Asun, Persona.hasColleague, datos.Raul))
119+
g.add((datos.Oscar, Persona.hasColleague, datos.Asun))
120+
121+
g.add((datos.Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
122+
g.add((datos.Oscar, Persona.hasName, Literal("Óscar Corcho García")))
123+
g.add((datos.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
124+
125+
g.add((datos.Asun, Persona.hasHomePage, Literal("http://www.oeg-upm.net/")))
126+
127+
# Visualize the results
128+
for s, p, o in g:
129+
print(s,p,o)
130+
131+
r.validate_task_06_03(g)
132+
133+
"""**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**
134+
135+
"""
136+
137+
# TO DO
138+
foaf = Namespace("http://xmlns.com/foaf/0.1/")
139+
vc = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
140+
141+
142+
g.add((datos.Oscar, vc.Given, Literal("Oscar", datatype=XSD.string)))
143+
g.add((datos.Oscar, vc.Family, Literal("Corcho García", datatype=XSD.string)))
144+
g.add((datos.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string)))
145+
146+
147+
# Visualize the results
148+
for s, p, o in g:
149+
print(s,p,o)
150+
151+
# Validation. Do not remove
152+
r.validate_task_06_04(g)
153+
r.save_report("_Task_06")
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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/12wXkc6kVxTzhVFT805j26Qi9oXzLXzYe
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+
36+
# Visualize the results
37+
result = [] #list of tuples
38+
clase = set(g.subjects(RDF.type, RDFS.Class))
39+
clase.update(g.subjects(RDFS.subClassOf, None))
40+
for c in clase:
41+
sup = None
42+
for superclase in g.objects(c, RDFS.subClassOf):
43+
sup = superclase
44+
result.append((c, sup))
45+
46+
for r in result:
47+
print(r)
48+
49+
## Validation: Do not remove
50+
report.validate_07_1a(result)
51+
52+
"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**"""
53+
54+
query = """
55+
56+
SELECT ?c ?sc WHERE {
57+
?c rdf:type rdfs:Class .
58+
OPTIONAL { ?c rdfs:subClassOf ?sc }
59+
}
60+
"""
61+
62+
for r in g.query(query):
63+
print(r.c, r.sc)
64+
65+
## Validation: Do not remove
66+
report.validate_07_1b(query,g)
67+
68+
"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"**
69+
70+
"""
71+
72+
ns = Namespace("http://oeg.fi.upm.es/def/people#")
73+
74+
# variable to return
75+
individuals = []
76+
check = [ns.Person] #recojo todos los tipos que tengan alguna relacion con Person y leugo saco las isntancias
77+
78+
for t in check:
79+
for sub in g.subjects(RDFS.subClassOf, t):
80+
if sub not in check:
81+
check.append(sub)
82+
83+
for tp in check:
84+
for ind in g.subjects(RDF.type, tp):
85+
individuals.append(ind)
86+
87+
# visualize results
88+
for i in individuals:
89+
print(i)
90+
91+
# validation. Do not remove
92+
report.validate_07_02a(individuals)
93+
94+
"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**"""
95+
96+
query = """SELECT DISTINCT ?ind WHERE {
97+
?t rdfs:subClassOf* <http://oeg.fi.upm.es/def/people#Person> .
98+
?ind a ?t .
99+
}"""
100+
101+
for r in g.query(query):
102+
print(r.ind)
103+
# Visualize the results
104+
105+
## Validation: Do not remove
106+
report.validate_07_02b(g, query)
107+
108+
"""**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**"""
109+
110+
query = """SELECT ?name ?type WHERE {
111+
?x <http://oeg.fi.upm.es/def/people#knows> <http://oeg.fi.upm.es/def/people#Rocky> .
112+
?x <http://www.w3.org/2000/01/rdf-schema#label> ?name .
113+
?x a ?type .
114+
}"""
115+
# TO DO
116+
# Visualize the results
117+
for r in g.query(query):
118+
print(r.name, r.type)
119+
120+
## Validation: Do not remove
121+
report.validate_07_03(g, query)
122+
123+
"""**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**"""
124+
125+
query = """
126+
SELECT DISTINCT ?name WHERE {
127+
?x <http://www.w3.org/2000/01/rdf-schema#label> ?name .
128+
{
129+
?x <http://oeg.fi.upm.es/def/people#hasColleague> ?col1 .
130+
?col1 <http://oeg.fi.upm.es/def/people#ownsPet> ?pet .
131+
}
132+
UNION
133+
{
134+
?x <http://www.w3.org/2000/01/rdf-schema#label> ?name .
135+
?x <http://oeg.fi.upm.es/def/people#hasColleague> ?col1 .
136+
?col1 <http://oeg.fi.upm.es/def/people#hasColleague> ?col2 .
137+
?col2 <http://oeg.fi.upm.es/def/people#ownsPet> ?pet2 .
138+
}
139+
}
140+
"""
141+
142+
for r in g.query(query):
143+
print(r.name)
144+
145+
# TO DO
146+
# Visualize the results
147+
148+
## Validation: Do not remove
149+
report.validate_07_04(g,query)
150+
report.save_report("_Task_07")

0 commit comments

Comments
 (0)