Skip to content

Commit 413b0a1

Browse files
Merge pull request #2 from Angel-Astiazaran/Trabajo-Grupo
Trabajo grupo
2 parents 7875458 + 5f84cd7 commit 413b0a1

12 files changed

+26715
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"@context" : {
3+
"xsd" : "http://www.w3.org/2001/XMLSchema#",
4+
"vocab" : "http://example.org/specialVocab/Assignment2b",
5+
"fullname":"s:fullname",
6+
"hometown" : "vocab:hometown",
7+
"age" : "vocab:age",
8+
"almaMater" : "vocab:almaMater",
9+
"mothesrName" : "vocab:mothesrName",
10+
"fathesrName" : "vocab:fathesrName",
11+
"subject" : "http://example.org/subject",
12+
"teachers" : "http://example.org/teachers",
13+
"description" : "rdfs:comment"
14+
},
15+
"@id" : "http://example.org/specialVocab/Assignment2b/Angel-Astiazaran",
16+
"@type" : "vocab:student",
17+
"fullname" : "Angel Astiazaran Gafo",
18+
"hometown" : "Las Rozas",
19+
"age" : "22",
20+
"almaMater" : "Universidad Politecnica de Madrid",
21+
"mothersName" : "Virginia",
22+
"fathersName" : "Gonzalo",
23+
"subject" : [
24+
{
25+
"name" : "Linked Data",
26+
"teachers" : [
27+
{
28+
"name" : "Oscar Corcho"
29+
},
30+
{
31+
"name" : "Raul García Castro"
32+
}
33+
],
34+
"description" : "Data relationship with web and other types of web data"
35+
},
36+
{
37+
"name" : "Pattern Recognition",
38+
"teachers" : [
39+
{
40+
"name" : "Luis Bamuela"
41+
},
42+
{
43+
"name" : "Roberto Valle"
44+
}
45+
],
46+
"description" : "Design of algorithms that look for patterns or regularities in data"
47+
}
48+
]
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"@context": {
3+
"base": "http://example.org/",
4+
"xsd": "http://www.w3.org/2001/XMLSchema#"
5+
},
6+
"@graph": [
7+
{
8+
"@id": "base:Class01",
9+
"base:includes": [
10+
{
11+
"@id": "base:Sensor029",
12+
"base:hasMeasurement": {
13+
"@id": "base:Measurement8401",
14+
"base:hasTemperature": {
15+
"@type": "xsd:int",
16+
"@value": 29
17+
},
18+
"base:atTime": {
19+
"@value": "2010-06-12T12:00:12",
20+
"@type": "xsd:dateTime"
21+
}
22+
}
23+
},
24+
{
25+
"@id": "base:Computer101",
26+
"base:hasOwner": {
27+
"@id": "base:User10A",
28+
"base:hasName": {
29+
"@value": "Pedro",
30+
"@type": "xsd:string"
31+
}
32+
}
33+
}
34+
]
35+
}
36+
]
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# %% [markdown]
2+
# **Task 06: Modifying RDF(s)**
3+
4+
# %%
5+
!pip install rdflib
6+
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials"
7+
8+
# %% [markdown]
9+
# Read the RDF file as shown in class
10+
11+
# %%
12+
from rdflib import Graph, Namespace, Literal
13+
from rdflib.namespace import RDF, RDFS
14+
g = Graph()
15+
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
16+
g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False)
17+
g.parse(github_storage+"/rdf/example5.rdf", format="xml")
18+
19+
# %% [markdown]
20+
# Create a new class named Researcher
21+
22+
# %%
23+
ns = Namespace("http://somewhere#")
24+
g.add((ns.Researcher, RDF.type, RDFS.Class))
25+
for s, p, o in g:
26+
print(s,p,o)
27+
28+
# %% [markdown]
29+
# **TASK 6.1: Create a new class named "University"**
30+
#
31+
32+
# %%
33+
# TO DO:
34+
g.add((ns.University, RDF.type, RDFS.Class))
35+
# Visualize the results
36+
for s, p, o in g:
37+
print(s,p,o)
38+
39+
# %% [markdown]
40+
# **TASK 6.2: Add "Researcher" as a subclass of "Person"**
41+
42+
# %%
43+
# TO DO
44+
g.add((ns.Researcher, RDFS.subClassOf, ns.Person))
45+
# Visualize the results
46+
for s, p, o in g:
47+
print(s,p,o)
48+
49+
# %% [markdown]
50+
# **TASK 6.3: Create a new individual of Researcher named "Jane Smithers"**
51+
52+
# %%
53+
# TO DO
54+
g.add((ns.JaneSmith, RDF.type, ns.Researcher))
55+
# Visualize the results
56+
for s, p, o in g:
57+
print(s,p,o)
58+
59+
# %% [markdown]
60+
# **TASK 6.4: Add to the individual JaneSmithers the email address, fullName, given and family names. Use the https://schema.org vocabulary**
61+
62+
# %%
63+
# TO DO
64+
schema = Namespace("https://schema.org/")
65+
g.add((ns.JaneSmith, schema.email, Literal("jane.smith@example.com")))
66+
g.add((ns.JaneSmith, schema.name, Literal("Jane Smithers")))
67+
g.add((ns.JaneSmith, schema.givenName, Literal("Jane")))
68+
g.add((ns.JaneSmith, schema.familyName, Literal("Smithers")))
69+
# Visualize the results
70+
for s, p, o in g:
71+
print(s, p, o)
72+
73+
# %% [markdown]
74+
# **TASK 6.5: Add UPM as the university where John Smith works. Use the "https://example.org/ namespace**
75+
76+
# %%
77+
# TO DO
78+
g.add((ns.UPM, RDF.type, ns.University))
79+
g.add((ns.JohnSmith, schema.employer, ns.UPM))
80+
# Visualize the results
81+
for s, p, o in g:
82+
print(s, p, o)
83+
84+
# %% [markdown]
85+
# **Task 6.6: Add that Jown knows Jane using the FOAF vocabulary. Make sure the relationship exists.**
86+
87+
# %%
88+
# TO DO
89+
foaf = Namespace("http://xmlns.com/foaf/0.1/")
90+
g.add((ns.JohnSmith, foaf.knows, ns.JaneSmith))
91+
# Visualize the results
92+
for s, p, o in g:
93+
print(s, p, o)
94+
95+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# %% [markdown]
2+
# **Task 07: Querying RDF(s)**
3+
4+
# %%
5+
!pip install rdflib
6+
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials"
7+
8+
# %% [markdown]
9+
# First let's read the RDF file
10+
11+
# %%
12+
from rdflib import Graph, Namespace, Literal
13+
from rdflib.namespace import RDF, RDFS
14+
g = Graph()
15+
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
16+
g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False)
17+
g.parse(github_storage+"/rdf/example6.rdf", format="xml")
18+
19+
# Add the namespace and import the usage of SPARQL
20+
ns = Namespace("http://somewhere#")
21+
from rdflib.plugins.sparql import prepareQuery
22+
23+
# %% [markdown]
24+
# **TASK 7.1: List all subclasses of "LivingThing" with RDFLib and SPARQL**
25+
26+
# %%
27+
# TO DO
28+
# Visualize the results
29+
for s, p, o in g.triples((None, RDFS.subClassOf, ns.LivingThing)): # Cambiar ns.Person a ns.LivingThing
30+
print(s)
31+
print("\n")
32+
33+
#for r in g.query(q1):
34+
# print(r)
35+
query = prepareQuery("""SELECT ?s WHERE { ?s rdfs:subClassOf ns:LivingThing }""", initNs={"rdfs": RDFS, "ns": ns})
36+
for data in g.query(query):
37+
print(data)
38+
39+
# %% [markdown]
40+
# **TASK 7.2: List all individuals of "Person" with RDFLib and SPARQL (remember the subClasses)**
41+
#
42+
43+
# %%
44+
# TO DO
45+
# Visualize the results
46+
47+
for s, p, o in g.triples((None, RDF.type, ns.Person)):
48+
print(s)
49+
for s, p, o in g.triples((None, RDFS.subClassOf, ns.Person)):
50+
for s2, p2, o2 in g.triples((None, RDF.type, s)):
51+
print(s2)
52+
print("\n")
53+
q2 = prepareQuery("""select ?s where { {?s rdf:type ns:Person} union {?s2 rdfs:subClassOf ns:Person. ?s rdf:type ?s2} }""", initNs={"rdfs": RDFS, "ns": ns, "rdf": RDF})
54+
for data in g.query(q2):
55+
print(data)
56+
57+
# %% [markdown]
58+
# **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)**
59+
#
60+
61+
# %%
62+
from rdflib.plugins.sparql import prepareQuery
63+
64+
# Define the query
65+
q3 = prepareQuery("""
66+
SELECT ?s WHERE {
67+
{ ?s rdf:type ns:Person }
68+
UNION
69+
{ ?s rdf:type ns:Animal }
70+
}
71+
""", initNs={"rdf": RDF, "ns": ns})
72+
73+
# Execute the query and visualize the results
74+
for data in g.query(q3):
75+
print(data)
76+
77+
78+
# %% [markdown]
79+
# **TASK 7.4: List the name of the persons who know Rocky (in SPARQL only)**
80+
81+
# %%
82+
# Define the query
83+
q4 = prepareQuery("""
84+
SELECT ?name WHERE {
85+
?person rdf:type ns:Person .
86+
?person ns:knows ns:Rocky .
87+
?person vcard:FN ?name .
88+
}
89+
""", initNs={"rdf": RDF, "ns": ns, "vcard": Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")})
90+
91+
# Execute the query and visualize the results
92+
for data in g.query(q4):
93+
print(data)
94+
95+
96+
# %% [markdown]
97+
# **Task 7.5: List the name of those animals who know at least another animal in the graph (in SPARQL only)**
98+
99+
# %%
100+
# Define the query
101+
q5 = prepareQuery("""
102+
SELECT ?name WHERE {
103+
?animal rdf:type ns:Animal .
104+
?animal ns:knows ?otherAnimal .
105+
?otherAnimal rdf:type ns:Animal .
106+
?animal vcard:FN ?name .
107+
}
108+
""", initNs={"rdf": RDF, "ns": ns, "vcard": Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")})
109+
110+
# Execute the query and visualize the results
111+
for data in g.query(q5):
112+
print(data)
113+
114+
# %% [markdown]
115+
# **Task 7.6: List the age of all living things in descending order (in SPARQL only)**
116+
117+
# %%
118+
# Define the query
119+
q6 = prepareQuery("""
120+
SELECT ?livingThing ?age WHERE {
121+
?livingThing rdf:type ns:LivingThing .
122+
?livingThing ns:age ?age .
123+
}
124+
ORDER BY DESC(?age)
125+
""", initNs={"rdf": RDF, "ns": ns})
126+
127+
# Execute the query and visualize the results
128+
for data in g.query(q6):
129+
print(data)
130+
131+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# %% [markdown]
2+
# **Task 08: Completing missing data**
3+
4+
# %%
5+
!pip install rdflib
6+
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials"
7+
8+
# %%
9+
from rdflib import Graph, Namespace, Literal, URIRef
10+
g1 = Graph()
11+
g2 = Graph()
12+
g1.parse(github_storage+"/rdf/data01.rdf", format="xml")
13+
g2.parse(github_storage+"/rdf/data02.rdf", format="xml")
14+
15+
# %% [markdown]
16+
# Tarea: lista todos los elementos de la clase Person en el primer grafo (data01.rdf) y completa los campos (given name, family name y email) que puedan faltar con los datos del segundo grafo (data02.rdf). Puedes usar consultas SPARQL o iterar el grafo, o ambas cosas.
17+
18+
# %% [markdown]
19+
# **Importamos las librerias**
20+
21+
# %%
22+
from rdflib.namespace import RDF
23+
from rdflib.plugins.sparql import prepareQuery
24+
ns = Namespace("http://data.org#")
25+
vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")
26+
27+
# %% [markdown]
28+
# *Listamos todos los elementos de la clase Person*
29+
30+
# %%
31+
for s, p, o in g1.triples((None, RDF.type, ns.Person)):
32+
print(s)
33+
34+
# %% [markdown]
35+
# *Seleccionamos solo los campos de name, family name y emaul, para añadirlos al primero. Usare SPARQL.*
36+
37+
# %%
38+
q = prepareQuery("""select ?s ?p ?o where { ?s ?p ?o. filter(?p=vcard:FN || ?p=vcard:Given || ?p=vcard:EMAIL) }""", initNs={"vcard": vcard})
39+
for newData in g2.query(q):
40+
g1.add(newData)
41+
42+
# %% [markdown]
43+
# *Finalmente visualizamos el resultado*
44+
45+
# %%
46+
for s, p, o in g1:
47+
print(s, p, o)
48+
49+

0 commit comments

Comments
 (0)