diff --git a/Assignment2/fdzdani-fromRDFtoJSON-LD.jsonld b/Assignment2/fdzdani-fromRDFtoJSON-LD.jsonld new file mode 100644 index 00000000..aefb5081 --- /dev/null +++ b/Assignment2/fdzdani-fromRDFtoJSON-LD.jsonld @@ -0,0 +1,19 @@ +@prefix ex: . +@prefix xsd: . + +ex:ClassRoom03 + ex:contains ex:Sensor347 ; + ex:contains ex:Table322 . + +ex:Sensor347 + ex:hasMeasurement ex:Measurement8432 . + +ex:Measurement8432 + ex:hasTemperature "29"^^xsd:integer ; + ex:atTime "2022-09-12T12:01:12"^^xsd:dateTime . + +ex:Table322 + ex:hasOwner ex:ETSIInformaticos . + +ex:ETSIInformations + ex:hasName "ETSI Informaticos"^^xsd:string . diff --git a/Assignment2/fdzdani.png b/Assignment2/fdzdani.png new file mode 100644 index 00000000..0a59327a Binary files /dev/null and b/Assignment2/fdzdani.png differ diff --git a/Assignment4/fdzdani-190401/task06.py b/Assignment4/fdzdani-190401/task06.py new file mode 100644 index 00000000..8143291d --- /dev/null +++ b/Assignment4/fdzdani-190401/task06.py @@ -0,0 +1,95 @@ +# -*- coding: utf-8 -*- +"""Task06.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1L6O9Hd29jmBe8anyMBpzC59E8k4fzful + +**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"** + +""" + +# TO DO +g.add((ns.University, RDF.type, RDFS.Class)) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +"""**TASK 6.2: Add "Researcher" as a subclass of "Person"**""" + +# TO DO +g.add((ns.Researcher, RDFS.subClassOf, ns.Person)) + +# Visualize the results +for s, p, o in g: + print(s,p,o) + +"""**TASK 6.3: Create a new individual of Researcher named "Jane Smithers"**""" + +# TO DO +vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#") +g.add((ns.JaneSmithers, vcard.FN, Literal("Jane Smithers"))) +g.add((ns.JaneSmithers, RDF.type, ns.Researcher)) + +# Visualize the results +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**""" + +# TO DO +schema = Namespace("https://schema.org/") +g.add((schema.JaneSmitehers, vcard.EMAIL, Literal("janesmithers@example.com"))) +g.add((schema.JaneSmitehers, vcard.FN, Literal("Jane Smithers"))) +g.add((schema.JaneSmitehers, vcard.Given, Literal("Jane"))) +g.add((schema.JaneSmitehers, vcard.Family, Literal("Smithers"))) + +# Visualize the results +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**""" + +# TO DO +ex = Namespace("http://example.org/") +upm = ex.UPM +g.add((upm, RDF.type, ns.University)) +g.add((ns.JohnSmith, ex.worksAt, upm)) + +# Visualize the results +for s, p, o in g: + print(s, p, o) + +"""**Task 6.6: Add that Jown knows Jane using the FOAF vocabulary. Make sure the relationship exists.**""" + +# TO DO +from rdflib import FOAF +g.add((ns.JohnSmith, FOAF.knows, schema.JaneSmitehers)) + +# Visualize the results +for s, p, o in g: + print(s, p, o) diff --git a/Assignment4/fdzdani-190401/task07.py b/Assignment4/fdzdani-190401/task07.py new file mode 100644 index 00000000..8eb3f0ce --- /dev/null +++ b/Assignment4/fdzdani-190401/task07.py @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +"""Task07.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1kruGN2O8GBMzSSEPT_FFaduPyAhCF6Hn + +**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 s, p, o in g: + print(s,p,o) + +"""**TASK 7.1: List all subclasses of "LivingThing" with RDFLib and SPARQL**""" + +# TO DO +from rdflib.plugins.sparql import prepareQuery +vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#") +q1 = prepareQuery(""" +PREFIX rdfs: +PREFIX ns: + +SELECT ?subclass +WHERE { + ?subclass rdfs:subClassOf ns:LivingThing . +} +""", + initNs = { "vcard": vcard} +) + +# Visualize the results +for r in g.query(q1): + print(r) + +"""**TASK 7.2: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)** + +""" + +# TO DO +q2 = prepareQuery(""" +PREFIX rdfs: +PREFIX ns: + +SELECT ?individual +WHERE { + ?individual a ns:Person . +} +""", + initNs = { "vcard": vcard} +) + +# Visualize the results +for r in g.query(q2): + print(r) + +"""**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 +q3 = prepareQuery(""" +PREFIX ns: + +SELECT ?individual1 ?individual2 +WHERE { + ?individual1 a ns:Person . + ?individual2 a ns:Animal . +} +""", + initNs = { "vcard": vcard} +) + +# Visualize the results +for r in g.query(q3): + print(r) + +"""**TASK 7.4: List the name of the persons who know Rocky (in SPARQL only)**""" + +# TO DO +from rdflib import FOAF +q4 = prepareQuery(""" +PREFIX vcard: +PREFIX ns: + +SELECT ?person +WHERE { + ?person a ns:Person . + ?person foaf:knows ?Rocky . +} +""", initNs={"vcard": vcard, "foaf": FOAF}) + +# Visualize the results +for r in g.query(q4): + print(r) + +"""**Task 7.5: List the name of those animals who know at least another animal in the graph (in SPARQL only)**""" + +# TO DO +q5 = prepareQuery(""" +PREFIX vcard: +PREFIX ns: + +SELECT ?animal +WHERE { + ?animal a ns:Animal . + ?animal foaf:knows ?otherAnimal . + ?otherAnimal a ns:Animal . +} +""", initNs={"vcard": vcard, "foaf": FOAF}) + +# Visualize the results +for r in g.query(q5): + print(r) + +"""**Task 7.6: List the age of all living things in descending order (in SPARQL only)**""" + +# TO DO +q6 = prepareQuery(""" +PREFIX ns: + +SELECT ?living ?age +WHERE { + ?living a ns:LivingThing . + ?LivingThing foaf:age ?age . +} +ORDER BY DESC(?age) +""", initNs={"foaf": FOAF}) + +# Visualize the results +for r in g.query(q6): + print(r)