Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Assignment1/DatasetDescriptions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Pablo Gil Gil; pggil2019-design; Principales Parques y Jardines Municipales; htt
Rodrigo Nieto Rodríguez; Rodrix505; Puntos limpios fijos; https://datos.madrid.es/portal/site/egob/menuitem.c05c1f754a33a9fbe4b2e4b284f1a5a0/?vgnextoid=2bb427e0cb503410VgnVCM1000000b205a0aRCRD&vgnextchannel=374512b9ace9f310VgnVCM100000171f5a0aRCRD&vgnextfmt=default; Información de datos, ubicación, características, horarios, coordenadas de localización y servicios de los distintos puntos limpios fijos municipales de la ciudad de Madrid.
Yi Chen Chen; yichenchen2006; Principales parques y jardines municipales; https://datos.madrid.es/sites/v/index.jsp?vgnextoid=dc758935dde13410VgnVCM2000000c205a0aRCRD&vgnextchannel=374512b9ace9f310VgnVCM100000171f5a0aRCRD; Madrid ofrece un patrimonio verde de excepcional extensión y diversidad (más de 6.000 hectáreas, que suponen más de 18 metros cuadrados de parques y zonas verdes públicas por habitante de la ciudad).
Carolina Simal;carol-2406;Arbolado en parques y zonas verdes de Madrid;https://datos.madrid.es/egob/catalogo/300264-81-Arbolado-parques-historico.csv;Número de arboles por parque de Madrid de cada especie.
Luca Alvarez;lucaam06;Bomberos. Parques de bomberos;https://datos.madrid.es/egob/catalogo/211642-0-bomberos-parques.rdf;En este conjunto de datos, puede encontrar la relación de los trece parques de bomberos existentes en la ciudad de Madrid.
150 changes: 150 additions & 0 deletions Assignment4/Luca_Alvarez_24C006/task06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# -*- coding: utf-8 -*-
"""Task06_2025.ipynb

Automatically generated by Colab.

Original file is located at
https://colab.research.google.com/drive/1GKS6qAm9T4y76LWtB8ZP-ToTFBSDENE_

**Task 06: Modifying 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"

"""Import RDFLib main methods"""

from rdflib import Graph, Namespace, Literal, XSD
from rdflib.namespace import RDF, RDFS
from validation import Report
g = Graph()
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
r = Report()

"""Create a new class named Researcher"""

ns = Namespace("http://mydomain.org#")
g.add((ns.Researcher, RDF.type, RDFS.Class))
for s, p, o in g:
print(s,p,o)

"""**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.**"""

# this task is validated in the next step
ontology = Namespace("http://oeg.fi.upm.es/def/ontology#")
person = Namespace("http://oeg.fi.upm.es/def/people#")

"""**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**

"""


g.add((person.Person, RDF.type, RDFS.Class))
g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string)))

g.add((person.Professor, RDF.type, RDFS.Class))
g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string)))
g.add((person.Professor, RDFS.subClassOf, person.Person))

g.add((person.AssociateProfessor, RDF.type, RDFS.Class))
g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string)))
g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor))

g.add((person.FullProfessor, RDF.type, RDFS.Class))
g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string)))
g.add((person.FullProfessor, RDFS.subClassOf, person.Professor))

g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class))
g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string)))
g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor))

# Visualize the results
for s, p, o in g:
print(s,p,o)

# Validation. Do not remove
r.validate_task_06_01(g)

"""**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)**"""


g.add((person.hasColleague, RDF.type, RDF.Property))
g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
g.add((person.hasColleague, RDFS.domain, person.Person))
g.add((person.hasColleague, RDFS.range, person.Person))

g.add((person.hasName, RDF.type, RDF.Property))
g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
g.add((person.hasName, RDFS.domain, person.Person))
g.add((person.hasName, RDFS.range, RDFS.Literal))

g.add((person.hasHomePage, RDF.type, RDF.Property))
g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
g.add((person.hasHomePage, RDFS.domain, person.FullProfessor))
g.add((person.hasHomePage, RDFS.range, RDFS.Literal))

# Visualize the results
for s, p, o in g:
print(s,p,o)

# Validation. Do not remove
r.validate_task_06_02(g)

"""**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""


data=Namespace("http://oeg.fi.upm.es/resource/person/")
g.namespace_manager.bind('data', data, override=False)

g.add((data.Raul, RDF.type, person.InterimAssociateProfessor))
g.add((data.Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))

g.add((data.Asun, RDF.type, person.FullProfessor))
g.add((data.Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
g.add((data.Asun, person.hasHomePage, Literal("http://www.oeg-upm.net/")))
g.add((data.Asun, person.hasColleague, data.Raul))

g.add((data.Oscar, RDF.type, person.AssociateProfessor))
g.add((data.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
g.add((data.Oscar, person.hasName, Literal("Óscar Corcho García")))
g.add((data.Oscar, person.hasColleague, data.Asun))

# Visualize the results
for s, p, o in g:
print(s,p,o)

r.validate_task_06_03(g)

"""**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**

"""


vcard=Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
foaf=Namespace("http://xmlns.com/foaf/0.1/")
g.namespace_manager.bind('vcard', vcard, override=False)
g.namespace_manager.bind('foaf', foaf, override=False)

g.add((vcard.Family, RDF.type, RDF.Property))
g.add((vcard.Family, RDFS.range, XSD.string))

g.add((vcard.Given, RDF.type, RDF.Property))
g.add((vcard.Given, RDFS.range, XSD.string))

g.add((foaf.email, RDF.type, RDFS.Datatype))
g.add((foaf.email, RDFS.range, XSD.string))

g.add((data.Oscar, vcard.Family, Literal("Corcho García")))
g.add((data.Oscar, vcard.Given, Literal("Oscar")))
g.add((data.Oscar, foaf.email, Literal("ocorcho@fi.upm.es")))

# Visualize the results
for s, p, o in g:
print(s,p,o)

# Validation. Do not remove
r.validate_task_06_04(g)
r.save_report("_Task_06")
145 changes: 145 additions & 0 deletions Assignment4/Luca_Alvarez_24C006/task07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# -*- coding: utf-8 -*-
"""Task07_2025.ipynb

Automatically generated by Colab.

Original file is located at
https://colab.research.google.com/drive/13zP7djRTBeG5tsTNq-mV-TGtIeDmqV7W

**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**
"""

# Visualize the results
result = []
for s in g.subjects(RDF.type, RDFS.Class):
superclass = None
for o in g.objects(s, RDFS.subClassOf):
superclass = o
result.append((s, 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 = []
person_classes = set()
for s in g.subjects(RDFS.subClassOf, ns.Person):
person_classes.add(s)
person_classes.add(ns.Person)

for cls in person_classes:
for s in g.subjects(RDF.type, cls):
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 = """
SELECT ?ind WHERE {
?ind a ?type .
?type rdfs:subClassOf* <http://oeg.fi.upm.es/def/people#Person> .
}
"""

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 = """
SELECT ?name ?type WHERE {
?person <http://xmlns.com/foaf/0.1/knows> <http://oeg.fi.upm.es/def/people#> .
?person <http://xmlns.com/foaf/0.1/name> ?name .
?person a ?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 = """
SELECT DISTINCT ?name WHERE {
{
?x <http://xmlns.com/foaf/0.1/colleague> ?y .
?y a <http://oeg.fi.upm.es/def/people#Dog> .
?x <http://xmlns.com/foaf/0.1/name> ?name .
}
UNION
{
?x <http://xmlns.com/foaf/0.1/colleague> ?y .
?y <http://xmlns.com/foaf/0.1/colleague> ?z .
?z a <http://oeg.fi.upm.es/def/people#Dog> .
?x <http://xmlns.com/foaf/0.1/name> ?name .
}
}
"""

for r in g.query(query):
print(r.name)

# Visualize the results

## Validation: Do not remove
report.validate_07_04(g,query)
report.save_report("_Task_07")
40 changes: 40 additions & 0 deletions Assignment4/Luca_Alvarez_24C006/task08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""Task08.ipynb

Automatically generated by Colab.
**Task 08: Completing missing data**
"""

#!pip install rdflib
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2021-2022/master/Assignment4/course_materials"

from rdflib import Graph, Namespace, Literal, URIRef
g1 = Graph()
g2 = Graph()
g1.parse(github_storage+"/rdf/data01.rdf", format="xml")
g2.parse(github_storage+"/rdf/data02.rdf", format="xml")

"""Tarea: lista todos los elementos de la clase Person en el primer grafo (data01.rdf) y completa los campos (given name, family name y email) que puedan faltar con los datos del segundo grafo (data02.rdf). Puedes usar consultas SPARQL o iterar el grafo, o ambas cosas."""

from rdflib.namespace import RDF, RDFS

vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
ns = Namespace("http://data.org#Person")

# Iterar sobre individuos de clase Person en g1
for person in g1.subjects(RDF.type, ns.Person):
# Verificar si faltan propiedades
has_given = (person, vcard.Given) in g1
has_family = (person, vcard.Family) in g1
has_email = (person, vcard.EMAIL) in g1

# Buscar en g2 si faltan
if not has_given:
for given in g2.objects(person, vcard.Given):
g1.add((person, vcard.Given, given))
if not has_family:
for family in g2.objects(person, vcard.Family):
g1.add((person, vcard.Family, family))
if not has_email:
for email in g2.objects(person, foaf.email):
g1.add((person, vcard.EMAIL, email))
50 changes: 50 additions & 0 deletions Assignment4/Luca_Alvarez_24C006/task09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
"""Task09.ipynb
Automatically generated by Colab.
**Task 09: Data linking**
"""

#!pip install rdflib
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2021-2022/master/Assignment4/course_materials/"

from rdflib import Graph, Namespace, Literal, URIRef
g1 = Graph()
g2 = Graph()
g3 = Graph()
g1.parse(github_storage+"rdf/data03.rdf", format="xml")
g2.parse(github_storage+"rdf/data04.rdf", format="xml")

"""Busca individuos en los dos grafos y enlázalos mediante la propiedad OWL:sameAs, inserta estas coincidencias en g3. Consideramos dos individuos iguales si tienen el mismo apodo y nombre de familia. Ten en cuenta que las URI no tienen por qué ser iguales para un mismo individuo en los dos grafos."""

from rdflib.namespace import OWL
vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")

people_g1 = {}
people_g2 = {}

# Recorremos g1
for person in g1.subjects():
nick = None
family = None
for n in g1.objects(person, foaf.nick):
nick = str(n)
for f in g1.objects(person, vcard.Family):
family = str(f)
if nick and family:
people_g1[(nick, family)] = person

# Recorremos g2
for person in g2.subjects():
nick = None
family = None
for n in g2.objects(person, foaf.nick):
nick = str(n)
for f in g2.objects(person, vcard.Family):
family = str(f)
if nick and family:
people_g2[(nick, family)] = person

for key in people_g1:
if key in people_g2:
g3.add((people_g1[key], OWL.sameAs, people_g2[key]))

Loading
Loading