diff --git a/Assignment4/javierartegapuell-050388463/task06.py b/Assignment4/javierartegapuell-050388463/task06.py new file mode 100644 index 00000000..78634093 --- /dev/null +++ b/Assignment4/javierartegapuell-050388463/task06.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +"""Task06.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1HWBVS7CQVXSxOLzl_XCouK48N-UjAGHW + +**Task 06: Modifying RDF(s)** +""" + +!pip install rdflib +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials" + +"""Read the RDF file as shown in class""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False) +g.parse(github_storage+"/rdf/example5.rdf", format="xml") + +"""Create a new class named Researcher""" + +ns = Namespace("http://somewhere#") +g.add((ns.Researcher, RDF.type, RDFS.Class)) +for s, p, o in g: + print(s,p,o) + +"""**TASK 6.1: Create a new class named "University"** + + + + + +""" + +g.add((ns.University, RDF.type, RDFS.Class)) +for s, p, o in g: + print(s,p,o) + +"""**TASK 6.2: Add "Researcher" as a subclass of "Person"**""" + +g.add((ns.Researcher, RDFS.subClassOf, ns.Person)) +for s, p, o in g: + print(s,p,o) + +"""**TASK 6.3: Create a new individual of Researcher named "Jane Smithers"**""" + +fullName = Literal("Jane Smithers") +EX = Namespace("http://somewhere#Researcher") +VCARD = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") + +resource = (EX.JaneSmithers, VCARD.FN, fullName) + +print(resource) + +g.add(resource) + +for s, p, o in g: + print(s,p,o) + +"""**TASK 6.4: Add to the individual JaneSmithers the email address, fullName, given and family names. Use the https://schema.org vocabulary**""" + +EX = Namespace("http://somewhere") +SCHEMA = Namespace("https://schema.org/") + +g.add((EX.JaneSmithers, SCHEMA.EMAIL, Literal("jsmithers@example.org"))) +g.add((EX.JaneSmithers, SCHEMA.FN, Literal("Jane Smithers"))) +g.add((EX.JaneSmithers, SCHEMA.Given, Literal("Jane"))) +g.add((EX.JaneSmithers, SCHEMA.Family, Literal("Smithers"))) + +for s, p, o in g: + print(s,p,o) + +"""**TASK 6.5: Add UPM as the university where John Smith works. Use the "https://example.org/ namespace**""" + +EX = Namespace("http://example.org/") +johnURI = EX.JohnSmith + +VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#") +universityName = Literal("UPM") + +g.add((EX.johnURI, VCARD.UNI, universityName)) + +for subj, pred, obj in g: + print(subj,pred,obj) + +"""**Task 6.6: Add that Jown knows Jane using the FOAF vocabulary. Make sure the relationship exists.**""" + +from rdflib import FOAF + +EX = Namespace("http://example.org/") + +g.add((EX.Jown, FOAF.knows, EX.JaneSmithers)) +g.add((EX.JaneSmithers, VCARD.FN, Literal('Jane Smithers'))) +g.add((EX.Jown, VCARD.FN, Literal('Jown'))) + +for subj, pred, obj in g: + print(subj,pred,obj) + +from rdflib.plugins.sparql import prepareQuery + +query = prepareQuery(''' + SELECT ?Subject ?Subject2 WHERE { + ?Subject foaf:knows ?Subject2. + } +''', initNs={"foaf": FOAF}) + +print("Checking relationships:") +for r in g.query(query): + print(f"{r.Subject} knows {r.Subject2}") \ No newline at end of file diff --git a/Assignment4/javierartegapuell-050388463/task07.py b/Assignment4/javierartegapuell-050388463/task07.py new file mode 100644 index 00000000..af48d26b --- /dev/null +++ b/Assignment4/javierartegapuell-050388463/task07.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- +"""Task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1IEE3lmBXqvNbV2xwiv1lnTx3_pDJmx-r + +**Task 07: Querying RDF(s)** +""" + +!pip install rdflib +github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials" + +"""First let's read the RDF file""" + +from rdflib import Graph, Namespace, Literal +from rdflib.namespace import RDF, RDFS +g = Graph() +g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False) +g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False) +g.parse(github_storage+"/rdf/example6.rdf", format="xml") + +for subj, pred, obj in g: + print(subj,pred,obj) + +"""**TASK 7.1: List all subclasses of "LivingThing" with RDFLib and SPARQL**""" + +from rdflib.plugins.sparql import prepareQuery +from rdflib import Namespace + +ns = Namespace("http://somewhere#") + +q1 = prepareQuery(''' + SELECT ?Subclass WHERE { + ?Subclass rdfs:subClassOf ns:LivingThing. + } + ''', + initNs = { "rdfs": RDFS, "ns": ns } +) + +for r in g.query(q1): + print(r.Subclass) + +"""**TASK 7.2: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)** + +""" + +from rdflib.plugins.sparql import prepareQuery +from rdflib import Namespace + +ns = Namespace("http://somewhere#") + +# SPARQL query to list all individuals of Person and its subclasses!! +q2 = prepareQuery(''' + SELECT ?Subject WHERE { + ?Subject rdf:type ?type . + ?type rdfs:subClassOf* ns:Person . + } + ''', + initNs = { "rdfs": RDFS, "rdf": RDF, "ns": ns } +) + +for r in g.query(q2): + print(r.Subject) + +"""**TASK 7.3: List all individuals of just "Person" or "Animal". You do not need to list the individuals of the subclasses of person (in SPARQL only)** + +""" + +# TO DO +# Visualize the results + +q3 = prepareQuery(''' + SELECT ?Subject WHERE { + { + ?Subject rdf:type ns:Person . + } + UNION + { + ?Subject rdf:type ?type . + ?type rdfs:subClassOf* ns:Animal . + } + } + ''', + initNs = { "rdfs": RDFS, "rdf": RDF, "ns": ns } +) + +for r in g.query(q3): + print(r.Subject) + +"""**TASK 7.4: List the name of the persons who know Rocky (in SPARQL only)**""" + +from rdflib import FOAF + +q4 = prepareQuery(''' + SELECT ?Name WHERE { + ?Name foaf:knows ns:RockySmith + } + ''', + initNs = { "rdfs": RDFS, "foaf": FOAF, "ns": ns } +) + +for r in g.query(q4): + print(f"{r.Name} knows Rocky Smith") + +"""**Task 7.5: List the name of those animals who know at least another animal in the graph (in SPARQL only)**""" + +from rdflib import FOAF + + +q5 = prepareQuery(''' + SELECT DISTINCT ?AnimalName WHERE { + ?AnimalName foaf:knows ?OtherAnimal . + ?AnimalName rdf:type ns:Animal . + ?OtherAnimal rdf:type ns:Animal + } +''', +initNs={"foaf": FOAF, "ns": ns} +) + +for r in g.query(q5): + print(r.AnimalName) + +"""**Task 7.6: List the age of all living things in descending order (in SPARQL only)**""" + +q6 = prepareQuery(''' + SELECT ?LivThing ?age WHERE { + ?LivThing foaf:age ?age. + } + ORDER BY DESC(?age) +''', + initNs = { "rdfs": RDFS, "ns": ns } +) + +for r in g.query(q6): + print(f"{r.LivThing} has an age of {r.age}") \ No newline at end of file