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/1tRMx6f429cKbgv3iBq9rV4eJWbhU5KH_
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+ g .bind ("ontology" , ontology )
39+ person = Namespace ("http://oeg.fi.upm.es/def/people#" )
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 ((person .Person , RDF .type , RDFS .Class ))
48+ g .add ((person .Person , RDFS .label , Literal ("Person" , datatype = XSD .string )))
49+ g .add ((person .Professor , RDF .type , RDFS .Class ))
50+ g .add ((person .Professor , RDFS .label , Literal ("Professor" , datatype = XSD .string )))
51+ g .add ((person .AssociateProfessor , RDF .type , RDFS .Class ))
52+ g .add ((person .AssociateProfessor , RDFS .label , Literal ("AssociateProfessor" , datatype = XSD .string )))
53+ g .add ((person .InterimAssociateProfessor , RDF .type , RDFS .Class ))
54+ g .add ((person .InterimAssociateProfessor , RDFS .label , Literal ("InterimAssociateProfessor" , datatype = XSD .string )))
55+ g .add ((person .FullProfessor , RDF .type , RDFS .Class ))
56+ g .add ((person .FullProfessor , RDFS .label , Literal ("FullProfessor" , datatype = XSD .string )))
57+
58+ g .add ((person .Professor , RDFS .subClassOf , person .Person ))
59+ g .add ((person .FullProfessor , RDFS .subClassOf , person .Professor ))
60+ g .add ((person .AssociateProfessor , RDFS .subClassOf , person .Professor ))
61+ g .add ((person .InterimAssociateProfessor , RDFS .subClassOf , person .AssociateProfessor ))
62+ # Visualize the results
63+ for s , p , o in g :
64+ print (s ,p ,o )
65+
66+ # Validation. Do not remove
67+ r .validate_task_06_01 (g )
68+
69+ """**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)**"""
70+
71+ # TO DO
72+
73+ g .add ((person .hasName , RDF .type , RDF .Property ))
74+ g .add ((person .hasName , RDFS .label , Literal ("hasName" , datatype = XSD .string )))
75+ g .add ((person .hasName , RDFS .domain , person .Person ))
76+ g .add ((person .hasName , RDFS .range , RDFS .Literal ))
77+
78+ g .add ((person .hasHomePage , RDF .type , RDF .Property ))
79+ g .add ((person .hasHomePage , RDFS .label , Literal ("hasHomePage" , datatype = XSD .string )))
80+ g .add ((person .hasHomePage , RDFS .domain , person .FullProfessor ))
81+ g .add ((person .hasHomePage , RDFS .range , RDFS .Literal ))
82+
83+
84+ g .add ((person .hasColleague , RDF .type , RDF .Property ))
85+ g .add ((person .hasColleague , RDFS .label , Literal ("hasColleague" , datatype = XSD .string )))
86+ g .add ((person .hasColleague , RDFS .domain , person .Person ))
87+ g .add ((person .hasColleague , RDFS .range , person .Person ))
88+
89+ # Visualize the results
90+ for s , p , o in g :
91+ print (s ,p ,o )
92+
93+ # Validation. Do not remove
94+ r .validate_task_06_02 (g )
95+
96+ """**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**"""
97+
98+ # TO DO
99+ data = Namespace ("http://oeg.fi.upm.es/resource/person/" )
100+
101+ g .add ((data .Oscar , RDF .type , person .AssociateProfessor ))
102+ g .add ((data .Oscar , RDFS .label , Literal ("Oscar" , datatype = XSD .string )))
103+ g .add ((data .Oscar , person .hasName , Literal ("Óscar Corcho García" )))
104+ g .add ((data .Oscar , person .hasColleague , data .Asun ))
105+
106+ g .add ((data .Asun , RDF .type , person .FullProfessor ))
107+ g .add ((data .Asun , RDFS .label , Literal ("Asun" , datatype = XSD .string )))
108+ g .add ((data .Asun , person .hasHomePage , Literal ("http://www.oeg-upm.net/" )))
109+ g .add ((data .Asun , person .hasColleague , data .Raul ))
110+
111+ g .add ((data .Raul , RDF .type , person .InterimAssociateProfessor ))
112+ g .add ((data .Raul , RDFS .label , Literal ("Raul" , datatype = XSD .string )))
113+
114+ # Visualize the results
115+ for s , p , o in g :
116+ print (s ,p ,o )
117+
118+ r .validate_task_06_03 (g )
119+
120+ """**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**
121+
122+ """
123+
124+ # TO DO
125+ vcard = Namespace ("http://www.w3.org/2001/vcard-rdf/3.0/" )
126+ foaf = Namespace ("http://xmlns.com/foaf/0.1/" )
127+
128+ g .add ((vcard .Given , RDF .type , RDF .Property ))
129+ g .add ((vcard .Given , RDFS .range , XSD .string ))
130+
131+ g .add ((vcard .Family , RDF .type , RDF .Property ))
132+ g .add ((vcard .Family , RDFS .range , XSD .string ))
133+
134+
135+ g .add ((foaf .email , RDF .type , RDFS .Datatype ))
136+ g .add ((foaf .email , RDFS .range , XSD .string ))
137+
138+ g .add ((data .Oscar , vcard .Family , Literal ("Corcho García" )))
139+ g .add ((data .Oscar , vcard .Given , Literal ("Oscar" )))
140+ g .add ((data .Oscar , foaf .email , Literal ("ocorcho@fi.upm.es" )))
141+
142+ # Visualize the results
143+ for s , p , o in g :
144+ print (s ,p ,o )
145+
146+ # Validation. Do not remove
147+ r .validate_task_06_04 (g )
148+ r .save_report ("_Task_06" )
0 commit comments