Skip to content

Commit c069e5a

Browse files
authored
Merge pull request #193 from riddhichitkara/master
Riddhi_Chitkara_24C023_Assignment4
2 parents 9e30ab9 + e301b65 commit c069e5a

3 files changed

Lines changed: 549 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)