-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial: Morph streams
This tutorial shows examples of SPARQLstream queries using Morph-streams.
The Morph-web host (e.g. http://streams.linkeddata.es
, or http://linkeddata2.dia.fi.upm.es:9000) is MORPH_HOST
in all examples.
You can choose one of the use cases in the Demo home:
Choose the Social Sensor Demo (using Esper) in the home menu.
Morph-streams can access existing streams using SPARQL queries, provided that you specify mappings. Let's consider this stream that detects people in a room, running on Esper CEP:
detections {roomid:string,person:string,time:string}
For example, this stream may contain tuples as the following:
r1,alice,2013-10-10T10:00
But of course we want to query this data through an ontology...
Let's use this ontology:
It is an oversimplified ontology where an observation encapsulates something that a sensor has observed. In this case the data includes who was observed (a person), and where (in a room).
We can query the data using Morph-streams and the SPARQLstream query language. In this example we will launch a continuous query.
Go to MORPH_HOST/query/social
. You can write a query or choose one of the predefined ones on the right panel, e.g. all observations when carl was detected in the last 30 seconds:
PREFIX sr4ld: <http://streamreasoning.org/ontologies/social#>
PREFIX pers: <http://streamreasoning.org/data/person/id/>
SELECT ?obs
FROM NAMED STREAM <http://streamreasoning.org/data/social.srdf> [NOW - 30 S]
WHERE {
?obs sr4ld:who pers:carl.
}
Then press 'Query' and the continuous query will be registered.
So far you will not see any results, as you have only registered the query. to see some data you will have to pull results in the next step.
The previous step registers a query in the system. Now the query has been given an identifier that you can use to retreive results by pulling.
Press the Pull button and you will get the results of the continuous query
You can also remove the query when you no longer need it.
If you want to receive results as soon as they are available (and not polling every certain amount of time) you can Listen to a query, using a WebSocket. WebSockets implement full-duplex communication via TCP, and are supported by most browsers. In this way your client can receive data from the server in push mode.
Go to MORPH_HOST/register/social
and you will have again a textarea for writing or copying your query. You can use the same query as above as well, then press 'Set Query' and you will notice that the websocket URL has been built, e.g.:
ws://linkeddata2.dia.fi.upm.es:9000/push?query=PREFIX%20sr4l...
.
Then press 'Connect', which enables you to register the query and receive the results as soon as they are available. They will appear in the console log, in JSON format, e.g.:
{ "head": { "vars": [ "obs" ] } ,
"results":
{ "bindings":
[ { "obs": { "type": "uri" , "value": "http://streamreasoning.org/data/obs/id/r2/bob" } }]
}
}
###Changing the mappings So far all wueries have used the mappings provided by us by default. you can change them by clicking on the 'Custom mapping' checkbox, and editing the R2RML mapping.
For example you can change the URI template for a Person, instead of this predicate map:
rr:predicateObjectMap [
rr:predicate sr4ld:who;
rr:objectMap [rr:template "http://streamreasoning.org/data/person/id/{person}"]];
You can define the following:
rr:predicateObjectMap [
rr:predicate sr4ld:who;
rr:objectMap [rr:template "http://someotherplace.org/persons/Person/{person}"]];
Now that you have seen how queries are registered and data pushed and pulled, we can see the underlying rewritten queries. By clicking on the 'Show underlying query' checkbox you can see what is sent to the DSMS or CEP or middleware.
In this use case, we are using GSN, and the example is based on One-off instantaneous queries, and not continuous as in the previous example.
You can go to `MORPH_HOST/query/emt´ and again, write a SPARQLstream query or use one of the predefined ones on the left, e.g. get all bus stop observations in the last 5 mins:
PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
PREFIX qudt: <http://data.nasa.gov/qudt/owl/qudt#>
PREFIX emt: <http://emt.linkeddata.es/data#>
SELECT ?timeto ?obs ?av
FROM NAMED STREAM <http://emt.linkeddata.es/data#busstops.srdf> [NOW - 300 S]
WHERE {
?obs a emt:BusObservation.
?obs ssn:observationResult ?output.
?output emt:timeToBusValue ?av.
?av qudt:numericValue ?timeto.
}
You will get results back immediately, and a query in this case is not registered.
All previous examples display results in the web application as data in an html page. The same queries can be evalauted by calling a RESTful SPARQLstream service. As an example you can use the same query form as in the previous example: MORPH_HOST/query/emt
.
Then you can again write a SPARQLstream query or pick one, and click on the 'Set REST Query' linker. The generated RESTful call is something like:
MORPH_HOST/emt/sparqlstream?query=ENCODEDQUERY
Where `ENCODEDQUERY´is the SPARQLStream encoded for a URL. E.g.:
Results are returned in Json format:
{
"head": {
"vars": [ "timeto" , "obs" , "av" ]
} ,
"results": {
"bindings": [
{
"timeto": { "datatype": "http://www.w3.org/2001/XMLSchema#string" , "type": "typed-literal" , "value": "999999" } ,
"obs": { "type": "uri" , "value": "http://transporte.linkeddata.es/emt/busstop/id/44/busline/147/observation/20/10/2013%2010:35:38%20%2B0200" } ,
"av": { "type": "uri" , "value": "http://transporte.linkeddata.es/emt/busstop/id/44/busline/147/timeToBusValue/20/10/2013%2010:35:38%20%2B0200" }
} ,