Skip to content

Commit 0bd6b62

Browse files
authored
Merge pull request #182 from dusihan-hub/master
Add Task06 and Task07 with validation
2 parents b820a4d + 5ca792e commit 0bd6b62

3 files changed

Lines changed: 564 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# -*- coding: utf-8 -*-
2+
"""Task06
3+
4+
Automatically generated by Colab.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1e0oOdsx6Ab002N8PRAwoIJg1K0Lmvrt3
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/people#")
38+
person= Namespace("http://oeg.fi.upm.es/resource/person/")
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+
people = Namespace("http://oeg.fi.upm.es/def/people#")
45+
46+
Person = people.Person
47+
Professor = people.Professor
48+
AssociateProfessor = people.AssociateProfessor
49+
InterimAssociateProfessor = people.InterimAssociateProfessor
50+
FullProfessor = people.FullProfessor
51+
52+
for cls in [Person, Professor, AssociateProfessor, InterimAssociateProfessor, FullProfessor]:
53+
g.add((cls, RDF.type, RDFS.Class))
54+
55+
g.add((Professor, RDFS.subClassOf, Person))
56+
g.add((AssociateProfessor, RDFS.subClassOf, Professor))
57+
g.add((InterimAssociateProfessor, RDFS.subClassOf, AssociateProfessor))
58+
g.add((FullProfessor, RDFS.subClassOf, Professor))
59+
60+
labels = {
61+
Person: "Person",
62+
Professor: "Professor",
63+
AssociateProfessor: "AssociateProfessor",
64+
InterimAssociateProfessor: "InterimAssociateProfessor",
65+
FullProfessor: "FullProfessor"
66+
}
67+
for cls, label in labels.items():
68+
g.add((cls, RDFS.label, Literal(label, datatype=XSD.string)))
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+
# TO DO
76+
hasColleague = people.hasColleague
77+
hasName = people.hasName
78+
hasHomePage = people.hasHomePage
79+
80+
81+
for prop in [hasColleague, hasName, hasHomePage]:
82+
g.add((prop, RDF.type, RDF.Property))
83+
84+
g.add((hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string)))
85+
g.add((hasName, RDFS.label, Literal("hasName", datatype=XSD.string)))
86+
g.add((hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string)))
87+
88+
89+
g.add((hasColleague, RDFS.domain, people.Person))
90+
g.add((hasColleague, RDFS.range, people.Person))
91+
92+
g.add((hasName, RDFS.domain, people.Person))
93+
g.add((hasName, RDFS.range, RDFS.Literal))
94+
95+
g.add((hasHomePage, RDFS.domain, people.FullProfessor))
96+
g.add((hasHomePage, RDFS.range, RDFS.Literal))
97+
98+
# Visualize the results
99+
for s, p, o in g:
100+
print(s,p,o)
101+
102+
# Validation. Do not remove
103+
r.validate_task_06_02(g)
104+
105+
"""**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""
106+
107+
# TO DO
108+
Oscar = person.Oscar
109+
Asun = person.Asun
110+
Raul = person.Raul
111+
112+
g.add((Oscar, RDF.type, people.FullProfessor))
113+
g.add((Asun, RDF.type, people.AssociateProfessor))
114+
g.add((Raul, RDF.type, people.InterimAssociateProfessor))
115+
116+
g.add((Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string)))
117+
g.add((Asun, RDFS.label, Literal("Asun", datatype=XSD.string)))
118+
g.add((Raul, RDFS.label, Literal("Raul", datatype=XSD.string)))
119+
120+
g.add((Oscar, people.hasColleague, Asun))
121+
g.add((Oscar, people.hasName, Literal("Óscar Corcho García", datatype=XSD.string)))
122+
g.add((Asun, people.hasColleague, Raul))
123+
g.add((Asun, people.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string)))
124+
g.add((Raul, people.hasColleague, Oscar))
125+
g.add((Raul, people.hasName, Literal("Raul", datatype=XSD.string)))
126+
# Visualize the results
127+
for s, p, o in g:
128+
print(s,p,o)
129+
130+
r.validate_task_06_03(g)
131+
132+
"""**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**
133+
134+
"""
135+
136+
# TO DO
137+
VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/")
138+
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
139+
140+
g.add((FOAF.email, RDF.type, RDFS.Datatype))
141+
g.add((FOAF.eamil, RDFS.range, XSD.string))
142+
143+
g.add((VCARD.Given, RDF.type, RDF.Property))
144+
g.add((VCARD.Given, RDFS.range, XSD.string))
145+
146+
g.add((VCARD.Family, RDF.type, RDF.Property))
147+
g.add((VCARD.Family, RDFS.range, XSD.string))
148+
149+
g.add((Oscar, VCARD.Family, Literal("Corcho García")))
150+
g.add((Oscar, VCARD.Given, Literal("Oscar")))
151+
g.add((Oscar, FOAF.email, Literal("ocorcho@fi.upm.es")))
152+
# Visualize the results
153+
for s, p, o in g:
154+
print(s,p,o)
155+
156+
# Validation. Do not remove
157+
r.validate_task_06_04(g)
158+
r.save_report("_Task_06")
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# -*- coding: utf-8 -*-
2+
"""Task07
3+
4+
Automatically generated by Colab.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1Bxpkh82-TvYgHEubBrgXRwWz75gVTiUY
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+
result = []
36+
for c in g.subjects(RDF.type, RDFS.Class):
37+
superclass = g.value(subject=c, predicate=RDFS.subClassOf)
38+
result.append((c, superclass))
39+
40+
# Visualize the results
41+
for r in result:
42+
print(r)
43+
44+
## Validation: Do not remove
45+
report.validate_07_1a(result)
46+
47+
"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**"""
48+
49+
query = """
50+
SELECT ?c ?sc
51+
WHERE {
52+
?c a rdfs:Class .
53+
OPTIONAL { ?c rdfs:subClassOf ?sc . }
54+
FILTER(STRSTARTS(STR(?c), "http://oeg.fi.upm.es/def/people#"))
55+
}
56+
"""
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+
# variable to return
70+
individuals = []
71+
classes = set()
72+
classes.add(ns.Person)
73+
def get_subclasses(c):
74+
for s in g.subjects(RDFS.subClassOf, c):
75+
if s not in classes:
76+
classes.add(s)
77+
get_subclasses(s)
78+
79+
get_subclasses(ns.Person)
80+
81+
for c in classes:
82+
for ind in g.subjects(RDF.type, c):
83+
individuals.append(ind)
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 = """
95+
SELECT ?ind
96+
WHERE {
97+
?ind a ?c .
98+
?c rdfs:subClassOf* <http://oeg.fi.upm.es/def/people#Person> .
99+
}
100+
"""
101+
102+
103+
for r in g.query(query):
104+
print(r.ind)
105+
# Visualize the results
106+
107+
## Validation: Do not remove
108+
report.validate_07_02b(g, query)
109+
110+
"""**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**"""
111+
112+
query = """
113+
SELECT ?name ?type
114+
WHERE{
115+
?name <http://oeg.fi.upm.es/def/people#knows> <http://oeg.fi.upm.es/def/people#Rocky>.
116+
?name a ?type
117+
}
118+
"""
119+
120+
# TO DO
121+
# Visualize the results
122+
for r in g.query(query):
123+
print(r.name, r.type)
124+
125+
## Validation: Do not remove
126+
report.validate_07_03(g, query)
127+
128+
"""**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**"""
129+
130+
query = """
131+
SELECT ?name ?type
132+
WHERE{
133+
?x <http://oeg.fi.upm.es/def/people#ownsPet> ?y.
134+
{?name <http://oeg.fi.upm.es/def/people#hasColleague> ?x}
135+
UNION
136+
{?z <http://oeg.fi.upm.es/def/people#hasColleague> ?x.
137+
?name <http://oeg.fi.upm.es/def/people#hasColleague> ?z.}
138+
}
139+
"""
140+
for r in g.query(query):
141+
print(r.name)
142+
143+
# TO DO
144+
# Visualize the results
145+
146+
## Validation: Do not remove
147+
report.validate_07_04(g,query)
148+
report.save_report("_Task_07")

0 commit comments

Comments
 (0)