Skip to content

Commit f47c852

Browse files
authored
Merge pull request #75 from AlbertoGilCas/master
Assigment_4_AlbertoGil
2 parents 196474d + e786923 commit f47c852

8 files changed

Lines changed: 835 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)