|
| 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 | + |
0 commit comments