Skip to content
Merged
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
120 changes: 120 additions & 0 deletions Assignment4/Assigment4_Gonzalo_Hernandez_24C060/Task06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
##Task 06: Modifying RDF(s)
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
ont = Namespace("http://oeg.fi.upm.es/def/ontology#")
per = 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
#Persona
g.add((per.Person, RDF.type, RDFS.Class))
g.add((per.Person, RDFS.label, Literal("Person", datatype=XSD.string)))
#Profesor
g.add((per.Professor, RDF.type, RDFS.Class))
g.add((per.Professor, RDFS.subClassOf, per.Person))
g.add((per.Professor, RDFS.label, Literal("Professor", datatype=XSD.string)))
#Professor COmpleto
g.add((per.FullProfessor, RDF.type, RDFS.Class))
g.add((per.FullProfessor, RDFS.subClassOf, per.Professor))
g.add((per.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string)))
#Profesor Asociado
g.add((per.AssociateProfessor, RDF.type, RDFS.Class))
g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor))
g.add((per.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string)))
#Profesor INterino
g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class))
g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor))
g.add((per.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string)))
# 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
#Propiedad hasName
g.add((per.hasName, RDF.type, RDF.Property))
g.add((per.hasName, RDFS.domain, per.Person))
g.add((per.hasName, RDFS.range, RDFS.Literal))
g.add((per.hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
#Propiedad hasColleague
g.add((per.hasColleague, RDF.type, RDF.Property))
g.add((per.hasColleague, RDFS.domain, per.Person))
g.add((per.hasColleague, RDFS.range, per.Person))
g.add((per.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
#Propiedad hasHomePage
g.add((per.hasHomePage, RDF.type, RDF.Property))
g.add((per.hasHomePage, RDFS.domain, per.FullProfessor))
g.add((per.hasHomePage, RDFS.range, RDFS.Literal))
g.add((per.hasHomePage, RDFS.label, Literal("hasHomePage", 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
#Namespace
data = Namespace("http://oeg.fi.upm.es/resource/person/")
#Oscar
g.add((data.Oscar, RDF.type, per.AssociateProfessor))
g.add((data.Oscar, per.hasColleague, data.Asun))
g.add((data.Oscar, per.hasName, Literal("Oscar Corcho García", datatype=XSD.string)))
g.add((data.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
#Asun
g.add((data.Asun, RDF.type, per.FullProfessor))
g.add((data.Asun, per.hasColleague, data.Raul))
g.add((data.Asun, per.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string)))
g.add((data.Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
#Raul
g.add((data.Raul, RDF.type, per.InterimAssociateProfessor))
g.add((data.Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
# Visualize the results
for s, p, o in g:
print(s,p,o)

##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
#Namespace
foaf = Namespace("http://xmlns.com/foaf/0.1/")
vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
#Nuevas relaciones
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.eamil, RDFS.range, XSD.string))
#Más info de Oscar
g.add((data.Oscar, vcard.Given, Literal("Oscar", datatype=XSD.string)))
g.add((data.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string)))
g.add((data.Oscar, foaf.email, Literal("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")
110 changes: 110 additions & 0 deletions Assignment4/Assigment4_Gonzalo_Hernandez_24C060/Task07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
##Task 07: Querying RDF(s)
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
#Lista classUri
result=[]
for sb in g.subjects(RDF.type, RDFS.Class):
sc=None
for Sc in g.objects(sb,RDFS.subClassOf):
sc=Sc
result.append((sb,sc))
for cl, sc in result:
short_c = g.namespace_manager.normalizeUri(cl)
short_sc = g.namespace_manager.normalizeUri(sc) if sc else None
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 rdf:type 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 subclases(cl):
sb=[]
for s,p,o in g.triples((None,RDFS.subClassOf,cl)):
sb.append(s)
sb += subclases(s)
return sb
clases= subclases(ns.Person)
clases.append(ns.Person)
for cl in clases:
for s,p,o in g.triples((None,RDF.type,cl)):
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{?c rdfs:subClassOf* <http://oeg.fi.upm.es/def/people#Person>. ?ind a ?c}"

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{
?name <http://oeg.fi.upm.es/def/people#knows> <http://oeg.fi.upm.es/def/people#Rocky>.
?name 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 ?name WHERE{
{ ?name people:hasColleague ?c1.
?c1 people:ownsPet ?dog.}
UNION{
?name people:hasColleague ?c2.
?c2 people:hasColleague ?c3.
?c3 people:ownsPet ?dog.}
}
"""

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")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hierarchy OK
TASK 6.1 OK
TASK 6.2 OK
TASK 6.4 OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
TASK 7.1a OK
TASK 7.1b OK
TASK 7.2a OK
TASK 7.2b OK
TASK 7.3 OK
TASK 7.4 OK
Loading