Skip to content

Commit db2f6bc

Browse files
authored
Merge pull request #43 from martufg/master
Assignment4 - Marta Elena Fernandez 23C066
2 parents d3a87c3 + b7127cd commit db2f6bc

3 files changed

Lines changed: 561 additions & 0 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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/1ILVvM3BcJgPzetzR6PF_HPT_Pr7lJG1l
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+
ontology = Namespace("http://www.oeg-upm.net/Ontology#")
38+
person = Namespace("http://oeg.fi.upm.es/def/people#")
39+
40+
g.namespace_manager.bind('ontology', ontology)
41+
g.namespace_manager.bind('person', person)
42+
43+
"""**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**
44+
45+
"""
46+
47+
# TO DO
48+
# Visualize the results
49+
50+
g.remove((None, None, None))
51+
52+
g.add((person.Person, RDF.type, RDFS.Class))
53+
g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string)))
54+
g.add((person.Professor, RDF.type, RDFS.Class))
55+
g.add((person.Professor, RDFS.subClassOf, person.Person))
56+
g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string)))
57+
g.add((person.FullProfessor, RDF.type, RDFS.Class))
58+
g.add((person.FullProfessor, RDFS.subClassOf, person.Professor))
59+
g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string)))
60+
g.add((person.AssociateProfessor, RDF.type, RDFS.Class))
61+
g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor))
62+
g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string)))
63+
g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class))
64+
g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor))
65+
g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string)))
66+
# Visualize the results
67+
for s, p, o in g:
68+
print(s,p,o)
69+
70+
# Validation. Do not remove
71+
r.validate_task_06_01(g)
72+
73+
"""**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)**"""
74+
75+
# TO DO
76+
# Visualize the results
77+
g.add((ontology.hasColleague, RDF.type, RDF.Property))
78+
g.add((ontology.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
79+
g.add((ontology.hasColleague, RDFS.domain, person.Person))
80+
g.add((ontology.hasColleague, RDFS.range, person.Person))
81+
82+
g.add((ontology.hasName, RDF.type, RDF.Property))
83+
g.add((ontology.hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
84+
g.add((ontology.hasName, RDFS.domain, person.Person))
85+
g.add((ontology.hasName, RDFS.range, RDFS.Literal))
86+
87+
g.add((ontology.hasHomePage, RDF.type, RDF.Property))
88+
g.add((ontology.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
89+
g.add((ontology.hasHomePage, RDFS.domain, person.FullProfessor))
90+
g.add((ontology.hasHomePage, RDFS.range, RDFS.Literal))
91+
92+
for s, p, o in g:
93+
print(s,p,o)
94+
95+
# Validation. Do not remove
96+
r.validate_task_06_02(g)
97+
98+
"""**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""
99+
100+
# TO DO
101+
# Visualize the results
102+
resource = Namespace("http://oeg.fi.upm.es/resource/person/")
103+
104+
g.add((resource.Oscar, RDF.type, ns.AssociateProfessor))
105+
g.add((resource.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
106+
g.add((resource.Oscar, ns.hasName, Literal("Oscar Corcho García", datatype=XSD.string)))
107+
g.add((resource.Oscar, ns.hasColleague, resource.Asun))
108+
109+
g.add((resource.Asun, RDF.type, ns.FullProfessor))
110+
g.add((resource.Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
111+
g.add((resource.Asun, ns.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string)))
112+
g.add((resource.Asun, ns.hasColleague, resource.Raul))
113+
114+
g.add((resource.Raul, RDF.type, ns.InterimAssociateProfessor))
115+
g.add((resource.Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
116+
117+
for s, p, o in g:
118+
print(s,p,o)
119+
120+
r.validate_task_06_03(g)
121+
122+
"""**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**
123+
124+
"""
125+
126+
# TO DO
127+
# Visualize the results
128+
129+
VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
130+
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
131+
132+
g.add((resource.Oscar, VCARD.Given, Literal("Oscar", datatype=XSD.string)))
133+
g.add((resource.Oscar, VCARD.Family, Literal("Corcho", datatype=XSD.string)))
134+
g.add((resource.Oscar, FOAF.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string)))
135+
136+
for s, p, o in g:
137+
print(s,p,o)
138+
139+
# Validation. Do not remove
140+
r.validate_task_06_04(g)
141+
r.save_report("_Task_06")
142+
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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/1Q3qHe6JYS1JeBz6dgZ396d2psyxia-rF
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+
# Visualize the results
36+
result = [] #list of tuples
37+
classes = set(g.subjects(RDF.type, RDFS.Class))
38+
classes.update(g.subjects(RDFS.subClassOf, None))
39+
for c in classes:
40+
superclasses = list(g.objects(c, RDFS.subClassOf))
41+
if not superclasses:
42+
result.append((c, None))
43+
else:
44+
for sc in superclasses:
45+
result.append((c, sc))
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+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
56+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
57+
SELECT ?c ?sc WHERE {
58+
?c rdf:type rdfs:Class .
59+
OPTIONAL {
60+
?c rdfs:subClassOf ?sc .
61+
}
62+
}
63+
"""
64+
65+
for r in g.query(query):
66+
print(r.c, r.sc)
67+
68+
## Validation: Do not remove
69+
report.validate_07_1b(query,g)
70+
71+
"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"**
72+
73+
"""
74+
75+
ns = Namespace("http://oeg.fi.upm.es/def/people#")
76+
77+
# variable to return
78+
individuals = []
79+
def is_person_subclass(cls):
80+
if cls==ns.Person:
81+
return True
82+
for s, p, o in g.triples((cls, RDFS.subClassOf, None)):
83+
if is_person_subclass(o):
84+
return True
85+
return False
86+
87+
for s, p, o in g.triples((None, RDF.type, None)):
88+
if o==ns.Person or is_person_subclass(o):
89+
individuals.append(s)
90+
# visualize results
91+
for i in individuals:
92+
print(i)
93+
94+
# validation. Do not remove
95+
report.validate_07_02a(individuals)
96+
97+
"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**"""
98+
99+
query = """
100+
PREFIX people: <http://oeg.fi.upm.es/def/people#>
101+
SELECT DISTINCT ?ind
102+
WHERE {
103+
?c rdfs:subClassOf* people:Person .
104+
?ind a ?c .
105+
}
106+
"""
107+
108+
for r in g.query(query):
109+
print(r.ind)
110+
# Visualize the results
111+
112+
## Validation: Do not remove
113+
report.validate_07_02b(g, query)
114+
115+
"""**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**"""
116+
117+
# TO DO
118+
query = """
119+
PREFIX people: <http://oeg.fi.upm.es/def/people#>
120+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
121+
SELECT ?name ?type WHERE {
122+
?person people:knows people:Rocky .
123+
?person rdfs:label ?name .
124+
?person rdf:type ?type .
125+
}
126+
"""
127+
128+
# Visualize the results
129+
for r in g.query(query):
130+
print(r.name, r.type)
131+
132+
## Validation: Do not remove
133+
report.validate_07_03(g, query)
134+
135+
"""**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**"""
136+
137+
query = """
138+
PREFIX people: <http://oeg.fi.upm.es/def/people#>
139+
SELECT DISTINCT ?name WHERE {
140+
?person rdfs:label ?name .
141+
{
142+
?person people:hasColleague ?colleague1 .
143+
?colleague1 people:ownsPet ?pet1 .
144+
} UNION {
145+
?person people:hasColleague ?colleague1 .
146+
?colleague1 people:hasColleague ?colleague2 .
147+
?colleague2 people:ownsPet ?pet2 .
148+
}
149+
}
150+
"""
151+
152+
for r in g.query(query):
153+
print(r.name)
154+
155+
# TO DO
156+
# Visualize the results
157+
158+
## Validation: Do not remove
159+
report.validate_07_04(g,query)
160+
report.save_report("_Task_07")
161+

0 commit comments

Comments
 (0)