|
| 1 | +#%% [markdown] |
| 2 | +# **Task 06: Modifying RDF(s)** |
| 3 | + |
| 4 | +# %% |
| 5 | + |
| 6 | + |
| 7 | +# %% |
| 8 | +#%pip install rdflib |
| 9 | +import urllib.request |
| 10 | +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' |
| 11 | +urllib.request.urlretrieve(url, 'validation.py') |
| 12 | +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" |
| 13 | + |
| 14 | +# %% [markdown] |
| 15 | +# Import RDFLib main methods |
| 16 | + |
| 17 | +# %% |
| 18 | +from rdflib import Graph, Namespace, Literal, XSD |
| 19 | +from rdflib.namespace import RDF, RDFS |
| 20 | +from validation import Report |
| 21 | +g = Graph() |
| 22 | +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) |
| 23 | +r = Report() |
| 24 | + |
| 25 | +# %% [markdown] |
| 26 | +# Create a new class named Researcher |
| 27 | + |
| 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 | +# %% [markdown] |
| 35 | +# **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.** |
| 36 | + |
| 37 | +# %% |
| 38 | +person = Namespace("http://oeg-upm.net/people#") |
| 39 | +ontology = Namespace("http://oeg-upm.net/ontology#") |
| 40 | + |
| 41 | +g.bind('person', person) |
| 42 | +g.bind('ontology', ontology) |
| 43 | + |
| 44 | +# %% [markdown] |
| 45 | +# **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** |
| 46 | +# |
| 47 | + |
| 48 | +# %% |
| 49 | +people = Namespace("http://oeg.fi.upm.es/def/people#") |
| 50 | +g.bind("people", people) |
| 51 | + |
| 52 | + |
| 53 | +person = people.Person |
| 54 | +professor = people.Professor |
| 55 | +associate = people.AssociateProfessor |
| 56 | +interim = people.InterimAssociateProfessor |
| 57 | +full = people.FullProfessor |
| 58 | + |
| 59 | +g.add((person, RDF.type, RDFS.Class)) |
| 60 | +g.add((person, RDFS.label, Literal("Person", datatype=XSD.string))) |
| 61 | + |
| 62 | +g.add((professor, RDF.type, RDFS.Class)) |
| 63 | +g.add((professor, RDFS.label, Literal("Professor", datatype=XSD.string))) |
| 64 | +g.add((professor, RDFS.subClassOf, person)) |
| 65 | + |
| 66 | +g.add((associate, RDF.type, RDFS.Class)) |
| 67 | +g.add((associate, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) |
| 68 | +g.add((associate, RDFS.subClassOf, professor)) |
| 69 | + |
| 70 | +g.add((interim, RDF.type, RDFS.Class)) |
| 71 | +g.add((interim, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) |
| 72 | +g.add((interim, RDFS.subClassOf, associate)) |
| 73 | + |
| 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 | + |
| 79 | +# Visualize the results |
| 80 | +for s, p, o in g: |
| 81 | + print(s,p,o) |
| 82 | + |
| 83 | +# %% |
| 84 | +# Validation. Do not remove |
| 85 | +r.validate_task_06_01(g) |
| 86 | + |
| 87 | +# %% [markdown] |
| 88 | +# **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)** |
| 89 | + |
| 90 | +# %% |
| 91 | +hasName = people.hasName |
| 92 | +hasColleague = people.hasColleague |
| 93 | +hasHomePage = people.hasHomePage |
| 94 | + |
| 95 | +g.add((hasName, RDF.type, RDF.Property)) |
| 96 | +g.add((hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) |
| 97 | +g.add((hasName, RDFS.domain, person)) |
| 98 | +g.add((hasName, RDFS.range, RDFS.Literal)) |
| 99 | + |
| 100 | +g.add((hasColleague, RDF.type, RDFS.subPropertyOf)) |
| 101 | +g.add((hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) |
| 102 | +g.add((hasColleague, RDFS.domain, person)) |
| 103 | +g.add((hasColleague, RDFS.range, person)) |
| 104 | + |
| 105 | +g.add((hasHomePage, RDF.type, RDFS.subPropertyOf)) |
| 106 | +g.add((hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) |
| 107 | +g.add((hasHomePage, RDFS.domain, full)) |
| 108 | +g.add((hasHomePage, RDFS.range, RDFS.Literal)) |
| 109 | + |
| 110 | +for s, p, o in g: |
| 111 | + print(s,p,o) |
| 112 | + |
| 113 | +# %% |
| 114 | +# Validation. Do not remove |
| 115 | +r.validate_task_06_02(g) |
| 116 | + |
| 117 | +# %% [markdown] |
| 118 | +# **TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."** |
| 119 | + |
| 120 | +# %% |
| 121 | +indiv = Namespace("http://oeg.fi.upm.es/resource/person/") |
| 122 | + |
| 123 | +oscar = indiv.Oscar |
| 124 | +asun = indiv.Asun |
| 125 | +raul = indiv.Raul |
| 126 | +g.add((oscar, RDF.type, full)) |
| 127 | +g.add((oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) |
| 128 | +g.add((oscar, hasColleague, indiv.Asun)) |
| 129 | +g.add((oscar, hasName, Literal("Oscar García", datatype=XSD.string))) |
| 130 | + |
| 131 | +g.add((asun, RDF.type, full)) |
| 132 | +g.add((asun, RDFS.label, Literal("Asun", datatype=XSD.string))) |
| 133 | +g.add((asun, hasColleague, oscar)) |
| 134 | +g.add((asun, hasHomePage, Literal("https://www.oeg-upm.net/", datatype=XSD.string))) |
| 135 | + |
| 136 | +g.add((raul, RDFS.label, Literal("Raul", datatype=XSD.string))) |
| 137 | +g.add((raul, RDF.type, interim)) |
| 138 | + |
| 139 | +# Visualize the results |
| 140 | +for s, p, o in g: |
| 141 | + print(s,p,o) |
| 142 | + |
| 143 | +# %% |
| 144 | +r.validate_task_06_03(g) |
| 145 | + |
| 146 | +# %% [markdown] |
| 147 | +# **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** |
| 148 | +# |
| 149 | + |
| 150 | +# %% |
| 151 | +vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") |
| 152 | +foaf = Namespace("http://xmlns.com/foaf/0.1/") |
| 153 | + |
| 154 | +g.add((oscar, vcard.Given, Literal("Oscar", datatype=XSD.string))) |
| 155 | +g.add((oscar, vcard.Family, Literal("García", datatype=XSD.string))) |
| 156 | +g.add((oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string))) |
| 157 | +for s, p, o in g: |
| 158 | + print(s,p,o) |
| 159 | + |
| 160 | +# %% |
| 161 | +# Validation. Do not remove |
| 162 | +r.validate_task_06_04(g) |
| 163 | +r.save_report("_Task_06") |
| 164 | + |
| 165 | + |
0 commit comments