From 117cd84c884586e48774ef5b13d177a1f2493fdc Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Mon, 20 Oct 2025 20:19:05 +0200 Subject: [PATCH 01/11] Assignment 4 Implement validation checks for RDF graphs including class hierarchy, property domains and ranges, and individual properties. --- .../Carlos_Cumbrado_24C055/validation.py | 258 ++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 Assignment4/Carlos_Cumbrado_24C055/validation.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/validation.py b/Assignment4/Carlos_Cumbrado_24C055/validation.py new file mode 100644 index 00000000..6024e0b5 --- /dev/null +++ b/Assignment4/Carlos_Cumbrado_24C055/validation.py @@ -0,0 +1,258 @@ +from rdflib import Graph, Namespace, Literal, XSD +from rdflib.namespace import RDF, RDFS + +VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") +FOAF = Namespace("http://xmlns.com/foaf/0.1/") + +class Report: + def __init__(self): + self.__report = "" + + def domain_and_range_correspond_to_input(self, g,propertyURI,correct_domain,correct_range): + domain = g.value(subject=propertyURI, predicate=RDFS.domain) + range = g.value(subject=propertyURI, predicate=RDFS.range) + if domain is None or range is None: + return False + if domain != correct_domain or range != correct_range: + return False + return True + + def does_it_have_label(self, g, entity): + label = g.value(subject=entity, predicate=RDFS.label) + if label is None: + return False + return True + + def namespace_is_correct_class(self, entity): + if entity is None: + return False + if "http://oeg.fi.upm.es/def/people#" not in entity: + return False + return True + + def namespace_is_correct_instance(self, entity): + if entity is None: + return False + if "http://oeg.fi.upm.es/resource/person/" not in entity: + return False + return True + + def is_subClassOf(self, g, subClass, superClass): + candidate = g.value(subject=subClass, predicate=RDFS.subClassOf, object=None) + if candidate is None or superClass not in candidate: + return False + return True + + def __add_to_report(self, message): + print(message) + self.__report = self.__report + message + "\n" + + def validate_task_06_01(self, g): + error = False + professorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Professor", datatype=XSD.string)) + personURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Person", datatype=XSD.string)) + associateProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("AssociateProfessor", datatype=XSD.string)) + interimURI = g.value(subject=None, predicate=RDFS.label, object=Literal("InterimAssociateProfessor", datatype=XSD.string)) + fProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("FullProfessor", datatype=XSD.string)) + classes = [professorURI,personURI,associateProfessorURI,interimURI, fProfessorURI] + # check namespace and existence + for i in classes: + if i is None: + self.__add_to_report("ERROR: One of the classes is missing its correct label! I cannot retrieve it") + error = True + return + if self.namespace_is_correct_class(i): + print("The namespace is correct for " + str(i)) + else: + self.__add_to_report("ERROR: The namespace is not correct for " + str(i)) + error = True + # check class hierarchy + if self.is_subClassOf(g, professorURI, personURI) and \ + self.is_subClassOf(g, associateProfessorURI, professorURI) and \ + self.is_subClassOf(g, interimURI, associateProfessorURI) and \ + self.is_subClassOf(g, fProfessorURI, professorURI): + self.__add_to_report("Hierarchy OK") + else: + self.__add_to_report("ERROR: Hierarchy is missing a subclassOf statement") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.1") + else: + self.__add_to_report("TASK 6.1 OK") + + def validate_task_06_02(self, g): + # check properties + error = False + hasColleague = g.value(subject=None, predicate=RDFS.label, object=Literal("hasColleague", datatype=XSD.string)) + hasName = g.value(subject=None, predicate=RDFS.label, object=Literal("hasName", datatype=XSD.string)) + hasHomePage = g.value(subject=None, predicate=RDFS.label, object=Literal("hasHomePage", datatype=XSD.string)) + personURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Person", datatype=XSD.string)) + fullProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("FullProfessor", datatype=XSD.string)) + properties = [hasColleague, hasName, hasHomePage] + for i in properties: + if i is None: + self.__add_to_report("ERROR: One of the properties is missing its correct label! I cannot retrieve it") + error = True + return + if not self.domain_and_range_correspond_to_input(g,hasColleague,personURI,personURI): + self.__add_to_report("ERROR: hasColleague has an incorrect domain or range") + error = True + if not self.domain_and_range_correspond_to_input(g,hasName,personURI,RDFS.Literal): + self.__add_to_report("ERROR: hasName has an incorrect domain or range") + error = True + if not self.domain_and_range_correspond_to_input(g,hasHomePage,fullProfessorURI,RDFS.Literal): + self.__add_to_report("ERROR: hasHomePage has an incorrect domain or range") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.2") + else: + self.__add_to_report("TASK 6.2 OK") + + def validate_task_06_03(self, g): + # check all individuals can be retrieved through their label + error = False + oscar = g.value(subject=None, predicate=RDFS.label, object=Literal("Oscar", datatype=XSD.string)) + asun = g.value(subject=None, predicate=RDFS.label, object=Literal("Asun", datatype=XSD.string)) + raul = g.value(subject=None, predicate=RDFS.label, object=Literal("Raul", datatype=XSD.string)) + if oscar is None or asun is None or raul is None: + self.__add_to_report("ERROR: One of the individuals is missing its correct label! I cannot retrieve it") + error = True + # check all individuals have the correct namespace + if not self.namespace_is_correct_instance(oscar): + self.__add_to_report("ERROR: Oscar has an incorrect namespace") + error = True + if not self.namespace_is_correct_instance(asun): + self.__add_to_report("ERROR: Asun has an incorrect namespace") + error = True + if not self.namespace_is_correct_instance(raul): + self.__add_to_report("ERROR: Raul has an incorrect namespace") + error = True + # check all individuals have their properties + oscar_properties = [] + for p in g.predicates(subject=oscar): + oscar_properties.append(p) + asun_properties = [] + for p in g.predicates(subject=asun): + asun_properties.append(p) + if oscar_properties is None or asun_properties is None: + self.__add_to_report("ERROR: One of the individuals has no properties") + error = True + if len(oscar_properties) != 4 or len(asun_properties) != 4: + # oscar: type, label, hasColleague, hasName. + # asun: type, label, hasHomePage, hasColleague + self.__add_to_report("ERROR: One of the individuals has the wrong number of properties") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.3") + else: + self.__add_to_report("TASK 6.3 OK") + + def validate_task_06_04(self, g): + error = False + target_properties = [VCARD.Given, VCARD.Family, FOAF.email] + #retrieve all triples from Oscar. + oscar_properties = [] + oscar = g.value(subject=None, predicate=RDFS.label, object=Literal("Oscar", datatype=XSD.string)) + for p in g.predicates(subject=oscar): + oscar_properties.append(p) + if oscar_properties is None: + self.__add_to_report("ERROR: Oscar has no properties") + error = True + # do they have the correct ns? + for i in target_properties: + if i not in oscar_properties: + self.__add_to_report("ERROR: One of the properties from Oscar has no correct namespace or does not exist. Please double check") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.4") + else: + self.__add_to_report("TASK 6.4 OK") + + def save_report(self, task): + report_name = "report_result" + task + ".txt" + with open(report_name, "w", encoding="utf-8") as f: + f.write(self.__report) + + def validate_07_01(self, result, task): + error = False + if len(result) != 7: + self.__add_to_report("ERROR: The number of classes returned is not correct") + error = True + for c,sc in result: + # Anything except Person and Animal must have a superclass + if sc == None and "Person" not in str(c) and "Animal" not in str(c): + self.__add_to_report("The class "+str(c)+" has no superclass") + error = True + if "Person" not in str(c) and "Animal" not in str(c) \ + and "Professor" not in str(c) and "Student" not in str(c) \ + and "FullProfessor" not in str(c) and "AssociateProfessor" not in str(c) \ + and "AssociateProfessor" not in str(c) and "Instructor" not in str(c) \ + and "InterimAssociateProfessor" not in str(c): + self.__add_to_report("ERROR: incorrect class retrieved") + error = True + if not error: + self.__add_to_report(task+" OK") + + def validate_07_1a(self, result): + self.validate_07_01(result, "TASK 7.1a") + + def validate_07_1b(self, query, g): + aux = g.query(query) + aux_dict = [] + for r in g.query(query): + aux_dict.append((r.c, r.sc)) + self.validate_07_01(aux_dict, "TASK 7.1b") + + def validate_07_02(self,result, task): + error = False + if len(result) != 3: + self.__add_to_report("ERROR: The number of individuals returned is not correct") + error = True + for i in result: + if "Asun" not in i and "Raul" not in i and "Oscar" not in i: + self.__add_to_report("ERROR: The individual "+str(i)+" is not correct") + error = True + if error == False: + self.__add_to_report(task+" OK") + + + def validate_07_02a(self, individuals): + self.validate_07_02(individuals, "TASK 7.2a") + + def validate_07_02b(self, g, query): + error = False + aux = g.query(query) + aux_dict = [] + for r in g.query(query): + if (r.ind is None): + self.__add_to_report("ERROR: Variable used to retrieve the individuals is not correct!") + error = True + else: + aux_dict.append(r.ind) + self.validate_07_02(aux_dict, "TASK 7.2b") + + def validate_07_03(self, g, query): + error = False + entities = g.query(query) + if len(list(entities)) != 3: + self.__add_to_report("ERROR: The number of individuals returned is not correct") + error = True + for i in entities: + if "Asun" not in i.name and "Raul" not in i.name and "Fantasma" not in i.name: + self.__add_to_report("ERROR: An individual returned is not correct") + error = True + if not error: + self.__add_to_report("TASK 7.3 OK") + + def validate_07_04(self, g, query): + error = False + entities = g.query(query) + if len(list(entities)) != 3: + self.__add_to_report("ERROR: The number of individuals returned is not correct") + error = True + for i in entities: + if "Asun" not in i.name and "Raul" not in i.name and "Oscar" not in i.name: + self.__add_to_report("ERROR: An individual returned is not correct") + error = True + if not error: + self.__add_to_report("TASK 7.4 OK") From 527b39770777c168413587e527be03a4610e35b1 Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Mon, 20 Oct 2025 20:46:44 +0200 Subject: [PATCH 02/11] Add files via upload --- Assignment4/Carlos_Cumbrado_24C055/task06.py | 139 +++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Assignment4/Carlos_Cumbrado_24C055/task06.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task06.py b/Assignment4/Carlos_Cumbrado_24C055/task06.py new file mode 100644 index 00000000..bc89e3ca --- /dev/null +++ b/Assignment4/Carlos_Cumbrado_24C055/task06.py @@ -0,0 +1,139 @@ +# -*- coding: utf-8 -*- +"""Task06.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task06.ipynb + +**Task 06: Modifying RDF(s)** +""" + +!pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +"""Import RDFLib main methods""" + +from rdflib import Graph, Namespace, Literal, XSD +from rdflib.namespace import RDF, RDFS +from validation import Report +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +r = Report() + +"""Create a new class named Researcher""" + +ns = Namespace("http://mydomain.org#") +g.add((ns.Researcher, RDF.type, RDFS.Class)) +for s, p, o in g: + print(s,p,o) + +"""**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.**""" + +# this task is validated in the next step +persona = Namespace("http://oeg.fi.upm.es/def/people#") +ontologia = Namespace("http://oeg.fi.upm.es/def/ontology#") + +"""**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** + +""" + +# TO DO + +g.add((person.Person, RDF.type, RDFS.Class)) +g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string))) + +g.add((person.Professor, RDF.type, RDFS.Class)) +g.add((person.Professor, RDFS.subClassOf, person.Person)) +g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) + +g.add((person.FullProfessor, RDF.type, RDFS.Class)) +g.add((person.FullProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) + +g.add((person.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) + +g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor)) +g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) + + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_01(g) + +"""**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)**""" + +# TO DO + +g.add((person.hasHomePage, RDF.type, RDF.Property)) +g.add((person.hasName, RDF.type, RDF.Property)) +g.add((person.hasColleague, RDF.type, RDF.Property)) + +g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) +g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) +g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) + +g.add((person.hasHomePage, RDFS.domain, person.FullProfessor)) +g.add((person.hasName, RDFS.domain, person.Person)) +g.add((person.hasColleague, RDFS.domain, person.Person)) + +g.add((person.hasHomePage, RDFS.range, RDFS.Literal)) +g.add((person.hasName, RDFS.range, RDFS.Literal)) +g.add((person.hasColleague, RDFS.range, person.Person)) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_02(g) + +""":**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**""" + +# TO DO + +g.add((person.Oscar, RDF.type, person.FullProfessor)) +g.add((person.Asun, RDF.type, person.FullProfessor)) +g.add((person.Raul, RDF.type, person.InterimAssociateProfessor)) + +g.add((person.Oscar, person.hasColleague, person.Asun)) +g.add((person.Asun, person.hasColleague, person.Raul)) + +g.add((person.Oscar, person.hasName, Literal("Óscar Corcho García"))) +g.add((person.Asun, person.hasHomePage, Literal("http://oeg.fi.upm.es/"))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +r.validate_task_06_03(g) + +"""**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** + +""" + +# TO DO + +foaf = Namespace("http://xmlns.com/foaf/0.1/") +vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") + +g.add((person.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string))) +g.add((person.Oscar, vcard.Given, Literal("Óscar", datatype=XSD.string))) +g.add((person.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_04(g) +r.save_report("_Task_06") \ No newline at end of file From 1bbfcfb847e464ed93e901b2af3a92107f6ce953 Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Mon, 20 Oct 2025 20:48:29 +0200 Subject: [PATCH 03/11] Comment out rdflib installation in task06.py Comment out the pip install command for rdflib --- Assignment4/Carlos_Cumbrado_24C055/task06.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assignment4/Carlos_Cumbrado_24C055/task06.py b/Assignment4/Carlos_Cumbrado_24C055/task06.py index bc89e3ca..0deeaf15 100644 --- a/Assignment4/Carlos_Cumbrado_24C055/task06.py +++ b/Assignment4/Carlos_Cumbrado_24C055/task06.py @@ -9,7 +9,7 @@ **Task 06: Modifying RDF(s)** """ -!pip install rdflib +#!pip install rdflib import urllib.request url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' urllib.request.urlretrieve(url, 'validation.py') @@ -136,4 +136,4 @@ # Validation. Do not remove r.validate_task_06_04(g) -r.save_report("_Task_06") \ No newline at end of file +r.save_report("_Task_06") From ea7541f042361f83b7d1a351e45736187db964ba Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Mon, 20 Oct 2025 20:56:05 +0200 Subject: [PATCH 04/11] Add files via upload --- Assignment4/Carlos_Cumbrado_24C055/task07.py | 159 +++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 Assignment4/Carlos_Cumbrado_24C055/task07.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task07.py b/Assignment4/Carlos_Cumbrado_24C055/task07.py new file mode 100644 index 00000000..d3b4b8a2 --- /dev/null +++ b/Assignment4/Carlos_Cumbrado_24C055/task07.py @@ -0,0 +1,159 @@ +# -*- coding: utf-8 -*- +"""Task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task07.ipynb + +**Task 07: Querying RDF(s)** +""" + +!pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +from validation import Report + +"""First let's read the RDF file""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +# Do not change the name of the variables +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +g.parse(github_storage+"/rdf/data06.ttl", format="TTL") +report = Report() + +"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** +**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** +""" + +# TO DO +result = [] + +for s, p, o in g.triples((None, RDF.type, RDFS.Class)): + superclass = None + for _, _, sup in g.triples((s, RDFS.subClassOf, None)): + superclass = sup + result.append((s, superclass)) + +# Visualize the results +result = [] +for r in result: + print(r) + +## Validation: Do not remove +report.validate_07_1a(result) + +"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" + +query = """ +SELECT ?c ?sc +WHERE { + ?c rdf:type rdfs:Class . + OPTIONAL { ?c rdfs:subClassOf ?sc . } +} +""" + +for r in g.query(query): + print(r.c, r.sc) + +## Validation: Do not remove +report.validate_07_1b(query,g) + +"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"** + +""" + +ns = Namespace("http://oeg.fi.upm.es/def/people#") + +# variable to return +individuals = [] + + +subclasses = set([ns.Person]) +for s, p, o in g.triples((None, RDFS.subClassOf, ns.Person)): + subclasses.add(s) + for sub, _, sup in g.triples((None, RDFS.subClassOf, s)): + subclasses.add(sub) + +for subclass in subclasses: + for ind, _, _ in g.triples((None, RDF.type, subclass)): + individuals.append(ind) + +# visualize results +for i in individuals: + print(i) + +# validation. Do not remove +report.validate_07_02a(individuals) + +"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**""" + +query = """ +SELECT DISTINCT ?ind +WHERE { + ?ind rdf:type ?class . + ?class rdfs:subClassOf* . +} +""" + +for r in g.query(query): + print(r.ind) +# Visualize the results + +## Validation: Do not remove +report.validate_07_02b(g, query) + +"""**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**""" + +query = """ +SELECT ?name ?type +WHERE { + ?x . + ?x ?name . + ?x rdf:type ?type . +} +""" +# Visualize the results +for r in g.query(query): + print(r.name, r.type) + +## Validation: Do not remove +report.validate_07_03(g, query) + +"""**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**""" + +query = """ +SELECT DISTINCT ?name +WHERE { + { + ?x ?y . + ?y ?dog . + ?dog rdf:type . + ?x ?name . + } + UNION + { + ?x ?y . + ?y ?z . + ?z ?dog . + ?dog rdf:type . + ?x ?name . + } +} +""" + + +for r in g.query(query): + print(r.name) + +# TO DO +# Visualize the results + +## Validation: Do not remove +report.validate_07_04(g,query) +report.save_report("_Task_07") \ No newline at end of file From b66873baa0365472cf8f336e6d3ead2fd5a3d3cd Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Mon, 20 Oct 2025 20:59:16 +0200 Subject: [PATCH 05/11] Refactor SPARQL query for colleagues with dogs --- Assignment4/Carlos_Cumbrado_24C055/task07.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Assignment4/Carlos_Cumbrado_24C055/task07.py b/Assignment4/Carlos_Cumbrado_24C055/task07.py index d3b4b8a2..ca8df0bb 100644 --- a/Assignment4/Carlos_Cumbrado_24C055/task07.py +++ b/Assignment4/Carlos_Cumbrado_24C055/task07.py @@ -32,7 +32,6 @@ """ # TO DO -result = [] for s, p, o in g.triples((None, RDF.type, RDFS.Class)): superclass = None @@ -128,22 +127,21 @@ """**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**""" query = """ +PREFIX people: + SELECT DISTINCT ?name WHERE { { - ?x ?y . - ?y ?dog . - ?dog rdf:type . - ?x ?name . + ?person people:hasColleague ?colleague . + ?colleague people:ownsPet ?dog . } UNION { - ?x ?y . - ?y ?z . - ?z ?dog . - ?dog rdf:type . - ?x ?name . + ?person people:hasColleague ?c1 . + ?c1 people:hasColleague ?c2 . + ?c2 people:ownsPet ?dog . } + ?person people:hasName ?name . } """ @@ -156,4 +154,4 @@ ## Validation: Do not remove report.validate_07_04(g,query) -report.save_report("_Task_07") \ No newline at end of file +report.save_report("_Task_07") From 7fa15238cc4fa233f6a255da7ce000ea781f500c Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Tue, 21 Oct 2025 13:05:21 +0200 Subject: [PATCH 06/11] Delete Assignment4/Carlos_Cumbrado_24C055/task06.py --- Assignment4/Carlos_Cumbrado_24C055/task06.py | 139 ------------------- 1 file changed, 139 deletions(-) delete mode 100644 Assignment4/Carlos_Cumbrado_24C055/task06.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task06.py b/Assignment4/Carlos_Cumbrado_24C055/task06.py deleted file mode 100644 index 0deeaf15..00000000 --- a/Assignment4/Carlos_Cumbrado_24C055/task06.py +++ /dev/null @@ -1,139 +0,0 @@ -# -*- coding: utf-8 -*- -"""Task06.ipynb - -Automatically generated by Colab. - -Original file is located at - https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task06.ipynb - -**Task 06: Modifying RDF(s)** -""" - -#!pip install rdflib -import urllib.request -url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' -urllib.request.urlretrieve(url, 'validation.py') -github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" - -"""Import RDFLib main methods""" - -from rdflib import Graph, Namespace, Literal, XSD -from rdflib.namespace import RDF, RDFS -from validation import Report -g = Graph() -g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) -r = Report() - -"""Create a new class named Researcher""" - -ns = Namespace("http://mydomain.org#") -g.add((ns.Researcher, RDF.type, RDFS.Class)) -for s, p, o in g: - print(s,p,o) - -"""**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.**""" - -# this task is validated in the next step -persona = Namespace("http://oeg.fi.upm.es/def/people#") -ontologia = Namespace("http://oeg.fi.upm.es/def/ontology#") - -"""**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** - -""" - -# TO DO - -g.add((person.Person, RDF.type, RDFS.Class)) -g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string))) - -g.add((person.Professor, RDF.type, RDFS.Class)) -g.add((person.Professor, RDFS.subClassOf, person.Person)) -g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) - -g.add((person.FullProfessor, RDF.type, RDFS.Class)) -g.add((person.FullProfessor, RDFS.subClassOf, person.Professor)) -g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) - -g.add((person.AssociateProfessor, RDF.type, RDFS.Class)) -g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor)) -g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) - -g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class)) -g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor)) -g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) - - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -# Validation. Do not remove -r.validate_task_06_01(g) - -"""**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)**""" - -# TO DO - -g.add((person.hasHomePage, RDF.type, RDF.Property)) -g.add((person.hasName, RDF.type, RDF.Property)) -g.add((person.hasColleague, RDF.type, RDF.Property)) - -g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) -g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) -g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) - -g.add((person.hasHomePage, RDFS.domain, person.FullProfessor)) -g.add((person.hasName, RDFS.domain, person.Person)) -g.add((person.hasColleague, RDFS.domain, person.Person)) - -g.add((person.hasHomePage, RDFS.range, RDFS.Literal)) -g.add((person.hasName, RDFS.range, RDFS.Literal)) -g.add((person.hasColleague, RDFS.range, person.Person)) - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -# Validation. Do not remove -r.validate_task_06_02(g) - -""":**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**""" - -# TO DO - -g.add((person.Oscar, RDF.type, person.FullProfessor)) -g.add((person.Asun, RDF.type, person.FullProfessor)) -g.add((person.Raul, RDF.type, person.InterimAssociateProfessor)) - -g.add((person.Oscar, person.hasColleague, person.Asun)) -g.add((person.Asun, person.hasColleague, person.Raul)) - -g.add((person.Oscar, person.hasName, Literal("Óscar Corcho García"))) -g.add((person.Asun, person.hasHomePage, Literal("http://oeg.fi.upm.es/"))) - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -r.validate_task_06_03(g) - -"""**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** - -""" - -# TO DO - -foaf = Namespace("http://xmlns.com/foaf/0.1/") -vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") - -g.add((person.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string))) -g.add((person.Oscar, vcard.Given, Literal("Óscar", datatype=XSD.string))) -g.add((person.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string))) - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -# Validation. Do not remove -r.validate_task_06_04(g) -r.save_report("_Task_06") From 459c20505cc2f408186684d2580a0ae49bcd490b Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Tue, 21 Oct 2025 13:05:31 +0200 Subject: [PATCH 07/11] Delete Assignment4/Carlos_Cumbrado_24C055/task07.py --- Assignment4/Carlos_Cumbrado_24C055/task07.py | 157 ------------------- 1 file changed, 157 deletions(-) delete mode 100644 Assignment4/Carlos_Cumbrado_24C055/task07.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task07.py b/Assignment4/Carlos_Cumbrado_24C055/task07.py deleted file mode 100644 index ca8df0bb..00000000 --- a/Assignment4/Carlos_Cumbrado_24C055/task07.py +++ /dev/null @@ -1,157 +0,0 @@ -# -*- coding: utf-8 -*- -"""Task07.ipynb - -Automatically generated by Colab. - -Original file is located at - https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task07.ipynb - -**Task 07: Querying RDF(s)** -""" - -!pip install rdflib -import urllib.request -url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' -urllib.request.urlretrieve(url, 'validation.py') -github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" - -from validation import Report - -"""First let's read the RDF file""" - -from rdflib import Graph, Namespace, Literal -from rdflib.namespace import RDF, RDFS -# Do not change the name of the variables -g = Graph() -g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) -g.parse(github_storage+"/rdf/data06.ttl", format="TTL") -report = Report() - -"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** -**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** -""" - -# TO DO - -for s, p, o in g.triples((None, RDF.type, RDFS.Class)): - superclass = None - for _, _, sup in g.triples((s, RDFS.subClassOf, None)): - superclass = sup - result.append((s, superclass)) - -# Visualize the results -result = [] -for r in result: - print(r) - -## Validation: Do not remove -report.validate_07_1a(result) - -"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" - -query = """ -SELECT ?c ?sc -WHERE { - ?c rdf:type rdfs:Class . - OPTIONAL { ?c rdfs:subClassOf ?sc . } -} -""" - -for r in g.query(query): - print(r.c, r.sc) - -## Validation: Do not remove -report.validate_07_1b(query,g) - -"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"** - -""" - -ns = Namespace("http://oeg.fi.upm.es/def/people#") - -# variable to return -individuals = [] - - -subclasses = set([ns.Person]) -for s, p, o in g.triples((None, RDFS.subClassOf, ns.Person)): - subclasses.add(s) - for sub, _, sup in g.triples((None, RDFS.subClassOf, s)): - subclasses.add(sub) - -for subclass in subclasses: - for ind, _, _ in g.triples((None, RDF.type, subclass)): - individuals.append(ind) - -# visualize results -for i in individuals: - print(i) - -# validation. Do not remove -report.validate_07_02a(individuals) - -"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**""" - -query = """ -SELECT DISTINCT ?ind -WHERE { - ?ind rdf:type ?class . - ?class rdfs:subClassOf* . -} -""" - -for r in g.query(query): - print(r.ind) -# Visualize the results - -## Validation: Do not remove -report.validate_07_02b(g, query) - -"""**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**""" - -query = """ -SELECT ?name ?type -WHERE { - ?x . - ?x ?name . - ?x rdf:type ?type . -} -""" -# Visualize the results -for r in g.query(query): - print(r.name, r.type) - -## Validation: Do not remove -report.validate_07_03(g, query) - -"""**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**""" - -query = """ -PREFIX people: - -SELECT DISTINCT ?name -WHERE { - { - ?person people:hasColleague ?colleague . - ?colleague people:ownsPet ?dog . - } - UNION - { - ?person people:hasColleague ?c1 . - ?c1 people:hasColleague ?c2 . - ?c2 people:ownsPet ?dog . - } - ?person people:hasName ?name . -} -""" - - -for r in g.query(query): - print(r.name) - -# TO DO -# Visualize the results - -## Validation: Do not remove -report.validate_07_04(g,query) -report.save_report("_Task_07") From f3156779dd82c7af8ba430a723e08b2a39225260 Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Tue, 21 Oct 2025 13:05:43 +0200 Subject: [PATCH 08/11] Add files via upload --- Assignment4/Carlos_Cumbrado_24C055/task06.py | 145 +++++++++++++++++ Assignment4/Carlos_Cumbrado_24C055/task07.py | 159 +++++++++++++++++++ 2 files changed, 304 insertions(+) create mode 100644 Assignment4/Carlos_Cumbrado_24C055/task06.py create mode 100644 Assignment4/Carlos_Cumbrado_24C055/task07.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task06.py b/Assignment4/Carlos_Cumbrado_24C055/task06.py new file mode 100644 index 00000000..5fab1c10 --- /dev/null +++ b/Assignment4/Carlos_Cumbrado_24C055/task06.py @@ -0,0 +1,145 @@ +# -*- coding: utf-8 -*- +"""Task06.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task06.ipynb + +**Task 06: Modifying RDF(s)** +""" + +#!pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +"""Import RDFLib main methods""" + +from rdflib import Graph, Namespace, Literal, XSD +from rdflib.namespace import RDF, RDFS +from validation import Report +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +r = Report() + +"""Create a new class named Researcher""" + +ns = Namespace("http://mydomain.org#") +g.add((ns.Researcher, RDF.type, RDFS.Class)) +for s, p, o in g: + print(s,p,o) + +"""**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.**""" + +# this task is validated in the next step +person = Namespace("http://oeg.fi.upm.es/def/people#") +ontologia = Namespace("http://oeg.fi.upm.es/def/ontology#") + +"""**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** + +""" + +# TO DO + +g.add((person.Person, RDF.type, RDFS.Class)) +g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string))) + +g.add((person.Professor, RDF.type, RDFS.Class)) +g.add((person.Professor, RDFS.subClassOf, person.Person)) +g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) + +g.add((person.FullProfessor, RDF.type, RDFS.Class)) +g.add((person.FullProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) + +g.add((person.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) + +g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor)) +g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) + + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_01(g) + +"""**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)**""" + +# TO DO + +g.add((person.hasHomePage, RDF.type, RDF.Property)) +g.add((person.hasName, RDF.type, RDF.Property)) +g.add((person.hasColleague, RDF.type, RDF.Property)) + +g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) +g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) +g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) + +g.add((person.hasHomePage, RDFS.domain, person.FullProfessor)) +g.add((person.hasName, RDFS.domain, person.Person)) +g.add((person.hasColleague, RDFS.domain, person.Person)) + +g.add((person.hasHomePage, RDFS.range, RDFS.Literal)) +g.add((person.hasName, RDFS.range, RDFS.Literal)) +g.add((person.hasColleague, RDFS.range, person.Person)) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_02(g) + +""":**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**""" + +# TO DO + +person_ind = Namespace("http://oeg.fi.upm.es/resource/person/") + +g.add((person_ind.Oscar, RDF.type, person.FullProfessor)) +g.add((person_ind.Asun, RDF.type, person.FullProfessor)) +g.add((person_ind.Raul, RDF.type, person.InterimAssociateProfessor)) + +g.add((person_ind.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) +g.add((person_ind.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) +g.add((person_ind.Raul, RDFS.label, Literal("Raul", datatype=XSD.string))) + +g.add((person_ind.Oscar, person.hasColleague, person_ind.Asun)) +g.add((person_ind.Asun, person.hasColleague, person_ind.Raul)) + +g.add((person_ind.Oscar, person.hasName, Literal("Óscar Corcho García", datatype=XSD.string))) +g.add((person_ind.Asun, person.hasHomePage, Literal("http://oeg.fi.upm.es/", datatype=XSD.string))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +r.validate_task_06_03(g) + +"""**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** + +""" + +# TO DO + +foaf = Namespace("http://xmlns.com/foaf/0.1/") +vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") + +g.add((person_ind.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string))) +g.add((person_ind.Oscar, vcard.Given, Literal("Óscar", datatype=XSD.string))) +g.add((person_ind.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_04(g) +r.save_report("_Task_06") \ No newline at end of file diff --git a/Assignment4/Carlos_Cumbrado_24C055/task07.py b/Assignment4/Carlos_Cumbrado_24C055/task07.py new file mode 100644 index 00000000..6e65effa --- /dev/null +++ b/Assignment4/Carlos_Cumbrado_24C055/task07.py @@ -0,0 +1,159 @@ +# -*- coding: utf-8 -*- +"""Task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task07.ipynb + +**Task 07: Querying RDF(s)** +""" + +#!pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +from validation import Report + +"""First let's read the RDF file""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +# Do not change the name of the variables +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +g.parse(github_storage+"/rdf/data06.ttl", format="TTL") +report = Report() + +"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** +**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** +""" + +# TO DO + +result = [] +for s, p, o in g.triples((None, RDF.type, RDFS.Class)): + superclass = None + for _, _, sup in g.triples((s, RDFS.subClassOf, None)): + superclass = sup + result.append((s, superclass)) + +# Visualize the results + +for r in result: + print(r) + +## Validation: Do not remove +report.validate_07_1a(result) + +"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" + +query = """ +SELECT ?c ?sc +WHERE { + ?c rdf:type rdfs:Class . + OPTIONAL { ?c rdfs:subClassOf ?sc . } +} +""" + +for r in g.query(query): + print(r.c, r.sc) + +## Validation: Do not remove +report.validate_07_1b(query,g) + +"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"** + +""" + +ns = Namespace("http://oeg.fi.upm.es/def/people#") + +# variable to return +individuals = [] + + +subclasses = set([ns.Person]) +for s, p, o in g.triples((None, RDFS.subClassOf, ns.Person)): + subclasses.add(s) + for sub, _, sup in g.triples((None, RDFS.subClassOf, s)): + subclasses.add(sub) + +for subclass in subclasses: + for ind, _, _ in g.triples((None, RDF.type, subclass)): + individuals.append(ind) + +# visualize results +for i in individuals: + print(i) + +# validation. Do not remove +report.validate_07_02a(individuals) + +"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**""" + +query = """ +SELECT DISTINCT ?ind +WHERE { + ?ind rdf:type ?class . + ?class rdfs:subClassOf* . +} +""" + +for r in g.query(query): + print(r.ind) +# Visualize the results + +## Validation: Do not remove +report.validate_07_02b(g, query) + +"""**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**""" + +query = """ +SELECT ?name ?type +WHERE { + ?x . + ?x ?name . + ?x rdf:type ?type . +} +""" +# Visualize the results +for r in g.query(query): + print(r.name, r.type) + +## Validation: Do not remove +report.validate_07_03(g, query) + +"""**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**""" + +query = """ +SELECT DISTINCT ?name +WHERE { + { + ?x ?y . + ?y ?dog . + ?dog rdf:type . + ?x ?name . + } + UNION + { + ?x ?y . + ?y ?z . + ?z ?dog . + ?dog rdf:type . + ?x ?name . + } +} +""" + + +for r in g.query(query): + print(r.name) + +# TO DO +# Visualize the results + +## Validation: Do not remove +report.validate_07_04(g,query) +report.save_report("_Task_07") \ No newline at end of file From 04299f1a5476c42be6490a58ce1a122475915a31 Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Tue, 21 Oct 2025 15:22:11 +0200 Subject: [PATCH 09/11] Delete Assignment4/Carlos_Cumbrado_24C055/task06.py --- Assignment4/Carlos_Cumbrado_24C055/task06.py | 145 ------------------- 1 file changed, 145 deletions(-) delete mode 100644 Assignment4/Carlos_Cumbrado_24C055/task06.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task06.py b/Assignment4/Carlos_Cumbrado_24C055/task06.py deleted file mode 100644 index 5fab1c10..00000000 --- a/Assignment4/Carlos_Cumbrado_24C055/task06.py +++ /dev/null @@ -1,145 +0,0 @@ -# -*- coding: utf-8 -*- -"""Task06.ipynb - -Automatically generated by Colab. - -Original file is located at - https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task06.ipynb - -**Task 06: Modifying RDF(s)** -""" - -#!pip install rdflib -import urllib.request -url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' -urllib.request.urlretrieve(url, 'validation.py') -github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" - -"""Import RDFLib main methods""" - -from rdflib import Graph, Namespace, Literal, XSD -from rdflib.namespace import RDF, RDFS -from validation import Report -g = Graph() -g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) -r = Report() - -"""Create a new class named Researcher""" - -ns = Namespace("http://mydomain.org#") -g.add((ns.Researcher, RDF.type, RDFS.Class)) -for s, p, o in g: - print(s,p,o) - -"""**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.**""" - -# this task is validated in the next step -person = Namespace("http://oeg.fi.upm.es/def/people#") -ontologia = Namespace("http://oeg.fi.upm.es/def/ontology#") - -"""**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** - -""" - -# TO DO - -g.add((person.Person, RDF.type, RDFS.Class)) -g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string))) - -g.add((person.Professor, RDF.type, RDFS.Class)) -g.add((person.Professor, RDFS.subClassOf, person.Person)) -g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) - -g.add((person.FullProfessor, RDF.type, RDFS.Class)) -g.add((person.FullProfessor, RDFS.subClassOf, person.Professor)) -g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) - -g.add((person.AssociateProfessor, RDF.type, RDFS.Class)) -g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor)) -g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) - -g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class)) -g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor)) -g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) - - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -# Validation. Do not remove -r.validate_task_06_01(g) - -"""**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)**""" - -# TO DO - -g.add((person.hasHomePage, RDF.type, RDF.Property)) -g.add((person.hasName, RDF.type, RDF.Property)) -g.add((person.hasColleague, RDF.type, RDF.Property)) - -g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) -g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) -g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) - -g.add((person.hasHomePage, RDFS.domain, person.FullProfessor)) -g.add((person.hasName, RDFS.domain, person.Person)) -g.add((person.hasColleague, RDFS.domain, person.Person)) - -g.add((person.hasHomePage, RDFS.range, RDFS.Literal)) -g.add((person.hasName, RDFS.range, RDFS.Literal)) -g.add((person.hasColleague, RDFS.range, person.Person)) - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -# Validation. Do not remove -r.validate_task_06_02(g) - -""":**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**""" - -# TO DO - -person_ind = Namespace("http://oeg.fi.upm.es/resource/person/") - -g.add((person_ind.Oscar, RDF.type, person.FullProfessor)) -g.add((person_ind.Asun, RDF.type, person.FullProfessor)) -g.add((person_ind.Raul, RDF.type, person.InterimAssociateProfessor)) - -g.add((person_ind.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) -g.add((person_ind.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) -g.add((person_ind.Raul, RDFS.label, Literal("Raul", datatype=XSD.string))) - -g.add((person_ind.Oscar, person.hasColleague, person_ind.Asun)) -g.add((person_ind.Asun, person.hasColleague, person_ind.Raul)) - -g.add((person_ind.Oscar, person.hasName, Literal("Óscar Corcho García", datatype=XSD.string))) -g.add((person_ind.Asun, person.hasHomePage, Literal("http://oeg.fi.upm.es/", datatype=XSD.string))) - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -r.validate_task_06_03(g) - -"""**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** - -""" - -# TO DO - -foaf = Namespace("http://xmlns.com/foaf/0.1/") -vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") - -g.add((person_ind.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string))) -g.add((person_ind.Oscar, vcard.Given, Literal("Óscar", datatype=XSD.string))) -g.add((person_ind.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string))) - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -# Validation. Do not remove -r.validate_task_06_04(g) -r.save_report("_Task_06") \ No newline at end of file From be5c43d3437a177041eceda9ff905436e2f91303 Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Tue, 21 Oct 2025 15:22:26 +0200 Subject: [PATCH 10/11] Delete Assignment4/Carlos_Cumbrado_24C055/task07.py --- Assignment4/Carlos_Cumbrado_24C055/task07.py | 159 ------------------- 1 file changed, 159 deletions(-) delete mode 100644 Assignment4/Carlos_Cumbrado_24C055/task07.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task07.py b/Assignment4/Carlos_Cumbrado_24C055/task07.py deleted file mode 100644 index 6e65effa..00000000 --- a/Assignment4/Carlos_Cumbrado_24C055/task07.py +++ /dev/null @@ -1,159 +0,0 @@ -# -*- coding: utf-8 -*- -"""Task07.ipynb - -Automatically generated by Colab. - -Original file is located at - https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task07.ipynb - -**Task 07: Querying RDF(s)** -""" - -#!pip install rdflib -import urllib.request -url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' -urllib.request.urlretrieve(url, 'validation.py') -github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" - -from validation import Report - -"""First let's read the RDF file""" - -from rdflib import Graph, Namespace, Literal -from rdflib.namespace import RDF, RDFS -# Do not change the name of the variables -g = Graph() -g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) -g.parse(github_storage+"/rdf/data06.ttl", format="TTL") -report = Report() - -"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** -**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** -""" - -# TO DO - -result = [] -for s, p, o in g.triples((None, RDF.type, RDFS.Class)): - superclass = None - for _, _, sup in g.triples((s, RDFS.subClassOf, None)): - superclass = sup - result.append((s, superclass)) - -# Visualize the results - -for r in result: - print(r) - -## Validation: Do not remove -report.validate_07_1a(result) - -"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" - -query = """ -SELECT ?c ?sc -WHERE { - ?c rdf:type rdfs:Class . - OPTIONAL { ?c rdfs:subClassOf ?sc . } -} -""" - -for r in g.query(query): - print(r.c, r.sc) - -## Validation: Do not remove -report.validate_07_1b(query,g) - -"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"** - -""" - -ns = Namespace("http://oeg.fi.upm.es/def/people#") - -# variable to return -individuals = [] - - -subclasses = set([ns.Person]) -for s, p, o in g.triples((None, RDFS.subClassOf, ns.Person)): - subclasses.add(s) - for sub, _, sup in g.triples((None, RDFS.subClassOf, s)): - subclasses.add(sub) - -for subclass in subclasses: - for ind, _, _ in g.triples((None, RDF.type, subclass)): - individuals.append(ind) - -# visualize results -for i in individuals: - print(i) - -# validation. Do not remove -report.validate_07_02a(individuals) - -"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**""" - -query = """ -SELECT DISTINCT ?ind -WHERE { - ?ind rdf:type ?class . - ?class rdfs:subClassOf* . -} -""" - -for r in g.query(query): - print(r.ind) -# Visualize the results - -## Validation: Do not remove -report.validate_07_02b(g, query) - -"""**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**""" - -query = """ -SELECT ?name ?type -WHERE { - ?x . - ?x ?name . - ?x rdf:type ?type . -} -""" -# Visualize the results -for r in g.query(query): - print(r.name, r.type) - -## Validation: Do not remove -report.validate_07_03(g, query) - -"""**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**""" - -query = """ -SELECT DISTINCT ?name -WHERE { - { - ?x ?y . - ?y ?dog . - ?dog rdf:type . - ?x ?name . - } - UNION - { - ?x ?y . - ?y ?z . - ?z ?dog . - ?dog rdf:type . - ?x ?name . - } -} -""" - - -for r in g.query(query): - print(r.name) - -# TO DO -# Visualize the results - -## Validation: Do not remove -report.validate_07_04(g,query) -report.save_report("_Task_07") \ No newline at end of file From a0b78f1e18045bfdd28ca976462eebf12072b799 Mon Sep 17 00:00:00 2001 From: CarlosCumbrado Date: Tue, 21 Oct 2025 15:24:13 +0200 Subject: [PATCH 11/11] Add files via upload --- Assignment4/Carlos_Cumbrado_24C055/task06.py | 145 ++++++++++++++++++ Assignment4/Carlos_Cumbrado_24C055/task07.py | 150 +++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100644 Assignment4/Carlos_Cumbrado_24C055/task06.py create mode 100644 Assignment4/Carlos_Cumbrado_24C055/task07.py diff --git a/Assignment4/Carlos_Cumbrado_24C055/task06.py b/Assignment4/Carlos_Cumbrado_24C055/task06.py new file mode 100644 index 00000000..5fab1c10 --- /dev/null +++ b/Assignment4/Carlos_Cumbrado_24C055/task06.py @@ -0,0 +1,145 @@ +# -*- coding: utf-8 -*- +"""Task06.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task06.ipynb + +**Task 06: Modifying RDF(s)** +""" + +#!pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +"""Import RDFLib main methods""" + +from rdflib import Graph, Namespace, Literal, XSD +from rdflib.namespace import RDF, RDFS +from validation import Report +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +r = Report() + +"""Create a new class named Researcher""" + +ns = Namespace("http://mydomain.org#") +g.add((ns.Researcher, RDF.type, RDFS.Class)) +for s, p, o in g: + print(s,p,o) + +"""**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.**""" + +# this task is validated in the next step +person = Namespace("http://oeg.fi.upm.es/def/people#") +ontologia = Namespace("http://oeg.fi.upm.es/def/ontology#") + +"""**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** + +""" + +# TO DO + +g.add((person.Person, RDF.type, RDFS.Class)) +g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string))) + +g.add((person.Professor, RDF.type, RDFS.Class)) +g.add((person.Professor, RDFS.subClassOf, person.Person)) +g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) + +g.add((person.FullProfessor, RDF.type, RDFS.Class)) +g.add((person.FullProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) + +g.add((person.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) + +g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor)) +g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) + + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_01(g) + +"""**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)**""" + +# TO DO + +g.add((person.hasHomePage, RDF.type, RDF.Property)) +g.add((person.hasName, RDF.type, RDF.Property)) +g.add((person.hasColleague, RDF.type, RDF.Property)) + +g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) +g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) +g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) + +g.add((person.hasHomePage, RDFS.domain, person.FullProfessor)) +g.add((person.hasName, RDFS.domain, person.Person)) +g.add((person.hasColleague, RDFS.domain, person.Person)) + +g.add((person.hasHomePage, RDFS.range, RDFS.Literal)) +g.add((person.hasName, RDFS.range, RDFS.Literal)) +g.add((person.hasColleague, RDFS.range, person.Person)) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_02(g) + +""":**TASK 6.3: Create the individuals shown in slide 36 under "Datos". Link them with the same relationships shown in the diagram."**""" + +# TO DO + +person_ind = Namespace("http://oeg.fi.upm.es/resource/person/") + +g.add((person_ind.Oscar, RDF.type, person.FullProfessor)) +g.add((person_ind.Asun, RDF.type, person.FullProfessor)) +g.add((person_ind.Raul, RDF.type, person.InterimAssociateProfessor)) + +g.add((person_ind.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) +g.add((person_ind.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) +g.add((person_ind.Raul, RDFS.label, Literal("Raul", datatype=XSD.string))) + +g.add((person_ind.Oscar, person.hasColleague, person_ind.Asun)) +g.add((person_ind.Asun, person.hasColleague, person_ind.Raul)) + +g.add((person_ind.Oscar, person.hasName, Literal("Óscar Corcho García", datatype=XSD.string))) +g.add((person_ind.Asun, person.hasHomePage, Literal("http://oeg.fi.upm.es/", datatype=XSD.string))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +r.validate_task_06_03(g) + +"""**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** + +""" + +# TO DO + +foaf = Namespace("http://xmlns.com/foaf/0.1/") +vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") + +g.add((person_ind.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", datatype=XSD.string))) +g.add((person_ind.Oscar, vcard.Given, Literal("Óscar", datatype=XSD.string))) +g.add((person_ind.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_04(g) +r.save_report("_Task_06") \ No newline at end of file diff --git a/Assignment4/Carlos_Cumbrado_24C055/task07.py b/Assignment4/Carlos_Cumbrado_24C055/task07.py new file mode 100644 index 00000000..699f9263 --- /dev/null +++ b/Assignment4/Carlos_Cumbrado_24C055/task07.py @@ -0,0 +1,150 @@ +# -*- coding: utf-8 -*- +"""Task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/github/CarlosCumbrado/Curso2025-2026-DataScience/blob/master/Assignment4/course_materials/notebooks/Task07.ipynb + +**Task 07: Querying RDF(s)** +""" + +#!pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +from validation import Report + +"""First let's read the RDF file""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +# Do not change the name of the variables +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +g.parse(github_storage+"/rdf/data06.ttl", format="TTL") +report = Report() + +"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** +**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** +""" + +# TO DO + +result = [] +for s, p, o in g.triples((None, RDF.type, RDFS.Class)): + superclass = None + for _, _, sup in g.triples((s, RDFS.subClassOf, None)): + superclass = sup + result.append((s, superclass)) + +# Visualize the results + +for r in result: + print(r) + +## Validation: Do not remove +report.validate_07_1a(result) + +"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" + +query = """ +SELECT ?c ?sc +WHERE { + ?c rdf:type rdfs:Class . + OPTIONAL { ?c rdfs:subClassOf ?sc . } +} +""" + +for r in g.query(query): + print(r.c, r.sc) + +## Validation: Do not remove +report.validate_07_1b(query,g) + +"""**TASK 7.2a: List all individuals of "Person" with RDFLib (remember the subClasses). Return the individual URIs in a list called "individuals"** + +""" + +ns = Namespace("http://oeg.fi.upm.es/def/people#") + +# variable a devolver +individuals = [] + +subclasses = set() +to_visit = [ns.Person] +while to_visit: + cls = to_visit.pop() + if cls in subclasses: + continue + subclasses.add(cls) + for sub, _, _ in g.triples((None, RDFS.subClassOf, cls)): + if sub not in subclasses: + to_visit.append(sub) + +for subclass in subclasses: + for ind, _, _ in g.triples((None, RDF.type, subclass)): + individuals.append(ind) + +# visualize results +for i in individuals: + print(i) + +# validation. Do not remove +report.validate_07_02a(individuals) + +"""**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**""" + +query = """ +SELECT DISTINCT ?ind +WHERE { + ?ind rdf:type ?class . + ?class rdfs:subClassOf* . +} +""" + +for r in g.query(query): + print(r.ind) +# Visualize the results + +## Validation: Do not remove +report.validate_07_02b(g, query) + +"""**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**""" + +query = """ +SELECT ?name ?type WHERE{ + ?name . + ?name a ?type +} +""" +# Visualize the results +for r in g.query(query): + print(r.name, r.type) + +## Validation: Do not remove +report.validate_07_03(g, query) + +"""**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**""" + +query = """ +SELECT ?name WHERE { + ?p ?m. + {?name ?p} + UNION + {?p2 ?p. ?name ?p2.} +} +""" + + +for r in g.query(query): + print(r.name) + +# TO DO +# Visualize the results + +## Validation: Do not remove +report.validate_07_04(g,query) +report.save_report("_Task_07") \ No newline at end of file