Skip to content

Commit 4b3ae44

Browse files
authored
Assignment4/Channa_Pan_24C039 (#80)
* Create Channa_Pan_24C039 * Delete Assignment4/Channa_Pan_24C039 * Create task06.py * Add files via upload
1 parent b3cc40d commit 4b3ae44

3 files changed

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

0 commit comments

Comments
 (0)