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

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

XML and Semantic Web Technologies. III. Semantic Web / 1. Ressource Description Framework (RDF)

Querying the Semantic Web

SPARQL QUERY LANGUAGE WEB:

Day 2. RISIS Linked Data Course

Implementing and extending SPARQL queries over DLVHEX

SPARQL. Fausto Giunchiglia and Mattia Fumagallli. University of Trento

SPARQL. Dr Nicholas Gibbins

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

Semantic Days 2011 Tutorial Semantic Web Technologies

MI-PDB, MIE-PDB: Advanced Database Systems

Semantic Web Systems Querying Jacques Fleuriot School of Informatics

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

SPAR-QL. Mario Arrigoni Neri

Building Blocks of Linked Data

Querying RDF & RDFS. Several query languages exist to retrieve

Semantic Web Information Management

BASI DI DATI II 2 modulo Parte VIII: SPARQL

RESOURCES DESCRIPTION FRAMEWORK: RDF

Multi-agent and Semantic Web Systems: Querying

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

Unit 1 a Bird s Eye View on RDF(S), OWL & SPARQL

2. RDF Semantic Web Basics Semantic Web

Semantic Web. Querying on the Web: XQuery, RDQL, SparQL. Morteza Amini. Sharif University of Technology Fall 94-95

Introducing Linked Data

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

Unit 2 RDF Formal Semantics in Detail

SPARQL ME-E4300 Semantic Web,

BUILDING THE SEMANTIC WEB

Querying Semantic Web Data

Resource Description Framework (RDF)

Chapter 3 Querying RDF stores with SPARQL

Linked Data: What Now? Maine Library Association 2017

Formalising the Semantic Web. (These slides have been written by Axel Polleres, WU Vienna)

Reminder: RDF triples

Jena.

RuleML and SWRL, Proof and Trust

SPARQL: An RDF Query Language

Ontological Modeling: Part 2

XML and Semantic Web Technologies. II. XML / 6. XML Query Language (XQuery)

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

Interacting with Linked Data Part I: General Introduction

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

SPARQL เอกสารหล ก ใน มคอ.3

Simplified RDF Syntax for Power System Model Exchange

Web NDL Authorities SPARQL API Specication

Profiles Research Networking Software API Guide

SEMANTIC WEB 07 SPARQL TUTORIAL BY EXAMPLE: DBPEDIA IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Semantic Web In Depth: Resource Description Framework. Dr Nicholas Gibbins 32/4037

Part II. Representation of Meta-Information

COMP9321 Web Application Engineering

SADI Semantic Web Services

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

Semantic Web Technologies

Mapping Relational Data to RDF with Virtuoso's RDF Views

Web 2.0 Lecture 8: Resource Description Framework

Introduction to a Web of Linked Data

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

Query. Ewan Klein. MASWS 12 February Multi-agent Semantic Web Systems: Query. Ewan Klein. Outline. Introduction RSS.

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

INF3580/4580 Semantic Technologies Spring 2017

2. Knowledge Representation Applied Artificial Intelligence

Finding Similarity and Comparability from Merged Hetero Data of the Semantic Web by Using Graph Pattern Matching

Linking Data with RDF

Storage and Querying

Linked data and its role in the semantic web. Dave Reynolds, Epimorphics

On the Semantics of Heterogeneous Querying of Relational, XML and RDF Data with XSPARQL

Today s Plan. 1 Repetition: RDF. 2 Jena: Basic Datastructures. 3 Jena: Inspecting Models. 4 Jena: I/O. 5 Example. 6 Jena: ModelFactory and ModelMaker

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

Semantic Web. Lecture 12: SW Programming Dr. Knarig Arabshian

Filter: Evaluable Expression. List Syntax. Tests. Filter: Evaluable Expression. Tests : functions. Tests 06/09/2013. (1?x v )

Today s Plan. 1 Repetition: RDF. 2 Jena: Basic Datastructures. 3 Jena: Inspecting Models. 4 Jena: I/O. 5 Example. 6 Jena: ModelFactory and ModelMaker

Outline RDF. RDF Schema (RDFS) RDF Storing. Semantic Web and Metadata What is RDF and what is not? Why use RDF? RDF Elements

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

The Hoonoh Ontology for describing Trust Relationships in Information Seeking

Semantic Web and Python Concepts to Application development

Scalable Reduction of Large Datasets to Interesting Subsets

Enabling Semantic Web Programming by Integrating RDF and Common Lisp

Making BioPAX SPARQL

RDFS. Suresh Manandhar* & Dimitar Kazakov

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

SPARQL Protocol And RDF Query Language

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

An RDF-based Distributed Expert System

Using SPARQL with RDFS and OWL Entailment

KBS Knowledge-Based Systems Group

XML and Semantic Web Technologies. II. XML / 3. XML Namespaces

Semantic Web. Repositories. Copyright 2009 Dieter Fensel and Federico Facca

INF3580/4580 Semantic Technologies Spring 2015

Mustafa Jarrar: Lecture Notes on RDF Schema Birzeit University, Version 3. RDFS RDF Schema. Mustafa Jarrar. Birzeit University

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

Web Science & Technologies University of Koblenz Landau, Germany. RDF Schema. Steffen Staab. Semantic Web

Semantic Web. RDF and RDF Schema. Morteza Amini. Sharif University of Technology Spring 90-91

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

An Introduction to Linked Open Data

OSM Lecture (14:45-16:15) Takahira Yamaguchi. OSM Exercise (16:30-18:00) Susumu Tamagawa

TRIPLE An RDF Query, Inference, and Transformation Language

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

Semantic Web. MPRI : Web Data Management. Antoine Amarilli Friday, January 11th 1/29

The Semantic Web. What is the Semantic Web?

Transcription:

XML and Semantic Web Technologies XML and Semantic Web Technologies III. Semantic Web / 3. SPARQL Query Language for RDF Lars Schmidt-Thieme Information Systems and Machine Learning Lab (ISMLL) Institute of Economics and Information Systems & Institute of Computer Science University of Hildesheim http://www.ismll.uni-hildesheim.de Course on XML and Semantic Web Technologies, summer term 2009 1/45 XML and Semantic Web Technologies III. Semantic Web / 3. SPARQL Query Language for RDF 1. Basic SPARQL queries 2. Conditional SPARQL Queries 3. Metaqueries/Returning RDF 4. A small integrated example Course on XML and Semantic Web Technologies, summer term 2009 1/45

XML and Semantic Web Technologies / 1. Basic SPARQL queries SPARQL Specification The SPARQL specification consists of the following parts: SPARQL Query Language for RDF (REC-2008/01/15) SPARQL Query Results XML Format (REC-2008/01/15) SPARQL Protocol for RDF (REC 2008/01/15) as well as a further document on use cases and requirements. SPARQL is a query language for RDF that has a non-xml syntax, makes use of (parts of) XPath as expression language, makes use of N3 notation Course on XML and Semantic Web Technologies, summer term 2009 1/45 XML and Semantic Web Technologies / 1. Basic SPARQL queries A simple SPARQL query 1@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. 2@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. 3@prefix : <http://www.cgnm.de/rdf/sokrates.rdfs#>. 4:Mortal rdf:type rdfs:class. 5:Human rdf:type rdfs:class. 6:Human rdfs:subclassof :Mortal. 7:Sokrates rdf:type :Human. Figure 1: A sample RDF data file (here in N3, but any notation will do). 1select?c 2where { <http://www.cgnm.de/rdf/sokrates.rdfs#sokrates> 3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 4?c } Figure 2: A simple SPARQL query. Course on XML and Semantic Web Technologies, summer term 2009 2/45

XML and Semantic Web Technologies / 1. Basic SPARQL queries Executing SPARQL queries SPARQL is implemented in ARQ, the Jena query processor (by HP). sparql data sokrates.n3 query sokrates-simple.sq 1------------------------------------------------ 2 c 3 ================================================ 4 <http://www.cgnm.de/rdf/sokrates.rdfs#human> 5------------------------------------------------ Figure 3: The result of the simple SPARQL query. SPARQL operates on a RDF graph / set of triples explicitly materialized or specified implicitely by a an explicitely materialized RDF graph and a set of inference rules. The ready-to-use commandline tools of ARQ do not support inference yet. Course on XML and Semantic Web Technologies, summer term 2009 3/45 XML and Semantic Web Technologies / 1. Basic SPARQL queries Executing SPARQL queries on Windows download ARQ, the Jena query processor, from http://jena.sourceforge.net/arq/ (current version is 2.7.0). add environment variable ARQROOT, pointing to the root path where ARQ is extracted to. add %ARQROOT%\bat to system path. Course on XML and Semantic Web Technologies, summer term 2009 4/45

XML and Semantic Web Technologies / 1. Basic SPARQL queries Graph Pattern A SPARQL query basically consists of a query graph pattern (plus some additional information). The simplest form of a graph pattern is a sequence of triples in N3 notation, i.e., syntax meaning < URI > relative URI reference " string " untyped literal " string "^^< URI > typed literal _: NCName anonymous node name integer =" integer " ^^xsd:integer double =" double " ^^xsd:double true, false =" true" ^^xsd:boolean? NCName variable Course on XML and Semantic Web Technologies, summer term 2009 5/45 XML and Semantic Web Technologies / 1. Basic SPARQL queries Graph Pattern Matching The basic operation is to retrieve all substitutions of the variables s.t. the query graph pattern after substitution is a subgraph of the source graph. Course on XML and Semantic Web Technologies, summer term 2009 6/45

XML and Semantic Web Technologies / 1. Basic SPARQL queries In SPARQL, URIs could be abbreviated either by declaring a namespace prefix and using QNames (as in N3) or by declaring a base URI and using relative URIs. 1 prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 2prefix : <http://www.cgnm.de/rdf/sokrates.rdfs#> 3select?c 4where { :Sokrates rdf:type?c } Figure 4: Simple SPARQL query using namespace prefixes. 1base <http://www.cgnm.de/rdf/sokrates.rdfs> 2 prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 3select?c 4where { <#Sokrates> rdf:type?c } Figure 5: Simple SPARQL query using namespace prefixes and a base URI. Course on XML and Semantic Web Technologies, summer term 2009 7/45 XML and Semantic Web Technologies / 1. Basic SPARQL queries SPARQL query syntax Query := ( base QuotedURIref )? ( prefix NCNAME_PREFIX? : QuotedURIref )* (ask selectdistinct? ( Var + *) construct ConstructTemplate describe ( ( Var URI )+ *) ) (fromnamed? URI )* ( where GraphPattern )? (orderby OrderCondition + )? (limit INTEGER )? (offset INTEGER )? There is at most one unnamedfrom clause allowed (background graph). Comments start with#. URI := QuotedURIref QName Var := ($?) StringLiteral Course on XML and Semantic Web Technologies, summer term 2009 8/45

XML and Semantic Web Technologies / 1. Basic SPARQL queries Kinds of SPARQL Queries / Outputs SPARQL supports 3 1 2 different query types: 1.ask returnstrue, if there is at least one substitution, elsefalse, 2.select returns a the set of substitution tuples (like SQL), 3. construct returns a RDF graph build from the substition tuples and a template (eventually a subgraph of the original graph or a newly constructed graph), 4. describe also returns a RDF graph with some implementation-dependent contents. Course on XML and Semantic Web Technologies, summer term 2009 9/45 XML and Semantic Web Technologies III. Semantic Web / 3. SPARQL Query Language for RDF 1. Basic SPARQL queries 2. Conditional SPARQL Queries 3. Metaqueries/Returning RDF 4. A small integrated example Course on XML and Semantic Web Technologies, summer term 2009 10/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries A Fresh Example 1@prefix xs: <http://www.w3.org/2001/xmlschema#>. 2@prefix : <http://www.cgnm.de/rdf/family#>. 3:Anne :age "45"^^xs:integer; :marriedto :Bert ; :motherof :Clara, :Dennis. 4:Bert :age "49"^^xs:integer; :marriedto :Anne ; :fatherof :Clara, :Dennis. 5:Clara :age "24"^^xs:integer; :marriedto :Emil; :motherof :Fred, :Gisa. 6:Dennis :age "22"^^xs:integer. 7:Emil :age "27"^^xs:integer; :marriedto :Clara; :fatherof :Fred, :Gisa. 8:Fred :age "2"^^xs:integer. 9:Gisa :age "1"^^xs:integer. Figure 6: A fresh example. Course on XML and Semantic Web Technologies, summer term 2009 10/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Kinds of Graph Patterns GraphPattern :={ PatternElement (. PatternElement )*} PatternElement := Triples GraphPattern optional? GraphPattern filter Expression GraphPattern union GraphPattern * graph ( Var BlankNode URI ) GraphPattern Course on XML and Semantic Web Technologies, summer term 2009 11/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Optional Graph Patterns 1prefix : <http://www.cgnm.de/rdf/family#> 1 x y 2select * 3where {?x :marriedto?y } Figure 7: Query for married persons. 2 =================== 3 :Emil :Clara 4 :Anne :Bert 5 :Bert :Anne 6 :Clara :Emil 7------------------- Figure 8: Result. 1prefix : <http://www.cgnm.de/rdf/family#> 1------------------------- 2select * 3where {?x :age?z. 4 optional {?x :marriedto?y } } Figure 9: Query for all persons and their spouse (if any). 2 x z y 3 ========================= 4 :Fred 2 5 :Emil 27 :Clara 6 :Bert 49 :Anne 7 :Anne 45 :Bert 8 :Clara 24 :Emil 9 :Dennis 22 10 :Gisa 1 11------------------------- Course on XML and Semantic Web Technologies, summer term 2009 12/45 Figure 10: Result. XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Constraints /filter 1prefix : <http://www.cgnm.de/rdf/family#> 1---------------- 2select * 2 x z 3where {?x :age?z. 3 ================ 4 filter (?z >= 18) } Figure 11: Query for all adult persons. 4 :Emil 27 5 :Bert 49 6 :Anne 45 7 :Clara 24 8 :Dennis 22 9---------------- Figure 12: Result. Course on XML and Semantic Web Technologies, summer term 2009 13/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Constraints /filter (2) Also " negative" queries are possible. 1prefix : <http://www.cgnm.de/rdf/family#> 1----------------- 2 s 2 age 3SELECT?s?age 4WHERE {?s :age?age. 3 ================= 4 :Gisa 1 5 optional {?s :marriedto?x}. 5 :Fred 2 6 filter (!bound(?x)) 6 :Dennis 22 7} 7----------------- Figure 13: Query for all non-marries persons. Figure 14: Result. Course on XML and Semantic Web Technologies, summer term 2009 14/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Unions of Graph Patterns ("group patterns") /union 1prefix : <http://www.cgnm.de/rdf/family#> 1-------------------------- 2select * 3where { {?x :marriedto?y } 4 union {?x :age?z. 5 filter (?z < 18) } } Figure 15: Query for all married persons and all non-adults. 2 x y z 3 ========================== 4 :Emil :Clara 5 :Anne :Bert 6 :Bert :Anne 7 :Clara :Emil 8 :Fred 2 9 :Gisa 1 10-------------------------- Figure 16: Result. Tuples matching several operand graph patterns of a union graph pattern are contained several times in the result. Course on XML and Semantic Web Technologies, summer term 2009 15/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Restricting returned values (1) /filter,union 1prefix : <http://www.cgnm.de/rdf/family#> 1---------------- 2select?x?z 3where { 4 { 5?x :age?z. 6 filter (?z >= 18) 7 } union { 8?x :age?y. 9 filter(?y < 18) 10 } 11 } Figure 17: Query for all persons, ignoring age is. 2 x z 3 ================ 4 :Emil 27 5 :Dennis 22 6 :Clara 24 7 :Bert 49 8 :Anne 45 9 :Gisa 10 :Fred 11---------------- Figure 18: Result. Course on XML and Semantic Web Technologies, summer term 2009 16/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Querying Several Sources /graph SPARQL can deal with several sources at once: there is one unnamed default source (background graph) and there are arbitrary many named further sources (named graphs) (where names are specified as URIs) In thefrom clause (and in implementations), the URI names of a source often are used to locate and retrieve the resource. E.g., in ARQ named sources can be explicitly given by the--namedgraph <URI> commandline option, where the file or http URI is used to retrieve the source. Course on XML and Semantic Web Technologies, summer term 2009 17/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Querying Several Sources / Example (1/2) 1@prefix xs: <http://www.w3.org/2001/xmlschema#>. 2@prefix : <http://www.cgnm.de/rdf/family-miller#>. 3@prefix r: <http://www.cgnm.de/rdf/relatives#>. 4:Anne r:age "45"^^xs:integer; r:marriedto :Bert ; r:motherof :Clara, :Dennis. 5:Bert r:age "49"^^xs:integer; r:marriedto :Anne ; r:fatherof :Clara, :Dennis. 6:Clara r:age "24"^^xs:integer; r:marriedto :Emil; r:motherof :Fred, :Gisa. 7:Dennis r:age "22"^^xs:integer. 8:Emil r:age "27"^^xs:integer; r:marriedto :Clara; r:fatherof :Fred, :Gisa. 9:Fred r:age "2"^^xs:integer. 10:Gisa r:age "1"^^xs:integer. Figure 19: Data about the Miller family. 1@prefix xs: <http://www.w3.org/2001/xmlschema#>. 2@prefix miller: <http://www.cgnm.de/rdf/family-miller#>. 3@prefix r: <http://www.cgnm.de/rdf/relatives#>. 4@prefix : <http://www.cgnm.de/rdf/family-smith#>. 5:Adam r:age "52"^^xs:integer; r:marriedto :Britta ; r:motherof :Emil. 6:Emil r:age "27"^^xs:integer; r:marriedto miller:clara; r:fatherof miller:fred, miller:g Figure 20: Data about the Smith family. Course on XML and Semantic Web Technologies, summer term 2009 18/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Querying Several Sources / Example (2/2) 1prefix : <http://www.cgnm.de/rdf/relatives#> 2prefix miller: <http://www.cgnm.de/rdf/family-miller#> 3prefix smith: <http://www.cgnm.de/rdf/family-smith#> 4select * 5where { 6 {?x :marriedto?y } 7 union 8 { graph <file:family-smith.n3> 9 {?x :marriedto?y } } 10} Figure 21: Query for all married people in these two families. 1 x y 2 =============================== 3 miller:clara miller:emil 4 miller:anne miller:bert 5 miller:bert miller:anne 6 miller:emil miller:clara 7 smith:emil miller:clara 8 smith:adam smith:britta 9------------------------------- Figure 22: Result. Course on XML and Semantic Web Technologies, summer term 2009 19/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Specifying Data Sources /from clause from may be used to explicitly point to a target resource URI. 1prefix : <http://www.cgnm.de/rdf/family-miller#> 2prefix r: <http://www.cgnm.de/rdf/relatives#> 3 4SELECT * 5FROM <file:family-miller.n3> 6WHERE { 7?s r:age?age. FILTER(?age >30) 8} Figure 23: Sample Query forfrom. 1--------------- 2 s age 3 =============== 4 :Bert 49 5 :Anne 45 6--------------- Figure 24: Result. Course on XML and Semantic Web Technologies, summer term 2009 20/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Specifying Data Sources /from named clause from named may be used to use arbitrarily many background graphs in the query. URIs are resolved against the namespace declarations in the query. 1prefix r: <http://www.cgnm.de/rdf/relatives#> 2prefix miller: <http://www.cgnm.de/rdf/family-miller#> 3prefix smith: <http://www.cgnm.de/rdf/family-smith#> 4SELECT?age?s 5FROM NAMED <file:family-smith.n3> 6WHERE { 7 graph <file:family-smith.n3> {?s r:age?age. FILTER(?age >30)} 8} Figure 25: Querying the ages of persons in the smith family. 1-------------------- 2 age s 3 ==================== 4 52 smith:adam 5-------------------- Figure 26: Result. Course on XML and Semantic Web Technologies, summer term 2009 21/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Specifying Data Sources /from named clause (2) from named may be used to use arbitrarily many background graphs in the query. URIs are resolved against the namespace declarations in the query. 1prefix r: <http://www.cgnm.de/rdf/relatives#> 2prefix miller: <http://www.cgnm.de/rdf/family-miller#> 3prefix smith: <http://www.cgnm.de/rdf/family-smith#> 4SELECT?g?age?s 5FROM NAMED <file:family-smith.n3> 6FROM NAMED <file:family-miller.n3> 7WHERE { 8 graph?g {?s r:age?age. FILTER(?age >30)} 9} Figure 27: Querying the ages of persons in different background graphs. 1------------------------------------------ 2 g age s 3 ========================================== 4 <family-smith.n3> 52 smith:adam 5 <family-miller.n3> 49 miller:bert 6 <family-miller.n3> 45 miller:anne 7------------------------------------------ Figure 28: Result. Course on XML and Semantic Web Technologies, summer term 2009 22/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Querying implicit Data Sources One Drawback of from/ from named is that background graphs need to be known at query writing time. This holds only true in very rare situations. It is (e.g. with ARQ) easily possible, to dynamically add (named) background graphs (" context" s) using the command line. Course on XML and Semantic Web Technologies, summer term 2009 23/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Querying implicit Data Sources / Example sparql namedgraph family-miller.n3 query from-implicit-named.sq 1prefix r: <http://www.cgnm.de/rdf/relatives#> 2prefix miller: <http://www.cgnm.de/rdf/family-miller#> 3prefix smith: <http://www.cgnm.de/rdf/family-smith#> 4SELECT?g?age?s 5WHERE { 6 graph?g {?s r:age?age}. 7 FILTER(?age >30) 8} Figure 29: Querying the ages of persons in unknown named background graphs. 1------------------------------------------ 2 g age s 3 ========================================== 4 <family-miller.n3> 49 miller:bert 5 <family-miller.n3> 45 miller:anne 6------------------------------------------ Figure 30: Result. Course on XML and Semantic Web Technologies, summer term 2009 24/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Querying implicit Data Sources / Example (2) sparql namedgraph family-miller.n3 namedgraph family-smith.n3 query from-implicit-named.sq 1prefix r: <http://www.cgnm.de/rdf/relatives#> 2prefix miller: <http://www.cgnm.de/rdf/family-miller#> 3prefix smith: <http://www.cgnm.de/rdf/family-smith#> 4SELECT?g?age?s 5WHERE { 6 graph?g {?s r:age?age}. 7 FILTER(?age >30) 8} Figure 31: Querying the ages of persons in unknown named background graphs. 1------------------------------------------ 2 g age s 3 ========================================== 4 <family-smith.n3> 52 smith:adam 5 <family-miller.n3> 49 miller:bert 6 <family-miller.n3> 45 miller:anne 7------------------------------------------ Figure 32: Result. Course on XML and Semantic Web Technologies, summer term 2009 25/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Querying implicit Data Sources / Example (3) But: If triples are loaded as a background graph, they have to be queried from that location. sparql namedgraph family-miller.n3 query from-implicit-named.sq 1prefix r: <http://www.cgnm.de/rdf/relatives#> 2SELECT?age?s 3WHERE { 4?s r:age?age. FILTER(?age >30) 5} Figure 33: Querying the ages of persons in the default background graph. 1----------- 2 age s 3 =========== 4----------- Figure 34: Result. Course on XML and Semantic Web Technologies, summer term 2009 26/45 XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Sorting /order clause OrderCondition := FunctionCall Var (asc desc )(( FunctionCall Var )) 1prefix : <http://www.cgnm.de/rdf/family#> 1---------------- 2select * 3where {?x :age?z } 4order by desc (?z) desc (?x) 5 Figure 35: Query for all persons sorted by descending age and ascending URI. 2 x z 3 ================ 4 :Bert 49 5 :Anne 45 6 :Emil 27 7 :Clara 24 8 :Dennis 22 9 :Fred 2 10 :Gisa 1 11---------------- Figure 36: Result. Course on XML and Semantic Web Technologies, summer term 2009 27/45

XML and Semantic Web Technologies / 2. Conditional SPARQL Queries Simple Cursor Functionalities /limit andoffset clause 1prefix : <http://www.cgnm.de/rdf/family#> 1---------------- 2select * 2 x z 3where {?x :age?z } 4order by desc (?x) 5limit 2 6offset 3 3 ================ 4 :Clara 24 5 :Dennis 22 6---------------- Figure 37: Query for a subset of all persons sorted by descending age and ascending URI. Figure 38: Result. Course on XML and Semantic Web Technologies, summer term 2009 28/45 XML and Semantic Web Technologies III. Semantic Web / 3. SPARQL Query Language for RDF 1. Basic SPARQL queries 2. Conditional SPARQL Queries 3. Metaqueries/Returning RDF 4. A small integrated example Course on XML and Semantic Web Technologies, summer term 2009 29/45

XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF ask - Querying Statement existence It is possible to ask whether statement(s) in a source graph exist. 1prefix : <http://www.cgnm.de/rdf/family#> 2 3ask { 4 :Emil :fatherof :Fred. 5} Figure 39: Is Emil the father of Fred? 1Ask => Yes Figure 40: Result. Course on XML and Semantic Web Technologies, summer term 2009 29/45 XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF ask - a more complex example 1prefix : <http://www.cgnm.de/rdf/family#> 2 3ask { 4?x :fatherof?y. 5?x :age?age. 6 FILTER (?age <= 18). 7} Figure 41: A more complex example. 1Ask => No Figure 42: Result. Course on XML and Semantic Web Technologies, summer term 2009 30/45

XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF Selecting Subgraphs of the Source It is possible to copy a whole source on basis of its triples. 1select * where {?s?p?o } Figure 43: Query for all triples in the source. In the same manner, subsets of triples meeting some conditions can be selected, resulting in a subgraph of the source. Course on XML and Semantic Web Technologies, summer term 2009 31/45 XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF Creating New Triples New triples can be created by theconstruct query, a graph template that contains only triples and variables (but nooptional,graph etc. statements). ConstructTemplate :={ Triples (. Triples )*.? } The template is instantiated once for each result tuple, whereat variables are substituted by the values of result tuples. Course on XML and Semantic Web Technologies, summer term 2009 32/45

XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF Example 1prefix : <http://www.cgnm.de/rdf/family#> 2prefix r: <http://www.cgnm.de/rdf/relatives#> 3construct { 4?x r:marriedto?spouse. 5?x r:ismarried true } 6where {?x :marriedto?spouse } 7 Figure 44: Query that recodes the marriedof property. 1@prefix r: <http://www.cgnm.de/rdf/relativ 2@prefix xs: <http://www.w3.org/2001/xml 3@prefix : <http://www.cgnm.de/rdf/family# 4 5:Anne r:ismarried "true"^^xs:boolean ; 6 r:marriedto :Bert. 7:Emil r:ismarried "true"^^xs:boolean ; 8 r:marriedto :Clara. 9:Clara r:ismarried "true"^^xs:boolean ; 10 r:marriedto :Emil. 11:Bert r:ismarried "true"^^xs:boolean ; 12 r:marriedto :Anne. Figure 45: Result. Course on XML and Semantic Web Technologies, summer term 2009 33/45 XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF Example (2) 1prefix ismll: <http://www.ismll.de/rdf/meta#> 2prefix r: <http://www.cgnm.de/rdf/relatives#> 3prefix miller: <http://www.cgnm.de/rdf/family-miller#> 4prefix smith: <http://www.cgnm.de/rdf/family-smith#> 5 6construct { 7?x r:age?age. 8?x ismll:source?g. 9} 10where { 11 graph?g {?x r:age?age.} 12} 13 Figure 46: Adding source information. 1@prefix r: <http://www.cgnm.de/rdf/rela 2@prefix miller: <http://www.cgnm.de/rdf/fa 3@prefix smith: <http://www.cgnm.de/rdf/f 4@prefix ismll: <http://www.ismll.de/rdf/me 5 6 smith:adam 7 r:age 52 ; 8 ismll:source <file:///c:/users/busche/w 9 10 miller:anne 11 r:age 45 ; 12 ismll:source <file:///c:/users/busche/w 13 14 miller:gisa 15 r:age 1 ; 16 ismll:source <file:///c:/users/busche/w Course on XML and Semantic Web Technologies, summer term 2009 34/45

XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF 17 30 smith:emil 18 miller:dennis 31 r:age 27 ; 19 r:age 22 ; 32 ismll:source <file:///c:/users/busche/w 20 ismll:source <file:///c:/users/busche/workspace35_2/xml2009_script/skript/exa 33 21 34 miller:clara 22 miller:emil 35 r:age 24 ; 23 r:age 27 ; 36 ismll:source <file:///c:/users/busche/w 24 ismll:source <file:///c:/users/busche/workspace35_2/xml2009_script/skript/exa 37 25 38 miller:bert 26 miller:fred 39 r:age 49 ; 27 r:age 2 ; 40 ismll:source <file:///c:/users/busche/w 28 ismll:source <file:///c:/users/busche/workspace35_2/xml2009_script/skript/exa 29 Figure 47: Result. Course on XML and Semantic Web Technologies, summer term 2009 35/45 XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF Describe - returning all information about selected resources Describes a (set of) resources in terms of subject, predicate, and object. 1prefix : <http://www.cgnm.de/rdf/family#> 2 3describe :Anne Figure 48: Query describing Anne. 1@prefix r: <http://www.cgnm.de/rdf/relatives#>. 2@prefix xs: <http://www.w3.org/2001/xmlschema#>. 3@prefix : <http://www.cgnm.de/rdf/family#>. 4 5:Anne 6 :age 45 ; 7 :marriedto :Bert ; 8 :motherof :Clara ; 9 :motherof :Dennis. Figure 49: Result. Course on XML and Semantic Web Technologies, summer term 2009 36/45

XML and Semantic Web Technologies / 3. Metaqueries/Returning RDF Describe, Example (2) 1prefix : <http://www.cgnm.de/rdf/family#> 1@prefix xs: 2 2@prefix : 3describe?x?y { 3:Emil 4?x :age?age. 5 FILTER (?age >= 18). 6?x :fatherof?y. 7} Figure 50: Query describing fathers. 4 :age 27 ; 5 :fatherof :Fred ; 6 :fatherof :Gisa ; 7 :marriedto :Clara. 8:Fred 9 :age 2. 10:Clara 11 :age 24 ; 12 :marriedto :Emil ; 13 :motherof :Fred ; 14 :motherof :Gisa. 15:Gisa 16 :age 1. 17:Dennis 18 :age 22. 19:Bert 20 :age 49 ; :fatherof :Clara ; <http://www.w3.org/2001/x <http://www.cgnm.de/rdf/fam Course on XML and Semantic Web Technologies, summer term 2009 37/45 XML and Semantic Web Technologies III. Semantic Web / 3. SPARQL Query Language for RDF 1. Basic SPARQL queries 2. Conditional SPARQL Queries 3. Metaqueries/Returning RDF 4. A small integrated example Course on XML and Semantic Web Technologies, summer term 2009 38/45

XML and Semantic Web Technologies / 4. A small integrated example FOAF - Friend of a Friend The FOAF Project was founded to describe people, and their links, through a (standardized) vocabulatory. The FOAF Specification consists of its namespace document: FOAF Vocabulatory Specification 0.91 (SPEC-2007/11/02) available athttp://xmlns.com/foaf/spec/. Course on XML and Semantic Web Technologies, summer term 2009 38/45 XML and Semantic Web Technologies / 4. A small integrated example FOAF / Example 1<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 2 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 3 xmlns:foaf="http://xmlns.com/foaf/0.1/"> 4 <foaf:person rdf:id="me"> 5 <foaf:name>lars Schmidt-Thieme</foaf:name> 6 <foaf:title>herr Prof. Dr. Dr.</foaf:title> 7 <foaf:givenname>lars</foaf:givenname> 8 <foaf:family_name>schmidt-thieme</foaf:family_name> 9 <foaf:mbox_sha1sum>3ff97691d04aa172889553373a3467666bf7c100</foa 10 </foaf:person> 11 </rdf:rdf> Figure 52: Very basic information. Course on XML and Semantic Web Technologies, summer term 2009 39/45

XML and Semantic Web Technologies / 4. A small integrated example Combining FOAF with our example 1@prefix miller: <http://www.cgnm.de/rdf/family-miller#>. 2@prefix smith: <http://www.cgnm.de/rdf/family-smith#>. 3@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. 4@prefix foaf: <http://xmlns.com/foaf/0.1/>. 5 6smith:Emil foaf:name "Emil". 7smith:Emil rdf:type foaf:person. 8 9miller:Clara foaf:name "Clara". 10miller:Clara rdf:type foaf:person. 11 12smith:Emil foaf:knows miller:clara. Figure 53: Adding FOAF-Statements to our example. Course on XML and Semantic Web Technologies, summer term 2009 40/45 XML and Semantic Web Technologies / 4. A small integrated example 1prefix : 2prefix miller: 3prefix smith: 4prefix foaf: 5prefix rdf: 6 7SELECT * 8where { 9?x rdf:type foaf:person. 10} Figure 54: Query for all people. 1---------------- 2 x 3 ================ 4 miller:clara 5 smith:emil 6---------------- Figure 55: Result. Simple queries (1) <http://www.cgnm.de/rdf/relatives#> <http://www.cgnm.de/rdf/family-miller#> <http://www.cgnm.de/rdf/family-smith#> <http://xmlns.com/foaf/0.1/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#> Course on XML and Semantic Web Technologies, summer term 2009 41/45

XML and Semantic Web Technologies / 4. A small integrated example 1prefix : 2prefix miller: 3prefix smith: 4prefix foaf: 5prefix rdf: 6 7SELECT * 8where { 9?x rdf:type foaf:person. 10 graph <file:family-smith.n3> 11 {?x :marriedto?y} 12} Simple queries (2) <http://www.cgnm.de/rdf/relatives#> <http://www.cgnm.de/rdf/family-miller#> <http://www.cgnm.de/rdf/family-smith#> <http://xmlns.com/foaf/0.1/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#> Figure 56: Query for all people being married. 1----------------------------- 2 x y 3 ============================= 4 smith:emil miller:clara 5----------------------------- Figure 57: Result. Course on XML and Semantic Web Technologies, summer term 2009 42/45 XML and Semantic Web Technologies / 4. A small integrated example 1prefix : 2prefix miller: 3prefix smith: 4prefix foaf: 5prefix rdf: 6 7SELECT * where { 8?x rdf:type foaf:person. 9 graph <file:family-smith.n3> 10 {?x :marriedto?y} 11 graph <file:family-miller.n3> 12 {?y :marriedto?z. 13?y :age?y_age.} } Simple queries (3) <http://www.cgnm.de/rdf/relatives#> <http://www.cgnm.de/rdf/family-miller#> <http://www.cgnm.de/rdf/family-smith#> <http://xmlns.com/foaf/0.1/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#> Figure 58: Symmetric properties? Inferencing? 1--------------------------------------------------- 2 x y z y_age 3 =================================================== 4 smith:emil miller:clara miller:emil 24 5--------------------------------------------------- Figure 59: Result. Course on XML and Semantic Web Technologies, summer term 2009 43/45

XML and Semantic Web Technologies / 4. A small integrated example Simple queries (3) It is impossible with RDF(S) to define properties that define aspects like symmetry, identity, etc. 1prefix : <http://www.cgnm.de/rdf/relatives#> 2prefix miller: <http://www.cgnm.de/rdf/family-miller#> 3prefix smith: <http://www.cgnm.de/rdf/family-smith#> 4prefix foaf: <http://xmlns.com/foaf/0.1/> 5prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 6 7SELECT * where { 8?x rdf:type foaf:person. 9?z rdf:type foaf:person. 10 graph <file:family-smith.n3> {?x :marriedto?y} 11 graph <file:family-miller.n3> {?y :marriedto?z. 12?y :age?y_age.} } Figure 60: No inferencing. 1--------------------- 2 x z y y_age 3 ===================== 4--------------------- Figure 61: Result. Course on XML and Semantic Web Technologies, summer term 2009 44/45