diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.py" new file mode 100644 index 00000000..7dbf7b56 --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task06.py" @@ -0,0 +1,584 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nOOPLCHF7hLB" + }, + "source": [ + "**Task 06: Modifying RDF(s)**" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "Yl9npCt8n6m-", + "outputId": "576c3c73-0cf1-4cdb-c440-09a68c4da5ed", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: rdflib in /usr/local/lib/python3.12/dist-packages (7.3.0)\n", + "Requirement already satisfied: pyparsing<4,>=2.1.0 in /usr/local/lib/python3.12/dist-packages (from rdflib) (3.2.5)\n" + ] + } + ], + "source": [ + "!pip install rdflib\n", + "import urllib.request\n", + "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", + "urllib.request.urlretrieve(url, 'validation.py')\n", + "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XY7aPc86Bqoo" + }, + "source": [ + "Import RDFLib main methods" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "id": "9ERh415on7kF" + }, + "outputs": [], + "source": [ + "from rdflib import Graph, Namespace, Literal, XSD\n", + "from rdflib.namespace import RDF, RDFS\n", + "from validation import Report\n", + "g = Graph()\n", + "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", + "r = Report()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gM3DASkTQQ5Y" + }, + "source": [ + "Create a new class named Researcher" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "6vtudax8Xb7b", + "outputId": "78da9a2c-a7c6-4d23-eb2b-0b6a3865d421", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n" + ] + } + ], + "source": [ + "ns = Namespace(\"http://mydomain.org#\")\n", + "g.add((ns.Researcher, RDF.type, RDFS.Class))\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "markdown", + "source": [ + "**Task 6.0: Create new prefixes for \"ontology\" and \"person\" as shown in slide 14 of the Slidedeck 01a.RDF(s)-SPARQL shown in class.**" + ], + "metadata": { + "id": "ZX8w9jnV5Xhp" + } + }, + { + "cell_type": "code", + "source": [ + "# this task is validated in the next step\n", + "ont = Namespace(\"http://oeg.fi.upm.es/def/ontology#\")\n", + "per = Namespace(\"http://oeg.fi.upm.es/def/people#\")" + ], + "metadata": { + "id": "b1ZQgYgB5vi7" + }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qp1oe2Eddsvo" + }, + "source": [ + "**TASK 6.1: Reproduce the taxonomy of classes shown in slide 34 in class (all the classes under \"Vocabulario\", Slidedeck: 01a.RDF(s)-SPARQL). Add labels for each of them as they are in the diagram (exactly) with no language tags. Remember adding the correct datatype (xsd:String) when appropriate**\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "pnsrsgRUWF3A", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "0991abe3-0b11-464c-fb96-2a5c7bb481ba" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Persona\n", + "g.add((per.Person, RDF.type, RDFS.Class))\n", + "g.add((per.Person, RDFS.label, Literal(\"Person\", datatype=XSD.string)))\n", + "#Profesor\n", + "g.add((per.Professor, RDF.type, RDFS.Class))\n", + "g.add((per.Professor, RDFS.subClassOf, per.Person))\n", + "g.add((per.Professor, RDFS.label, Literal(\"Professor\", datatype=XSD.string)))\n", + "#Professor COmpleto\n", + "g.add((per.FullProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.FullProfessor, RDFS.subClassOf, per.Professor))\n", + "g.add((per.FullProfessor, RDFS.label, Literal(\"FullProfessor\", datatype=XSD.string)))\n", + "#Profesor Asociado\n", + "g.add((per.AssociateProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.AssociateProfessor, RDFS.subClassOf, per.Professor))\n", + "g.add((per.AssociateProfessor, RDFS.label, Literal(\"AssociateProfessor\", datatype=XSD.string)))\n", + "#Profesor INterino\n", + "g.add((per.InterimAssociateProfessor, RDF.type, RDFS.Class))\n", + "g.add((per.InterimAssociateProfessor, RDFS.subClassOf, per.AssociateProfessor))\n", + "g.add((per.InterimAssociateProfessor, RDFS.label, Literal(\"InterimAssociateProfessor\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_01(g)" + ], + "metadata": { + "id": "HdRPyusrjIuB", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "903e2013-11a1-4f1f-a818-be673e3ef670" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The namespace is correct for http://oeg.fi.upm.es/def/people#Professor\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#Person\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "The namespace is correct for http://oeg.fi.upm.es/def/people#FullProfessor\n", + "Hierarchy OK\n", + "TASK 6.1 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MXBqtBkJd22I" + }, + "source": [ + "**TASK 6.2: Add the 3 properties shown in slide 36. Add labels for each of them (exactly as they are in the slide, with no language tags), and their corresponding domains and ranges using RDFS. Remember adding the correct datatype (xsd:String) when appropriate. If a property has no range, make it a literal (string)**" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "53hZNtXsXCNq", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8d4dec5f-7ad2-4109-8729-4c9329e40cbd" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#label hasColleague\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#label hasName\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#label hasHomePage\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#range http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Propiedad hasName\n", + "g.add((per.hasName, RDF.type, RDF.Property))\n", + "g.add((per.hasName, RDFS.domain, per.Person))\n", + "g.add((per.hasName, RDFS.range, RDFS.Literal))\n", + "g.add((per.hasName, RDFS.label, Literal(\"hasName\", datatype=XSD.string)))\n", + "#Propiedad hasColleague\n", + "g.add((per.hasColleague, RDF.type, RDF.Property))\n", + "g.add((per.hasColleague, RDFS.domain, per.Person))\n", + "g.add((per.hasColleague, RDFS.range, per.Person))\n", + "g.add((per.hasColleague, RDFS.label, Literal(\"hasColleague\", datatype=XSD.string)))\n", + "#Propiedad hasHomePage\n", + "g.add((per.hasHomePage, RDF.type, RDF.Property))\n", + "g.add((per.hasHomePage, RDFS.domain, per.FullProfessor))\n", + "g.add((per.hasHomePage, RDFS.range, RDFS.Literal))\n", + "g.add((per.hasHomePage, RDFS.label, Literal(\"hasHomePage\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_02(g)" + ], + "metadata": { + "id": "pd2mE-vGaxbu", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "bd69c351-22f1-435b-c054-f7a9e12682f5" + }, + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 6.2 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OGct6k7Ld9O0" + }, + "source": [ + "**TASK 6.3: Create the individuals shown in slide 36 under \"Datos\". Link them with the same relationships shown in the diagram.\"**" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "jbMMSHSZcFcf", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c7acbcb1-2bf2-493a-b441-248382db1c60" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2000/01/rdf-schema#label Oscar\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#label hasColleague\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#label hasHomePage\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasHomePage http://www.oeg-upm.net/\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Raul\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Asun\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasName Oscar Corcho García\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#range http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#label hasName\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/2000/01/rdf-schema#label Raul\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/2000/01/rdf-schema#label Asun\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Namespace\n", + "data = Namespace(\"http://oeg.fi.upm.es/resource/person/\")\n", + "#Oscar\n", + "g.add((data.Oscar, RDF.type, per.AssociateProfessor))\n", + "g.add((data.Oscar, per.hasColleague, data.Asun))\n", + "g.add((data.Oscar, per.hasName, Literal(\"Oscar Corcho García\", datatype=XSD.string)))\n", + "g.add((data.Oscar, RDFS.label, Literal(\"Oscar\", datatype=XSD.string)))\n", + "#Asun\n", + "g.add((data.Asun, RDF.type, per.FullProfessor))\n", + "g.add((data.Asun, per.hasColleague, data.Raul))\n", + "g.add((data.Asun, per.hasHomePage, Literal(\"http://www.oeg-upm.net/\", datatype=XSD.string)))\n", + "g.add((data.Asun, RDFS.label, Literal(\"Asun\", datatype=XSD.string)))\n", + "#Raul\n", + "g.add((data.Raul, RDF.type, per.InterimAssociateProfessor))\n", + "g.add((data.Raul, RDFS.label, Literal(\"Raul\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "r.validate_task_06_03(g)" + ], + "metadata": { + "id": "-3q4Wv2EfYew", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "bf5b5a41-93d8-469d-aea1-d3416600ed3c" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 6.3 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tD383J__eHfV" + }, + "source": [ + "**TASK 6.4: Add to the individual person:Oscar the email address, given and family names. Use the properties already included in example 4 to describe Jane and John (https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials/rdf/example4.rdf). Do not import the namespaces, add them manually**\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "hWmwlAfBcgN-", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "a5f53eaa-4d3c-421d-cdf7-d2246714a5ec" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2000/01/rdf-schema#label Oscar\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#label hasColleague\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#label Professor\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#label FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#label hasHomePage\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://mydomain.org#Researcher http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasHomePage http://www.oeg-upm.net/\n", + "http://xmlns.com/foaf/0.1/eamil http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2001/XMLSchema#string\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Raul\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasColleague http://oeg.fi.upm.es/resource/person/Asun\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://oeg.fi.upm.es/def/people#hasName Oscar Corcho García\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Given http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2001/XMLSchema#string\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://xmlns.com/foaf/0.1/email ocorcho@fi.upm.es\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/2000/01/rdf-schema#range http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2001/vcard-rdf/3.0/Given Oscar\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Given http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Family http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2001/XMLSchema#string\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#label hasName\n", + "http://xmlns.com/foaf/0.1/email http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Datatype\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#label InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#range http://www.w3.org/2000/01/rdf-schema#Literal\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://www.w3.org/2000/01/rdf-schema#label AssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasColleague http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/def/people#hasName http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#hasHomePage http://www.w3.org/2000/01/rdf-schema#domain http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#AssociateProfessor\n", + "http://www.w3.org/2001/vcard-rdf/3.0/Family http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/1999/02/22-rdf-syntax-ns#Property\n", + "http://oeg.fi.upm.es/resource/person/Oscar http://www.w3.org/2001/vcard-rdf/3.0/Family Corcho García\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/2000/01/rdf-schema#label Raul\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Class\n", + "http://oeg.fi.upm.es/resource/person/Asun http://www.w3.org/2000/01/rdf-schema#label Asun\n", + "http://oeg.fi.upm.es/def/people#Professor http://www.w3.org/2000/01/rdf-schema#subClassOf http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/resource/person/Raul http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Person http://www.w3.org/2000/01/rdf-schema#label Person\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Namespace\n", + "foaf = Namespace(\"http://xmlns.com/foaf/0.1/\")\n", + "vcard = Namespace(\"http://www.w3.org/2001/vcard-rdf/3.0/\")\n", + "#Nuevas relaciones\n", + "g.add((vcard.Family, RDF.type, RDF.Property))\n", + "g.add((vcard.Family, RDFS.range, XSD.string))\n", + "g.add((vcard.Given, RDF.type, RDF.Property))\n", + "g.add((vcard.Given, RDFS.range, XSD.string))\n", + "g.add((foaf.email, RDF.type, RDFS.Datatype))\n", + "g.add((foaf.eamil, RDFS.range, XSD.string))\n", + "#Más info de Oscar\n", + "g.add((data.Oscar, vcard.Given, Literal(\"Oscar\", datatype=XSD.string)))\n", + "g.add((data.Oscar, vcard.Family, Literal(\"Corcho García\", datatype=XSD.string)))\n", + "g.add((data.Oscar, foaf.email, Literal(\"ocorcho@fi.upm.es\", datatype=XSD.string)))\n", + "# Visualize the results\n", + "for s, p, o in g:\n", + " print(s,p,o)" + ] + }, + { + "cell_type": "code", + "source": [ + "# Validation. Do not remove\n", + "r.validate_task_06_04(g)\n", + "r.save_report(\"_Task_06\")" + ], + "metadata": { + "id": "Y1NiIQMyfgyF", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "39343cb6-9199-47f8-ceef-f93e3579465c" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 6.4 OK\n" + ] + } + ] + } + ], + "metadata": { + "colab": { + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.py" new file mode 100644 index 00000000..22dfc676 --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/Task07.py" @@ -0,0 +1,529 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nOOPLCHF7hLB" + }, + "source": [ + "**Task 07: Querying RDF(s)**" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "id": "Yl9npCt8n6m-", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6048b058-a425-482b-e893-d08cabde2dba" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: rdflib in /usr/local/lib/python3.12/dist-packages (7.3.0)\n", + "Requirement already satisfied: pyparsing<4,>=2.1.0 in /usr/local/lib/python3.12/dist-packages (from rdflib) (3.2.5)\n" + ] + } + ], + "source": [ + "!pip install rdflib\n", + "import urllib.request\n", + "url = 'https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/refs/heads/master/Assignment4/course_materials/python/validation.py'\n", + "urllib.request.urlretrieve(url, 'validation.py')\n", + "github_storage = \"https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2025-2026/master/Assignment4/course_materials\"" + ] + }, + { + "cell_type": "code", + "source": [ + "from validation import Report" + ], + "metadata": { + "id": "FmnGjffDT92V" + }, + "execution_count": 36, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XY7aPc86Bqoo" + }, + "source": [ + "First let's read the RDF file" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "id": "9ERh415on7kF" + }, + "outputs": [], + "source": [ + "from rdflib import Graph, Namespace, Literal\n", + "from rdflib.namespace import RDF, RDFS\n", + "# Do not change the name of the variables\n", + "g = Graph()\n", + "g.namespace_manager.bind('ns', Namespace(\"http://somewhere#\"), override=False)\n", + "g.parse(github_storage+\"/rdf/data06.ttl\", format=\"TTL\")\n", + "report = Report()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qp1oe2Eddsvo" + }, + "source": [ + "**TASK 7.1a: For all classes, list each classURI. If the class belogs to another class, then list its superclass.**\n", + "**Do the exercise in RDFLib returning a list of Tuples: (class, superclass) called \"result\". If a class does not have a super class, then return None as the superclass**" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "id": "tRcSWuMHOXBl", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5a78292b-bf5c-4e7a-c205-fa060666a05c" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'), None)\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Animal'), None)\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Student'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Person'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#FullProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#Professor'))\n", + "(rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#InterimAssociateProfessor'), rdflib.term.URIRef('http://oeg.fi.upm.es/def/people#AssociateProfessor'))\n" + ] + } + ], + "source": [ + "# TO DO\n", + "#Lista classUri\n", + "result=[]\n", + "for sb in g.subjects(RDF.type, RDFS.Class):\n", + " sc=None\n", + " for Sc in g.objects(sb,RDFS.subClassOf):\n", + " sc=Sc\n", + " result.append((sb,sc))\n", + "for cl, sc in result:\n", + " short_c = g.namespace_manager.normalizeUri(cl)\n", + " short_sc = g.namespace_manager.normalizeUri(sc) if sc else None\n", + "for r in result:\n", + " print(r)" + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_1a(result)" + ], + "metadata": { + "id": "uvEpQQrTlMPH", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "a3263309-6ed8-4115-a52c-a4fc5f313e7d" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.1a OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**TASK 7.1b: Repeat the same exercise in SPARQL, returning the variables ?c (class) and ?sc (superclass)**" + ], + "metadata": { + "id": "kbY-jqw6klr9" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"Select ?c ?sc WHERE {?c rdf:type rdfs:Class. OPTIONAL {?c rdfs:subClassOf ?sc.}}\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.c, r.sc)\n" + ], + "metadata": { + "id": "NGAG7l9UklMC", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "911fc9bf-649b-4e26-b505-e570aaaf6f3d" + }, + "execution_count": 40, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Person None\n", + "http://oeg.fi.upm.es/def/people#Animal None\n", + "http://oeg.fi.upm.es/def/people#Professor http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#Student http://oeg.fi.upm.es/def/people#Person\n", + "http://oeg.fi.upm.es/def/people#FullProfessor http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#AssociateProfessor http://oeg.fi.upm.es/def/people#Professor\n", + "http://oeg.fi.upm.es/def/people#InterimAssociateProfessor http://oeg.fi.upm.es/def/people#AssociateProfessor\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_1b(query,g)" + ], + "metadata": { + "id": "9zf4vgVHlKR3", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "fe8922de-8c50-4c09-a02c-878d8331817c" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.1b OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gM3DASkTQQ5Y" + }, + "source": [ + "**TASK 7.2a: List all individuals of \"Person\" with RDFLib (remember the subClasses). Return the individual URIs in a list called \"individuals\"**\n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "id": "LiKSPHRzS-XJ", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "ca690103-c288-4cd5-e1e5-915db0971675" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ], + "source": [ + "ns = Namespace(\"http://oeg.fi.upm.es/def/people#\")\n", + "\n", + "# variable to return\n", + "individuals = []\n", + "def subclases(cl):\n", + " sb=[]\n", + " for s,p,o in g.triples((None,RDFS.subClassOf,cl)):\n", + " sb.append(s)\n", + " sb += subclases(s)\n", + " return sb\n", + "clases= subclases(ns.Person)\n", + "clases.append(ns.Person)\n", + "for cl in clases:\n", + " for s,p,o in g.triples((None,RDF.type,cl)):\n", + " individuals.append(s)\n", + "# visualize results\n", + "for i in individuals:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "source": [ + "# validation. Do not remove\n", + "report.validate_07_02a(individuals)" + ], + "metadata": { + "id": "ONrAls5uiX1G", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6de60947-5fec-4cd1-f11e-184cbb99538a" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.2a OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**TASK 7.2b: Repeat the same exercise in SPARQL, returning the individual URIs in a variable ?ind**" + ], + "metadata": { + "id": "up-952A-za7A" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"SELECT ?ind WHERE{?c rdfs:subClassOf* . ?ind a ?c}\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.ind)\n", + "# Visualize the results" + ], + "metadata": { + "id": "ipYiEVbTzbR0", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5bd40168-4205-4e6e-95b1-1a40c701f64d" + }, + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_02b(g, query)" + ], + "metadata": { + "id": "s-Hu2LxRjUQt", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "40f2fb8d-714b-4c9d-da6d-6aa256068f6b" + }, + "execution_count": 45, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.2b OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**TASK 7.3: List the name and type of those who know Rocky (in SPARQL only). Use name and type as variables in the query**" + ], + "metadata": { + "id": "3NyI7M2VNr9R" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"\"\"SELECT ?name ?type WHERE{\n", + " ?name .\n", + " ?name rdf:type ?type.}\"\"\"\n", + "# TO DO\n", + "# Visualize the results\n", + "for r in g.query(query):\n", + " print(r.name, r.type)\n" + ], + "metadata": { + "id": "I_CNoIKdNpbx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "b8b6082d-6242-49b0-f167-62c2b8303205" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun http://oeg.fi.upm.es/def/people#FullProfessor\n", + "http://oeg.fi.upm.es/def/people#Raul http://oeg.fi.upm.es/def/people#InterimAssociateProfessor\n", + "http://oeg.fi.upm.es/def/people#Fantasma http://oeg.fi.upm.es/def/people#Animal\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_03(g, query)" + ], + "metadata": { + "id": "Zf3JS7tEhS2t", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "48c53bb4-bde3-45ef-e1a5-bafa4eb06b0f" + }, + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.3 OK\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**Task 7.4: List the name of those entities who have a colleague with a dog, or that have a collegue who has a colleague who has a dog (in SPARQL). Return the results in a variable called name**" + ], + "metadata": { + "id": "kyjGsyxDPa2C" + } + }, + { + "cell_type": "code", + "source": [ + "query = \"\"\"\n", + "PREFIX people: \n", + "Select ?name WHERE{\n", + " { ?name people:hasColleague ?c1.\n", + " ?c1 people:ownsPet ?dog.}\n", + " UNION{\n", + " ?name people:hasColleague ?c2.\n", + " ?c2 people:hasColleague ?c3.\n", + " ?c3 people:ownsPet ?dog.}\n", + " }\n", + "\"\"\"\n", + "\n", + "for r in g.query(query):\n", + " print(r.name)\n", + "\n", + "# TO DO\n", + "# Visualize the results" + ], + "metadata": { + "id": "yoVwVZUAPaLm", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "835ead6c-de3c-4c52-f709-eebbf4888938" + }, + "execution_count": 48, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "http://oeg.fi.upm.es/def/people#Asun\n", + "http://oeg.fi.upm.es/def/people#Oscar\n", + "http://oeg.fi.upm.es/def/people#Raul\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Validation: Do not remove\n", + "report.validate_07_04(g,query)\n", + "report.save_report(\"_Task_07\")" + ], + "metadata": { + "id": "zcTZE7ngj2fc", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f750091a-c97f-4f31-fd96-e2827aac782e" + }, + "execution_count": 51, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TASK 7.4 OK\n" + ] + } + ] + } + ], + "metadata": { + "colab": { + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git "a/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" new file mode 100644 index 00000000..6024e0b5 --- /dev/null +++ "b/Assignment4/Assigment4_Gonzalo_Hern\303\241ndez_24C060/validation.py" @@ -0,0 +1,258 @@ +from rdflib import Graph, Namespace, Literal, XSD +from rdflib.namespace import RDF, RDFS + +VCARD = Namespace("http://www.w3.org/2001/vcard-rdf/3.0/") +FOAF = Namespace("http://xmlns.com/foaf/0.1/") + +class Report: + def __init__(self): + self.__report = "" + + def domain_and_range_correspond_to_input(self, g,propertyURI,correct_domain,correct_range): + domain = g.value(subject=propertyURI, predicate=RDFS.domain) + range = g.value(subject=propertyURI, predicate=RDFS.range) + if domain is None or range is None: + return False + if domain != correct_domain or range != correct_range: + return False + return True + + def does_it_have_label(self, g, entity): + label = g.value(subject=entity, predicate=RDFS.label) + if label is None: + return False + return True + + def namespace_is_correct_class(self, entity): + if entity is None: + return False + if "http://oeg.fi.upm.es/def/people#" not in entity: + return False + return True + + def namespace_is_correct_instance(self, entity): + if entity is None: + return False + if "http://oeg.fi.upm.es/resource/person/" not in entity: + return False + return True + + def is_subClassOf(self, g, subClass, superClass): + candidate = g.value(subject=subClass, predicate=RDFS.subClassOf, object=None) + if candidate is None or superClass not in candidate: + return False + return True + + def __add_to_report(self, message): + print(message) + self.__report = self.__report + message + "\n" + + def validate_task_06_01(self, g): + error = False + professorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Professor", datatype=XSD.string)) + personURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Person", datatype=XSD.string)) + associateProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("AssociateProfessor", datatype=XSD.string)) + interimURI = g.value(subject=None, predicate=RDFS.label, object=Literal("InterimAssociateProfessor", datatype=XSD.string)) + fProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("FullProfessor", datatype=XSD.string)) + classes = [professorURI,personURI,associateProfessorURI,interimURI, fProfessorURI] + # check namespace and existence + for i in classes: + if i is None: + self.__add_to_report("ERROR: One of the classes is missing its correct label! I cannot retrieve it") + error = True + return + if self.namespace_is_correct_class(i): + print("The namespace is correct for " + str(i)) + else: + self.__add_to_report("ERROR: The namespace is not correct for " + str(i)) + error = True + # check class hierarchy + if self.is_subClassOf(g, professorURI, personURI) and \ + self.is_subClassOf(g, associateProfessorURI, professorURI) and \ + self.is_subClassOf(g, interimURI, associateProfessorURI) and \ + self.is_subClassOf(g, fProfessorURI, professorURI): + self.__add_to_report("Hierarchy OK") + else: + self.__add_to_report("ERROR: Hierarchy is missing a subclassOf statement") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.1") + else: + self.__add_to_report("TASK 6.1 OK") + + def validate_task_06_02(self, g): + # check properties + error = False + hasColleague = g.value(subject=None, predicate=RDFS.label, object=Literal("hasColleague", datatype=XSD.string)) + hasName = g.value(subject=None, predicate=RDFS.label, object=Literal("hasName", datatype=XSD.string)) + hasHomePage = g.value(subject=None, predicate=RDFS.label, object=Literal("hasHomePage", datatype=XSD.string)) + personURI = g.value(subject=None, predicate=RDFS.label, object=Literal("Person", datatype=XSD.string)) + fullProfessorURI = g.value(subject=None, predicate=RDFS.label, object=Literal("FullProfessor", datatype=XSD.string)) + properties = [hasColleague, hasName, hasHomePage] + for i in properties: + if i is None: + self.__add_to_report("ERROR: One of the properties is missing its correct label! I cannot retrieve it") + error = True + return + if not self.domain_and_range_correspond_to_input(g,hasColleague,personURI,personURI): + self.__add_to_report("ERROR: hasColleague has an incorrect domain or range") + error = True + if not self.domain_and_range_correspond_to_input(g,hasName,personURI,RDFS.Literal): + self.__add_to_report("ERROR: hasName has an incorrect domain or range") + error = True + if not self.domain_and_range_correspond_to_input(g,hasHomePage,fullProfessorURI,RDFS.Literal): + self.__add_to_report("ERROR: hasHomePage has an incorrect domain or range") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.2") + else: + self.__add_to_report("TASK 6.2 OK") + + def validate_task_06_03(self, g): + # check all individuals can be retrieved through their label + error = False + oscar = g.value(subject=None, predicate=RDFS.label, object=Literal("Oscar", datatype=XSD.string)) + asun = g.value(subject=None, predicate=RDFS.label, object=Literal("Asun", datatype=XSD.string)) + raul = g.value(subject=None, predicate=RDFS.label, object=Literal("Raul", datatype=XSD.string)) + if oscar is None or asun is None or raul is None: + self.__add_to_report("ERROR: One of the individuals is missing its correct label! I cannot retrieve it") + error = True + # check all individuals have the correct namespace + if not self.namespace_is_correct_instance(oscar): + self.__add_to_report("ERROR: Oscar has an incorrect namespace") + error = True + if not self.namespace_is_correct_instance(asun): + self.__add_to_report("ERROR: Asun has an incorrect namespace") + error = True + if not self.namespace_is_correct_instance(raul): + self.__add_to_report("ERROR: Raul has an incorrect namespace") + error = True + # check all individuals have their properties + oscar_properties = [] + for p in g.predicates(subject=oscar): + oscar_properties.append(p) + asun_properties = [] + for p in g.predicates(subject=asun): + asun_properties.append(p) + if oscar_properties is None or asun_properties is None: + self.__add_to_report("ERROR: One of the individuals has no properties") + error = True + if len(oscar_properties) != 4 or len(asun_properties) != 4: + # oscar: type, label, hasColleague, hasName. + # asun: type, label, hasHomePage, hasColleague + self.__add_to_report("ERROR: One of the individuals has the wrong number of properties") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.3") + else: + self.__add_to_report("TASK 6.3 OK") + + def validate_task_06_04(self, g): + error = False + target_properties = [VCARD.Given, VCARD.Family, FOAF.email] + #retrieve all triples from Oscar. + oscar_properties = [] + oscar = g.value(subject=None, predicate=RDFS.label, object=Literal("Oscar", datatype=XSD.string)) + for p in g.predicates(subject=oscar): + oscar_properties.append(p) + if oscar_properties is None: + self.__add_to_report("ERROR: Oscar has no properties") + error = True + # do they have the correct ns? + for i in target_properties: + if i not in oscar_properties: + self.__add_to_report("ERROR: One of the properties from Oscar has no correct namespace or does not exist. Please double check") + error = True + if error: + self.__add_to_report("ERROR IN TASK 6.4") + else: + self.__add_to_report("TASK 6.4 OK") + + def save_report(self, task): + report_name = "report_result" + task + ".txt" + with open(report_name, "w", encoding="utf-8") as f: + f.write(self.__report) + + def validate_07_01(self, result, task): + error = False + if len(result) != 7: + self.__add_to_report("ERROR: The number of classes returned is not correct") + error = True + for c,sc in result: + # Anything except Person and Animal must have a superclass + if sc == None and "Person" not in str(c) and "Animal" not in str(c): + self.__add_to_report("The class "+str(c)+" has no superclass") + error = True + if "Person" not in str(c) and "Animal" not in str(c) \ + and "Professor" not in str(c) and "Student" not in str(c) \ + and "FullProfessor" not in str(c) and "AssociateProfessor" not in str(c) \ + and "AssociateProfessor" not in str(c) and "Instructor" not in str(c) \ + and "InterimAssociateProfessor" not in str(c): + self.__add_to_report("ERROR: incorrect class retrieved") + error = True + if not error: + self.__add_to_report(task+" OK") + + def validate_07_1a(self, result): + self.validate_07_01(result, "TASK 7.1a") + + def validate_07_1b(self, query, g): + aux = g.query(query) + aux_dict = [] + for r in g.query(query): + aux_dict.append((r.c, r.sc)) + self.validate_07_01(aux_dict, "TASK 7.1b") + + def validate_07_02(self,result, task): + error = False + if len(result) != 3: + self.__add_to_report("ERROR: The number of individuals returned is not correct") + error = True + for i in result: + if "Asun" not in i and "Raul" not in i and "Oscar" not in i: + self.__add_to_report("ERROR: The individual "+str(i)+" is not correct") + error = True + if error == False: + self.__add_to_report(task+" OK") + + + def validate_07_02a(self, individuals): + self.validate_07_02(individuals, "TASK 7.2a") + + def validate_07_02b(self, g, query): + error = False + aux = g.query(query) + aux_dict = [] + for r in g.query(query): + if (r.ind is None): + self.__add_to_report("ERROR: Variable used to retrieve the individuals is not correct!") + error = True + else: + aux_dict.append(r.ind) + self.validate_07_02(aux_dict, "TASK 7.2b") + + def validate_07_03(self, g, query): + error = False + entities = g.query(query) + if len(list(entities)) != 3: + self.__add_to_report("ERROR: The number of individuals returned is not correct") + error = True + for i in entities: + if "Asun" not in i.name and "Raul" not in i.name and "Fantasma" not in i.name: + self.__add_to_report("ERROR: An individual returned is not correct") + error = True + if not error: + self.__add_to_report("TASK 7.3 OK") + + def validate_07_04(self, g, query): + error = False + entities = g.query(query) + if len(list(entities)) != 3: + self.__add_to_report("ERROR: The number of individuals returned is not correct") + error = True + for i in entities: + if "Asun" not in i.name and "Raul" not in i.name and "Oscar" not in i.name: + self.__add_to_report("ERROR: An individual returned is not correct") + error = True + if not error: + self.__add_to_report("TASK 7.4 OK")