From 5da505e7c31a1b1c62fcaee0209d38de25eec8d7 Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Fri, 24 Oct 2025 15:47:24 +0200 Subject: [PATCH 1/8] Add files via upload --- .../task06.py" | 145 ++++++++++ .../task07.py" | 133 +++++++++ .../validation.py" | 258 ++++++++++++++++++ 3 files changed, 536 insertions(+) create mode 100644 "Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task06.py" create mode 100644 "Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" create mode 100644 "Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task06.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task06.py" new file mode 100644 index 00000000..6299f6f3 --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/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/drive/1BbLFeAgTPcLHiRqNBqy-DBeofpex4le5 + +**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 +ont = Namespace("http://oeg.fi.upm.es/def/ontology#") +per = Namespace("http://oeg.fi.upm.es/def/people#") + +"""**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 +#Persona +g.add((per.Person, RDF.type, RDFS.Class)) +g.add((per.Person, RDFS.label, Literal("Person", datatype=XSD.string))) +#Profesor +g.add((per.Professor, RDF.type, RDFS.Class)) +g.add((per.Professor, RDFS.subClassOf, per.Person)) +g.add((per.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) +#Professor COmpleto +g.add((per.FullProfessor, RDF.type, RDFS.Class)) +g.add((per.FullProfessor, RDFS.subClassOf, per.Professor)) +g.add((per.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) +#Profesor Asociado +g.add((per.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor)) +g.add((per.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) +#Profesor INterino +g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor)) +g.add((per.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 +#Propiedad hasName +g.add((per.hasName, RDF.type, RDF.Property)) +g.add((per.hasName, RDFS.domain, per.Person)) +g.add((per.hasName, RDFS.range, RDFS.Literal)) +g.add((per.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) +#Propiedad hasColleague +g.add((per.hasColleague, RDF.type, RDF.Property)) +g.add((per.hasColleague, RDFS.domain, per.Person)) +g.add((per.hasColleague, RDFS.range, per.Person)) +g.add((per.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) +#Propiedad hasHomePage +g.add((per.hasHomePage, RDF.type, RDF.Property)) +g.add((per.hasHomePage, RDFS.domain, per.FullProfessor)) +g.add((per.hasHomePage, RDFS.range, RDFS.Literal)) +g.add((per.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) +# 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 +#Namespace +data = Namespace("http://oeg.fi.upm.es/resource/person/") +#Oscar +g.add((data.Oscar, RDF.type, per.AssociateProfessor)) +g.add((data.Oscar, per.hasColleague, data.Asun)) +g.add((data.Oscar, per.hasName, Literal("Oscar Corcho García", datatype=XSD.string))) +g.add((data.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) +#Asun +g.add((data.Asun, RDF.type, per.FullProfessor)) +g.add((data.Asun, per.hasColleague, data.Raul)) +g.add((data.Asun, per.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string))) +g.add((data.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) +#Raul +g.add((data.Raul, RDF.type, per.InterimAssociateProfessor)) +g.add((data.Raul, RDFS.label, Literal("Raul", 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 +#Namespace +foaf = Namespace("http://xmlns.com/foaf/0.1/") +vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") +#Nuevas relaciones +g.add((vcard.Family, RDF.type, RDF.Property)) +g.add((vcard.Family, RDFS.range, XSD.string)) +g.add((vcard.Given, RDF.type, RDF.Property)) +g.add((vcard.Given, RDFS.range, XSD.string)) +g.add((foaf.email, RDF.type, RDFS.Datatype)) +g.add((foaf.eamil, RDFS.range, XSD.string)) +#Más info de Oscar +g.add((data.Oscar, vcard.Given, Literal("Oscar", datatype=XSD.string))) +g.add((data.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string))) +g.add((data.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", 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/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" new file mode 100644 index 00000000..4c05b942 --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" @@ -0,0 +1,133 @@ +# -*- coding: utf-8 -*- +"""Task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1gnhtqOSZOup-u6MULPbr1pmssLHx1CZz + +**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 +#Lista classUri +result=[] +for sb in g.subjects(RDF.type, RDFS.Class): + sc=None + for Sc in g.objects(sb,RDFS.subClassOf): + sc=Sc + result.append((sb,Sc)) +for cl, sc in result: + short_c = g.namespace_manager.normalizeUri(cl) + short_sc = g.namespace_manager.normalizeUri(sc) if sc else None +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 = [] +def subclases(cl): + sb=[] + for s,p,o in g.triples((None,RDFS.subClassOf,cl)): + sb.append(s) + sb += subclases(s) + return sb +clases= subclases(ns.Person) +clases.append(ns.Person) +for cl in clases: + for s,p,o in g.triples((None,RDF.type,cl)): + individuals.append(s) +# 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 ?ind WHERE{?c rdfs:subClassOf* . ?ind a ?c}" + +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 rdf:type ?type.}""" +# TO DO +# 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 ?name WHERE{ + { ?name people:hasColleague ?c1. + ?c1 people:ownsPet ?dog.} + UNION{ + ?name people:hasColleague ?c2. + ?c2 people:hasColleague ?c3. + ?c3 people:ownsPet ?dog.} + } +""" + +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 diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" new file mode 100644 index 00000000..09bea35e --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/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 16e13629fa77ea73ae7feb03b216ae2c244591e7 Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Sun, 26 Oct 2025 13:10:51 +0100 Subject: [PATCH 2/8] Assigment4_Gonzalo_Hernandez --- .../Task06.ipynb" | 495 +++++++++++++ .../Task07.ipynb" | 681 ++++++++++++++++++ .../report_result_Task_06.txt" | 5 + .../report_result_Task_07.txt" | 6 + .../validation.py" | 258 +++++++ 5 files changed, 1445 insertions(+) create mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" create mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" create mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" create mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" create mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" new file mode 100644 index 00000000..db35ee15 --- /dev/null +++ "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" @@ -0,0 +1,495 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "nOOPLCHF7hLB" + }, + "source": [ + "**Task 06: Modifying RDF(s)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 9809, + "status": "ok", + "timestamp": 1761321271933, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "Yl9npCt8n6m-", + "outputId": "213bca10-042e-41ca-e0b4-1f2444732fcf" + }, + "outputs": [ + { + "ename": "", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31mRunning cells with 'base (Python 3.13.5)' requires the ipykernel package.\n", + "\u001b[1;31mCreate a Python Environment with the required packages." + ] + } + ], + "source": [ + "!pip install rdflib\n", + "import urllib.request\n", + "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", + "urllib.request.urlretrieve(url, 'validation.py')\n", + "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XY7aPc86Bqoo" + }, + "source": [ + "Import RDFLib main methods" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "executionInfo": { + "elapsed": 68, + "status": "ok", + "timestamp": 1761321272006, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "9ERh415on7kF" + }, + "outputs": [], + "source": [ + "from rdflib import Graph, Namespace, Literal, XSD\n", + "from rdflib.namespace import RDF, RDFS\n", + "from validation import Report\n", + "g = Graph()\n", + "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", + "r = Report()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gM3DASkTQQ5Y" + }, + "source": [ + "Create a new class named Researcher" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 21, + "status": "ok", + "timestamp": 1761321272030, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "6vtudax8Xb7b", + "outputId": "b2f2a41a-3bdf-4361-9d1f-f2610c1f0f30" + }, + "outputs": [], + "source": [ + "ns = Namespace(\"http://mydomain.org#\")\n", + "g.add((ns.Researcher, RDF.type, RDFS.Class))\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZX8w9jnV5Xhp" + }, + "source": [ + "**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.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "executionInfo": { + "elapsed": 11, + "status": "ok", + "timestamp": 1761321272042, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "b1ZQgYgB5vi7" + }, + "outputs": [], + "source": [ + "# this task is validated in the next step\n", + "ont = Namespace(\"http://oeg.fi.upm.es/def/ontology#\")\n", + "per = Namespace(\"http://oeg.fi.upm.es/def/people#\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qp1oe2Eddsvo" + }, + "source": [ + "**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**\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 5, + "status": "ok", + "timestamp": 1761321272049, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "pnsrsgRUWF3A", + "outputId": "0a7e2c9a-c1ec-4063-f570-ed62d101511f" + }, + "outputs": [], + "source": [ + "# TO DO\n", + "#Persona\n", + "g.add((per.Person, RDF.type, RDFS.Class))\n", + "g.add((per.Person, RDFS.label, Literal(\"Person\", datatype=XSD.string)))\n", + "#Profesor\n", + "g.add((per.Professor, RDF.type, RDFS.Class))\n", + "g.add((per.Professor, RDFS.subClassOf, per.Person))\n", + "g.add((per.Professor, RDFS.label, Literal(\"Professor\", datatype=XSD.string)))\n", + "#Professor COmpleto\n", + "g.add((per.FullProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.FullProfessor, RDFS.subClassOf, per.Professor))\n", + "g.add((per.FullProfessor, RDFS.label, Literal(\"FullProfessor\", datatype=XSD.string)))\n", + "#Profesor Asociado\n", + "g.add((per.AssociateProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor))\n", + "g.add((per.AssociateProfessor, RDFS.label, Literal(\"AssociateProfessor\", datatype=XSD.string)))\n", + "#Profesor INterino\n", + "g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor))\n", + "g.add((per.InterimAssociateProfessor, RDFS.label, Literal(\"InterimAssociateProfessor\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 14, + "status": "ok", + "timestamp": 1761322852753, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "HdRPyusrjIuB", + "outputId": "9100a4cd-8ac9-4109-9f04-0e53c7fb45eb" + }, + "outputs": [], + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_01(g)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MXBqtBkJd22I" + }, + "source": [ + "**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)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 22, + "status": "ok", + "timestamp": 1761240880664, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "53hZNtXsXCNq", + "outputId": "6bca568b-c887-4b5d-bc62-0595963edaa8" + }, + "outputs": [], + "source": [ + "# TO DO\n", + "#Propiedad hasName\n", + "g.add((per.hasName, RDF.type, RDF.Property))\n", + "g.add((per.hasName, RDFS.domain, per.Person))\n", + "g.add((per.hasName, RDFS.range, RDFS.Literal))\n", + "g.add((per.hasName, RDFS.label, Literal(\"hasName\", datatype=XSD.string)))\n", + "#Propiedad hasColleague\n", + "g.add((per.hasColleague, RDF.type, RDF.Property))\n", + "g.add((per.hasColleague, RDFS.domain, per.Person))\n", + "g.add((per.hasColleague, RDFS.range, per.Person))\n", + "g.add((per.hasColleague, RDFS.label, Literal(\"hasColleague\", datatype=XSD.string)))\n", + "#Propiedad hasHomePage\n", + "g.add((per.hasHomePage, RDF.type, RDF.Property))\n", + "g.add((per.hasHomePage, RDFS.domain, per.FullProfessor))\n", + "g.add((per.hasHomePage, RDFS.range, RDFS.Literal))\n", + "g.add((per.hasHomePage, RDFS.label, Literal(\"hasHomePage\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 20, + "status": "ok", + "timestamp": 1761240882759, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "pd2mE-vGaxbu", + "outputId": "cabd7ea2-fef8-417b-dff5-94b9e23001d5" + }, + "outputs": [], + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_02(g)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OGct6k7Ld9O0" + }, + "source": [ + "**TASK 6.3: Create the individuals shown in slide 36 under \"Datos\". Link them with the same relationships shown in the diagram.\"**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 13, + "status": "ok", + "timestamp": 1761240965479, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "jbMMSHSZcFcf", + "outputId": "f6a0e42c-25a3-4a80-83b7-3b21e39b243a" + }, + "outputs": [], + "source": [ + "# TO DO\n", + "#Namespace\n", + "data = Namespace(\"http://oeg.fi.upm.es/resource/person/\")\n", + "#Oscar\n", + "g.add((data.Oscar, RDF.type, per.AssociateProfessor))\n", + "g.add((data.Oscar, per.hasColleague, data.Asun))\n", + "g.add((data.Oscar, per.hasName, Literal(\"Oscar Corcho García\", datatype=XSD.string)))\n", + "g.add((data.Oscar, RDFS.label, Literal(\"Oscar\", datatype=XSD.string)))\n", + "#Asun\n", + "g.add((data.Asun, RDF.type, per.FullProfessor))\n", + "g.add((data.Asun, per.hasColleague, data.Raul))\n", + "g.add((data.Asun, per.hasHomePage, Literal(\"http://www.oeg-upm.net/\", datatype=XSD.string)))\n", + "g.add((data.Asun, RDFS.label, Literal(\"Asun\", datatype=XSD.string)))\n", + "#Raul\n", + "g.add((data.Raul, RDF.type, per.InterimAssociateProfessor))\n", + "g.add((data.Raul, RDFS.label, Literal(\"Raul\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 16, + "status": "ok", + "timestamp": 1761240968505, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "-3q4Wv2EfYew", + "outputId": "d8b7531a-b028-49df-d303-7e8d47f6903e" + }, + "outputs": [], + "source": [ + "r.validate_task_06_03(g)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tD383J__eHfV" + }, + "source": [ + "**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**\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 14, + "status": "ok", + "timestamp": 1761241072430, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "hWmwlAfBcgN-", + "outputId": "64a0d523-5957-4034-ef00-e0f09a0c7628" + }, + "outputs": [], + "source": [ + "# TO DO\n", + "#Namespace\n", + "foaf = Namespace(\"http://xmlns.com/foaf/0.1/\")\n", + "vcard = Namespace(\"http://www.w3.org/2001/vcard-rdf/3.0/\")\n", + "#Nuevas relaciones\n", + "g.add((vcard.Family, RDF.type, RDF.Property))\n", + "g.add((vcard.Family, RDFS.range, XSD.string))\n", + "g.add((vcard.Given, RDF.type, RDF.Property))\n", + "g.add((vcard.Given, RDFS.range, XSD.string))\n", + "g.add((foaf.email, RDF.type, RDFS.Datatype))\n", + "g.add((foaf.eamil, RDFS.range, XSD.string))\n", + "#Más info de Oscar\n", + "g.add((data.Oscar, vcard.Given, Literal(\"Oscar\", datatype=XSD.string)))\n", + "g.add((data.Oscar, vcard.Family, Literal(\"Corcho García\", datatype=XSD.string)))\n", + "g.add((data.Oscar, foaf.email, Literal(\"ocorcho@fi.upm.es\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 17, + "status": "ok", + "timestamp": 1761241075136, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "Y1NiIQMyfgyF", + "outputId": "514cc283-789c-4ab0-9ab8-049307aa21d1" + }, + "outputs": [], + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_04(g)\n", + "r.save_report(\"_Task_06\")" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" new file mode 100644 index 00000000..45372d0d --- /dev/null +++ "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" @@ -0,0 +1,681 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "nOOPLCHF7hLB" + }, + "source": [ + "**Task 07: Querying RDF(s)**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 5297, + "status": "ok", + "timestamp": 1761241613452, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "Yl9npCt8n6m-", + "outputId": "0a1b0036-4921-4cfd-b5fa-2fe1cadc5133" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\"pip\" no se reconoce como un comando interno o externo,\n", + "programa o archivo por lotes ejecutable.\n" + ] + } + ], + "source": [ + "!pip install rdflib\n", + "import urllib.request\n", + "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", + "urllib.request.urlretrieve(url, 'validation.py')\n", + "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "executionInfo": { + "elapsed": 23, + "status": "ok", + "timestamp": 1761241615539, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "FmnGjffDT92V" + }, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'rdflib'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mvalidation\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Report\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Clase\\KG\\GitHub\\Assigment4_Gonzalo_Hernández_24C060\\validation.py:1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mrdflib\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Graph, Namespace, Literal, XSD\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mrdflib\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mnamespace\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m RDF, RDFS\n\u001b[32m 4\u001b[39m VCARD = Namespace(\u001b[33m\"\u001b[39m\u001b[33mhttp://www.w3.org/2001/vcard-rdf/3.0/\u001b[39m\u001b[33m\"\u001b[39m)\n", + "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'rdflib'" + ] + } + ], + "source": [ + "from validation import Report" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XY7aPc86Bqoo" + }, + "source": [ + "First let's read the RDF file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "executionInfo": { + "elapsed": 91, + "status": "ok", + "timestamp": 1761241616525, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "9ERh415on7kF" + }, + "outputs": [], + "source": [ + "from rdflib import Graph, Namespace, Literal\n", + "from rdflib.namespace import RDF, RDFS\n", + "# Do not change the name of the variables\n", + "g = Graph()\n", + "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", + "g.parse(github_storage+\"/rdf/data06.ttl\", format=\"TTL\")\n", + "report = Report()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qp1oe2Eddsvo" + }, + "source": [ + "**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**\n", + "**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**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 9, + "status": "ok", + "timestamp": 1761242287620, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "tRcSWuMHOXBl", + "outputId": "167acaf6-2b9f-40c0-84ec-8c4050b976b4" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Animal'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Student'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#FullProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#InterimAssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Lista classUri\n", + "result=[]\n", + "for sb in g.subjects(RDF.type, RDFS.Class):\n", + " sc=None\n", + " for Sc in g.objects(sb,RDFS.subClassOf):\n", + " sc=Sc\n", + " result.append((sb,Sc))\n", + "for cl, sc in result:\n", + " short_c = g.namespace_manager.normalizeUri(cl)\n", + " short_sc = g.namespace_manager.normalizeUri(sc) if sc else None\n", + "for r in result:\n", + " print(r)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 5, + "status": "ok", + "timestamp": 1761242289556, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "uvEpQQrTlMPH", + "outputId": "8c9bd1d8-6428-47fb-b06f-c6d4874aba22" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TASK 7.1a OK\n" + ] + } + ], + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_1a(result)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kbY-jqw6klr9" + }, + "source": [ + "**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 45, + "status": "ok", + "timestamp": 1761242480558, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "NGAG7l9UklMC", + "outputId": "aec82c1a-1643-49a0-bf08-483ff77483b9" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "http://oeg.fi.upm.es/def/people#Person None\n", + "http://oeg.fi.upm.es/def/people#Animal None\n", + "http://oeg.fi.upm.es/def/people#Professor http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#Student http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://oeg.fi.upm.es/def/people#AssociateProfessor\n" + ] + } + ], + "source": [ + "query = \"Select ?c ?sc WHERE {?c rdf:type rdfs:Class. OPTIONAL {?c rdfs:subClassOf ?sc.}}\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.c, r.sc)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 17, + "status": "ok", + "timestamp": 1761242482582, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "9zf4vgVHlKR3", + "outputId": "4294c94d-0b20-4352-c483-3b83ba9fc2fb" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TASK 7.1b OK\n" + ] + } + ], + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_1b(query,g)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gM3DASkTQQ5Y" + }, + "source": [ + "**TASK 7.2a: List all individuals of \"Person\" with RDFLib (remember the subClasses). Return the individual URIs in a list called \"individuals\"**\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 49, + "status": "ok", + "timestamp": 1761243807271, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "LiKSPHRzS-XJ", + "outputId": "b52dfb9d-10ff-40b9-c844-62190a16a65d" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ], + "source": [ + "ns = Namespace(\"http://oeg.fi.upm.es/def/people#\")\n", + "\n", + "# variable to return\n", + "individuals = []\n", + "def subclases(cl):\n", + " sb=[]\n", + " for s,p,o in g.triples((None,RDFS.subClassOf,cl)):\n", + " sb.append(s)\n", + " sb += subclases(s)\n", + " return sb\n", + "clases= subclases(ns.Person)\n", + "clases.append(ns.Person)\n", + "for cl in clases:\n", + " for s,p,o in g.triples((None,RDF.type,cl)):\n", + " individuals.append(s)\n", + "# visualize results\n", + "for i in individuals:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 18, + "status": "ok", + "timestamp": 1761243810074, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "ONrAls5uiX1G", + "outputId": "b7e389d9-4f71-425f-df0e-d4e1bd308907" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TASK 7.2a OK\n" + ] + } + ], + "source": [ + "# validation. Do not remove\n", + "report.validate_07_02a(individuals)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "up-952A-za7A" + }, + "source": [ + "**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 23, + "status": "ok", + "timestamp": 1761243995727, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "ipYiEVbTzbR0", + "outputId": "1494aa3a-ef9d-4185-b412-e6dd66e07fae" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ], + "source": [ + "query = \"SELECT ?ind WHERE{?c rdfs:subClassOf* . ?ind a ?c}\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.ind)\n", + "# Visualize the results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 17, + "status": "ok", + "timestamp": 1761244027657, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "s-Hu2LxRjUQt", + "outputId": "a7b70ca9-558d-483b-93f5-b86201904056" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TASK 7.2b OK\n" + ] + } + ], + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_02b(g, query)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3NyI7M2VNr9R" + }, + "source": [ + "**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**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 40, + "status": "ok", + "timestamp": 1761244226601, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "I_CNoIKdNpbx", + "outputId": "0b78e861-9e2c-414a-f12d-a2d8126cbed9" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#Raul http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Fantasma http://oeg.fi.upm.es/def/people#Animal\n" + ] + } + ], + "source": [ + "query = \"\"\"SELECT ?name ?type WHERE{\n", + " ?name .\n", + " ?name rdf:type ?type.}\"\"\"\n", + "# TO DO\n", + "# Visualize the results\n", + "for r in g.query(query):\n", + " print(r.name, r.type)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 9, + "status": "ok", + "timestamp": 1761244228693, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "Zf3JS7tEhS2t", + "outputId": "92b68ec3-6c93-4805-ebb2-bd6b851628ca" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TASK 7.3 OK\n" + ] + } + ], + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_03(g, query)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kyjGsyxDPa2C" + }, + "source": [ + "**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**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 20, + "status": "ok", + "timestamp": 1761244939502, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "yoVwVZUAPaLm", + "outputId": "69bb43dd-b136-4afc-9a37-c888fe532ee2" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ], + "source": [ + "query = \"\"\"\n", + "PREFIX people: \n", + "Select ?name WHERE{\n", + " { ?name people:hasColleague ?c1.\n", + " ?c1 people:ownsPet ?dog.}\n", + " UNION{\n", + " ?name people:hasColleague ?c2.\n", + " ?c2 people:hasColleague ?c3.\n", + " ?c3 people:ownsPet ?dog.}\n", + " }\n", + "\"\"\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.name)\n", + "\n", + "# TO DO\n", + "# Visualize the results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "executionInfo": { + "elapsed": 27, + "status": "ok", + "timestamp": 1761244942138, + "user": { + "displayName": "Gonzalo Hernandez Vaillo", + "userId": "13071205746996859788" + }, + "user_tz": -120 + }, + "id": "zcTZE7ngj2fc", + "outputId": "858ccad3-d30c-4dd4-ce5a-5e427c445495" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TASK 7.4 OK\n" + ] + } + ], + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_04(g,query)\n", + "report.save_report(\"_Task_07\")" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" new file mode 100644 index 00000000..48d5fd16 --- /dev/null +++ "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" @@ -0,0 +1,5 @@ +Hierarchy OK +TASK 6.1 OK +TASK 6.2 OK +TASK 6.3 OK +TASK 6.4 OK diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" new file mode 100644 index 00000000..4d2ac917 --- /dev/null +++ "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" @@ -0,0 +1,6 @@ +TASK 7.1a OK +TASK 7.1b OK +TASK 7.2a OK +TASK 7.2b OK +TASK 7.3 OK +TASK 7.4 OK diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" new file mode 100644 index 00000000..6024e0b5 --- /dev/null +++ "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/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 88ff63e553dc212f299162eb6ca3912d1a9dd1a6 Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Sun, 26 Oct 2025 13:14:01 +0100 Subject: [PATCH 3/8] =?UTF-8?q?Delete=20Assigment4=5FGonzalo=5FHern=C3=A1n?= =?UTF-8?q?dez=5F24C060=20directory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Task06.ipynb" | 495 ------------- .../Task07.ipynb" | 681 ------------------ .../report_result_Task_06.txt" | 5 - .../report_result_Task_07.txt" | 6 - .../validation.py" | 258 ------- 5 files changed, 1445 deletions(-) delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" deleted file mode 100644 index db35ee15..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" +++ /dev/null @@ -1,495 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "nOOPLCHF7hLB" - }, - "source": [ - "**Task 06: Modifying RDF(s)**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 9809, - "status": "ok", - "timestamp": 1761321271933, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Yl9npCt8n6m-", - "outputId": "213bca10-042e-41ca-e0b4-1f2444732fcf" - }, - "outputs": [ - { - "ename": "", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31mRunning cells with 'base (Python 3.13.5)' requires the ipykernel package.\n", - "\u001b[1;31mCreate a Python Environment with the required packages." - ] - } - ], - "source": [ - "!pip install rdflib\n", - "import urllib.request\n", - "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", - "urllib.request.urlretrieve(url, 'validation.py')\n", - "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XY7aPc86Bqoo" - }, - "source": [ - "Import RDFLib main methods" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "executionInfo": { - "elapsed": 68, - "status": "ok", - "timestamp": 1761321272006, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "9ERh415on7kF" - }, - "outputs": [], - "source": [ - "from rdflib import Graph, Namespace, Literal, XSD\n", - "from rdflib.namespace import RDF, RDFS\n", - "from validation import Report\n", - "g = Graph()\n", - "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", - "r = Report()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gM3DASkTQQ5Y" - }, - "source": [ - "Create a new class named Researcher" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 21, - "status": "ok", - "timestamp": 1761321272030, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "6vtudax8Xb7b", - "outputId": "b2f2a41a-3bdf-4361-9d1f-f2610c1f0f30" - }, - "outputs": [], - "source": [ - "ns = Namespace(\"http://mydomain.org#\")\n", - "g.add((ns.Researcher, RDF.type, RDFS.Class))\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ZX8w9jnV5Xhp" - }, - "source": [ - "**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.**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "executionInfo": { - "elapsed": 11, - "status": "ok", - "timestamp": 1761321272042, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "b1ZQgYgB5vi7" - }, - "outputs": [], - "source": [ - "# this task is validated in the next step\n", - "ont = Namespace(\"http://oeg.fi.upm.es/def/ontology#\")\n", - "per = Namespace(\"http://oeg.fi.upm.es/def/people#\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "qp1oe2Eddsvo" - }, - "source": [ - "**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**\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 5, - "status": "ok", - "timestamp": 1761321272049, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "pnsrsgRUWF3A", - "outputId": "0a7e2c9a-c1ec-4063-f570-ed62d101511f" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Persona\n", - "g.add((per.Person, RDF.type, RDFS.Class))\n", - "g.add((per.Person, RDFS.label, Literal(\"Person\", datatype=XSD.string)))\n", - "#Profesor\n", - "g.add((per.Professor, RDF.type, RDFS.Class))\n", - "g.add((per.Professor, RDFS.subClassOf, per.Person))\n", - "g.add((per.Professor, RDFS.label, Literal(\"Professor\", datatype=XSD.string)))\n", - "#Professor COmpleto\n", - "g.add((per.FullProfessor, RDF.type, RDFS.Class))\n", - "g.add((per.FullProfessor, RDFS.subClassOf, per.Professor))\n", - "g.add((per.FullProfessor, RDFS.label, Literal(\"FullProfessor\", datatype=XSD.string)))\n", - "#Profesor Asociado\n", - "g.add((per.AssociateProfessor, RDF.type, RDFS.Class))\n", - "g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor))\n", - "g.add((per.AssociateProfessor, RDFS.label, Literal(\"AssociateProfessor\", datatype=XSD.string)))\n", - "#Profesor INterino\n", - "g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class))\n", - "g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor))\n", - "g.add((per.InterimAssociateProfessor, RDFS.label, Literal(\"InterimAssociateProfessor\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 14, - "status": "ok", - "timestamp": 1761322852753, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "HdRPyusrjIuB", - "outputId": "9100a4cd-8ac9-4109-9f04-0e53c7fb45eb" - }, - "outputs": [], - "source": [ - "# Validation. Do not remove\n", - "r.validate_task_06_01(g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "MXBqtBkJd22I" - }, - "source": [ - "**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)**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 22, - "status": "ok", - "timestamp": 1761240880664, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "53hZNtXsXCNq", - "outputId": "6bca568b-c887-4b5d-bc62-0595963edaa8" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Propiedad hasName\n", - "g.add((per.hasName, RDF.type, RDF.Property))\n", - "g.add((per.hasName, RDFS.domain, per.Person))\n", - "g.add((per.hasName, RDFS.range, RDFS.Literal))\n", - "g.add((per.hasName, RDFS.label, Literal(\"hasName\", datatype=XSD.string)))\n", - "#Propiedad hasColleague\n", - "g.add((per.hasColleague, RDF.type, RDF.Property))\n", - "g.add((per.hasColleague, RDFS.domain, per.Person))\n", - "g.add((per.hasColleague, RDFS.range, per.Person))\n", - "g.add((per.hasColleague, RDFS.label, Literal(\"hasColleague\", datatype=XSD.string)))\n", - "#Propiedad hasHomePage\n", - "g.add((per.hasHomePage, RDF.type, RDF.Property))\n", - "g.add((per.hasHomePage, RDFS.domain, per.FullProfessor))\n", - "g.add((per.hasHomePage, RDFS.range, RDFS.Literal))\n", - "g.add((per.hasHomePage, RDFS.label, Literal(\"hasHomePage\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 20, - "status": "ok", - "timestamp": 1761240882759, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "pd2mE-vGaxbu", - "outputId": "cabd7ea2-fef8-417b-dff5-94b9e23001d5" - }, - "outputs": [], - "source": [ - "# Validation. Do not remove\n", - "r.validate_task_06_02(g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OGct6k7Ld9O0" - }, - "source": [ - "**TASK 6.3: Create the individuals shown in slide 36 under \"Datos\". Link them with the same relationships shown in the diagram.\"**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 13, - "status": "ok", - "timestamp": 1761240965479, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "jbMMSHSZcFcf", - "outputId": "f6a0e42c-25a3-4a80-83b7-3b21e39b243a" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Namespace\n", - "data = Namespace(\"http://oeg.fi.upm.es/resource/person/\")\n", - "#Oscar\n", - "g.add((data.Oscar, RDF.type, per.AssociateProfessor))\n", - "g.add((data.Oscar, per.hasColleague, data.Asun))\n", - "g.add((data.Oscar, per.hasName, Literal(\"Oscar Corcho García\", datatype=XSD.string)))\n", - "g.add((data.Oscar, RDFS.label, Literal(\"Oscar\", datatype=XSD.string)))\n", - "#Asun\n", - "g.add((data.Asun, RDF.type, per.FullProfessor))\n", - "g.add((data.Asun, per.hasColleague, data.Raul))\n", - "g.add((data.Asun, per.hasHomePage, Literal(\"http://www.oeg-upm.net/\", datatype=XSD.string)))\n", - "g.add((data.Asun, RDFS.label, Literal(\"Asun\", datatype=XSD.string)))\n", - "#Raul\n", - "g.add((data.Raul, RDF.type, per.InterimAssociateProfessor))\n", - "g.add((data.Raul, RDFS.label, Literal(\"Raul\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 16, - "status": "ok", - "timestamp": 1761240968505, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "-3q4Wv2EfYew", - "outputId": "d8b7531a-b028-49df-d303-7e8d47f6903e" - }, - "outputs": [], - "source": [ - "r.validate_task_06_03(g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tD383J__eHfV" - }, - "source": [ - "**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**\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 14, - "status": "ok", - "timestamp": 1761241072430, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "hWmwlAfBcgN-", - "outputId": "64a0d523-5957-4034-ef00-e0f09a0c7628" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Namespace\n", - "foaf = Namespace(\"http://xmlns.com/foaf/0.1/\")\n", - "vcard = Namespace(\"http://www.w3.org/2001/vcard-rdf/3.0/\")\n", - "#Nuevas relaciones\n", - "g.add((vcard.Family, RDF.type, RDF.Property))\n", - "g.add((vcard.Family, RDFS.range, XSD.string))\n", - "g.add((vcard.Given, RDF.type, RDF.Property))\n", - "g.add((vcard.Given, RDFS.range, XSD.string))\n", - "g.add((foaf.email, RDF.type, RDFS.Datatype))\n", - "g.add((foaf.eamil, RDFS.range, XSD.string))\n", - "#Más info de Oscar\n", - "g.add((data.Oscar, vcard.Given, Literal(\"Oscar\", datatype=XSD.string)))\n", - "g.add((data.Oscar, vcard.Family, Literal(\"Corcho García\", datatype=XSD.string)))\n", - "g.add((data.Oscar, foaf.email, Literal(\"ocorcho@fi.upm.es\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 17, - "status": "ok", - "timestamp": 1761241075136, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Y1NiIQMyfgyF", - "outputId": "514cc283-789c-4ab0-9ab8-049307aa21d1" - }, - "outputs": [], - "source": [ - "# Validation. Do not remove\n", - "r.validate_task_06_04(g)\n", - "r.save_report(\"_Task_06\")" - ] - } - ], - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "display_name": "base", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.13.5" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" deleted file mode 100644 index 45372d0d..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" +++ /dev/null @@ -1,681 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "nOOPLCHF7hLB" - }, - "source": [ - "**Task 07: Querying RDF(s)**" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 5297, - "status": "ok", - "timestamp": 1761241613452, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Yl9npCt8n6m-", - "outputId": "0a1b0036-4921-4cfd-b5fa-2fe1cadc5133" - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\"pip\" no se reconoce como un comando interno o externo,\n", - "programa o archivo por lotes ejecutable.\n" - ] - } - ], - "source": [ - "!pip install rdflib\n", - "import urllib.request\n", - "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", - "urllib.request.urlretrieve(url, 'validation.py')\n", - "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "executionInfo": { - "elapsed": 23, - "status": "ok", - "timestamp": 1761241615539, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "FmnGjffDT92V" - }, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'rdflib'", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mvalidation\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Report\n", - "\u001b[36mFile \u001b[39m\u001b[32mc:\\Clase\\KG\\GitHub\\Assigment4_Gonzalo_Hernández_24C060\\validation.py:1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mrdflib\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Graph, Namespace, Literal, XSD\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mrdflib\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mnamespace\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m RDF, RDFS\n\u001b[32m 4\u001b[39m VCARD = Namespace(\u001b[33m\"\u001b[39m\u001b[33mhttp://www.w3.org/2001/vcard-rdf/3.0/\u001b[39m\u001b[33m\"\u001b[39m)\n", - "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'rdflib'" - ] - } - ], - "source": [ - "from validation import Report" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XY7aPc86Bqoo" - }, - "source": [ - "First let's read the RDF file" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "executionInfo": { - "elapsed": 91, - "status": "ok", - "timestamp": 1761241616525, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "9ERh415on7kF" - }, - "outputs": [], - "source": [ - "from rdflib import Graph, Namespace, Literal\n", - "from rdflib.namespace import RDF, RDFS\n", - "# Do not change the name of the variables\n", - "g = Graph()\n", - "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", - "g.parse(github_storage+\"/rdf/data06.ttl\", format=\"TTL\")\n", - "report = Report()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "qp1oe2Eddsvo" - }, - "source": [ - "**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**\n", - "**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**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 9, - "status": "ok", - "timestamp": 1761242287620, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "tRcSWuMHOXBl", - "outputId": "167acaf6-2b9f-40c0-84ec-8c4050b976b4" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Animal'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Student'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#FullProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#InterimAssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n" - ] - } - ], - "source": [ - "# TO DO\n", - "#Lista classUri\n", - "result=[]\n", - "for sb in g.subjects(RDF.type, RDFS.Class):\n", - " sc=None\n", - " for Sc in g.objects(sb,RDFS.subClassOf):\n", - " sc=Sc\n", - " result.append((sb,Sc))\n", - "for cl, sc in result:\n", - " short_c = g.namespace_manager.normalizeUri(cl)\n", - " short_sc = g.namespace_manager.normalizeUri(sc) if sc else None\n", - "for r in result:\n", - " print(r)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 5, - "status": "ok", - "timestamp": 1761242289556, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "uvEpQQrTlMPH", - "outputId": "8c9bd1d8-6428-47fb-b06f-c6d4874aba22" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.1a OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_1a(result)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kbY-jqw6klr9" - }, - "source": [ - "**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 45, - "status": "ok", - "timestamp": 1761242480558, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "NGAG7l9UklMC", - "outputId": "aec82c1a-1643-49a0-bf08-483ff77483b9" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Person None\n", - "http://oeg.fi.upm.es/def/people#Animal None\n", - "http://oeg.fi.upm.es/def/people#Professor http://oeg.fi.upm.es/def/people#Person\n", - "http://oeg.fi.upm.es/def/people#Student http://oeg.fi.upm.es/def/people#Person\n", - "http://oeg.fi.upm.es/def/people#FullProfessor http://oeg.fi.upm.es/def/people#Professor\n", - "http://oeg.fi.upm.es/def/people#AssociateProfessor http://oeg.fi.upm.es/def/people#Professor\n", - "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://oeg.fi.upm.es/def/people#AssociateProfessor\n" - ] - } - ], - "source": [ - "query = \"Select ?c ?sc WHERE {?c rdf:type rdfs:Class. OPTIONAL {?c rdfs:subClassOf ?sc.}}\"\n", - "\n", - "for r in g.query(query):\n", - " print(r.c, r.sc)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 17, - "status": "ok", - "timestamp": 1761242482582, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "9zf4vgVHlKR3", - "outputId": "4294c94d-0b20-4352-c483-3b83ba9fc2fb" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.1b OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_1b(query,g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gM3DASkTQQ5Y" - }, - "source": [ - "**TASK 7.2a: List all individuals of \"Person\" with RDFLib (remember the subClasses). Return the individual URIs in a list called \"individuals\"**\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 49, - "status": "ok", - "timestamp": 1761243807271, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "LiKSPHRzS-XJ", - "outputId": "b52dfb9d-10ff-40b9-c844-62190a16a65d" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun\n", - "http://oeg.fi.upm.es/def/people#Oscar\n", - "http://oeg.fi.upm.es/def/people#Raul\n" - ] - } - ], - "source": [ - "ns = Namespace(\"http://oeg.fi.upm.es/def/people#\")\n", - "\n", - "# variable to return\n", - "individuals = []\n", - "def subclases(cl):\n", - " sb=[]\n", - " for s,p,o in g.triples((None,RDFS.subClassOf,cl)):\n", - " sb.append(s)\n", - " sb += subclases(s)\n", - " return sb\n", - "clases= subclases(ns.Person)\n", - "clases.append(ns.Person)\n", - "for cl in clases:\n", - " for s,p,o in g.triples((None,RDF.type,cl)):\n", - " individuals.append(s)\n", - "# visualize results\n", - "for i in individuals:\n", - " print(i)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 18, - "status": "ok", - "timestamp": 1761243810074, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "ONrAls5uiX1G", - "outputId": "b7e389d9-4f71-425f-df0e-d4e1bd308907" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.2a OK\n" - ] - } - ], - "source": [ - "# validation. Do not remove\n", - "report.validate_07_02a(individuals)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "up-952A-za7A" - }, - "source": [ - "**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 23, - "status": "ok", - "timestamp": 1761243995727, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "ipYiEVbTzbR0", - "outputId": "1494aa3a-ef9d-4185-b412-e6dd66e07fae" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun\n", - "http://oeg.fi.upm.es/def/people#Oscar\n", - "http://oeg.fi.upm.es/def/people#Raul\n" - ] - } - ], - "source": [ - "query = \"SELECT ?ind WHERE{?c rdfs:subClassOf* . ?ind a ?c}\"\n", - "\n", - "for r in g.query(query):\n", - " print(r.ind)\n", - "# Visualize the results" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 17, - "status": "ok", - "timestamp": 1761244027657, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "s-Hu2LxRjUQt", - "outputId": "a7b70ca9-558d-483b-93f5-b86201904056" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.2b OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_02b(g, query)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3NyI7M2VNr9R" - }, - "source": [ - "**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**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 40, - "status": "ok", - "timestamp": 1761244226601, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "I_CNoIKdNpbx", - "outputId": "0b78e861-9e2c-414a-f12d-a2d8126cbed9" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun http://oeg.fi.upm.es/def/people#FullProfessor\n", - "http://oeg.fi.upm.es/def/people#Raul http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", - "http://oeg.fi.upm.es/def/people#Fantasma http://oeg.fi.upm.es/def/people#Animal\n" - ] - } - ], - "source": [ - "query = \"\"\"SELECT ?name ?type WHERE{\n", - " ?name .\n", - " ?name rdf:type ?type.}\"\"\"\n", - "# TO DO\n", - "# Visualize the results\n", - "for r in g.query(query):\n", - " print(r.name, r.type)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 9, - "status": "ok", - "timestamp": 1761244228693, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Zf3JS7tEhS2t", - "outputId": "92b68ec3-6c93-4805-ebb2-bd6b851628ca" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.3 OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_03(g, query)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kyjGsyxDPa2C" - }, - "source": [ - "**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**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 20, - "status": "ok", - "timestamp": 1761244939502, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "yoVwVZUAPaLm", - "outputId": "69bb43dd-b136-4afc-9a37-c888fe532ee2" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun\n", - "http://oeg.fi.upm.es/def/people#Oscar\n", - "http://oeg.fi.upm.es/def/people#Raul\n" - ] - } - ], - "source": [ - "query = \"\"\"\n", - "PREFIX people: \n", - "Select ?name WHERE{\n", - " { ?name people:hasColleague ?c1.\n", - " ?c1 people:ownsPet ?dog.}\n", - " UNION{\n", - " ?name people:hasColleague ?c2.\n", - " ?c2 people:hasColleague ?c3.\n", - " ?c3 people:ownsPet ?dog.}\n", - " }\n", - "\"\"\"\n", - "\n", - "for r in g.query(query):\n", - " print(r.name)\n", - "\n", - "# TO DO\n", - "# Visualize the results" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 27, - "status": "ok", - "timestamp": 1761244942138, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "zcTZE7ngj2fc", - "outputId": "858ccad3-d30c-4dd4-ce5a-5e427c445495" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.4 OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_04(g,query)\n", - "report.save_report(\"_Task_07\")" - ] - } - ], - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.6" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" deleted file mode 100644 index 48d5fd16..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" +++ /dev/null @@ -1,5 +0,0 @@ -Hierarchy OK -TASK 6.1 OK -TASK 6.2 OK -TASK 6.3 OK -TASK 6.4 OK diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" deleted file mode 100644 index 4d2ac917..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" +++ /dev/null @@ -1,6 +0,0 @@ -TASK 7.1a OK -TASK 7.1b OK -TASK 7.2a OK -TASK 7.2b OK -TASK 7.3 OK -TASK 7.4 OK diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" deleted file mode 100644 index 6024e0b5..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" +++ /dev/null @@ -1,258 +0,0 @@ -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 4e32693b26c508c9a4ffc4c08caaac4555e3aa54 Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Sun, 26 Oct 2025 13:19:30 +0100 Subject: [PATCH 4/8] act --- .../Task06.ipynb" | 495 ------------- .../Task07.ipynb" | 681 ------------------ .../report_result_Task_06.txt" | 5 - .../report_result_Task_07.txt" | 6 - .../validation.py" | 258 ------- .../task07.py" | 2 +- .../validation.py" | 516 ++++++------- 7 files changed, 259 insertions(+), 1704 deletions(-) delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" delete mode 100644 "Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" deleted file mode 100644 index db35ee15..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.ipynb" +++ /dev/null @@ -1,495 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "nOOPLCHF7hLB" - }, - "source": [ - "**Task 06: Modifying RDF(s)**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 9809, - "status": "ok", - "timestamp": 1761321271933, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Yl9npCt8n6m-", - "outputId": "213bca10-042e-41ca-e0b4-1f2444732fcf" - }, - "outputs": [ - { - "ename": "", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31mRunning cells with 'base (Python 3.13.5)' requires the ipykernel package.\n", - "\u001b[1;31mCreate a Python Environment with the required packages." - ] - } - ], - "source": [ - "!pip install rdflib\n", - "import urllib.request\n", - "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", - "urllib.request.urlretrieve(url, 'validation.py')\n", - "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XY7aPc86Bqoo" - }, - "source": [ - "Import RDFLib main methods" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "executionInfo": { - "elapsed": 68, - "status": "ok", - "timestamp": 1761321272006, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "9ERh415on7kF" - }, - "outputs": [], - "source": [ - "from rdflib import Graph, Namespace, Literal, XSD\n", - "from rdflib.namespace import RDF, RDFS\n", - "from validation import Report\n", - "g = Graph()\n", - "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", - "r = Report()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gM3DASkTQQ5Y" - }, - "source": [ - "Create a new class named Researcher" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 21, - "status": "ok", - "timestamp": 1761321272030, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "6vtudax8Xb7b", - "outputId": "b2f2a41a-3bdf-4361-9d1f-f2610c1f0f30" - }, - "outputs": [], - "source": [ - "ns = Namespace(\"http://mydomain.org#\")\n", - "g.add((ns.Researcher, RDF.type, RDFS.Class))\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ZX8w9jnV5Xhp" - }, - "source": [ - "**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.**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "executionInfo": { - "elapsed": 11, - "status": "ok", - "timestamp": 1761321272042, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "b1ZQgYgB5vi7" - }, - "outputs": [], - "source": [ - "# this task is validated in the next step\n", - "ont = Namespace(\"http://oeg.fi.upm.es/def/ontology#\")\n", - "per = Namespace(\"http://oeg.fi.upm.es/def/people#\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "qp1oe2Eddsvo" - }, - "source": [ - "**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**\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 5, - "status": "ok", - "timestamp": 1761321272049, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "pnsrsgRUWF3A", - "outputId": "0a7e2c9a-c1ec-4063-f570-ed62d101511f" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Persona\n", - "g.add((per.Person, RDF.type, RDFS.Class))\n", - "g.add((per.Person, RDFS.label, Literal(\"Person\", datatype=XSD.string)))\n", - "#Profesor\n", - "g.add((per.Professor, RDF.type, RDFS.Class))\n", - "g.add((per.Professor, RDFS.subClassOf, per.Person))\n", - "g.add((per.Professor, RDFS.label, Literal(\"Professor\", datatype=XSD.string)))\n", - "#Professor COmpleto\n", - "g.add((per.FullProfessor, RDF.type, RDFS.Class))\n", - "g.add((per.FullProfessor, RDFS.subClassOf, per.Professor))\n", - "g.add((per.FullProfessor, RDFS.label, Literal(\"FullProfessor\", datatype=XSD.string)))\n", - "#Profesor Asociado\n", - "g.add((per.AssociateProfessor, RDF.type, RDFS.Class))\n", - "g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor))\n", - "g.add((per.AssociateProfessor, RDFS.label, Literal(\"AssociateProfessor\", datatype=XSD.string)))\n", - "#Profesor INterino\n", - "g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class))\n", - "g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor))\n", - "g.add((per.InterimAssociateProfessor, RDFS.label, Literal(\"InterimAssociateProfessor\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 14, - "status": "ok", - "timestamp": 1761322852753, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "HdRPyusrjIuB", - "outputId": "9100a4cd-8ac9-4109-9f04-0e53c7fb45eb" - }, - "outputs": [], - "source": [ - "# Validation. Do not remove\n", - "r.validate_task_06_01(g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "MXBqtBkJd22I" - }, - "source": [ - "**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)**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 22, - "status": "ok", - "timestamp": 1761240880664, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "53hZNtXsXCNq", - "outputId": "6bca568b-c887-4b5d-bc62-0595963edaa8" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Propiedad hasName\n", - "g.add((per.hasName, RDF.type, RDF.Property))\n", - "g.add((per.hasName, RDFS.domain, per.Person))\n", - "g.add((per.hasName, RDFS.range, RDFS.Literal))\n", - "g.add((per.hasName, RDFS.label, Literal(\"hasName\", datatype=XSD.string)))\n", - "#Propiedad hasColleague\n", - "g.add((per.hasColleague, RDF.type, RDF.Property))\n", - "g.add((per.hasColleague, RDFS.domain, per.Person))\n", - "g.add((per.hasColleague, RDFS.range, per.Person))\n", - "g.add((per.hasColleague, RDFS.label, Literal(\"hasColleague\", datatype=XSD.string)))\n", - "#Propiedad hasHomePage\n", - "g.add((per.hasHomePage, RDF.type, RDF.Property))\n", - "g.add((per.hasHomePage, RDFS.domain, per.FullProfessor))\n", - "g.add((per.hasHomePage, RDFS.range, RDFS.Literal))\n", - "g.add((per.hasHomePage, RDFS.label, Literal(\"hasHomePage\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 20, - "status": "ok", - "timestamp": 1761240882759, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "pd2mE-vGaxbu", - "outputId": "cabd7ea2-fef8-417b-dff5-94b9e23001d5" - }, - "outputs": [], - "source": [ - "# Validation. Do not remove\n", - "r.validate_task_06_02(g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OGct6k7Ld9O0" - }, - "source": [ - "**TASK 6.3: Create the individuals shown in slide 36 under \"Datos\". Link them with the same relationships shown in the diagram.\"**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 13, - "status": "ok", - "timestamp": 1761240965479, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "jbMMSHSZcFcf", - "outputId": "f6a0e42c-25a3-4a80-83b7-3b21e39b243a" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Namespace\n", - "data = Namespace(\"http://oeg.fi.upm.es/resource/person/\")\n", - "#Oscar\n", - "g.add((data.Oscar, RDF.type, per.AssociateProfessor))\n", - "g.add((data.Oscar, per.hasColleague, data.Asun))\n", - "g.add((data.Oscar, per.hasName, Literal(\"Oscar Corcho García\", datatype=XSD.string)))\n", - "g.add((data.Oscar, RDFS.label, Literal(\"Oscar\", datatype=XSD.string)))\n", - "#Asun\n", - "g.add((data.Asun, RDF.type, per.FullProfessor))\n", - "g.add((data.Asun, per.hasColleague, data.Raul))\n", - "g.add((data.Asun, per.hasHomePage, Literal(\"http://www.oeg-upm.net/\", datatype=XSD.string)))\n", - "g.add((data.Asun, RDFS.label, Literal(\"Asun\", datatype=XSD.string)))\n", - "#Raul\n", - "g.add((data.Raul, RDF.type, per.InterimAssociateProfessor))\n", - "g.add((data.Raul, RDFS.label, Literal(\"Raul\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 16, - "status": "ok", - "timestamp": 1761240968505, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "-3q4Wv2EfYew", - "outputId": "d8b7531a-b028-49df-d303-7e8d47f6903e" - }, - "outputs": [], - "source": [ - "r.validate_task_06_03(g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tD383J__eHfV" - }, - "source": [ - "**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**\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 14, - "status": "ok", - "timestamp": 1761241072430, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "hWmwlAfBcgN-", - "outputId": "64a0d523-5957-4034-ef00-e0f09a0c7628" - }, - "outputs": [], - "source": [ - "# TO DO\n", - "#Namespace\n", - "foaf = Namespace(\"http://xmlns.com/foaf/0.1/\")\n", - "vcard = Namespace(\"http://www.w3.org/2001/vcard-rdf/3.0/\")\n", - "#Nuevas relaciones\n", - "g.add((vcard.Family, RDF.type, RDF.Property))\n", - "g.add((vcard.Family, RDFS.range, XSD.string))\n", - "g.add((vcard.Given, RDF.type, RDF.Property))\n", - "g.add((vcard.Given, RDFS.range, XSD.string))\n", - "g.add((foaf.email, RDF.type, RDFS.Datatype))\n", - "g.add((foaf.eamil, RDFS.range, XSD.string))\n", - "#Más info de Oscar\n", - "g.add((data.Oscar, vcard.Given, Literal(\"Oscar\", datatype=XSD.string)))\n", - "g.add((data.Oscar, vcard.Family, Literal(\"Corcho García\", datatype=XSD.string)))\n", - "g.add((data.Oscar, foaf.email, Literal(\"ocorcho@fi.upm.es\", datatype=XSD.string)))\n", - "# Visualize the results\n", - "for s, p, o in g:\n", - " print(s,p,o)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 17, - "status": "ok", - "timestamp": 1761241075136, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Y1NiIQMyfgyF", - "outputId": "514cc283-789c-4ab0-9ab8-049307aa21d1" - }, - "outputs": [], - "source": [ - "# Validation. Do not remove\n", - "r.validate_task_06_04(g)\n", - "r.save_report(\"_Task_06\")" - ] - } - ], - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "display_name": "base", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.13.5" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" deleted file mode 100644 index 45372d0d..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.ipynb" +++ /dev/null @@ -1,681 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "nOOPLCHF7hLB" - }, - "source": [ - "**Task 07: Querying RDF(s)**" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 5297, - "status": "ok", - "timestamp": 1761241613452, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Yl9npCt8n6m-", - "outputId": "0a1b0036-4921-4cfd-b5fa-2fe1cadc5133" - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\"pip\" no se reconoce como un comando interno o externo,\n", - "programa o archivo por lotes ejecutable.\n" - ] - } - ], - "source": [ - "!pip install rdflib\n", - "import urllib.request\n", - "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", - "urllib.request.urlretrieve(url, 'validation.py')\n", - "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "executionInfo": { - "elapsed": 23, - "status": "ok", - "timestamp": 1761241615539, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "FmnGjffDT92V" - }, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'rdflib'", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mvalidation\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Report\n", - "\u001b[36mFile \u001b[39m\u001b[32mc:\\Clase\\KG\\GitHub\\Assigment4_Gonzalo_Hernández_24C060\\validation.py:1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mrdflib\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Graph, Namespace, Literal, XSD\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mrdflib\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mnamespace\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m RDF, RDFS\n\u001b[32m 4\u001b[39m VCARD = Namespace(\u001b[33m\"\u001b[39m\u001b[33mhttp://www.w3.org/2001/vcard-rdf/3.0/\u001b[39m\u001b[33m\"\u001b[39m)\n", - "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'rdflib'" - ] - } - ], - "source": [ - "from validation import Report" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XY7aPc86Bqoo" - }, - "source": [ - "First let's read the RDF file" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "executionInfo": { - "elapsed": 91, - "status": "ok", - "timestamp": 1761241616525, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "9ERh415on7kF" - }, - "outputs": [], - "source": [ - "from rdflib import Graph, Namespace, Literal\n", - "from rdflib.namespace import RDF, RDFS\n", - "# Do not change the name of the variables\n", - "g = Graph()\n", - "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", - "g.parse(github_storage+\"/rdf/data06.ttl\", format=\"TTL\")\n", - "report = Report()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "qp1oe2Eddsvo" - }, - "source": [ - "**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**\n", - "**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**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 9, - "status": "ok", - "timestamp": 1761242287620, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "tRcSWuMHOXBl", - "outputId": "167acaf6-2b9f-40c0-84ec-8c4050b976b4" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Animal'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Student'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#FullProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", - "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#InterimAssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n" - ] - } - ], - "source": [ - "# TO DO\n", - "#Lista classUri\n", - "result=[]\n", - "for sb in g.subjects(RDF.type, RDFS.Class):\n", - " sc=None\n", - " for Sc in g.objects(sb,RDFS.subClassOf):\n", - " sc=Sc\n", - " result.append((sb,Sc))\n", - "for cl, sc in result:\n", - " short_c = g.namespace_manager.normalizeUri(cl)\n", - " short_sc = g.namespace_manager.normalizeUri(sc) if sc else None\n", - "for r in result:\n", - " print(r)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 5, - "status": "ok", - "timestamp": 1761242289556, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "uvEpQQrTlMPH", - "outputId": "8c9bd1d8-6428-47fb-b06f-c6d4874aba22" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.1a OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_1a(result)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kbY-jqw6klr9" - }, - "source": [ - "**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 45, - "status": "ok", - "timestamp": 1761242480558, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "NGAG7l9UklMC", - "outputId": "aec82c1a-1643-49a0-bf08-483ff77483b9" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Person None\n", - "http://oeg.fi.upm.es/def/people#Animal None\n", - "http://oeg.fi.upm.es/def/people#Professor http://oeg.fi.upm.es/def/people#Person\n", - "http://oeg.fi.upm.es/def/people#Student http://oeg.fi.upm.es/def/people#Person\n", - "http://oeg.fi.upm.es/def/people#FullProfessor http://oeg.fi.upm.es/def/people#Professor\n", - "http://oeg.fi.upm.es/def/people#AssociateProfessor http://oeg.fi.upm.es/def/people#Professor\n", - "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://oeg.fi.upm.es/def/people#AssociateProfessor\n" - ] - } - ], - "source": [ - "query = \"Select ?c ?sc WHERE {?c rdf:type rdfs:Class. OPTIONAL {?c rdfs:subClassOf ?sc.}}\"\n", - "\n", - "for r in g.query(query):\n", - " print(r.c, r.sc)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 17, - "status": "ok", - "timestamp": 1761242482582, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "9zf4vgVHlKR3", - "outputId": "4294c94d-0b20-4352-c483-3b83ba9fc2fb" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.1b OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_1b(query,g)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gM3DASkTQQ5Y" - }, - "source": [ - "**TASK 7.2a: List all individuals of \"Person\" with RDFLib (remember the subClasses). Return the individual URIs in a list called \"individuals\"**\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 49, - "status": "ok", - "timestamp": 1761243807271, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "LiKSPHRzS-XJ", - "outputId": "b52dfb9d-10ff-40b9-c844-62190a16a65d" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun\n", - "http://oeg.fi.upm.es/def/people#Oscar\n", - "http://oeg.fi.upm.es/def/people#Raul\n" - ] - } - ], - "source": [ - "ns = Namespace(\"http://oeg.fi.upm.es/def/people#\")\n", - "\n", - "# variable to return\n", - "individuals = []\n", - "def subclases(cl):\n", - " sb=[]\n", - " for s,p,o in g.triples((None,RDFS.subClassOf,cl)):\n", - " sb.append(s)\n", - " sb += subclases(s)\n", - " return sb\n", - "clases= subclases(ns.Person)\n", - "clases.append(ns.Person)\n", - "for cl in clases:\n", - " for s,p,o in g.triples((None,RDF.type,cl)):\n", - " individuals.append(s)\n", - "# visualize results\n", - "for i in individuals:\n", - " print(i)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 18, - "status": "ok", - "timestamp": 1761243810074, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "ONrAls5uiX1G", - "outputId": "b7e389d9-4f71-425f-df0e-d4e1bd308907" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.2a OK\n" - ] - } - ], - "source": [ - "# validation. Do not remove\n", - "report.validate_07_02a(individuals)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "up-952A-za7A" - }, - "source": [ - "**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 23, - "status": "ok", - "timestamp": 1761243995727, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "ipYiEVbTzbR0", - "outputId": "1494aa3a-ef9d-4185-b412-e6dd66e07fae" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun\n", - "http://oeg.fi.upm.es/def/people#Oscar\n", - "http://oeg.fi.upm.es/def/people#Raul\n" - ] - } - ], - "source": [ - "query = \"SELECT ?ind WHERE{?c rdfs:subClassOf* . ?ind a ?c}\"\n", - "\n", - "for r in g.query(query):\n", - " print(r.ind)\n", - "# Visualize the results" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 17, - "status": "ok", - "timestamp": 1761244027657, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "s-Hu2LxRjUQt", - "outputId": "a7b70ca9-558d-483b-93f5-b86201904056" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.2b OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_02b(g, query)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3NyI7M2VNr9R" - }, - "source": [ - "**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**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 40, - "status": "ok", - "timestamp": 1761244226601, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "I_CNoIKdNpbx", - "outputId": "0b78e861-9e2c-414a-f12d-a2d8126cbed9" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun http://oeg.fi.upm.es/def/people#FullProfessor\n", - "http://oeg.fi.upm.es/def/people#Raul http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", - "http://oeg.fi.upm.es/def/people#Fantasma http://oeg.fi.upm.es/def/people#Animal\n" - ] - } - ], - "source": [ - "query = \"\"\"SELECT ?name ?type WHERE{\n", - " ?name .\n", - " ?name rdf:type ?type.}\"\"\"\n", - "# TO DO\n", - "# Visualize the results\n", - "for r in g.query(query):\n", - " print(r.name, r.type)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 9, - "status": "ok", - "timestamp": 1761244228693, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "Zf3JS7tEhS2t", - "outputId": "92b68ec3-6c93-4805-ebb2-bd6b851628ca" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.3 OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_03(g, query)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kyjGsyxDPa2C" - }, - "source": [ - "**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**" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 20, - "status": "ok", - "timestamp": 1761244939502, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "yoVwVZUAPaLm", - "outputId": "69bb43dd-b136-4afc-9a37-c888fe532ee2" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "http://oeg.fi.upm.es/def/people#Asun\n", - "http://oeg.fi.upm.es/def/people#Oscar\n", - "http://oeg.fi.upm.es/def/people#Raul\n" - ] - } - ], - "source": [ - "query = \"\"\"\n", - "PREFIX people: \n", - "Select ?name WHERE{\n", - " { ?name people:hasColleague ?c1.\n", - " ?c1 people:ownsPet ?dog.}\n", - " UNION{\n", - " ?name people:hasColleague ?c2.\n", - " ?c2 people:hasColleague ?c3.\n", - " ?c3 people:ownsPet ?dog.}\n", - " }\n", - "\"\"\"\n", - "\n", - "for r in g.query(query):\n", - " print(r.name)\n", - "\n", - "# TO DO\n", - "# Visualize the results" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "executionInfo": { - "elapsed": 27, - "status": "ok", - "timestamp": 1761244942138, - "user": { - "displayName": "Gonzalo Hernandez Vaillo", - "userId": "13071205746996859788" - }, - "user_tz": -120 - }, - "id": "zcTZE7ngj2fc", - "outputId": "858ccad3-d30c-4dd4-ce5a-5e427c445495" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TASK 7.4 OK\n" - ] - } - ], - "source": [ - "## Validation: Do not remove\n", - "report.validate_07_04(g,query)\n", - "report.save_report(\"_Task_07\")" - ] - } - ], - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.6" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" deleted file mode 100644 index 48d5fd16..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_06.txt" +++ /dev/null @@ -1,5 +0,0 @@ -Hierarchy OK -TASK 6.1 OK -TASK 6.2 OK -TASK 6.3 OK -TASK 6.4 OK diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" deleted file mode 100644 index 4d2ac917..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/report_result_Task_07.txt" +++ /dev/null @@ -1,6 +0,0 @@ -TASK 7.1a OK -TASK 7.1b OK -TASK 7.2a OK -TASK 7.2b OK -TASK 7.3 OK -TASK 7.4 OK diff --git "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" "b/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" deleted file mode 100644 index 6024e0b5..00000000 --- "a/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" +++ /dev/null @@ -1,258 +0,0 @@ -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") diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" index 4c05b942..92c41fe8 100644 --- "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" @@ -38,7 +38,7 @@ sc=None for Sc in g.objects(sb,RDFS.subClassOf): sc=Sc - result.append((sb,Sc)) + result.append((sb,sc)) for cl, sc in result: short_c = g.namespace_manager.normalizeUri(cl) short_sc = g.namespace_manager.normalizeUri(sc) if sc else None diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" index 09bea35e..6024e0b5 100644 --- "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" @@ -1,258 +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 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 dd27c78737eabf35a8cde432d74989b089cc58bf Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Sun, 26 Oct 2025 13:25:24 +0100 Subject: [PATCH 5/8] Creado con Colab --- .../Task06.py" | 584 ++++++++++++++++++ 1 file changed, 584 insertions(+) create mode 100644 "Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.py" diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.py" new file mode 100644 index 00000000..7dbf7b56 --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.py" @@ -0,0 +1,584 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nOOPLCHF7hLB" + }, + "source": [ + "**Task 06: Modifying RDF(s)**" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "Yl9npCt8n6m-", + "outputId": "576c3c73-0cf1-4cdb-c440-09a68c4da5ed", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: rdflib in /usr/local/lib/python3.12/dist-packages (7.3.0)\n", + "Requirement already satisfied: pyparsing<4,>=2.1.0 in /usr/local/lib/python3.12/dist-packages (from rdflib) (3.2.5)\n" + ] + } + ], + "source": [ + "!pip install rdflib\n", + "import urllib.request\n", + "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", + "urllib.request.urlretrieve(url, 'validation.py')\n", + "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XY7aPc86Bqoo" + }, + "source": [ + "Import RDFLib main methods" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "9ERh415on7kF" + }, + "outputs": [], + "source": [ + "from rdflib import Graph, Namespace, Literal, XSD\n", + "from rdflib.namespace import RDF, RDFS\n", + "from validation import Report\n", + "g = Graph()\n", + "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", + "r = Report()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gM3DASkTQQ5Y" + }, + "source": [ + "Create a new class named Researcher" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "6vtudax8Xb7b", + "outputId": "78da9a2c-a7c6-4d23-eb2b-0b6a3865d421", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n" + ] + } + ], + "source": [ + "ns = Namespace(\"http://mydomain.org#\")\n", + "g.add((ns.Researcher, RDF.type, RDFS.Class))\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "markdown", + "source": [ + "**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.**" + ], + "metadata": { + "id": "ZX8w9jnV5Xhp" + } + }, + { + "cell_type": "code", + "source": [ + "# this task is validated in the next step\n", + "ont = Namespace(\"http://oeg.fi.upm.es/def/ontology#\")\n", + "per = Namespace(\"http://oeg.fi.upm.es/def/people#\")" + ], + "metadata": { + "id": "b1ZQgYgB5vi7" + }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qp1oe2Eddsvo" + }, + "source": [ + "**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**\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "pnsrsgRUWF3A", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "0991abe3-0b11-464c-fb96-2a5c7bb481ba" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Persona\n", + "g.add((per.Person, RDF.type, RDFS.Class))\n", + "g.add((per.Person, RDFS.label, Literal(\"Person\", datatype=XSD.string)))\n", + "#Profesor\n", + "g.add((per.Professor, RDF.type, RDFS.Class))\n", + "g.add((per.Professor, RDFS.subClassOf, per.Person))\n", + "g.add((per.Professor, RDFS.label, Literal(\"Professor\", datatype=XSD.string)))\n", + "#Professor COmpleto\n", + "g.add((per.FullProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.FullProfessor, RDFS.subClassOf, per.Professor))\n", + "g.add((per.FullProfessor, RDFS.label, Literal(\"FullProfessor\", datatype=XSD.string)))\n", + "#Profesor Asociado\n", + "g.add((per.AssociateProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor))\n", + "g.add((per.AssociateProfessor, RDFS.label, Literal(\"AssociateProfessor\", datatype=XSD.string)))\n", + "#Profesor INterino\n", + "g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor))\n", + "g.add((per.InterimAssociateProfessor, RDFS.label, Literal(\"InterimAssociateProfessor\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_01(g)" + ], + "metadata": { + "id": "HdRPyusrjIuB", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "903e2013-11a1-4f1f-a818-be673e3ef670" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The namespace is correct for http://oeg.fi.upm.es/def/people#Professor\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#Person\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#FullProfessor\n", + "Hierarchy OK\n", + "TASK 6.1 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MXBqtBkJd22I" + }, + "source": [ + "**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)**" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "53hZNtXsXCNq", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8d4dec5f-7ad2-4109-8729-4c9329e40cbd" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#label hasColleague\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#label hasName\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#label hasHomePage\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#range http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Propiedad hasName\n", + "g.add((per.hasName, RDF.type, RDF.Property))\n", + "g.add((per.hasName, RDFS.domain, per.Person))\n", + "g.add((per.hasName, RDFS.range, RDFS.Literal))\n", + "g.add((per.hasName, RDFS.label, Literal(\"hasName\", datatype=XSD.string)))\n", + "#Propiedad hasColleague\n", + "g.add((per.hasColleague, RDF.type, RDF.Property))\n", + "g.add((per.hasColleague, RDFS.domain, per.Person))\n", + "g.add((per.hasColleague, RDFS.range, per.Person))\n", + "g.add((per.hasColleague, RDFS.label, Literal(\"hasColleague\", datatype=XSD.string)))\n", + "#Propiedad hasHomePage\n", + "g.add((per.hasHomePage, RDF.type, RDF.Property))\n", + "g.add((per.hasHomePage, RDFS.domain, per.FullProfessor))\n", + "g.add((per.hasHomePage, RDFS.range, RDFS.Literal))\n", + "g.add((per.hasHomePage, RDFS.label, Literal(\"hasHomePage\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_02(g)" + ], + "metadata": { + "id": "pd2mE-vGaxbu", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "bd69c351-22f1-435b-c054-f7a9e12682f5" + }, + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 6.2 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OGct6k7Ld9O0" + }, + "source": [ + "**TASK 6.3: Create the individuals shown in slide 36 under \"Datos\". Link them with the same relationships shown in the diagram.\"**" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "jbMMSHSZcFcf", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c7acbcb1-2bf2-493a-b441-248382db1c60" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2000/01/rdf-schema#label Oscar\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#label hasColleague\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#label hasHomePage\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasHomePage http://www.oeg-upm.net/\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Raul\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Asun\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasName Oscar Corcho García\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#range http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#label hasName\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/2000/01/rdf-schema#label Raul\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/2000/01/rdf-schema#label Asun\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Namespace\n", + "data = Namespace(\"http://oeg.fi.upm.es/resource/person/\")\n", + "#Oscar\n", + "g.add((data.Oscar, RDF.type, per.AssociateProfessor))\n", + "g.add((data.Oscar, per.hasColleague, data.Asun))\n", + "g.add((data.Oscar, per.hasName, Literal(\"Oscar Corcho García\", datatype=XSD.string)))\n", + "g.add((data.Oscar, RDFS.label, Literal(\"Oscar\", datatype=XSD.string)))\n", + "#Asun\n", + "g.add((data.Asun, RDF.type, per.FullProfessor))\n", + "g.add((data.Asun, per.hasColleague, data.Raul))\n", + "g.add((data.Asun, per.hasHomePage, Literal(\"http://www.oeg-upm.net/\", datatype=XSD.string)))\n", + "g.add((data.Asun, RDFS.label, Literal(\"Asun\", datatype=XSD.string)))\n", + "#Raul\n", + "g.add((data.Raul, RDF.type, per.InterimAssociateProfessor))\n", + "g.add((data.Raul, RDFS.label, Literal(\"Raul\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "r.validate_task_06_03(g)" + ], + "metadata": { + "id": "-3q4Wv2EfYew", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "bf5b5a41-93d8-469d-aea1-d3416600ed3c" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 6.3 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tD383J__eHfV" + }, + "source": [ + "**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**\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "hWmwlAfBcgN-", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "a5f53eaa-4d3c-421d-cdf7-d2246714a5ec" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2000/01/rdf-schema#label Oscar\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#label hasColleague\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#label hasHomePage\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasHomePage http://www.oeg-upm.net/\n", + "http://xmlns.com/foaf/0.1/eamil http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2001/XMLSchema#string\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Raul\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Asun\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasName Oscar Corcho García\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Given http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2001/XMLSchema#string\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://xmlns.com/foaf/0.1/email ocorcho@fi.upm.es\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#range http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2001/vcard-rdf/3.0/Given Oscar\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Given http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Family http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2001/XMLSchema#string\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#label hasName\n", + "http://xmlns.com/foaf/0.1/email http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Datatype\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Family http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2001/vcard-rdf/3.0/Family Corcho García\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/2000/01/rdf-schema#label Raul\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/2000/01/rdf-schema#label Asun\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Namespace\n", + "foaf = Namespace(\"http://xmlns.com/foaf/0.1/\")\n", + "vcard = Namespace(\"http://www.w3.org/2001/vcard-rdf/3.0/\")\n", + "#Nuevas relaciones\n", + "g.add((vcard.Family, RDF.type, RDF.Property))\n", + "g.add((vcard.Family, RDFS.range, XSD.string))\n", + "g.add((vcard.Given, RDF.type, RDF.Property))\n", + "g.add((vcard.Given, RDFS.range, XSD.string))\n", + "g.add((foaf.email, RDF.type, RDFS.Datatype))\n", + "g.add((foaf.eamil, RDFS.range, XSD.string))\n", + "#Más info de Oscar\n", + "g.add((data.Oscar, vcard.Given, Literal(\"Oscar\", datatype=XSD.string)))\n", + "g.add((data.Oscar, vcard.Family, Literal(\"Corcho García\", datatype=XSD.string)))\n", + "g.add((data.Oscar, foaf.email, Literal(\"ocorcho@fi.upm.es\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_04(g)\n", + "r.save_report(\"_Task_06\")" + ], + "metadata": { + "id": "Y1NiIQMyfgyF", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "39343cb6-9199-47f8-ceef-f93e3579465c" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 6.4 OK\n" + ] + } + ] + } + ], + "metadata": { + "colab": { + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file From f17a23f8b6793c2529bd781bdca7e0a4f8883e8c Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Sun, 26 Oct 2025 13:26:06 +0100 Subject: [PATCH 6/8] =?UTF-8?q?Delete=20Assignment4/Assigment4=5FGonzalo?= =?UTF-8?q?=5FHern=C3=A1ndez=5F24C060/task06.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../task06.py" | 145 ------------------ 1 file changed, 145 deletions(-) delete mode 100644 "Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task06.py" diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task06.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task06.py" deleted file mode 100644 index 6299f6f3..00000000 --- "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/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/drive/1BbLFeAgTPcLHiRqNBqy-DBeofpex4le5 - -**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 -ont = Namespace("http://oeg.fi.upm.es/def/ontology#") -per = Namespace("http://oeg.fi.upm.es/def/people#") - -"""**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 -#Persona -g.add((per.Person, RDF.type, RDFS.Class)) -g.add((per.Person, RDFS.label, Literal("Person", datatype=XSD.string))) -#Profesor -g.add((per.Professor, RDF.type, RDFS.Class)) -g.add((per.Professor, RDFS.subClassOf, per.Person)) -g.add((per.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) -#Professor COmpleto -g.add((per.FullProfessor, RDF.type, RDFS.Class)) -g.add((per.FullProfessor, RDFS.subClassOf, per.Professor)) -g.add((per.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) -#Profesor Asociado -g.add((per.AssociateProfessor, RDF.type, RDFS.Class)) -g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor)) -g.add((per.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) -#Profesor INterino -g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class)) -g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor)) -g.add((per.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 -#Propiedad hasName -g.add((per.hasName, RDF.type, RDF.Property)) -g.add((per.hasName, RDFS.domain, per.Person)) -g.add((per.hasName, RDFS.range, RDFS.Literal)) -g.add((per.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) -#Propiedad hasColleague -g.add((per.hasColleague, RDF.type, RDF.Property)) -g.add((per.hasColleague, RDFS.domain, per.Person)) -g.add((per.hasColleague, RDFS.range, per.Person)) -g.add((per.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) -#Propiedad hasHomePage -g.add((per.hasHomePage, RDF.type, RDF.Property)) -g.add((per.hasHomePage, RDFS.domain, per.FullProfessor)) -g.add((per.hasHomePage, RDFS.range, RDFS.Literal)) -g.add((per.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) -# 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 -#Namespace -data = Namespace("http://oeg.fi.upm.es/resource/person/") -#Oscar -g.add((data.Oscar, RDF.type, per.AssociateProfessor)) -g.add((data.Oscar, per.hasColleague, data.Asun)) -g.add((data.Oscar, per.hasName, Literal("Oscar Corcho García", datatype=XSD.string))) -g.add((data.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) -#Asun -g.add((data.Asun, RDF.type, per.FullProfessor)) -g.add((data.Asun, per.hasColleague, data.Raul)) -g.add((data.Asun, per.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string))) -g.add((data.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) -#Raul -g.add((data.Raul, RDF.type, per.InterimAssociateProfessor)) -g.add((data.Raul, RDFS.label, Literal("Raul", 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 -#Namespace -foaf = Namespace("http://xmlns.com/foaf/0.1/") -vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") -#Nuevas relaciones -g.add((vcard.Family, RDF.type, RDF.Property)) -g.add((vcard.Family, RDFS.range, XSD.string)) -g.add((vcard.Given, RDF.type, RDF.Property)) -g.add((vcard.Given, RDFS.range, XSD.string)) -g.add((foaf.email, RDF.type, RDFS.Datatype)) -g.add((foaf.eamil, RDFS.range, XSD.string)) -#Más info de Oscar -g.add((data.Oscar, vcard.Given, Literal("Oscar", datatype=XSD.string))) -g.add((data.Oscar, vcard.Family, Literal("Corcho García", datatype=XSD.string))) -g.add((data.Oscar, foaf.email, Literal("ocorcho@fi.upm.es", 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 be5330b2f5535a2e034bb364dffaab177bd0cd8e Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Sun, 26 Oct 2025 13:26:15 +0100 Subject: [PATCH 7/8] =?UTF-8?q?Delete=20Assignment4/Assigment4=5FGonzalo?= =?UTF-8?q?=5FHern=C3=A1ndez=5F24C060/task07.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../task07.py" | 133 ------------------ 1 file changed, 133 deletions(-) delete mode 100644 "Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" deleted file mode 100644 index 92c41fe8..00000000 --- "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/task07.py" +++ /dev/null @@ -1,133 +0,0 @@ -# -*- coding: utf-8 -*- -"""Task07.ipynb - -Automatically generated by Colab. - -Original file is located at - https://colab.research.google.com/drive/1gnhtqOSZOup-u6MULPbr1pmssLHx1CZz - -**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 -#Lista classUri -result=[] -for sb in g.subjects(RDF.type, RDFS.Class): - sc=None - for Sc in g.objects(sb,RDFS.subClassOf): - sc=Sc - result.append((sb,sc)) -for cl, sc in result: - short_c = g.namespace_manager.normalizeUri(cl) - short_sc = g.namespace_manager.normalizeUri(sc) if sc else None -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 = [] -def subclases(cl): - sb=[] - for s,p,o in g.triples((None,RDFS.subClassOf,cl)): - sb.append(s) - sb += subclases(s) - return sb -clases= subclases(ns.Person) -clases.append(ns.Person) -for cl in clases: - for s,p,o in g.triples((None,RDF.type,cl)): - individuals.append(s) -# 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 ?ind WHERE{?c rdfs:subClassOf* . ?ind a ?c}" - -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 rdf:type ?type.}""" -# TO DO -# 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 ?name WHERE{ - { ?name people:hasColleague ?c1. - ?c1 people:ownsPet ?dog.} - UNION{ - ?name people:hasColleague ?c2. - ?c2 people:hasColleague ?c3. - ?c3 people:ownsPet ?dog.} - } -""" - -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 df34fad6c45463282c224be4379b3a206dbda3c9 Mon Sep 17 00:00:00 2001 From: gonzahv24 Date: Sun, 26 Oct 2025 13:27:17 +0100 Subject: [PATCH 8/8] Creado con Colab --- .../Task07.py" | 529 ++++++++++++++++++ 1 file changed, 529 insertions(+) create mode 100644 "Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.py" diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.py" new file mode 100644 index 00000000..22dfc676 --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.py" @@ -0,0 +1,529 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nOOPLCHF7hLB" + }, + "source": [ + "**Task 07: Querying RDF(s)**" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "id": "Yl9npCt8n6m-", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6048b058-a425-482b-e893-d08cabde2dba" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: rdflib in /usr/local/lib/python3.12/dist-packages (7.3.0)\n", + "Requirement already satisfied: pyparsing<4,>=2.1.0 in /usr/local/lib/python3.12/dist-packages (from rdflib) (3.2.5)\n" + ] + } + ], + "source": [ + "!pip install rdflib\n", + "import urllib.request\n", + "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", + "urllib.request.urlretrieve(url, 'validation.py')\n", + "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" + ] + }, + { + "cell_type": "code", + "source": [ + "from validation import Report" + ], + "metadata": { + "id": "FmnGjffDT92V" + }, + "execution_count": 36, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XY7aPc86Bqoo" + }, + "source": [ + "First let's read the RDF file" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "id": "9ERh415on7kF" + }, + "outputs": [], + "source": [ + "from rdflib import Graph, Namespace, Literal\n", + "from rdflib.namespace import RDF, RDFS\n", + "# Do not change the name of the variables\n", + "g = Graph()\n", + "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", + "g.parse(github_storage+\"/rdf/data06.ttl\", format=\"TTL\")\n", + "report = Report()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qp1oe2Eddsvo" + }, + "source": [ + "**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**\n", + "**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**" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "id": "tRcSWuMHOXBl", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5a78292b-bf5c-4e7a-c205-fa060666a05c" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'), None)\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Animal'), None)\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Student'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#FullProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#InterimAssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Lista classUri\n", + "result=[]\n", + "for sb in g.subjects(RDF.type, RDFS.Class):\n", + " sc=None\n", + " for Sc in g.objects(sb,RDFS.subClassOf):\n", + " sc=Sc\n", + " result.append((sb,sc))\n", + "for cl, sc in result:\n", + " short_c = g.namespace_manager.normalizeUri(cl)\n", + " short_sc = g.namespace_manager.normalizeUri(sc) if sc else None\n", + "for r in result:\n", + " print(r)" + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_1a(result)" + ], + "metadata": { + "id": "uvEpQQrTlMPH", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "a3263309-6ed8-4115-a52c-a4fc5f313e7d" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.1a OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**" + ], + "metadata": { + "id": "kbY-jqw6klr9" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"Select ?c ?sc WHERE {?c rdf:type rdfs:Class. OPTIONAL {?c rdfs:subClassOf ?sc.}}\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.c, r.sc)\n" + ], + "metadata": { + "id": "NGAG7l9UklMC", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "911fc9bf-649b-4e26-b505-e570aaaf6f3d" + }, + "execution_count": 40, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Person None\n", + "http://oeg.fi.upm.es/def/people#Animal None\n", + "http://oeg.fi.upm.es/def/people#Professor http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#Student http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://oeg.fi.upm.es/def/people#AssociateProfessor\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_1b(query,g)" + ], + "metadata": { + "id": "9zf4vgVHlKR3", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "fe8922de-8c50-4c09-a02c-878d8331817c" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.1b OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gM3DASkTQQ5Y" + }, + "source": [ + "**TASK 7.2a: List all individuals of \"Person\" with RDFLib (remember the subClasses). Return the individual URIs in a list called \"individuals\"**\n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "id": "LiKSPHRzS-XJ", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "ca690103-c288-4cd5-e1e5-915db0971675" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ], + "source": [ + "ns = Namespace(\"http://oeg.fi.upm.es/def/people#\")\n", + "\n", + "# variable to return\n", + "individuals = []\n", + "def subclases(cl):\n", + " sb=[]\n", + " for s,p,o in g.triples((None,RDFS.subClassOf,cl)):\n", + " sb.append(s)\n", + " sb += subclases(s)\n", + " return sb\n", + "clases= subclases(ns.Person)\n", + "clases.append(ns.Person)\n", + "for cl in clases:\n", + " for s,p,o in g.triples((None,RDF.type,cl)):\n", + " individuals.append(s)\n", + "# visualize results\n", + "for i in individuals:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "source": [ + "# validation. Do not remove\n", + "report.validate_07_02a(individuals)" + ], + "metadata": { + "id": "ONrAls5uiX1G", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6de60947-5fec-4cd1-f11e-184cbb99538a" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.2a OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**" + ], + "metadata": { + "id": "up-952A-za7A" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"SELECT ?ind WHERE{?c rdfs:subClassOf* . ?ind a ?c}\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.ind)\n", + "# Visualize the results" + ], + "metadata": { + "id": "ipYiEVbTzbR0", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5bd40168-4205-4e6e-95b1-1a40c701f64d" + }, + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_02b(g, query)" + ], + "metadata": { + "id": "s-Hu2LxRjUQt", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "40f2fb8d-714b-4c9d-da6d-6aa256068f6b" + }, + "execution_count": 45, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.2b OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**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**" + ], + "metadata": { + "id": "3NyI7M2VNr9R" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"\"\"SELECT ?name ?type WHERE{\n", + " ?name .\n", + " ?name rdf:type ?type.}\"\"\"\n", + "# TO DO\n", + "# Visualize the results\n", + "for r in g.query(query):\n", + " print(r.name, r.type)\n" + ], + "metadata": { + "id": "I_CNoIKdNpbx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "b8b6082d-6242-49b0-f167-62c2b8303205" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#Raul http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Fantasma http://oeg.fi.upm.es/def/people#Animal\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_03(g, query)" + ], + "metadata": { + "id": "Zf3JS7tEhS2t", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "48c53bb4-bde3-45ef-e1a5-bafa4eb06b0f" + }, + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.3 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**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**" + ], + "metadata": { + "id": "kyjGsyxDPa2C" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"\"\"\n", + "PREFIX people: \n", + "Select ?name WHERE{\n", + " { ?name people:hasColleague ?c1.\n", + " ?c1 people:ownsPet ?dog.}\n", + " UNION{\n", + " ?name people:hasColleague ?c2.\n", + " ?c2 people:hasColleague ?c3.\n", + " ?c3 people:ownsPet ?dog.}\n", + " }\n", + "\"\"\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.name)\n", + "\n", + "# TO DO\n", + "# Visualize the results" + ], + "metadata": { + "id": "yoVwVZUAPaLm", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "835ead6c-de3c-4c52-f709-eebbf4888938" + }, + "execution_count": 48, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_04(g,query)\n", + "report.save_report(\"_Task_07\")" + ], + "metadata": { + "id": "zcTZE7ngj2fc", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f750091a-c97f-4f31-fd96-e2827aac782e" + }, + "execution_count": 51, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.4 OK\n" + ] + } + ] + } + ], + "metadata": { + "colab": { + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file