Skip to content

Commit ab22394

Browse files
committed
Assigment4_ÁngelLuisDeleal
1 parent bc4e7ae commit ab22394

3 files changed

Lines changed: 570 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)