From 765f7393081680fd83db10522adfe7eae255adb7 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Fri, 24 Oct 2025 15:12:59 +0200 Subject: [PATCH 01/12] task06 --- Assignment4/Riddhi_Chitkara_24C023/task06 | 143 ++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 Assignment4/Riddhi_Chitkara_24C023/task06 diff --git a/Assignment4/Riddhi_Chitkara_24C023/task06 b/Assignment4/Riddhi_Chitkara_24C023/task06 new file mode 100644 index 00000000..deb9091b --- /dev/null +++ b/Assignment4/Riddhi_Chitkara_24C023/task06 @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +"""task06.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1bA_L6pJlymA__5jF1fTZm13By8nys_y- + +**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 +ontology = Namespace("http://www.oeg.fi.upm.net/Ontology#") +person = Namespace("http://www.oeg.fi.upm.net/Person#") +g.bind("ontology", ontology) +g.bind("person", person) + +g.bind("ns", Namespace("http://somewhere#")) +ns = Namespace("http://mydomain.org") +g.add((ns.Researcher, RDF.type, RDFS.Class)) + +"""**TASK 6.1: Reproduce the taxonomy of classes shown in slide 34 in class (all the classes under "Vocabulario", Slidedeck: 01a.RDF(s)-SPARQL). Add labels for each of them as they are in the diagram (exactly) with no language tags. Remember adding the correct datatype (xsd:String) when appropriate** + +""" + +# TO DO +g.remove((None, None, None)) + +g.add((ontology.Person, RDF.type, RDFS.Class)) +g.add((ontology.Person, RDFS.label, Literal("Person", datatype=XSD.string))) + +g.add((ontology.Professor, RDF.type, RDFS.Class)) +g.add((ontology.Professor, RDFS.subClassOf, ontology.Person)) +g.add((ontology.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) + +g.add((ontology.FullProfessor, RDF.type, RDFS.Class)) +g.add((ontology.FullProfessor, RDFS.subClassOf, ontology.Professor)) +g.add((ontology.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) + +g.add((ontology.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((ontology.AssociateProfessor, RDFS.subClassOf, ontology.Professor)) +g.add((ontology.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) + +g.add((ontology.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((ontology.InterimAssociateProfessor, RDFS.subClassOf, ontology.AssociateProfessor)) +g.add((ontology.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_01(g) + +"""**TASK 6.2: Add the 3 properties shown in slide 36. Add labels for each of them (exactly as they are in the slide, with no language tags), and their corresponding domains and ranges using RDFS. Remember adding the correct datatype (xsd:String) when appropriate. If a property has no range, make it a literal (string)**""" + +# TO DO +g.add((ns.hasColleague, RDF.type, RDF.Property)) +g.add((ns.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) +g.add((ns.hasColleague, RDFS.domain, ns.Person)) +g.add((ns.hasColleague, RDFS.range, ns.Person)) + +g.add((ns.hasName, RDF.type, RDF.Property)) +g.add((ns.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) +g.add((ns.hasName, RDFS.domain, ns.Person)) +g.add((ns.hasName, RDFS.range, RDFS.Literal)) + +g.add((ns.hasHomePage, RDF.type, RDF.Property)) +g.add((ns.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) +g.add((ns.hasHomePage, RDFS.domain, ns.FullProfessor)) +g.add((ns.hasHomePage, RDFS.range, RDFS.Literal)) +# 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 +resource = Namespace("http://oeg.fi.upm.es/resource/person/") + +g.add((resource.Oscar, RDF.type, ns.AssociateProfessor)) +g.add((resource.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) +g.add((resource.Oscar, ns.hasName, Literal("Oscar Corcho García", datatype=XSD.string))) +g.add((resource.Oscar, ns.hasColleague, resource.Asun)) + +g.add((resource.Asun, RDF.type, ns.FullProfessor)) +g.add((resource.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) +g.add((resource.Asun, ns.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string))) +g.add((resource.Asun, ns.hasColleague, resource.Raul)) + +g.add((resource.Raul, RDF.type, ns.InterimAssociateProfessor)) +g.add((resource.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 +VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") +FOAF = Namespace("http://xmlns.com/foaf/0.1/") + +g.add((resource.Oscar, VCARD.Given, Literal("Oscar", datatype=XSD.string))) +g.add((resource.Oscar, VCARD.Family, Literal("Corcho", datatype=XSD.string))) +g.add((resource.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") From 46921c4b0815c4c2182b1eca360aa9592f9b63b4 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Fri, 24 Oct 2025 15:14:24 +0200 Subject: [PATCH 02/12] task07 --- Assignment4/Riddhi_Chitkara_24C023/task07 | 154 ++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 Assignment4/Riddhi_Chitkara_24C023/task07 diff --git a/Assignment4/Riddhi_Chitkara_24C023/task07 b/Assignment4/Riddhi_Chitkara_24C023/task07 new file mode 100644 index 00000000..0932b93b --- /dev/null +++ b/Assignment4/Riddhi_Chitkara_24C023/task07 @@ -0,0 +1,154 @@ +# -*- coding: utf-8 -*- +"""task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1-XjrbfW5Nf7zWDzAUhOXRjW0RQQ6x-TI + +**Task 07: Querying RDF(s)** +""" + +# !pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +from validation import Report + +"""First let's read the RDF file""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +# Do not change the name of the variables +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +g.parse(github_storage+"/rdf/data06.ttl", format="TTL") +report = Report() + +"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** +**Do the exercise in RDFLib returning a list of Tuples: (class, superclass) called "result". If a class does not have a super class, then return None as the superclass** +""" + +# TO DO +result = [] #list of tuples +classes = set(g.subjects(RDF.type, RDFS.Class)) +classes.update(g.subjects(RDFS.subClassOf, None)) +for c in classes: + superclasses = list(g.objects(c, RDFS.subClassOf)) + if not superclasses: + result.append((c, None)) + else: + for sc in superclasses: + result.append((c, sc)) +# Visualize the results +for r in result: + print(r) + +## Validation: Do not remove +report.validate_07_1a(result) + +"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" + +query = """PREFIX rdfs: +PREFIX rdf: +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 is_person_subclass(cls): + if cls == ns.Person: + return True + for s, p, o in g.triples((cls, RDFS.subClassOf, None)): + if is_person_subclass(o): + return True + return False +for s, p, o in g.triples((None, RDF.type, None)): + if o == ns.Person or is_person_subclass(o): + 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 = """ +PREFIX ppl: +SELECT DISTINCT ?ind +WHERE { + ?p rdfs:subClassOf* ppl:Person . + ?ind a ?p . + }""" + +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**""" + +# TO DO +query = """ +PREFIX ppl: +PREFIX rdfs: +SELECT ?name ?type WHERE { + ?p ppl:knows ppl:Rocky . + ?p rdfs:label ?name . + ?p rdf:type ?type . +} +""" +# Visualize the results +for r in g.query(query): + print(r.name, r.type) + +## Validation: Do not remove +report.validate_07_03(g, query) + +"""**Task 7.4: List the name of those entities who have a colleague with a dog, or that have a collegue who has a colleague who has a dog (in SPARQL). Return the results in a variable called name**""" + +query = """ +PREFIX ppl: +SELECT DISTINCT ?name WHERE { + ?p rdfs:label ?name . + { + ?p ppl:hasColleague ?colleague1 . + ?colleague1 ppl:ownsPet ?pet1 . + } UNION { + ?p ppl:hasColleague ?colleague1 . + ?colleague1 ppl:hasColleague ?colleague2 . + ?colleague2 ppl:ownsPet ?pet2 . + } +} +""" + +for r in g.query(query): + print(r.name) + +# TO DO +# Visualize the results + +## Validation: Do not remove +report.validate_07_04(g,query) +report.save_report("_Task_07") From 834a39e18ac58b1e4666e476f6fa9888bd406dc6 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Fri, 24 Oct 2025 15:14:57 +0200 Subject: [PATCH 03/12] validation --- Assignment4/Riddhi_Chitkara_24C023/validation | 258 ++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 Assignment4/Riddhi_Chitkara_24C023/validation diff --git a/Assignment4/Riddhi_Chitkara_24C023/validation b/Assignment4/Riddhi_Chitkara_24C023/validation new file mode 100644 index 00000000..6024e0b5 --- /dev/null +++ b/Assignment4/Riddhi_Chitkara_24C023/validation @@ -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 326c1782d8e5c7dfed4825a346eafbba53fdca5a Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 19 Nov 2025 23:38:40 +0100 Subject: [PATCH 04/12] Create task06.py --- Assignment4/Riddhi_Chitkara_24C023/task06.py | 143 +++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 Assignment4/Riddhi_Chitkara_24C023/task06.py diff --git a/Assignment4/Riddhi_Chitkara_24C023/task06.py b/Assignment4/Riddhi_Chitkara_24C023/task06.py new file mode 100644 index 00000000..deb9091b --- /dev/null +++ b/Assignment4/Riddhi_Chitkara_24C023/task06.py @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +"""task06.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1bA_L6pJlymA__5jF1fTZm13By8nys_y- + +**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 +ontology = Namespace("http://www.oeg.fi.upm.net/Ontology#") +person = Namespace("http://www.oeg.fi.upm.net/Person#") +g.bind("ontology", ontology) +g.bind("person", person) + +g.bind("ns", Namespace("http://somewhere#")) +ns = Namespace("http://mydomain.org") +g.add((ns.Researcher, RDF.type, RDFS.Class)) + +"""**TASK 6.1: Reproduce the taxonomy of classes shown in slide 34 in class (all the classes under "Vocabulario", Slidedeck: 01a.RDF(s)-SPARQL). Add labels for each of them as they are in the diagram (exactly) with no language tags. Remember adding the correct datatype (xsd:String) when appropriate** + +""" + +# TO DO +g.remove((None, None, None)) + +g.add((ontology.Person, RDF.type, RDFS.Class)) +g.add((ontology.Person, RDFS.label, Literal("Person", datatype=XSD.string))) + +g.add((ontology.Professor, RDF.type, RDFS.Class)) +g.add((ontology.Professor, RDFS.subClassOf, ontology.Person)) +g.add((ontology.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) + +g.add((ontology.FullProfessor, RDF.type, RDFS.Class)) +g.add((ontology.FullProfessor, RDFS.subClassOf, ontology.Professor)) +g.add((ontology.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) + +g.add((ontology.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((ontology.AssociateProfessor, RDFS.subClassOf, ontology.Professor)) +g.add((ontology.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) + +g.add((ontology.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((ontology.InterimAssociateProfessor, RDFS.subClassOf, ontology.AssociateProfessor)) +g.add((ontology.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +# Validation. Do not remove +r.validate_task_06_01(g) + +"""**TASK 6.2: Add the 3 properties shown in slide 36. Add labels for each of them (exactly as they are in the slide, with no language tags), and their corresponding domains and ranges using RDFS. Remember adding the correct datatype (xsd:String) when appropriate. If a property has no range, make it a literal (string)**""" + +# TO DO +g.add((ns.hasColleague, RDF.type, RDF.Property)) +g.add((ns.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) +g.add((ns.hasColleague, RDFS.domain, ns.Person)) +g.add((ns.hasColleague, RDFS.range, ns.Person)) + +g.add((ns.hasName, RDF.type, RDF.Property)) +g.add((ns.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) +g.add((ns.hasName, RDFS.domain, ns.Person)) +g.add((ns.hasName, RDFS.range, RDFS.Literal)) + +g.add((ns.hasHomePage, RDF.type, RDF.Property)) +g.add((ns.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) +g.add((ns.hasHomePage, RDFS.domain, ns.FullProfessor)) +g.add((ns.hasHomePage, RDFS.range, RDFS.Literal)) +# 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 +resource = Namespace("http://oeg.fi.upm.es/resource/person/") + +g.add((resource.Oscar, RDF.type, ns.AssociateProfessor)) +g.add((resource.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) +g.add((resource.Oscar, ns.hasName, Literal("Oscar Corcho García", datatype=XSD.string))) +g.add((resource.Oscar, ns.hasColleague, resource.Asun)) + +g.add((resource.Asun, RDF.type, ns.FullProfessor)) +g.add((resource.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) +g.add((resource.Asun, ns.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string))) +g.add((resource.Asun, ns.hasColleague, resource.Raul)) + +g.add((resource.Raul, RDF.type, ns.InterimAssociateProfessor)) +g.add((resource.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 +VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") +FOAF = Namespace("http://xmlns.com/foaf/0.1/") + +g.add((resource.Oscar, VCARD.Given, Literal("Oscar", datatype=XSD.string))) +g.add((resource.Oscar, VCARD.Family, Literal("Corcho", datatype=XSD.string))) +g.add((resource.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") From fb79681b852e05e7c6ba4222a4b8f102a7fec396 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 19 Nov 2025 23:39:14 +0100 Subject: [PATCH 05/12] Create task07.py --- Assignment4/Riddhi_Chitkara_24C023/task07.py | 154 +++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 Assignment4/Riddhi_Chitkara_24C023/task07.py diff --git a/Assignment4/Riddhi_Chitkara_24C023/task07.py b/Assignment4/Riddhi_Chitkara_24C023/task07.py new file mode 100644 index 00000000..0932b93b --- /dev/null +++ b/Assignment4/Riddhi_Chitkara_24C023/task07.py @@ -0,0 +1,154 @@ +# -*- coding: utf-8 -*- +"""task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1-XjrbfW5Nf7zWDzAUhOXRjW0RQQ6x-TI + +**Task 07: Querying RDF(s)** +""" + +# !pip install rdflib +import urllib.request +url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' +urllib.request.urlretrieve(url, 'validation.py') +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" + +from validation import Report + +"""First let's read the RDF file""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +# Do not change the name of the variables +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +g.parse(github_storage+"/rdf/data06.ttl", format="TTL") +report = Report() + +"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** +**Do the exercise in RDFLib returning a list of Tuples: (class, superclass) called "result". If a class does not have a super class, then return None as the superclass** +""" + +# TO DO +result = [] #list of tuples +classes = set(g.subjects(RDF.type, RDFS.Class)) +classes.update(g.subjects(RDFS.subClassOf, None)) +for c in classes: + superclasses = list(g.objects(c, RDFS.subClassOf)) + if not superclasses: + result.append((c, None)) + else: + for sc in superclasses: + result.append((c, sc)) +# Visualize the results +for r in result: + print(r) + +## Validation: Do not remove +report.validate_07_1a(result) + +"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" + +query = """PREFIX rdfs: +PREFIX rdf: +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 is_person_subclass(cls): + if cls == ns.Person: + return True + for s, p, o in g.triples((cls, RDFS.subClassOf, None)): + if is_person_subclass(o): + return True + return False +for s, p, o in g.triples((None, RDF.type, None)): + if o == ns.Person or is_person_subclass(o): + 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 = """ +PREFIX ppl: +SELECT DISTINCT ?ind +WHERE { + ?p rdfs:subClassOf* ppl:Person . + ?ind a ?p . + }""" + +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**""" + +# TO DO +query = """ +PREFIX ppl: +PREFIX rdfs: +SELECT ?name ?type WHERE { + ?p ppl:knows ppl:Rocky . + ?p rdfs:label ?name . + ?p rdf:type ?type . +} +""" +# Visualize the results +for r in g.query(query): + print(r.name, r.type) + +## Validation: Do not remove +report.validate_07_03(g, query) + +"""**Task 7.4: List the name of those entities who have a colleague with a dog, or that have a collegue who has a colleague who has a dog (in SPARQL). Return the results in a variable called name**""" + +query = """ +PREFIX ppl: +SELECT DISTINCT ?name WHERE { + ?p rdfs:label ?name . + { + ?p ppl:hasColleague ?colleague1 . + ?colleague1 ppl:ownsPet ?pet1 . + } UNION { + ?p ppl:hasColleague ?colleague1 . + ?colleague1 ppl:hasColleague ?colleague2 . + ?colleague2 ppl:ownsPet ?pet2 . + } +} +""" + +for r in g.query(query): + print(r.name) + +# TO DO +# Visualize the results + +## Validation: Do not remove +report.validate_07_04(g,query) +report.save_report("_Task_07") From a7eb071f7a763d89bfac2f3f7f4dd70e7b7241b1 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 19 Nov 2025 23:39:46 +0100 Subject: [PATCH 06/12] Create validation.py --- .../Riddhi_Chitkara_24C023/validation.py | 258 ++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 Assignment4/Riddhi_Chitkara_24C023/validation.py diff --git a/Assignment4/Riddhi_Chitkara_24C023/validation.py b/Assignment4/Riddhi_Chitkara_24C023/validation.py new file mode 100644 index 00000000..6024e0b5 --- /dev/null +++ b/Assignment4/Riddhi_Chitkara_24C023/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 bab78c611e7d5552fbf0b48527f496ab50d995e4 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 19 Nov 2025 23:48:11 +0100 Subject: [PATCH 07/12] Delete Assignment4/Riddhi_Chitkara_24C023/task06 --- Assignment4/Riddhi_Chitkara_24C023/task06 | 143 ---------------------- 1 file changed, 143 deletions(-) delete mode 100644 Assignment4/Riddhi_Chitkara_24C023/task06 diff --git a/Assignment4/Riddhi_Chitkara_24C023/task06 b/Assignment4/Riddhi_Chitkara_24C023/task06 deleted file mode 100644 index deb9091b..00000000 --- a/Assignment4/Riddhi_Chitkara_24C023/task06 +++ /dev/null @@ -1,143 +0,0 @@ -# -*- coding: utf-8 -*- -"""task06.ipynb - -Automatically generated by Colab. - -Original file is located at - https://colab.research.google.com/drive/1bA_L6pJlymA__5jF1fTZm13By8nys_y- - -**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 -ontology = Namespace("http://www.oeg.fi.upm.net/Ontology#") -person = Namespace("http://www.oeg.fi.upm.net/Person#") -g.bind("ontology", ontology) -g.bind("person", person) - -g.bind("ns", Namespace("http://somewhere#")) -ns = Namespace("http://mydomain.org") -g.add((ns.Researcher, RDF.type, RDFS.Class)) - -"""**TASK 6.1: Reproduce the taxonomy of classes shown in slide 34 in class (all the classes under "Vocabulario", Slidedeck: 01a.RDF(s)-SPARQL). Add labels for each of them as they are in the diagram (exactly) with no language tags. Remember adding the correct datatype (xsd:String) when appropriate** - -""" - -# TO DO -g.remove((None, None, None)) - -g.add((ontology.Person, RDF.type, RDFS.Class)) -g.add((ontology.Person, RDFS.label, Literal("Person", datatype=XSD.string))) - -g.add((ontology.Professor, RDF.type, RDFS.Class)) -g.add((ontology.Professor, RDFS.subClassOf, ontology.Person)) -g.add((ontology.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) - -g.add((ontology.FullProfessor, RDF.type, RDFS.Class)) -g.add((ontology.FullProfessor, RDFS.subClassOf, ontology.Professor)) -g.add((ontology.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) - -g.add((ontology.AssociateProfessor, RDF.type, RDFS.Class)) -g.add((ontology.AssociateProfessor, RDFS.subClassOf, ontology.Professor)) -g.add((ontology.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) - -g.add((ontology.InterimAssociateProfessor, RDF.type, RDFS.Class)) -g.add((ontology.InterimAssociateProfessor, RDFS.subClassOf, ontology.AssociateProfessor)) -g.add((ontology.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) - -# Visualize the results -for s, p, o in g: - print(s,p,o) - -# Validation. Do not remove -r.validate_task_06_01(g) - -"""**TASK 6.2: Add the 3 properties shown in slide 36. Add labels for each of them (exactly as they are in the slide, with no language tags), and their corresponding domains and ranges using RDFS. Remember adding the correct datatype (xsd:String) when appropriate. If a property has no range, make it a literal (string)**""" - -# TO DO -g.add((ns.hasColleague, RDF.type, RDF.Property)) -g.add((ns.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) -g.add((ns.hasColleague, RDFS.domain, ns.Person)) -g.add((ns.hasColleague, RDFS.range, ns.Person)) - -g.add((ns.hasName, RDF.type, RDF.Property)) -g.add((ns.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) -g.add((ns.hasName, RDFS.domain, ns.Person)) -g.add((ns.hasName, RDFS.range, RDFS.Literal)) - -g.add((ns.hasHomePage, RDF.type, RDF.Property)) -g.add((ns.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) -g.add((ns.hasHomePage, RDFS.domain, ns.FullProfessor)) -g.add((ns.hasHomePage, RDFS.range, RDFS.Literal)) -# 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 -resource = Namespace("http://oeg.fi.upm.es/resource/person/") - -g.add((resource.Oscar, RDF.type, ns.AssociateProfessor)) -g.add((resource.Oscar, RDFS.label, Literal("Oscar", datatype=XSD.string))) -g.add((resource.Oscar, ns.hasName, Literal("Oscar Corcho García", datatype=XSD.string))) -g.add((resource.Oscar, ns.hasColleague, resource.Asun)) - -g.add((resource.Asun, RDF.type, ns.FullProfessor)) -g.add((resource.Asun, RDFS.label, Literal("Asun", datatype=XSD.string))) -g.add((resource.Asun, ns.hasHomePage, Literal("http://www.oeg-upm.net/", datatype=XSD.string))) -g.add((resource.Asun, ns.hasColleague, resource.Raul)) - -g.add((resource.Raul, RDF.type, ns.InterimAssociateProfessor)) -g.add((resource.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 -VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") -FOAF = Namespace("http://xmlns.com/foaf/0.1/") - -g.add((resource.Oscar, VCARD.Given, Literal("Oscar", datatype=XSD.string))) -g.add((resource.Oscar, VCARD.Family, Literal("Corcho", datatype=XSD.string))) -g.add((resource.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") From 4d68e36a3760d5e735a021a51a085ea195dfdc5f Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 19 Nov 2025 23:48:23 +0100 Subject: [PATCH 08/12] Delete Assignment4/Riddhi_Chitkara_24C023/task07 --- Assignment4/Riddhi_Chitkara_24C023/task07 | 154 ---------------------- 1 file changed, 154 deletions(-) delete mode 100644 Assignment4/Riddhi_Chitkara_24C023/task07 diff --git a/Assignment4/Riddhi_Chitkara_24C023/task07 b/Assignment4/Riddhi_Chitkara_24C023/task07 deleted file mode 100644 index 0932b93b..00000000 --- a/Assignment4/Riddhi_Chitkara_24C023/task07 +++ /dev/null @@ -1,154 +0,0 @@ -# -*- coding: utf-8 -*- -"""task07.ipynb - -Automatically generated by Colab. - -Original file is located at - https://colab.research.google.com/drive/1-XjrbfW5Nf7zWDzAUhOXRjW0RQQ6x-TI - -**Task 07: Querying RDF(s)** -""" - -# !pip install rdflib -import urllib.request -url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' -urllib.request.urlretrieve(url, 'validation.py') -github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials" - -from validation import Report - -"""First let's read the RDF file""" - -from rdflib import Graph, Namespace, Literal -from rdflib.namespace import RDF, RDFS -# Do not change the name of the variables -g = Graph() -g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) -g.parse(github_storage+"/rdf/data06.ttl", format="TTL") -report = Report() - -"""**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.** -**Do the exercise in RDFLib returning a list of Tuples: (class, superclass) called "result". If a class does not have a super class, then return None as the superclass** -""" - -# TO DO -result = [] #list of tuples -classes = set(g.subjects(RDF.type, RDFS.Class)) -classes.update(g.subjects(RDFS.subClassOf, None)) -for c in classes: - superclasses = list(g.objects(c, RDFS.subClassOf)) - if not superclasses: - result.append((c, None)) - else: - for sc in superclasses: - result.append((c, sc)) -# Visualize the results -for r in result: - print(r) - -## Validation: Do not remove -report.validate_07_1a(result) - -"""**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**""" - -query = """PREFIX rdfs: -PREFIX rdf: -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 is_person_subclass(cls): - if cls == ns.Person: - return True - for s, p, o in g.triples((cls, RDFS.subClassOf, None)): - if is_person_subclass(o): - return True - return False -for s, p, o in g.triples((None, RDF.type, None)): - if o == ns.Person or is_person_subclass(o): - 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 = """ -PREFIX ppl: -SELECT DISTINCT ?ind -WHERE { - ?p rdfs:subClassOf* ppl:Person . - ?ind a ?p . - }""" - -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**""" - -# TO DO -query = """ -PREFIX ppl: -PREFIX rdfs: -SELECT ?name ?type WHERE { - ?p ppl:knows ppl:Rocky . - ?p rdfs:label ?name . - ?p rdf:type ?type . -} -""" -# Visualize the results -for r in g.query(query): - print(r.name, r.type) - -## Validation: Do not remove -report.validate_07_03(g, query) - -"""**Task 7.4: List the name of those entities who have a colleague with a dog, or that have a collegue who has a colleague who has a dog (in SPARQL). Return the results in a variable called name**""" - -query = """ -PREFIX ppl: -SELECT DISTINCT ?name WHERE { - ?p rdfs:label ?name . - { - ?p ppl:hasColleague ?colleague1 . - ?colleague1 ppl:ownsPet ?pet1 . - } UNION { - ?p ppl:hasColleague ?colleague1 . - ?colleague1 ppl:hasColleague ?colleague2 . - ?colleague2 ppl:ownsPet ?pet2 . - } -} -""" - -for r in g.query(query): - print(r.name) - -# TO DO -# Visualize the results - -## Validation: Do not remove -report.validate_07_04(g,query) -report.save_report("_Task_07") From fd2bc0cc5c814bdafd6f5ff2f0f1d32bec33da58 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 19 Nov 2025 23:48:39 +0100 Subject: [PATCH 09/12] Delete Assignment4/Riddhi_Chitkara_24C023/validation --- Assignment4/Riddhi_Chitkara_24C023/validation | 258 ------------------ 1 file changed, 258 deletions(-) delete mode 100644 Assignment4/Riddhi_Chitkara_24C023/validation diff --git a/Assignment4/Riddhi_Chitkara_24C023/validation b/Assignment4/Riddhi_Chitkara_24C023/validation deleted file mode 100644 index 6024e0b5..00000000 --- a/Assignment4/Riddhi_Chitkara_24C023/validation +++ /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 2172da74a1fb89f3265f66ec4433074b49f22405 Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Sat, 29 Nov 2025 20:15:26 +0100 Subject: [PATCH 10/12] task06 corregido --- Assignment4/Riddhi_Chitkara_24C023/task06.py | 42 +++++++++----------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/Assignment4/Riddhi_Chitkara_24C023/task06.py b/Assignment4/Riddhi_Chitkara_24C023/task06.py index deb9091b..7456c2f5 100644 --- a/Assignment4/Riddhi_Chitkara_24C023/task06.py +++ b/Assignment4/Riddhi_Chitkara_24C023/task06.py @@ -4,12 +4,12 @@ Automatically generated by Colab. Original file is located at - https://colab.research.google.com/drive/1bA_L6pJlymA__5jF1fTZm13By8nys_y- + https://colab.research.google.com/drive/1dfYicakDhiNemXTLeXsdSi7fGggV4PGK **Task 06: Modifying RDF(s)** """ -# !pip install rdflib +!pip install rdflib import urllib.request url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' urllib.request.urlretrieve(url, 'validation.py') @@ -34,15 +34,11 @@ """**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 -ontology = Namespace("http://www.oeg.fi.upm.net/Ontology#") -person = Namespace("http://www.oeg.fi.upm.net/Person#") +ontology = Namespace("http://www.oeg-upm.net/ontology#") +person = Namespace("http://www.oeg-upm.net/person#") g.bind("ontology", ontology) g.bind("person", person) -g.bind("ns", Namespace("http://somewhere#")) -ns = Namespace("http://mydomain.org") -g.add((ns.Researcher, RDF.type, RDFS.Class)) - """**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** """ @@ -50,24 +46,24 @@ # TO DO g.remove((None, None, None)) -g.add((ontology.Person, RDF.type, RDFS.Class)) -g.add((ontology.Person, RDFS.label, Literal("Person", datatype=XSD.string))) +g.add((ns.Person, RDF.type, RDFS.Class)) +g.add((ns.Person, RDFS.label, Literal("Person", datatype=XSD.string))) -g.add((ontology.Professor, RDF.type, RDFS.Class)) -g.add((ontology.Professor, RDFS.subClassOf, ontology.Person)) -g.add((ontology.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) +g.add((ns.Professor, RDF.type, RDFS.Class)) +g.add((ns.Professor, RDFS.subClassOf, ns.Person)) +g.add((ns.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) -g.add((ontology.FullProfessor, RDF.type, RDFS.Class)) -g.add((ontology.FullProfessor, RDFS.subClassOf, ontology.Professor)) -g.add((ontology.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) +g.add((ns.FullProfessor, RDF.type, RDFS.Class)) +g.add((ns.FullProfessor, RDFS.subClassOf, ns.Professor)) +g.add((ns.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) -g.add((ontology.AssociateProfessor, RDF.type, RDFS.Class)) -g.add((ontology.AssociateProfessor, RDFS.subClassOf, ontology.Professor)) -g.add((ontology.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) +g.add((ns.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((ns.AssociateProfessor, RDFS.subClassOf, ns.Professor)) +g.add((ns.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) -g.add((ontology.InterimAssociateProfessor, RDF.type, RDFS.Class)) -g.add((ontology.InterimAssociateProfessor, RDFS.subClassOf, ontology.AssociateProfessor)) -g.add((ontology.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) +g.add((ns.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((ns.InterimAssociateProfessor, RDFS.subClassOf, ns.AssociateProfessor)) +g.add((ns.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) # Visualize the results for s, p, o in g: @@ -140,4 +136,4 @@ # Validation. Do not remove r.validate_task_06_04(g) -r.save_report("_Task_06") +r.save_report("_Task_06") \ No newline at end of file From 933847dec10be9ebf0eabc1fdd819321b638bd8a Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 10 Dec 2025 18:02:17 +0100 Subject: [PATCH 11/12] Add files via upload --- Assignment4/Riddhi_Chitkara_24C023/task06.py | 69 ++++++++++---------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/Assignment4/Riddhi_Chitkara_24C023/task06.py b/Assignment4/Riddhi_Chitkara_24C023/task06.py index 7456c2f5..998a1ec8 100644 --- a/Assignment4/Riddhi_Chitkara_24C023/task06.py +++ b/Assignment4/Riddhi_Chitkara_24C023/task06.py @@ -4,12 +4,12 @@ Automatically generated by Colab. Original file is located at - https://colab.research.google.com/drive/1dfYicakDhiNemXTLeXsdSi7fGggV4PGK + https://colab.research.google.com/drive/1bA_L6pJlymA__5jF1fTZm13By8nys_y- **Task 06: Modifying RDF(s)** """ -!pip install rdflib +#pip install rdflib import urllib.request url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' urllib.request.urlretrieve(url, 'validation.py') @@ -34,10 +34,11 @@ """**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 -ontology = Namespace("http://www.oeg-upm.net/ontology#") -person = Namespace("http://www.oeg-upm.net/person#") -g.bind("ontology", ontology) -g.bind("person", person) +ontology = Namespace("http://www.oeg-upm.net/Ontology#") +person = Namespace("http://oeg.fi.upm.es/def/people#") +g.namespace_manager.bind("ontology", ontology) +g.namespace_manager.bind("person", person) + """**TASK 6.1: Reproduce the taxonomy of classes shown in slide 34 in class (all the classes under "Vocabulario", Slidedeck: 01a.RDF(s)-SPARQL). Add labels for each of them as they are in the diagram (exactly) with no language tags. Remember adding the correct datatype (xsd:String) when appropriate** @@ -46,24 +47,24 @@ # TO DO g.remove((None, None, None)) -g.add((ns.Person, RDF.type, RDFS.Class)) -g.add((ns.Person, RDFS.label, Literal("Person", datatype=XSD.string))) +g.add((person.Person, RDF.type, RDFS.Class)) +g.add((person.Person, RDFS.label, Literal("Person", datatype=XSD.string))) -g.add((ns.Professor, RDF.type, RDFS.Class)) -g.add((ns.Professor, RDFS.subClassOf, ns.Person)) -g.add((ns.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) +g.add((person.Professor, RDF.type, RDFS.Class)) +g.add((person.Professor, RDFS.subClassOf, person.Person)) +g.add((person.Professor, RDFS.label, Literal("Professor", datatype=XSD.string))) -g.add((ns.FullProfessor, RDF.type, RDFS.Class)) -g.add((ns.FullProfessor, RDFS.subClassOf, ns.Professor)) -g.add((ns.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) +g.add((person.FullProfessor, RDF.type, RDFS.Class)) +g.add((person.FullProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.FullProfessor, RDFS.label, Literal("FullProfessor", datatype=XSD.string))) -g.add((ns.AssociateProfessor, RDF.type, RDFS.Class)) -g.add((ns.AssociateProfessor, RDFS.subClassOf, ns.Professor)) -g.add((ns.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) +g.add((person.AssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.AssociateProfessor, RDFS.subClassOf, person.Professor)) +g.add((person.AssociateProfessor, RDFS.label, Literal("AssociateProfessor", datatype=XSD.string))) -g.add((ns.InterimAssociateProfessor, RDF.type, RDFS.Class)) -g.add((ns.InterimAssociateProfessor, RDFS.subClassOf, ns.AssociateProfessor)) -g.add((ns.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) +g.add((person.InterimAssociateProfessor, RDF.type, RDFS.Class)) +g.add((person.InterimAssociateProfessor, RDFS.subClassOf, person.AssociateProfessor)) +g.add((person.InterimAssociateProfessor, RDFS.label, Literal("InterimAssociateProfessor", datatype=XSD.string))) # Visualize the results for s, p, o in g: @@ -75,20 +76,20 @@ """**TASK 6.2: Add the 3 properties shown in slide 36. Add labels for each of them (exactly as they are in the slide, with no language tags), and their corresponding domains and ranges using RDFS. Remember adding the correct datatype (xsd:String) when appropriate. If a property has no range, make it a literal (string)**""" # TO DO -g.add((ns.hasColleague, RDF.type, RDF.Property)) -g.add((ns.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) -g.add((ns.hasColleague, RDFS.domain, ns.Person)) -g.add((ns.hasColleague, RDFS.range, ns.Person)) - -g.add((ns.hasName, RDF.type, RDF.Property)) -g.add((ns.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) -g.add((ns.hasName, RDFS.domain, ns.Person)) -g.add((ns.hasName, RDFS.range, RDFS.Literal)) - -g.add((ns.hasHomePage, RDF.type, RDF.Property)) -g.add((ns.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) -g.add((ns.hasHomePage, RDFS.domain, ns.FullProfessor)) -g.add((ns.hasHomePage, RDFS.range, RDFS.Literal)) +g.add((person.hasColleague, RDF.type, RDF.Property)) +g.add((person.hasColleague, RDFS.label, Literal("hasColleague", datatype=XSD.string))) +g.add((person.hasColleague, RDFS.domain, person.Person)) +g.add((person.hasColleague, RDFS.range, person.Person)) + +g.add((person.hasName, RDF.type, RDF.Property)) +g.add((person.hasName, RDFS.label, Literal("hasName", datatype=XSD.string))) +g.add((person.hasName, RDFS.domain, person.Person)) +g.add((person.hasName, RDFS.range, RDFS.Literal)) + +g.add((person.hasHomePage, RDF.type, RDF.Property)) +g.add((person.hasHomePage, RDFS.label, Literal("hasHomePage", datatype=XSD.string))) +g.add((person.hasHomePage, RDFS.domain, person.FullProfessor)) +g.add((person.hasHomePage, RDFS.range, RDFS.Literal)) # Visualize the results for s, p, o in g: print(s,p,o) From e301b651c8b8554a22473f35a1ed24c4ba814fba Mon Sep 17 00:00:00 2001 From: riddhichitkara Date: Wed, 10 Dec 2025 18:03:58 +0100 Subject: [PATCH 12/12] Add files via upload --- Assignment4/Riddhi_Chitkara_24C023/task07.py | 21 +++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Assignment4/Riddhi_Chitkara_24C023/task07.py b/Assignment4/Riddhi_Chitkara_24C023/task07.py index 0932b93b..d6573390 100644 --- a/Assignment4/Riddhi_Chitkara_24C023/task07.py +++ b/Assignment4/Riddhi_Chitkara_24C023/task07.py @@ -9,7 +9,7 @@ **Task 07: Querying RDF(s)** """ -# !pip install rdflib +# pip install rdflib import urllib.request url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py' urllib.request.urlretrieve(url, 'validation.py') @@ -33,16 +33,13 @@ # TO DO result = [] #list of tuples -classes = set(g.subjects(RDF.type, RDFS.Class)) -classes.update(g.subjects(RDFS.subClassOf, None)) -for c in classes: - superclasses = list(g.objects(c, RDFS.subClassOf)) - if not superclasses: - result.append((c, None)) - else: - for sc in superclasses: - result.append((c, sc)) -# Visualize the results +for clase in g.subjects(RDF.type, RDFS.Class): + super = g.value(clase, RDFS.subClassOf) + if super: + result.append((clase, super)) + else: + result.append((clase,None)) +print(result) for r in result: print(r) @@ -151,4 +148,4 @@ def is_person_subclass(cls): ## Validation: Do not remove report.validate_07_04(g,query) -report.save_report("_Task_07") +report.save_report("_Task_07") \ No newline at end of file