- CS-E4410
- 3. SPARQL query language and protocol
- 3.3 More SPARQL
More SPARQL¶
In this section we will practice CONSTRUCT and federated queries We will display SPARQL query results on Google Maps, try out YASGUI’s advanced tools and learn about SPARQL Update language.
Data transformation with SPARQL CONSTRUCT¶
We continue querying the WarSampo endpoint http://ldf.fi/warsa/sparql.
In WarSampo, military ranks are represented as instances of the class
http://ldf.fi/schema/warsa/Rank
.
The ranks form a hierarchy using the dct:isPartOf property.
The dct prefix refers to http://purl.org/dc/terms/
, which is a part of
the Dublin Core Metadata Terms vocabulary.
Many other systems use the SKOS standard
to represent concept hierarchies.
Your task is to create a SPARQL CONSTRUCT query that transforms the existing dct:isPartOf hierarchy into a corresponding SKOS concept hierarchy. The created concepts should be instances of a suitable SKOS class. For the SKOS concepts, add SKOS properties for original labels, and the hierarchical relation using a proper predicate. You need to only worry about preferred labels. Alternative labels are not checked.
The SKOS Reference should be of assistance.
A+ presents the exercise submission form here.
Federated query¶
SPARQL allows you to query different SPARQL endpoints simultaneously using a federated query.
Create a federated query:
SELECT ?cityname ?latitude ?longitude ?photolabel ?thumbnailuri ...
that retrieves the coordinates of cities Maarianhamina and Nokia from the Finnish Geographic Names registry (PNR), enriched with the WarSampo wartime photographs that are taken in these cities. Send the federated query to the PNR endpoint http://ldf.fi/pnr/sparql, containing a subquery to WarSampo.
Hint: “Logical or” in SPARQL can be performed with UNION operator
{?subject :my_predicate "this"} UNION {?subject :my_predicate "that"}
or in FILTER FILTER (?object = "this" || ?object = "that")
.
Also, there is the
VALUES keyword.
The Finnish Geographic Names registry contains names of places, along with their coordinates.
The registry contains a separate class for every place type. Cities are instances of the
class http://ldf.fi/pnr-schema#place_type_540
.
The WarSampo data model contains a CIDOC CRM event for each photograph, depicting the
photography event. The photography event is linked to the photograph with a
crm:P94_has_created
property. These events are linked to place resources, that correspond to where the photography
took place with a crm:P7_took_place_at
property. Conveniently some of
these places are of type http://ldf.fi/pnr-schema#place_type_540
.
The namespace prefix crm refers to http://www.cidoc-crm.org/cidoc-crm/.
Hint: a DESCRIBE query (or a few) might help in understanding the data and finding proper predicates to use for the federated query.
Extend the query to also return the photograph thumbnail URI, which can be found by following,
from the photograph resource, the sequential links crm:P138i_has_representation
and
http://schema.org/contentUrl. There are multiple representational resources for
each photograph containing a photograph URI, but you should use the one that
is described as a thumbnail. Being described as a thumbnail can be identified
by having a small size in WarSampo schema, which can be determined with
http://ldf.fi/schema/warsa/photographs/size
and http://ldf.fi/schema/warsa/photographs/sm
.
Hint: Do not fetch city labels on the WarSampo part of the query. WarSampo may not have labels for all the cities and this will cause problems. Get the city labels on the PNR part of the query.
Note that the automatic tests do not use a full version of the WarSampo knowledge graph, and this means that some unnecessary restrictions that work without problem on the actual endpoint will cause an empty resultset with the automatic tests. If you are getting an empty resultset on the automatic tests you should try to remove some restrictions from the query.
A+ presents the exercise submission form here.
Using a SPARQL endpoint with JavaScript (3 pts)¶
Work on the federated query from the previous exercise to get one result row for each city by using aggregation methods. Aggregate the variable items to contain, as a string, the name of every photograph taken in the city, and the photograph URL. Use the symbols # and $ to separate photograph labels and URLs like this: “label1#url1 $ label2#url2 $ …”.
Hint: Useful SPARQL keywords: CONCAT, STR, BIND, GROUP_CONCAT, GROUP BY
Use this HTML page template
to visualize your results on the world map
provided by the
Google Maps JavaScript API. You can download the template with e.g.
wget myurl
. You can also open the template as text from this
link
and save it by right-clicking
the page and selecting “Save as…” (hotkey: Ctrl+S). The filename should end in
.html for a browser to interpret the file as a HTML file.
Modify the JavaScript inside the HTML file to include your results on the map.
You only need to add proper values to endpoint
and query
variables within the code. Then open the file with a modern browser.
When you click a marker on the map, an info window will open, showing the city name and the names and photographs taken there.
In case something goes wrong, you can add console logging (see the console.log() used in the template), to debug your JavaScript code and make sure your variables have the values you would expect. The messages sent to console.log() show up in the Console tab of web browser developer tools. You may also modify the page template in any way you want.
NOTE: The tests break if you try to group by aggregate variable (here items).
A+ presents the exercise submission form here.
Visualizing SPARQL results with YASGUI¶
Using YASGUI and the SPARQL endpoint of DBpedia (https://dbpedia.org/sparql),
visualize how many people described in DBpedia have been born (dbo:birthPlace
)
in each Finnish city or town. Finnish cities and towns have a dct:subject
relation to dbc:Cities_and_towns_in_Finland
. Create a pie chart visualization,
which can be found in YASGUI by clicking Google Chart → Chart config → Charts → Pie → Select your preferred pie chart.
Include only the 20 cities with the largest amount of known people born in them, and
use the English labels for cities.
YASGUI should automatically define the prefixes dct and dbc when you write them, or you can manually define them with:
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dct: <http://purl.org/dc/terms/>
SPARQL Update¶
The SPARQL Update language provides ways to modify RDF data stored in a SPARQL endpoint.
Examine the following SPARQL Update request and answer the following questionnaire.
WITH <http://ldf.fi/history/kb>
DELETE {
?u2 ?pred ?obj .
?u ?p ?u2 .
}
INSERT {
?u ?p ?u1 .
}
WHERE {
VALUES ?class { <http://www.cidoc-crm.org/cidoc-crm/E21_Person> } .
GRAPH <http://ldf.fi/history/kb> {
?u1 a ?class .
?u2 a ?class .
FILTER (?u1 != ?u2 && str(?u1) > str(?u2)) .
?u1 <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?u2 <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?u2 ?pred ?obj .
?u ?p ?u2 .
}
}
Serving RDF data¶
Apache Fuseki is a triplestore and SPARQL server. Install Fuseki2 on your own computer or the computers in the lab. Instructions for installation are given here. Basically you’ll just have to download and unpack the Fuseki distribution package. For a reference on publishing Linked Data, you can also read this additional learning material.
Once the Fuseki server is running (run the script fuseki-server in the unzipped directoy, or fuseki-server.bat for Windows) and data loaded, open http://localhost:3030 in your browser. You should see the Fuseki management application interface. Create a new dataset, and upload the turtle files found in this zip file. The files contain a taxonomy of Finnish birds, and characteristics annotations for some bird species.
IMPORTANT NOTE: Uploading data may not work properly with Google Chrome, so use for example Firefox instead, if you have problems.
Query the local endpoint using e.g. the YASGUI tool, which is also directly
provided in the Fuseki installation. Find out the English labels for the
characteristics of chaffinch (label "chaffinch"@en
). You can inspect the data
model by doing, for example, DESCRIBE queries.
A+ presents the exercise submission form here.