Skip to content

Commit 8a27d4f

Browse files
authored
Merge pull request #205 from PabloMartin2006/master
Assignment 4 sin tildes.
2 parents 76f6389 + b64f8d8 commit 8a27d4f

3 files changed

Lines changed: 531 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)