Skip to content

Commit b00c270

Browse files
entrega_assingment_4
1 parent 58214fc commit b00c270

3 files changed

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

0 commit comments

Comments
 (0)