SPARQL: An RDF Query Language

Similar documents
Linking Data with RDF

Semantic Web and Natural Language Processing

RDF AND SPARQL. Part IV: Syntax of SPARQL. Dresden, August Sebastian Rudolph ICCL Summer School

Knowledge Representation for the Semantic Web

Querying the Semantic Web

Semantic Web Information Management

MI-PDB, MIE-PDB: Advanced Database Systems

a paradigm for the Introduction to Semantic Web Semantic Web Angelica Lo Duca IIT-CNR Linked Open Data:

Semantic Web Fundamentals

SPARQL Protocol And RDF Query Language

Semantic Web Fundamentals

Semantics. KR4SW Winter 2011 Pascal Hitzler 1

Knowledge Representation for the Semantic Web

Reasoning with Rules SWRL as Example. Jan Pettersen Nytun, UIA

Final Exam. Semantic Web Wiltrud Kessler WS 2014/15. Please do not forget to write your name or initials on every sheet you hand in.

infoh509 xml & web technologies lecture 9: sparql Stijn Vansummeren February 14, 2017

SPARQL QUERY LANGUAGE WEB:

Ontological Modeling: Part 2

Introduction to Semantic Web Databases. Version 1 Prepared By: Amgad Madkour Ph.D. Candidate Purdue University April 2018

Day 2. RISIS Linked Data Course

3. Queries Applied Artificial Intelligence Prof. Dr. Bernhard Humm Faculty of Computer Science Hochschule Darmstadt University of Applied Sciences

Today: RDF syntax. + conjunctive queries for OWL. KR4SW Winter 2010 Pascal Hitzler 3

RDF AND SPARQL. Part V: Semantics of SPARQL. Dresden, August Sebastian Rudolph ICCL Summer School

KNOWLEDGE GRAPHS. Lecture 4: Introduction to SPARQL. TU Dresden, 6th Nov Markus Krötzsch Knowledge-Based Systems

FOUNDATIONS OF SEMANTIC WEB TECHNOLOGIES

SPARQL. Fausto Giunchiglia and Mattia Fumagallli. University of Trento

An Introduction to the Semantic Web. Jeff Heflin Lehigh University

CS Knowledge Representation and Reasoning (for the Semantic Web)

Semantic Information Retrieval: An Ontology and RDFbased

FOUNDATIONS OF SEMANTIC WEB TECHNOLOGIES

KNOWLEDGE GRAPHS. Lecture 11: Cypher / Knowledge Graph Quality. TU Dresden, 8th Jan Markus Krötzsch Knowledge-Based Systems

Semantic Web Technologies: Assignment 1. Axel Polleres Siemens AG Österreich

Knowledge Representation RDF Turtle Namespace

Semantics. Matthew J. Graham CACR. Methods of Computational Science Caltech, 2011 May 10. matthew graham

SEMANTIC WEB 03 RDF DATA MODEL RESOURCE DESCRIPTION FRAMEWORK IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Grid Resources Search Engine based on Ontology

From the Web to the Semantic Web: RDF and RDF Schema

Lars Schmidt-Thieme, Information Systems and Machine Learning Lab (ISMLL), University of Hildesheim, Germany, Course on XML and Semantic Web

Deep integration of Python with Semantic Web technologies

Orchestrating Music Queries via the Semantic Web

Multi-agent and Semantic Web Systems: RDF Data Structures

Extracting knowledge from Ontology using Jena for Semantic Web

SRM UNIVERSITY. : Batch1: TP1102 Batch2: TP406

Schema-Agnostic Query Rewriting in SPARQL 1.1

Semantic Web Technologies

Semantic Web Modeling Languages Part I: RDF

FOUNDATIONS OF DATABASES AND QUERY LANGUAGES

The P2 Registry

Implementing and extending SPARQL queries over DLVHEX

Using SPARQL with RDFS and OWL Entailment

2. Knowledge Representation Applied Artificial Intelligence

Semantic Web Systems Querying Jacques Fleuriot School of Informatics

FOUNDATIONS OF SEMANTIC WEB TECHNOLOGIES

Contents. G52IWS: The Semantic Web. The Semantic Web. Semantic web elements. Semantic Web technologies. Semantic Web Services

H1 Spring B. Programmers need to learn the SOAP schema so as to offer and use Web services.

Library of Congress BIBFRAME Pilot. NOTSL Fall Meeting October 30, 2015

Making BioPAX SPARQL

RDF AND SPARQL. Part III: Semantics of RDF(S) Dresden, August Sebastian Rudolph ICCL Summer School

XML and Semantic Web Technologies. III. Semantic Web / 3. SPARQL Query Language for RDF

SPARQL ME-E4300 Semantic Web,

For return on 19 January 2018 (late submission: 2 February 2018)

The Semantic Web Revisited. Nigel Shadbolt Tim Berners-Lee Wendy Hall

Semantic Days 2011 Tutorial Semantic Web Technologies

SPARQL. Dr Nicholas Gibbins

Using RDF to Model the Structure and Process of Systems

Knowledge Representation for the Semantic Web

The Semantic Web. Web Programming. Uta Priss ZELL, Ostfalia University. The Semantic Web RDF and OWL Ontologies

Ontological Modeling: Part 7

Knowledge Representation VII - IKT507. SPARQL stands for SPARQL Protocol And RDF Query Language

Chapter 3 Querying RDF stores with SPARQL

KNOWLEDGE GRAPHS. Lecture 2: Encoding Graphs with RDF. TU Dresden, 23th Oct Markus Krötzsch Knowledge-Based Systems

SEMANTIC WEB AN INTRODUCTION. Luigi De

INF3580/4580 Semantic Technologies Spring 2017

Tony Mallia Edmond Scientific

Domain Specific Semantic Web Search Engine

Table of Contents. iii

Web NDL Authorities SPARQL API Specication

PECULIARITIES OF LINKED DATA PROCESSING IN SEMANTIC APPLICATIONS. Sergey Shcherbak, Ilona Galushka, Sergey Soloshich, Valeriy Zavgorodniy

Semantic Web Engineering

Semantic Web and Python Concepts to Application development

The Implementation of Semantic Web Technology in Traditional Plant Medicine

Profiles Research Networking Software API Guide

Querying RDF & RDFS. Several query languages exist to retrieve

Ontological Modeling: Part 14

Developing markup metaschemas to support interoperation among resources with different markup schemas

Semantic reasoning for dynamic knowledge bases. Lionel Médini M2IA Knowledge Dynamics 2018

Semantic Web Solutions

SPARQL By Example: The Cheat Sheet

Knowledge Engineering with Semantic Web Technologies

Efficient Querying of Web Services Using Ontologies

RuleML and SWRL, Proof and Trust

Select all persons who belong to the class Father. SPARQL query PREFIX g: <

COMPUTER AND INFORMATION SCIENCE JENA DB. Group Abhishek Kumar Harshvardhan Singh Abhisek Mohanty Suhas Tumkur Chandrashekhara

SADI Semantic Web Services

Semantic Integration with Apache Jena and Apache Stanbol

KNOWLEDGE GRAPHS. Lecture 3: Modelling in RDF/Introduction to SPARQL. TU Dresden, 30th Oct Markus Krötzsch Knowledge-Based Systems

The Semantic Web DEFINITIONS & APPLICATIONS

Interacting with Linked Data Part I: General Introduction

Semantic-Based Web Mining Under the Framework of Agent

arxiv: v1 [cs.ai] 19 Oct 2017

Transcription:

SPARQL: An RDF Query Language Wiltrud Kessler Institut für Maschinelle Sprachverarbeitung Universität Stuttgart Semantic Web Winter 2015/16 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. http://creativecommons.org/licenses/by-nc-sa/3.0/

The Semantic Web Stack [W3C, Tim Berners-Lee] User Interface, Software Agents Trust Proof Logic, Rules Ontology, OWL SPARQL RDFS RDF XML, XMLSchema, Namespaces Encryption Digital Signatures URI Unicode, UTF-8 Wiltrud Kessler SPARQL: An RDF Query Language 2 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 3 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 4 / 49

Accessing Information from OWL Documents We have lots of information in RDF, RDFS and OWL. How do we access it? We can use reasoning to get information from OWL: What are the instances of class X? Which classes are a subclass of Z? To which classes does x belong? But sometimes we need a different type of information than OWL and the reasoner can give us: Which German labels are contained in the ontology? Which property connects the individuals x and y? Which two pizzas have a common topping? Wiltrud Kessler SPARQL: An RDF Query Language 5 / 49

SPARQL: SPARQL Protocol and RDF Query Language SPARQL is a recursive acronym for SPARQL Protocol and RDF Query Language. SPARQL is pronounced like sparkle. SPARQL is a W3C recommendation since 2008. Apart from the query language we are presenting here, SPARQL is also a protocol for the transmission of queries and a way of encoding the results in XML. SPARQL is intended for querying RDF, but OWL is based on RDF, so we can use SPARQL to query our ontologies. Wiltrud Kessler SPARQL: An RDF Query Language 6 / 49

SPARQL Basics SPARQL matches graph patterns in the query with statements that form an RDF graph. Every match of the query pattern is one returned line. If the query contains variables, the variables get bound to the parts of the triple that correspond to them. Several patterns are regarded as a conjunction (as if connected by and). Wiltrud Kessler SPARQL: An RDF Query Language 7 / 49

SPARQL Basic Pattern Matching Example query pattern:?book dbo:author dbr:j._r._r._tolkien. dbo:author?book dbr:j. R. R. Tolkien Example RDF triple: dbr:the_hobbit dbo:author dbr:j._r._r._tolkien. dbo:author dbr:the Hobbit dbr:j. R. R. Tolkien dbo:author and dbr:j._r._r._tolkien match,?book gets bound to dbr:the_hobbit.?book dbr:the Hobbit dbo:author dbo:author dbr:j. R. R. Tolkien dbr:j. R. R. Tolkien Wiltrud Kessler SPARQL: An RDF Query Language 8 / 49

SPARQL Basic Syntax Query: PREFIX dbr: <http://dbpedia.org/resource/> PREFIX dbo: <http://dbpedia.org/ontology/> SELECT?book WHERE {?book dbo:author dbr:j._r._r._tolkien. Result:?book dbr:the_hobbit dbr:the_two_towers dbr:the_return_of_the_king dbr:the_fellowship_of_the_ring dbr:the_silmarillion... Wiltrud Kessler SPARQL: An RDF Query Language 9 / 49

SPARQL Example (1) Query:?book dbo:author?author.?author rdf:type?personclass.?book dbo:literarygenre?genre. Query pattern:?book dbo:literarygenre?genre dbo:author?author RDF graph: dbr:the Hobbit rdf:type dbo:literarygenre?personclass dbr:children s literature dbo:author dbo:literarygenre dbr:fantasy dbr:j. R. R. Tolkien rdf:type dbo:writer Wiltrud Kessler SPARQL: An RDF Query Language 10 / 49

SPARQL Example (2) Matching: dbo:literarygenre?book dbo:literarygenre dbr:the Hobbit dbo:literarygenre dbo:author dbo:literarygenre dbo:author?genre dbr:children s literature?genre dbr:fantasy?author dbr:j. R. R. Tolkien rdf:type rdf:type?personclass dbo:writer Result:?book?author?personclass?genre dbr:the Hobbit dbr:j. R. R. Tolkien dbo:writer dbr:children s literature dbr:the Hobbit dbr:j. R. R. Tolkien dbo:writer dbr:fantasy Wiltrud Kessler SPARQL: An RDF Query Language 11 / 49

SPARQL Example (3) Query: PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dbo: <http://dbpedia.org/ontology/> SELECT?book?author?personclass?genre WHERE {?book dbo:author?author.?author rdf:type?personclass.?book dbo:literarygenre?genre. Result:?book?author?personclass?genre dbr:the Hobbit dbr:j. R. R. Tolkien dbo:writer dbr:children s literature dbr:the Hobbit dbr:j. R. R. Tolkien dbo:writer dbr:fantasy dbr:a Memory of Light dbr:brandon Sanderson dbo:artist dbr:fantasy literature dbr:a Memory of Light dbr:brandon Sanderson dbo:writer dbr:fantasy literature... Wiltrud Kessler SPARQL: An RDF Query Language 12 / 49

SPARQL Syntax Prefix PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX declares a namespace and the abbreviating prefix that can be used to access it. Wiltrud Kessler SPARQL: An RDF Query Language 13 / 49

SPARQL Syntax Result Clause SELECT?book?author?personclass?genre The result clause identifies what information to return from the query. Variables are denoted by a? in front of the name. Only the information bound to the variables in the result clause is returned. The number of lines returned is depending on the number of triples found, each line gives one possible way the variables can be bound to things in the graph. Wiltrud Kessler SPARQL: An RDF Query Language 14 / 49

SPARQL Syntax Query Patterns WHERE {?book dbo:author?author.?author rdf:type?personclass.?book dbo:literarygenre?genre. The query pattern is the main part of the SPARQL query. Query patterns are written in Turtle-Syntax for RDF. Query patterns are graph patterns for statements with subject, predicate and object. Patterns may contain variables replacing any part of the statement. Every pattern is terminated by a dot. A sequence of patterns is interpreted as a conjunction. Wiltrud Kessler SPARQL: An RDF Query Language 15 / 49

SPARQL and Data Types ex:bsp1 ex:p "test". ex:bsp2 ex:p "test"^^xsd:string. ex:bsp3 ex:p "test"@de. ex:bsp4 ex:p "42"^^xsd:integer. Remember: Literals are typed in RDF. SPARQL queries are sensitive to data types, exact matches are necessary. The query?subject ex:p "test". will give ex:bsp1 as the only result. To get ex:bsp3, the query?subject ex:p "test"@de. must be used. For numbers a short form?subject ex:p 42. is possible, the number is interpreted as xsd:integer, xsd:decimal, or xsd:double according to the surface form. Wiltrud Kessler SPARQL: An RDF Query Language 16 / 49

Querying Ontologies (1) We can query OWL ontologies, as OWL is based on RDF. We just have to know how the thing we want to query is defined in the ontology. For example, we want to know what pizzas are made of, i.e., query the restrictions on classes that define the possible toppings for a specific class of pizza. This is the corresponding definition of one pizza: pz:margherita rdfs:subclassof pz:namedpizza. pz:margherita rdfs:subclassof _:node1. _:node1 rdf:type owl:restriction. _:node1 owl:onproperty pz:hastopping. _:node1 owl:somevaluesfrom pz:tomatotopping To query, just replace the interesting parts with variables. Wiltrud Kessler SPARQL: An RDF Query Language 17 / 49

Querying Ontologies (2) Query: SELECT?pizza?topping WHERE {?pizza rdfs:subclassof pz:namedpizza.?pizza rdfs:subclassof?restriction.?restriction rdf:type owl:restriction.?restriction owl:onproperty pz:hastopping.?restriction owl:somevaluesfrom?topping. Result:?pizza?topping pz:american pz:peperonisausagetopping pz:american pz:tomatotopping pz:american pz:mozzarellatopping pz:margherita pz:tomatotopping pz:margherita pz:mozzarellatopping...... Wiltrud Kessler SPARQL: An RDF Query Language 18 / 49

Quiz: Basic SPARQL Queries Given are RDF statements about mountains in this form: dbr:birkenkopf rdf:type dbo:mountain. dbr:birkenkopf dbp:elevationm 511. dbr:birkenkopf dbp:name "Birkenkopf". dbr:birkenkopf dbo:locationarea dbr:baden-wurttemberg. Complete the query to retrieve name and height of all mountains: SELECT?name?elevation WHERE {... A?name rdfs:subclassof?restriction.?restriction owl:onproperty dbp:elevationm.?restriction owl:somevaluesfrom?elevation. B?name rdf:type dbo:mountain.?name dbp:elevationm?elevation. C?mountain rdf:type dbo:mountain.?mountain dbp:elevationm?elevation.?mountain dbp:name?name. Wiltrud Kessler SPARQL: An RDF Query Language 19 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 20 / 49

OPTIONAL Patterns are regarded as a conjunction, this query lists only books that have a genre specified with dbo:literarygenre, books without genre are not listed:?book dbo:author?author.?book dbo:literarygenre?genre. To list all books and then, if they have one, also their genre, we can use OPTIONAL. Books that don t have a genre are returned, but the variable?genre is unbound. Several statement can occur after OPTIONAL, they are read as a conjunction. Several OPTIONAL parts can be included in one query, they are evaluated separately. Wiltrud Kessler SPARQL: An RDF Query Language 21 / 49

OPTIONAL Example (1) Query: SELECT?book?author?genre WHERE {?book dbo:author?author. OPTIONAL {?book dbo:literarygenre?genre. Result:?book?author?genre dbr:the Hobbit dbr:j. R. R. Tolkien dbr:children s literature dbr:the Hobbit dbr:j. R. R. Tolkien dbr:fantasy dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien dbr:the Quest of Erebor dbr:j. R. R. Tolkien dbr:fantasy dbr:songs for the Philologists dbr:j. R. R. Tolkien... Wiltrud Kessler SPARQL: An RDF Query Language 22 / 49

OPTIONAL Example (2) Query: SELECT?book?author?genre?releasedate WHERE {?book dbo:author?author. OPTIONAL {?book dbo:literarygenre?genre. OPTIONAL {?book dbp:releasedate?releasedate. Result:?book?author?genre?releasedate dbr:the Hobbit dbr:j. R. R. Tolkien dbr:children s literature 1937-09-21 dbr:the Hobbit dbr:j. R. R. Tolkien dbr:fantasy 1937-09-21 dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien dbr:the Quest of Erebor dbr:j. R. R. Tolkien dbr:fantasy dbr:songs for the Philologists dbr:j. R. R. Tolkien 1936... Wiltrud Kessler SPARQL: An RDF Query Language 23 / 49

OPTIONAL Example (3) Query: SELECT?book?author?genre?releasedate WHERE {?book dbo:author?author. OPTIONAL {?book dbo:literarygenre?genre.?book dbp:releasedate?releasedate. Result:?book?author?genre?releasedate dbr:the Hobbit dbr:j. R. R. Tolkien dbr:children s literature 1937-09-21 dbr:the Hobbit dbr:j. R. R. Tolkien dbr:fantasy 1937-09-21 dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien dbr:the Quest of Erebor dbr:j. R. R. Tolkien dbr:songs for the Philologists dbr:j. R. R. Tolkien... Wiltrud Kessler SPARQL: An RDF Query Language 24 / 49

Quiz: OPTIONAL Some mountains have an alternative name (dbp:altname), some have a first person to ascent (dbo:firstascentperson). Add this information to the query (if available): SELECT?name?altname?person WHERE {?mountain rdf:type dbo:mountain.?mountain dbp:name?name.... A?mountain dbp:altname?altname.?mountain dbo:firstascentperson?person. B OPTIONAL {?mountain dbp:altname?altname. OPTIONAL {?mountain dbo:firstascentperson?person. C OPTIONAL {?mountain dbp:altname?altname.?mountain dbo:firstascentperson?person. D?mountain dbp:altname?altname. OPTIONAL {?mountain dbo:firstascentperson?person. Wiltrud Kessler SPARQL: An RDF Query Language 25 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 26 / 49

UNION A sequence of patterns in a SPARQL query is always interpreted as a conjunction (logical and). To use the logical or for parts of the pattern, pattern alternatives must be used. Pattern alternatives are specified with the UNION keyword. Wiltrud Kessler SPARQL: An RDF Query Language 27 / 49

UNION Example Query: SELECT?book?author?releasedate WHERE {?book dbo:author?author. {?book dbp:releasedate?releasedate. UNION {?book dbp:pubdate?releasedate. Result:?book?author?releasedate dbr:the Hobbit dbr:j. R. R. Tolkien 1937-09-21 dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien 1962 dbr:the Quest of Erebor dbr:j. R. R. Tolkien 1980 dbr:songs for the Philologists dbr:j. R. R. Tolkien 1936... Wiltrud Kessler SPARQL: An RDF Query Language 28 / 49

Combining OPTIONAL and UNION OPTIONAL and UNION are left-associative: pattern OPTIONAL { pattern OPTIONAL { pattern is equivalent to { pattern OPTIONAL { pattern OPTIONAL { pattern Both operators have the same precedence: {s1 p1 o1 OPTIONAL {s2 p2 o2 UNION {s3 p3 o3 OPTIONAL {s4 p4 o4 OPTIONAL {s5 p5 o5 is equivalent to { { { {s1 p1 o1 OPTIONAL {s2 p2 o2 UNION {s3 p3 o3 OPTIONAL {s4 p4 o4 OPTIONAL {s5 p5 o5 Additional { may always be used to group patterns. Wiltrud Kessler SPARQL: An RDF Query Language 29 / 49

OPTIONAL and UNION Example (1) Query: SELECT?book?author?releasedate WHERE {?book dbo:author?author. OPTIONAL { {?book dbp:releasedate?releasedate. UNION {?book dbp:pubdate?releasedate. Result:?book?author?releasedate dbr:the Hobbit dbr:j. R. R. Tolkien 1937-09-21 dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien 1962 dbr:the Quest of Erebor dbr:j. R. R. Tolkien 1980 dbr:songs for the Philologists dbr:j. R. R. Tolkien 1936 dbr:the Children of Húrin dbr:j. R. R. Tolkien... Wiltrud Kessler SPARQL: An RDF Query Language 30 / 49

OPTIONAL and UNION Example (2) Query: SELECT?book?author?releasedate?illustrator WHERE {?book dbo:author?author. {?book dbp:releasedate?releasedate. UNION {?book dbp:pubdate?releasedate. OPTIONAL {?book dbo:illustrator?illustrator. Result:?book?author?releasedate?illustrator dbr:the Hobbit dbr:j. R. R. Tolkien 1937-09-21 dbr:j. R. R. Tolkien dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien 1962 dbr:pauline Baynes dbr:the Quest of Erebor dbr:j. R. R. Tolkien 1980 dbr:songs for the Philologists dbr:j. R. R. Tolkien 1936... Wiltrud Kessler SPARQL: An RDF Query Language 31 / 49

OPTIONAL and UNION Example (3) Query: SELECT?book?author?releasedate?illustrator WHERE {?book dbo:author?author. {?book dbp:releasedate?releasedate. UNION {?book dbp:pubdate?releasedate. OPTIONAL {?book dbo:illustrator?illustrator. Result:?book?author?releasedate?illustrator dbr:the Hobbit dbr:j. R. R. Tolkien 1937-09-21 dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien 1962 dbr:pauline Baynes dbr:the Quest of Erebor dbr:j. R. R. Tolkien 1980 dbr:songs for the Philologists dbr:j. R. R. Tolkien 1936... Wiltrud Kessler SPARQL: An RDF Query Language 32 / 49

Quiz: UNION Some locations are given with dbo:locationarea some with dbp:location. Replace the given query pattern to handle both: SELECT?mountain?location WHERE {?mountain rdf:type dbo:mountain.?mountain dbo:locationarea?location. A?mountain rdf:type dbo:mountain. {?mountain dbo:locationarea?location. UNION {?mountain dbp:location?location. B {?mountain rdf:type dbo:mountain.?mountain dbo:locationarea?location. UNION {?mountain dbp:location?location. C?mountain rdf:type dbo:mountain.?mountain dbo:locationarea?location. OPTIONAL {?mountain dbp:location?location. Wiltrud Kessler SPARQL: An RDF Query Language 33 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 34 / 49

FILTER Sometimes it is useful to restrict the values of a variable. Example: List all books published after 1970. FILTER can be used to filter out lines in the result for which the filter expression evaluates to false. A filter is always applied to the whole pattern group in which the filter appears. Filters can be connected with boolean operators: &&,,! Wiltrud Kessler SPARQL: An RDF Query Language 35 / 49

FILTER Example (1) Query: SELECT?book?author?releasedate WHERE {?book dbo:author?author. {?book dbp:releasedate?releasedate. UNION {?book dbp:pubdate?releasedate. FILTER (?releasedate > 1950 ) Result:?book?author?releasedate dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien 1962 dbr:the Quest of Erebor dbr:j. R. R. Tolkien 1980 dbr:songs for the Philologists dbr:j. R. R. Tolkien 1936... Wiltrud Kessler SPARQL: An RDF Query Language 36 / 49

FILTER Example (2) Query: SELECT?book?author?releasedate WHERE {?book dbo:author?author. {?book dbp:releasedate?releasedate. UNION {?book dbp:pubdate?releasedate. FILTER (?releasedate > 1970 ) Result:?book?author?releasedate dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien dbr:the Quest of Erebor dbr:j. R. R. Tolkien 1980 dbr:songs for the Philologists dbr:j. R. R. Tolkien 1936... Wiltrud Kessler SPARQL: An RDF Query Language 37 / 49

FILTER Syntax For numerical types, different operators for comparison are defined: <, =, >, <=, >=,!= For other types, only = and!= are defined. Literals of incompatible types cannot be compared. Arithmetic operators can be used in filters: FILTER (?weight / (?height *?height) >= 25 ) There are several other predefined filters (see literature), e.g.: BOUND(A) true if A is a bound variable isuri(a) true if A is a URI isliteral(a) true if A is a literal DATATYPE(A) returns the URI of the datatype of A REGEX(A,B) true if regular expression B can be found in A Wiltrud Kessler SPARQL: An RDF Query Language 38 / 49

FILTER Example (3) Query: SELECT?book?author?releasedate WHERE {?book dbo:author?author. {?book dbp:releasedate?releasedate. UNION {?book dbp:pubdate?releasedate. FILTER (?releasedate > 1950 &&!((?releasedate + 5) >= 1970) ) Result:?book?author?releasedate dbr:the Adventures of Tom Bombadil dbr:j. R. R. Tolkien 1962... Wiltrud Kessler SPARQL: An RDF Query Language 39 / 49

Quiz: FILTER Determine at which point the line FILTER (?elevation > 8000) has to be inserted, so that only 8000ers are retrieved. 1 SELECT?mountain?name?elevation?location 2 WHERE { 3?mountain rdf:type dbo:mountain. 4?mountain dbp:name?name. 5 [A] 6 OPTIONAL { 7?mountain dbp:location?location. 8 [B] 9 10 OPTIONAL { 11?mountain dbp:elevationm?elevation. 12 [C] 13 14 [D] 15 Wiltrud Kessler SPARQL: An RDF Query Language 40 / 49

More... Modifiers to influence the presentation: ORDER BY establishes the ordering of the resulting lines by some variable, ascending or descending. LIMIT limits the number of solutions returned. OFFSET causes the solutions generated to start after the specified number of solutions (only predictable in combination with ORDER BY). DISTINCT eliminates duplicate solutions (a solution that binds the same variables to the same RDF terms as another solution). Different query forms, besides SELECT covered here, there is ASK, DESCRIBE and CONSTRUCT. Wiltrud Kessler SPARQL: An RDF Query Language 41 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 42 / 49

Explicit and Inferred Information SELECT?author WHERE {?book dbo:author?author.?author rdf:type dbo:person. In the ontology we have several subclasses of dbo:person (e.g. dbo:writer or dbo:athlete) and some instances of these. What is the result of the above query? We would expect to get all authors if something is a instance of a subclass of Person, it is also an instance of Person. Unfortunately this does not work, because SPARQL does no inference, it queries only what is explicitly contained in the graph (it queries RDF, not RDFS or OWL!). Wiltrud Kessler SPARQL: An RDF Query Language 43 / 49

How to Include Inferred Information We cannot (or don t want to) enumerate all posible levels of subclasses of subclasses of... We want to query inferred information! But SPARQL is only a query language, there is no inference in the query language itself. Fortunately, some implementations (e.g., Jena) have a reasoner included. Otherwise, it is possible to let a reasoner work on the ontology and then export the ontology including inferred statements (in Protégé this should work with Menu Export inferred axioms as ontology ). Wiltrud Kessler SPARQL: An RDF Query Language 44 / 49

Exercise: SPARQL Write some SPARQL queries that your application might use. Discuss your solution with your neighbour. Example: List the title and year of writing (if available) of all songs written by Michael Jackson. SELECT?title?year WHERE {?song rdf:type ex:song.?song ex:writtenby ex:michaeljackson.?song ex:hastitle?title. OPTIONAL {?song ex:writtenin?date.?date ex:hasyear?year. Wiltrud Kessler SPARQL: An RDF Query Language 45 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 46 / 49

Summary: SPARQL matches graph patterns in the query with an RDF graph, if the query contains variables, the variables get bound to the parts of the triple that correspond to them. Several patterns are regarded as a conjunction, to express a disjunction ( or ), UNION can be used. OPTIONAL can be used to include the information if available, otherwise the result is returned with the variable unbound. FILTER can be used to filter out lines in the result for which the filter expression evaluates to false. We can only query explicit information, to access inferred information, a reasoner must be run on the ontology first. Wiltrud Kessler SPARQL: An RDF Query Language 47 / 49

Outline Basic SPARQL OPTIONAL UNION FILTER Inference Summary References Wiltrud Kessler SPARQL: An RDF Query Language 48 / 49

Suggested Reading [HKRS08] Pascal Hitzler, Markus Krötzsch, Sebastian Rudolph and York Sure. Semantic Web. Grundlagen. Springer textbook, 2008. (Chapter 7) [HKR09] Pascal Hitzler, Markus Krötzsch and Sebastian Rudolph. Foundations of Semantic Web Technologies. Chapman & Hall/CRC, 2009. (Chapter 7) Wiltrud Kessler SPARQL: An RDF Query Language 49 / 49