Skip to content

Commit d3a87c3

Browse files
authored
Merge pull request #44 from yichenchen2006/master
Assignment 4
2 parents df28c4a + 7ca9ba2 commit d3a87c3

3 files changed

Lines changed: 537 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)