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