|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | from rdflib import Graph, Namespace, URIRef |
6 | | -from rdflib.plugins.stores.sparqlstore import SPARQLStore |
7 | | -from rdflib.term import Identifier |
| 6 | +from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore, SPARQLStore |
| 7 | +from rdflib.term import Identifier, Literal |
| 8 | +from rdflib.namespace import RDF, SKOS |
| 9 | + |
| 10 | +# Shows examples of the useage of SPARQLStore and SPARQLUpdateStore against local SPARQL1.1 endpoint if |
| 11 | +# available. This assumes SPARQL1.1 query/update endpoints running locally at |
| 12 | +# http://localhost:3030/db/ |
| 13 | +# |
| 14 | +# It uses the same endpoint as the test_dataset.py! |
| 15 | +# |
| 16 | +# For the tests here to run, you can for example start fuseki with: |
| 17 | +# ./fuseki-server --mem --update /db |
| 18 | + |
| 19 | +# THIS WILL ADD DATA TO THE /db dataset |
8 | 20 |
|
9 | 21 | if __name__ == "__main__": |
10 | 22 | dbo = Namespace("http://dbpedia.org/ontology/") |
| 23 | + dbr = Namespace("http://dbpedia.org/resource/") |
| 24 | + |
| 25 | + # EXAMPLE Update Store 1: |
| 26 | + graph = Graph("SPARQLUpdateStore", identifier="http://dbpedia.org") |
| 27 | + graph.open(("http://localhost:3030/db/sparql", "http://localhost:3030/db/update")) |
| 28 | + graph.add((dbr.Berlin, dbo.populationTotal, Literal(3))) |
| 29 | + graph.add((dbr.Brisbane, dbo.populationTotal, Literal(2))) |
| 30 | + |
| 31 | + # EXAMPLE Update Store 2: |
| 32 | + st = SPARQLUpdateStore(query_endpoint="http://localhost:3030/db/sparql", update_endpoint="http://localhost:3030/db/update") |
| 33 | + graph = Graph(store=st, identifier="http://dbpedia.org") |
| 34 | + graph.add((dbr["Category:Capitals_in_Europe"], RDF.type, SKOS.Concept)) |
| 35 | + graph.add((dbr["Category:Holy_Grail"], RDF.type, SKOS.Concept)) |
| 36 | + graph.add((dbr["Category:Hospital_ships_of_Japan"], RDF.type, SKOS.Concept)) |
11 | 37 |
|
12 | | - # EXAMPLE 1: using a Graph with the Store type string set to "SPARQLStore" |
| 38 | + |
| 39 | + # EXAMPLE Store 1: using a Graph with the Store type string set to "SPARQLStore" |
13 | 40 | graph = Graph("SPARQLStore", identifier="http://dbpedia.org") |
14 | | - graph.open("http://dbpedia.org/sparql") |
| 41 | + graph.open("http://localhost:3030/db/sparql") |
15 | 42 |
|
16 | 43 | pop = graph.value(URIRef("http://dbpedia.org/resource/Berlin"), dbo.populationTotal) |
17 | 44 | assert isinstance(pop, Identifier) |
|
23 | 50 | ) |
24 | 51 | print() |
25 | 52 |
|
26 | | - # EXAMPLE 2: using a SPARQLStore object directly |
27 | | - st = SPARQLStore(query_endpoint="http://dbpedia.org/sparql") |
| 53 | + # EXAMPLE Query 2: using a SPARQLStore object directly |
| 54 | + st = SPARQLStore(query_endpoint="http://localhost:3030/db/sparql") |
28 | 55 |
|
29 | 56 | for p in st.objects( |
30 | 57 | URIRef("http://dbpedia.org/resource/Brisbane"), dbo.populationTotal |
|
35 | 62 | ) |
36 | 63 | print() |
37 | 64 |
|
38 | | - # EXAMPLE 3: doing RDFlib triple navigation using SPARQLStore as a Graph() |
| 65 | + # EXAMPLE Query 3: doing RDFlib triple navigation using SPARQLStore as a Graph() |
39 | 66 | print("Triple navigation using SPARQLStore as a Graph():") |
40 | 67 | graph = Graph("SPARQLStore", identifier="http://dbpedia.org") |
41 | | - graph.open("http://dbpedia.org/sparql") |
| 68 | + graph.open("http://localhost:3030/db/sparql") |
| 69 | + # we are asking DBPedia for 3 skos:Concept instances |
| 70 | + count = 0 |
| 71 | + |
| 72 | + for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept): |
| 73 | + count += 1 |
| 74 | + print(f"\t- {s}") |
| 75 | + if count >= 3: |
| 76 | + break |
| 77 | + |
| 78 | + |
| 79 | + # EXAMPLE Query 4: doing RDFlib triple navigation using a Graph() with a SPARQLStore backend |
| 80 | + print("Triple navigation using a Graph() with a SPARQLStore backend:") |
| 81 | + st = SPARQLStore(query_endpoint="http://localhost:3030/db/sparql") |
| 82 | + graph = Graph(store=st) |
42 | 83 | # we are asking DBPedia for 3 skos:Concept instances |
43 | 84 | count = 0 |
44 | | - from rdflib.namespace import RDF, SKOS |
45 | 85 |
|
46 | 86 | for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept): |
47 | 87 | count += 1 |
48 | 88 | print(f"\t- {s}") |
49 | 89 | if count >= 3: |
50 | 90 | break |
51 | 91 |
|
52 | | - # EXAMPLE 4: using a SPARQL endpoint that requires Basic HTTP authentication |
| 92 | + # EXAMPLE Store 5: using a SPARQL endpoint that requires Basic HTTP authentication |
53 | 93 | # NOTE: this example won't run since the endpoint isn't live (or real) |
54 | 94 | sparql_store = SPARQLStore( |
55 | 95 | query_endpoint="http://fake-sparql-endpoint.com/repository/x", |
|
0 commit comments