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
165 changes: 165 additions & 0 deletions Assignment4/Alberto_Barreiro_24C008/task06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#%% [markdown]
# **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"

# %% [markdown]
# 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()

# %% [markdown]
# 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)

# %% [markdown]
# **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.**

# %%
person = Namespace("http://oeg-upm.net/people#")
ontology = Namespace("http://oeg-upm.net/ontology#")

g.bind('person', person)
g.bind('ontology', ontology)

# %% [markdown]
# **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**
#

# %%
people = Namespace("http://oeg.fi.upm.es/def/people#")
g.bind("people", people)


person = people.Person
professor = people.Professor
associate = people.AssociateProfessor
interim = people.InterimAssociateProfessor
full = people.FullProfessor

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

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

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

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

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


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

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

# %% [markdown]
# **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)**

# %%
hasName = people.hasName
hasColleague = people.hasColleague
hasHomePage = people.hasHomePage

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

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

g.add((hasHomePage, RDF.type, RDFS.subPropertyOf))
g.add((hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
g.add((hasHomePage, RDFS.domain, full))
g.add((hasHomePage, RDFS.range, RDFS.Literal))

for s, p, o in g:
print(s,p,o)

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

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

# %%
indiv = Namespace("http://oeg.fi.upm.es/resource/person/")

oscar = indiv.Oscar
asun = indiv.Asun
raul = indiv.Raul
g.add((oscar, RDF.type, full))
g.add((oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
g.add((oscar, hasColleague, indiv.Asun))
g.add((oscar, hasName, Literal("Oscar García", datatype=XSD.string)))

g.add((asun, RDF.type, full))
g.add((asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
g.add((asun, hasColleague, oscar))
g.add((asun, hasHomePage, Literal("https://www.oeg-upm.net/", datatype=XSD.string)))

g.add((raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
g.add((raul, RDF.type, interim))

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

# %%
r.validate_task_06_03(g)

# %% [markdown]
# **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.add((oscar, vcard.Given, Literal("Oscar", datatype=XSD.string)))
g.add((oscar, vcard.Family, Literal("García", datatype=XSD.string)))
g.add((oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string)))
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")


170 changes: 170 additions & 0 deletions Assignment4/Alberto_Barreiro_24C008/task07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# %% [markdown]
# **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

# %% [markdown]
# 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()

# %% [markdown]
# **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**

# %%
result = []
for cls in g.subjects(RDF.type, RDFS.Class):
superclasses = list(g.objects(cls, RDFS.subClassOf))
if superclasses:
for sup in superclasses:
result.append((str(cls), str(sup)))
else:
result.append((str(cls), None))
for r in result:
print(r)

# %%
## Validation: Do not remove
report.validate_07_1a(result)

# %% [markdown]
# **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)

# %% [markdown]
# **TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"**
#

# %%
people = Namespace("http://oeg.fi.upm.es/def/people#")

def is_subclass_of_person(c):
if c == people.Person:
return True
for _, _, sup in g.triples((c, RDFS.subClassOf, None)):
if is_subclass_of_person(sup):
return True
return False

individuals = []
vistos = set()

for s, p, o in g.triples((None, RDF.type, None)):
if o == people.Person or is_subclass_of_person(o):
if s not in vistos:
vistos.add(s)
individuals.append(s)

for i in individuals:
print(i)

# %%
# validation. Do not remove
report.validate_07_02a(individuals)

# %% [markdown]
# **TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**

# %%

query = """
PREFIX people: <http://oeg.fi.upm.es/def/people#>
SELECT DISTINCT ?ind
WHERE {
?ind rdf:type ?c .
?c rdfs:subClassOf* people:Person .
}
"""
for r in g.query(query):
print(r.ind)
# Visualize the results

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

# %% [markdown]
# **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 = """
PREFIX people: <http://oeg.fi.upm.es/def/people#>
SELECT ?name ?type
where{
?x people:knows people:Rocky .
?x rdfs:label ?name .
?x 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)

# %% [markdown]
# **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 DISTINCT ?name WHERE {
?x rdfs:label ?name .
{
?x people:hasColleague ?colleague .
?z people:ownsPet ?pet .
} UNION {
?x people:hasColleague ?colleague1 .
?y people:hasColleague ?colleague2 .
?z people:ownsPet ?pet .
}
}
"""

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


Loading