sparqling cocktails

23
Developing for the Semantic Web by Timea Turdean 21.11.2015 #devfest15 Vienna

Upload: timea-turdean

Post on 11-Jan-2017

9.311 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: SPARQLing cocktails

Developing for the Semantic Web

by Timea Turdean21.11.2015

#devfest15 Vienna

Page 2: SPARQLing cocktails

SEMANTIC WEB & LINKED DATA

2http://dbpedia.org/resource/Sir_Tim_Berners_Lee

Triplehttp://example.org/myProject/Triple

the form of subject–predicate–object

expressions<?s ?p ?o>

World Wide Web Consortium (w3.org)

English computer scientists

RDFhttp://dbpedia.org/resource/Resource_Description_Framework

http://www.w3.org/2004/02/skos/core#definition

http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://example.org/Timea-

Custom-Scheme/contained_in

http://example.org/Timea-Custom-Scheme/knows_to_use

Page 3: SPARQLing cocktails

3

Page 5: SPARQLing cocktails

5FEATURES & FUNCTIONALITY

● Tap into your Linked Data endpoint● Query Linked Data● Display your Linked Data● Display OPEN Linked Data● The power of Linked Data● BONUS *An improved search

Page 6: SPARQLing cocktails

Tap into your Linked Data endpoint

▸ data contains:6 ▸ data is available through a SPARQL endpoint

Page 7: SPARQLing cocktails

Tap into your Linked Data endpoint

<http://vocabulary.semantic-web.at/cocktails/8f09ee6f-d5b9-4b8a-aa17-b0665fae4e83> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept> .

<http://vocabulary.semantic-web.at/cocktails/8f09ee6f-d5b9-4b8a-aa17-b0665fae4e83> <http://www.w3.org/2004/02/skos/core#prefLabel> "Brandy"@en .

<http://vocabulary.semantic-web.at/cocktails/8f09ee6f-d5b9-4b8a-aa17-b0665fae4e83> <http://www.w3.org/2004/02/skos/core#altLabel> "Grape spirit"@en .

<http://vocabulary.semantic-web.at/cocktails/8f09ee6f-d5b9-4b8a-aa17-b0665fae4e83> <http://www.w3.org/2004/02/skos/core#definition> "Brandy (from brandywine, derived from Dutch brandewijn\u2014\"burnt wine\") is a spirit produced by distilling wine. Brandy generally contains 35\u201360% alcohol by volume and is typically taken as an after-dinner drink. Some brandies are aged in wooden casks, some are coloured with caramel colouring to imitate the effect of aging, and some brandies are produced using a combination of both aging and colouring."@en .

<http://vocabulary.semantic-web.at/cocktails/8f09ee6f-d5b9-4b8a-aa17-b0665fae4e83> <http://www.w3.org/2004/02/skos/core#narrower> <http://vocabulary.semantic-web.at/cocktails/16b625c5-3930-4cf8-a75b-b25e72f2bfb6> .

<http://vocabulary.semantic-web.at/cocktails/16b625c5-3930-4cf8-a75b-b25e72f2bfb6> <http://www.w3.org/2004/02/skos/core#prefLabel> "Calvados"@en .

7

Page 8: SPARQLing cocktails

SPARQL

8 SELECT * WHERE {

?s ?p ?o}

SELECT * WHERE{

?s ?p ?o}

Page 9: SPARQLing cocktails

Query Linked Data

▸ Give me all Alcoholic Beverages:PREFIX skos:<http://www.w3.org/2004/02/skos/core#>SELECT ?label WHERE {

<http://vocabulary.semantic-web.at/cocktails/f3000285-36b0-4ffe-af90-740c2dd8fff5> skos:narrower ?o .?o skos:prefLabel ?label .

}▸ Results:

"Brandy"@en"Fortified wine"@en"Gin"@en"Liqueur"@en"Rum"@en"Schnapps"@en"Tequila"@en"Vodka"@en"Whisky"@en"Wine"@en

9

Page 10: SPARQLing cocktails

Tap into your Linked Data endpoint

public class SPARQLendpointConnection extends HttpClient {

URL sparqlEndpointURL = null;

NameValuePair queryParam = new NameValuePair( "query", "QUERY");

List<NameValuePair> urlParams = new ArrayList();

List<Header> headers = new ArrayList<>() ;

public SPARQLendpointConnection (URL sparqlEndpointURL) {

this.sparqlEndpointURL = sparqlEndpointURL ;

this.addQueryParameter( "query", "QUERY");

this.addQueryParameter( "content-type" , "application/json" );

super .getParams().setParameter( "http.protocol.version" , HttpVersion. HTTP_1_1);

super .getParams().setParameter( "http.protocol.content-charset" , "UTF-8");

}

public void addQueryParameter (String key, String value) {

if (value.equals( "QUERY")) {

this.queryParam = new NameValuePair(key , value);

} else {

this.urlParams.add(new NameValuePair(key , value));

}

} [...]

}

10

Page 11: SPARQLing cocktails

Display your Linked Data

public class SPARQLendpointConnection extends HttpClient {

public TupleQueryResult runAndParseSelectQuery (String query) throws IOException {

InputStream in = null;

TupleQueryResult tqr = null;

try {

in = IOUtils. toInputStream(runSelectQuery(query)) ;

tqr = QueryResultIO. parse(in, TupleQueryResultFormat. JSON);

return tqr;

} catch (QueryResultParseException | TupleQueryResultHandlerException | UnsupportedQueryResultFormatException ex) {

throw new IOException(ex) ;

} finally {

if (in != null) {

in.close() ;

}

}

}

}

11

Page 12: SPARQLing cocktails

Display your Linked Data

public class SPARQLendpointConnection extends HttpClient {

public String runSelectQuery (String query) throws IOException {

PostMethod post = new PostMethod(this.sparqlEndpointURL .toString()) ;

NameValuePair[] params = this.urlParams.toArray(new NameValuePair[ this.urlParams.size() + 1]);

params[(params. length - 1)] = new NameValuePair( queryParam.getName(), query);

post.setRequestBody(params) ;

for (Header h : this.headers) { post.addRequestHeader(h) ; }

int statusCode;

String response ;

try {

statusCode = super.executeMethod(post) ;

response = post.getResponseBodyAsString() ;

if (statusCode != HttpStatus. SC_OK) {

System. out.println(statusCode) ;

}} finally {

post.releaseConnection() ;

}

return response;

}}

12

Page 13: SPARQLing cocktails

Tap into your Linked Data endpoint

import org.apache.commons.httpclient.* ;

import org.apache.commons.httpclient.methods.PostMethod ;13 org.apache.commons.httpclient.jar

commons.io.jar

import org.apache.commons.io.IOUtils ;

sesame-query.jar

import org.openrdf.query.TupleQueryResult ;

import org.openrdf.query.TupleQueryResultHandlerException ;

sesame-queryresultio-api.jar; sesame-queryresultio-sparqljson.jar

import org.openrdf.query.resultio.QueryResultIO ;

import org.openrdf.query.resultio.QueryResultParseException ;

import org.openrdf.query.resultio.TupleQueryResultFormat ;

import org.openrdf.query.resultio.UnsupportedQueryResultFormatException ;

Page 14: SPARQLing cocktails

Display your Linked Data

public void test() throws Exception {

String value = "";

SPARQLendpointConnection myConncetion = new SPARQLendpointConnection( new URL("http://vocabulary.semantic-web.at/PoolParty/sparql/cocktails" ));

TupleQueryResult tqr = myConnection.runAndParseSelectQuery(

"PREFIX skos:<http://www.w3.org/2004/02/skos/core#> \n" +

"SELECT ?label WHERE { \n" +

"<http://vocabulary.semantic-web.at/cocktails/f3000285-36b0-4ffe-af90-740c2dd8fff5> skos:narrower ?p . \n" +

"?p skos:prefLabel ?label \n" +

"}");

BindingSet bs = null;

try {

while (tqr.hasNext()) {

bs = tqr.next() ;

value = bs.getValue( "label").toString() ;

System.out.println(bs.getValue( "label"));

}} finally {

tqr.close() ;

}}

14

Page 15: SPARQLing cocktails

Display your Linked Data

"Brandy"@en

"Fortified wine"@en

"Gin"@en

"Liqueur"@en

"Rum"@en

"Schnapps"@en

"Tequila"@en

"Vodka"@en

"Whisky"@en

"Wine"@en

15 RESULTS

Page 16: SPARQLing cocktails

Display your Linked Data

private ModelAndView mavChooseIngredients;

mavChooseIngredients = new ModelAndView( "cocktails/index" );

mavChooseIngredients .addObject("myMenu", this.retrieveMainAlcoholicBeverages()) ;

[..]

public List<BindingSet> retrieveMainAlcoholicBeverages () throws

IOException, QueryEvaluationException {

return QueryResults. asList(myConnection .runAndParseSelectQuery(

"PREFIX skos:<http://www.w3.org/2004/02/skos/core#> \n" +

"SELECT ?label WHERE { \n" +

"<http://vocabulary.semantic-web.at/cocktails/f3000285-36b0-4ffe-af90-740c2dd8fff5> skos:narrower ?p . \n" +

"?p skos:prefLabel ?label \n" +

"}"

));

}

16

Page 17: SPARQLing cocktails

Display your Linked Data

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core " %>

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " %>

[...]

<c:forEach items="${myMenu}" var="bindingSet">

<li class="entity">

${bindingSet.getValue( 'label').stringValue() }

</li>

</c:forEach>

17 index.jsp

Page 19: SPARQLing cocktails

The POWER of Linked data

▸ easy change of data ▸ cost efficient▸ graph algorithms

19

Page 21: SPARQLing cocktails

Thank you!

21

Page 22: SPARQLing cocktails

Connect

Timea TurdeanTechnical Consultant, Semantic Web Company

[email protected]▸ http://at.linkedin.com/in/timeaturdean▸ http://timeaturdean.com

22

© Semantic Web Company - http://www.semantic-web.at/ and http://www.poolparty.biz/