Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 2a/2b #45

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Assignment2/fdzdani-fromRDFtoJSON-LD.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@prefix ex: <http://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

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 .
Binary file added Assignment2/fdzdani.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 95 additions & 0 deletions Assignment4/fdzdani-190401/task06.py
Original file line number Diff line number Diff line change
@@ -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)
143 changes: 143 additions & 0 deletions Assignment4/fdzdani-190401/task07.py
Original file line number Diff line number Diff line change
@@ -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: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ns: <http://somewhere#>

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: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ns: <http://somewhere#>

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: <http://somewhere#>

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: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX ns: <http://somewhere#>

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: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX ns: <http://somewhere#>

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: <http://somewhere#>

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)