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
@@ -1,4 +1,5 @@
Your name; Your GitHub user; Dataset name; Dataset URL; Dataset brief description
Alfredo Antonio Aleix Ayuga; maincato; Area of parks and green areas of Madrid; https://data.europa.eu/data/datasets/https-datos-madrid-es-egob-catalogo-300266-0-arbolado-superficie?locale=en; Este dataset contiene información sobre parques y zonas verdes en Madrid.
Mohamed Khachani; mohamed-khachani; Mobiliario urbano. Papeleras; https://datos.madrid.es/portal/site/egob/menuitem.c05c1f754a33a9fbe4b2e4b284f1a5a0/?vgnextoid=9c8d5949a2a8a510VgnVCM2000001f4a900aRCRD&vgnextchannel=374512b9ace9f310VgnVCM100000171f5a0aRCRD&vgnextfmt=default; Este dataset recoge más de 92.000 papeleras de Madrid con su ubicación y modelo (según elementos urbanos normalizados), excluyendo las situadas en parques históricos y forestales.
Channa Pan; ChannaPan; Variedades de rosas en la Rosaleda del Parque del Oeste; https://datos.gob.es/es/catalogo/l01280796-variedades-de-rosas-en-la-rosaleda-del-parque-del-oeste; Las variedades de rosas de la Rosaleda del Parque del Oeste que se relacionan pertenecen a tres colecciones: variedades antiguas, variedades modernas y especies silvestres de la Península Ibérica.
Adrián Gómez; adrigomez6; Inventario de zonas verdes; https://datos.madrid.es/egob/catalogo/300153-0-zonas-verdes-inventario.dcat; Este conjunto de datos ofrece los datos en formato reutilizable del Inventario de Vías Públicas y Zonas Verdes, que recoge los bienes y derechos del Ayuntamiento de Madrid y sus Organismos Públicos de acuerdo a la Instrucción 5/2014, el Epígrafe correspondiente a Parques, Jardines y otras Zonas Verdes.
Expand Down
135 changes: 135 additions & 0 deletions Assignment4/maincato_089065/task06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
"""Task06.ipynb

Automatically generated by Colab.

Original file is located at
https://colab.research.google.com/drive/12UnKd89GDXjQSh86kg_KiWhITpKxHKK9

**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("https://www.oeg-upm.net/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**

"""

# TO DO
g.add((person.Person, RDF.type, RDFS.Class))
g.add((person.Professor, RDF.type, RDFS.Class))
g.add((person.AssociateProfessor, RDF.type, RDFS.Class))
g.add((person.FullProfessor, RDF.type, RDFS.Class))
g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class))
g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string)))
g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string)))
g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string)))
g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string)))
g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string)))
g.add((person.Professor, RDFS.subClassOf, person.Person))
g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor))
g.add((person.FullProfessor, RDFS.subClassOf, person.Professor))
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)**"""

# TO DO
g.add((person.hasColleague, RDF.type, RDF.Property))
g.add((person.hasColleague, RDFS.domain, ontology.Person))
g.add((person.hasColleague, RDFS.range, ontology.University))
g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
g.add((person.hasHomePage, RDF.type, RDF.Property))
g.add((person.hasHomePage, RDFS.domain, ontology.Researcher))
g.add((person.hasHomePage, RDFS.range, ontology.University))
g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
g.add((person.hasName, RDF.type, RDF.Property))
g.add((person.hasName, RDFS.domain, ontology.Person))
g.add((person.hasName, RDFS.range, ontology.University))
g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
# 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."**"""

# TO DO
data=Namespace("http://oeg.fi.upm.es/resource/person/")

g.add((data.Asun, RDF.type, person.FullProfessor))
g.add((data.Oscar, RDF.type, person.AssociateProfessor))
g.add((data.Raul, RDF.type, person.InterimAssociateProfessor))
g.add((data.Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
g.add((data.Asun, person.hasColleague, data.Raul))
g.add((data.Oscar, person.hasColleague, data.Asun))
g.add((data.Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
g.add((data.Oscar, person.hasName, Literal("Óscar Corcho García")))
g.add((data.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
g.add((data.Asun, person.hasHomePage, Literal("http://www.oeg-upm.net/")))
# 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**

"""

# TO DO
vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
foaf = Namespace("http://xmlns.com/foaf/0.1/")

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, RDF.Property))
g.add((foaf.email, RDFS.range, XSD.string))

g.add((person.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string)))
g.add((person.Oscar, vcard.Given, Literal("Oscar", datatype=XSD.string)))
g.add((person.Oscar, foaf.email, Literal("oscar.ocorcho@fi.upm.es", datatype=XSD.string)))
# 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")
131 changes: 131 additions & 0 deletions Assignment4/maincato_089065/task07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# -*- coding: utf-8 -*-
"""Task07.ipynb

Automatically generated by Colab.

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

**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 = [(c, g.value(subject=c, predicate=RDFS.subClassOf, object=None)) for c,p,o in g.triples((None, RDF.type, RDFS.Class))]
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)**"""

query1 = "SELECT ?c ?sc WHERE {?c rdf:type rdfs:Class. OPTIONAL {?c rdfs:subClassOf ?sc.}}"

for r in g.query(query1):
print(r.c, r.sc)

## Validation: Do not remove
report.validate_07_1b(query1,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 get_subclasses(cls):
subclasses = set(g.subjects(RDFS.subClassOf, cls))
all_subs = set(subclasses)
for s in subclasses:
all_subs = all_subs.union(get_subclasses(s))
return all_subs

classes = {ns.Person} | get_subclasses(ns.Person)

for c in classes:
for ind in g.subjects(RDF.type, c):
individuals.append(ind)

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

query2 = """
SELECT DISTINCT ?ind
WHERE {
?clase rdfs:subClassOf* <http://oeg.fi.upm.es/def/people#Person> .
?ind a ?clase .
}
"""

for r in g.query(query2):
print(r.ind)
# Visualize the results

## Validation: Do not remove
report.validate_07_02b(g, query2)

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

query3 = """SELECT ?name ?type WHERE{
?name <http://oeg.fi.upm.es/def/people#knows> <http://oeg.fi.upm.es/def/people#Rocky>.
?name a ?type .
}"""
# TO DO
for r in g.query(query3):
print(r.name, r.type)

## Validation: Do not remove
report.validate_07_03(g, query3)

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

query4 = """
SELECT ?name
WHERE {
?p <http://oeg.fi.upm.es/def/people#ownsPet> ?m.
{?p <http://oeg.fi.upm.es/def/people#ownsPet> ?pet .
?name <http://oeg.fi.upm.es/def/people#hasColleague> ?p .}
UNION
{?p2 <http://oeg.fi.upm.es/def/people#hasColleague> ?p.
?name <http://oeg.fi.upm.es/def/people#hasColleague> ?p2.}
}
"""

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

# TO DO
# Visualize the results

## Validation: Do not remove
report.validate_07_04(g,query4)
report.save_report("_Task_07")
Loading
Loading