SlideShare a Scribd company logo
Emanuele Della Valle [email_address] http://emanueledellavalle.org   Querying the Semantic Web with SPARQL
Share, Remix, Reuse — Legally This work is licensed under the Creative Commons Attribution 3.0 Unported License.  Your are free: to Share  — to copy, distribute and transmit the work to Remix  — to adapt the work Under the following conditions Attribution  — You must attribute the work by inserting “ © applied-semantic-web.org” at the end of each reused slide a credits slide stating “These slides are partially based on “Querying the Semantic Web with SPARQL” by Emanuele Della Valle  http://applied-semantic-web.org/2010/03/05_SPARQL.ppt   To view a copy of this license, visit  http://creativecommons.org/licenses/by/3.0/
Query: SPARQL
SPARQL in a nutshell  What is SPARQL? SPARQL  is the query language of the Semantic Web stays for  S PARQL  P rotocol  a nd  R DF  Q uery  L anguage A Query Language ...: Find names and websites of contributors to  PlanetRDF :  PREFIX foaf: <http://xmlns.com/foaf/0.1/>  SELECT ?name ?website  FROM <http://planetrdf.com/bloggers.rdf>  WHERE { ?person foaf:weblog ?website ;   foaf:name ?name .    ?website a foaf:Document }  ... and a Protocol. http://.../qps?  query-lang=http://www.w3.org/TR/rdf-sparql-query/ &graph-id=http://planetrdf.com/bloggers.rdf &query=PREFIX foaf: <http://xmlns.com/foaf/0.1/...
SPARQL in a nutshell  Why SPARQL? SPARQL let us  Pull values from structured and semi-structured data represented in RDF Explore RDF data by querying unknown relationships Perform complex joins of disparate RDF repositories in a single query Transform RDF data from one vocabulary to another Develop higher-level cross-platform application
SPARQL in a nutshell  Anatomy of a SPARQL query
SPARQL in a nutshell  Triple Pattern Syntax Turtle-like: URIs, QNames, literals, convenience syntax. Adds variables to get basic patterns  ?var Variable names are a subset of NCNames (no &quot;-&quot; or &quot;.&quot;) E.g.,  simple ?s a dbpedia-owl:Drug . complex ?s a dbpedia-owl:Drug . ?s skos:subject category:Anxiolytics . ?s ?p dbpedia:Kidney .  Adds  OPTIONAL to cope with semi-structured nature of RDF FILTER to select solution according to some criteria UNION operator to get complex patterns
SPARQL in a nutshell   Writing a Simple Query Query PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>  SELECT ?s  WHERE { ?s a dbpedia-owl:Drug . } Results  (click to run query on  http://dbpedia.org/isparql/ ) ?s http://dbpedia.org/resource/Budesonide   http://dbpedia.org/resource/Aciclovir http://dbpedia.org/resource/Salbutamol   …
SPARQL in a nutshell   Writing a bit more complex query Query PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?category  WHERE { ?drug a dbpedia-owl:Drug ;   skos:subject ?category . } Results  (click to run query on  http://dbpedia.org/isparql/ ) ?drug ?category http://dbpedia.org/resource/Budesonide   http://dbpedia.org/resource/Category:Glucocorticoids   http://dbpedia.org/resource/Budesonide   http://dbpedia.org/resource/Category:Asthma   http://dbpedia.org/resource/Aciclovir http://dbpedia.org/resource/Category:Antivirals   http://dbpedia.org/resource/Aciclovir http://dbpedia.org/resource/Category:Purines   … …
SPARQL in a nutshell  Basic Patterns A basic Patter is a set of triple patterns, all of which  must be matched . In this case m atches the graph  means find a set of bindings such that the substitution of variables for values  creates a subgraph  that is in the set of triples making up the graph.
SPARQL in a nutshell  Matching RDF literals – Language Tag  1/2 Query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug  WHERE { ?drug a dbpedia-owl:Drug ; rdfs:label  &quot;Budesonide&quot;  . } Results  (click to run query on  http://dbpedia.org/isparql/ ) 0 results!!! Explanation All RDF-S label have a language tag
SPARQL in a nutshell  Matching RDF literals – Language Tag  2/2 Query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug  WHERE { ?drug a dbpedia-owl:Drug ; rdfs:label &quot;Budesonide&quot; @en  . } Results  (click to run query on  http://dbpedia.org/isparql/ ) ?drug http://dbpedia.org/resource/Budesonide
SPARQL in a nutshell  Matching RDF literals – numerical values As in the case of language tags, if the literals are typed (i.e.,  &quot;259.34&quot;^^xsd:float ), they do not match if they are not given explicitly. Query PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug  WHERE { ?drug a dbpedia-owl:Drug ;   dbpprop:chemspiderid &quot;4777&quot; ^^xsd:integer  . } Results  (click to run query on  http://dbpedia.org/isparql/ ) ?drug http://dbpedia.org/resource/Propranolol
SPARQL in a nutshell  RDF Term Constraints SPARQL allows restricting solutions by applying the FILTER clause.  An RDF term bound to a variable appears in the results if the FILTER expression, applied to the term, evaluates to TRUE.  Query PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?mp WHERE { ?drug a dbpedia-owl:Drug ;   dbpprop:meltingPoint ?mp .   FILTER ( ?mp < 30 ) } Results  (click to run query on  http://dbpedia.org/isparql/ )   ?drug ?mp http://dbpedia.org/resource/Levorphanol   23 http://dbpedia.org/resource/Streptomycin   12 … …
SPARQL in a nutshell  RDF Term Constraints – regex SPARQL FILTERs allows also restricting values of strings using the  regex() Query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?c WHERE { ?drug a dbpedia-owl:Drug ;   rdfs:comment ?c .   FILTER(  regex (?c, &quot;Asthma&quot;)) } Results  (click to run query on  http://dbpedia.org/isparql/ ) ?drug ?c http://dbpedia.org/resource/Budesonide   …  örtlichen Behandlung von  Asthma  bronchiale, ... http://dbpedia.org/resource/Salbutamol   …  Bronchospasmolytikum bei  Asthma  bronchiale … … …
SPARQL in a nutshell  Value Tests Notation for value comparison: <, >, =, <=, >= and !=  Test functions Check if a variable is bound: BOUND Check the type of resource bound: isIRI, isBLANK, isLITERAL Accessing accessories: LANG, DATATYPE Logic operators: || and && Comparing strings: REGEX, langMatches Constructor functions: bool, dbl, flt, dec, int, dT, str, IRI Extensible Value Testing E.g., FILTER ( aGeo:distance(?axLoc, ?ayLoc, ?bxLoc, ?byLoc) < 10 ) .  (see  http://www.w3.org/TR/rdf-sparql-query/#extensionFunctions  )
SPARQL in a nutshell  More Sophisticated Graph Patterns RDF is &quot;semi structured&quot; and has no integrity constrains SPARQL addresses this issue with Group patterns  match if all subpatterns match and all constraints are satisfied In SPARQL syntax, groups are  { … } OPTIONAL graph patterns  accommodate the need to add information to a result but without the query failing just because some information is missing. In SPARQL syntax,  OPTIONAL { … } UNION graph patterns  allows to match alternatives  In SPARQL syntax,  { … } UNION { … }
SPARQL in a nutshell  Optional Graph Patterns Query PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?mp WHERE { ?drug a dbpedia-owl:Drug .   OPTIONAL { ?drug dbpprop:meltingPoint ?mp }   } Results  (click to run query on  http://dbpedia.org/isparql/ ) ?drug ?mp http://dbpedia.org/resource/Budesonide   http://dbpedia.org/resource/Aciclovir   256.5 … …
SPARQL in a nutshell  Matching alternatives with UNION Query PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> PREFIX dbpedia: <http://dbpedia.org/resource/> SELECT ?drug  WHERE { ?drug a dbpedia-owl:Drug .   { ?drug dbpprop:metabolism dbpedia:Kidney }   UNION   { ?drug dbpprop:excretion dbpedia:Kidney }   } Results  (click to run query on  http://dbpedia.org/isparql/ ) dbpprop:metabolism dbpprop:excretion ?drug http://dbpedia.org/resource/Doripenem   http://dbpedia.org/resource/Alprazolam   http://dbpedia.org/resource/Piroxicam   http://dbpedia.org/resource/Amlodipine   …
SPARQL in a nutshell  Defining the Dataset with the FROM clause A SPARQL query may  specify the dataset to be used for matching  by using  the FROM clause and  the FROM NAMED clause Ex. PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>  PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT ?label ?graph  FROM <http://dbpedia.org/resource/Category:Anxiolytics> FROM NAMED <http://dbpedia.org/resource/Propranolol> WHERE {    ?s rdfs:label ?label .   GRAPH ?graph { ?graph skos:subject ?s .  } } Results below are very different from  those  of the same query without FROM clauses ?label  ?graph [email_address] http://dbpedia.org/resource/Propranolol
SPARQL in a nutshell  A difficult to solve problem 1/3 Image you don’t know dbpedia SPARQL endpoint, you discovered using  Sindice  the graph  http://dbpedia.org/resource/ Category:Anxiolytics  and you want to get the rdfs:labels of the anxiolytics.  If you write the query PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbp-cat: <http://dbpedia.org/resource/Category:> SELECT ?label  FROM <http://dbpedia.org/resource/Category:Anxiolytics> WHERE {   ?s skos:subject dbp-cat:Anxiolytics .    ?s rdfs:label ?label . }  You get no results because the labels are in different graphs such as http://dbpedia.org/resource/Alprazolam   […] http://dbpedia.org/resource/ZK-93423
SPARQL in a nutshell  A difficult to solve problem 2/3 A solution is writing the query PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbp-cat: <http://dbpedia.org/resource/Category:> SELECT ?label  FROM <http://dbpedia.org/resource/Category:Anxiolytics> FROM <http://dbpedia.org/resource/Alprazolam> FROM […] FROM <http://dbpedia.org/resource/ZK-93423> WHERE {   ?s skos:subject dbp-cat:Anxiolytics .    ?s rdfs:label ?label . }  But this means knowing the answer already
SPARQL in a nutshell  A difficult to solve problem 3/3 The  Semantic Web Client  provides a solution to the problem by dynamically retrieving information from the Semantic Web It dereferences HTTP URIs,  It follows rdfs:seeAlso links, and  it queries the  Sindice  search engine. Querying the Semantic Web with the following query … PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbp-cat: <http://dbpedia.org/resource/Category:> SELECT ?label  WHERE {   ?s skos:subject dbp-cat:Anxiolytics .    ?s rdfs:label ?label . }  …  gives the correct results even if no FROM clause is given and no specific SPARQL endpoint is used.
SPARQL in a nutshell  Result Forms Besides selecting tables of values, SPARQL allows three other types of queries: ASK - returns a boolean answering, does the query have any results? CONSTRUCT - uses variable bindings to return new RDF triples DESCRIBE - returns server-determined RDF about the queried resources SELECT and ASK results can be returned as XML or JSON.  CONSTRUCT and DESCRIBE results can be returned via any RDF serialization (e.g. RDF/XML or Turtle).
SPARQL in a nutshell  SPARQL Resources SPARQL Frequently Asked Questions http://thefigtrees.net/lee/sw/sparql-faq SPARQL implementations - community maintained list of open-source and commercial SPARQL engines http://esw.w3.org/topic/SparqlImplementations   Public SPARQL endpoints - community maintained list http://esw.w3.org/topic/SparqlEndpoints   SPARQL extensions - collection of SPARQL extensions implemented in various SPARQL engines http://esw.w3.org/topic/SPARQL/Extensions
Emanuele Della Valle [email_address] http://emanueledellavalle.org   Querying the Semantic Web with SPARQL

More Related Content

PPT
Introduction to RDF
PDF
RDF 해설서
PDF
Semantic web
PDF
Functional requirements for bibliographic records & functional requirements f...
ODP
Dublin core Presentation
PPTX
Introduction to EAD
PPTX
The Semantic Web #6 - RDF Schema
Introduction to RDF
RDF 해설서
Semantic web
Functional requirements for bibliographic records & functional requirements f...
Dublin core Presentation
Introduction to EAD
The Semantic Web #6 - RDF Schema

What's hot (20)

PPTX
RDF Data Model
PPT
RDF and OWL
PPTX
RDF 개념 및 구문 소개
PPTX
RDF data model
PPT
A Brief Introduction to Encoded Archival Description
PPTX
SHACL by example
PPTX
The Semantic Web #9 - Web Ontology Language (OWL)
PDF
Linked Data의 RDF 어휘 이해하고 체험하기 - FOAF, SIOC, SKOS를 중심으로 -
PDF
Linked Open Data Tutorial
PPTX
Library congress subject headings
PPT
PPTX
Apache Hive Tutorial
PPTX
PDF
Querying Bio2RDF data
PDF
Chapter 1 semantic web
PPT
FRBR presentation by Bwsrang Basumatary
PDF
Resource description framework
PPTX
Web crawler and applications
PPT
RDF Data Model
RDF and OWL
RDF 개념 및 구문 소개
RDF data model
A Brief Introduction to Encoded Archival Description
SHACL by example
The Semantic Web #9 - Web Ontology Language (OWL)
Linked Data의 RDF 어휘 이해하고 체험하기 - FOAF, SIOC, SKOS를 중심으로 -
Linked Open Data Tutorial
Library congress subject headings
Apache Hive Tutorial
Querying Bio2RDF data
Chapter 1 semantic web
FRBR presentation by Bwsrang Basumatary
Resource description framework
Web crawler and applications
Ad

Viewers also liked (10)

PDF
Datos abiertos gencat ES
PPT
Lenguaje de recuperación de datos en la web (sparql)
PDF
PDF
Information Retrieval using Semantic Similarity
PPT
SPARQL in the Semantic Web
PPT
PPTX
Information retrieval system!
PPTX
Introduction to Information Retrieval
PPTX
Web Semantica. La nueva Generación de Portales. (Infotec - SemanticWebBuilder...
Datos abiertos gencat ES
Lenguaje de recuperación de datos en la web (sparql)
Information Retrieval using Semantic Similarity
SPARQL in the Semantic Web
Information retrieval system!
Introduction to Information Retrieval
Web Semantica. La nueva Generación de Portales. (Infotec - SemanticWebBuilder...
Ad

Similar to Querying the Semantic Web with SPARQL (20)

PPTX
Semantic web meetup – sparql tutorial
PPT
Semantic Web
PPT
Semantic Web
PPT
From SQL to SPARQL
PDF
Notes from the Library Juice Academy courses on “SPARQL Fundamentals”: Univer...
PPTX
A Little SPARQL in your Analytics
PPTX
What;s Coming In SPARQL2?
PDF
Bio ontologies and semantic technologies
PPTX
SPARQL
PDF
Sparql a simple knowledge query
PDF
Federation and Navigation in SPARQL 1.1
PPTX
SPARQL Cheat Sheet
PPTX
SPARQL introduction and training (130+ slides with exercices)
PDF
Introduction to Bio SPARQL
PPTX
SWT Lecture Session 4 - SW architectures and SPARQL
PPTX
Sparql
PPTX
GDG Meets U event - Big data & Wikidata - no lies codelab
PPTX
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
PPTX
Triplestore and SPARQL
PPT
SPARQL Query Forms
Semantic web meetup – sparql tutorial
Semantic Web
Semantic Web
From SQL to SPARQL
Notes from the Library Juice Academy courses on “SPARQL Fundamentals”: Univer...
A Little SPARQL in your Analytics
What;s Coming In SPARQL2?
Bio ontologies and semantic technologies
SPARQL
Sparql a simple knowledge query
Federation and Navigation in SPARQL 1.1
SPARQL Cheat Sheet
SPARQL introduction and training (130+ slides with exercices)
Introduction to Bio SPARQL
SWT Lecture Session 4 - SW architectures and SPARQL
Sparql
GDG Meets U event - Big data & Wikidata - no lies codelab
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
Triplestore and SPARQL
SPARQL Query Forms

More from Emanuele Della Valle (20)

PDF
Taming velocity - a tale of four streams
PDF
Stream reasoning
PPTX
Work in progress on Inductive Stream Reasoning
PPTX
Big Data and Data Science W's
PPT
Knowledge graphs in search engines
PPTX
La città dei balocchi 2017 in numeri - Fluxedo
PPTX
Stream Reasoning: a summary of ten years of research and a vision for the nex...
PPTX
ACQUA: Approximate Continuous Query Answering over Streams and Dynamic Linked...
PDF
Stream reasoning: an approach to tame the velocity and variety dimensions of ...
PDF
Big Data: how to use it to create value
PPTX
Listening to the pulse of our cities with Stream Reasoning (and few more tech...
PPT
Ist16-04 An introduction to RDF
PPT
Ist16-03 An Introduction to the Semantic Web
PPT
Ist16-02 HL7 from v2 (syntax) to v3 (semantics)
PPT
IST16-01 - Introduction to Interoperability and Semantic Technologies
PDF
Stream reasoning: mastering the velocity and the variety dimensions of Big Da...
PPTX
On Stream Reasoning
PPTX
Listening to the pulse of our cities fusing Social Media Streams and Call Dat...
PPTX
Social listener-brera-design-district-2015-03
PDF
City Data Fusion for Event Management (in Italiano)
Taming velocity - a tale of four streams
Stream reasoning
Work in progress on Inductive Stream Reasoning
Big Data and Data Science W's
Knowledge graphs in search engines
La città dei balocchi 2017 in numeri - Fluxedo
Stream Reasoning: a summary of ten years of research and a vision for the nex...
ACQUA: Approximate Continuous Query Answering over Streams and Dynamic Linked...
Stream reasoning: an approach to tame the velocity and variety dimensions of ...
Big Data: how to use it to create value
Listening to the pulse of our cities with Stream Reasoning (and few more tech...
Ist16-04 An introduction to RDF
Ist16-03 An Introduction to the Semantic Web
Ist16-02 HL7 from v2 (syntax) to v3 (semantics)
IST16-01 - Introduction to Interoperability and Semantic Technologies
Stream reasoning: mastering the velocity and the variety dimensions of Big Da...
On Stream Reasoning
Listening to the pulse of our cities fusing Social Media Streams and Call Dat...
Social listener-brera-design-district-2015-03
City Data Fusion for Event Management (in Italiano)

Recently uploaded (20)

PDF
Getting Started with Data Integration: FME Form 101
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
August Patch Tuesday
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
project resource management chapter-09.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Encapsulation theory and applications.pdf
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
1. Introduction to Computer Programming.pptx
Getting Started with Data Integration: FME Form 101
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
August Patch Tuesday
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
project resource management chapter-09.pdf
Heart disease approach using modified random forest and particle swarm optimi...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
A comparative study of natural language inference in Swahili using monolingua...
Unlocking AI with Model Context Protocol (MCP)
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
WOOl fibre morphology and structure.pdf for textiles
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
1 - Historical Antecedents, Social Consideration.pdf
Tartificialntelligence_presentation.pptx
Enhancing emotion recognition model for a student engagement use case through...
Encapsulation theory and applications.pdf
A novel scalable deep ensemble learning framework for big data classification...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
1. Introduction to Computer Programming.pptx

Querying the Semantic Web with SPARQL

  • 1. Emanuele Della Valle [email_address] http://emanueledellavalle.org Querying the Semantic Web with SPARQL
  • 2. Share, Remix, Reuse — Legally This work is licensed under the Creative Commons Attribution 3.0 Unported License. Your are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions Attribution — You must attribute the work by inserting “ © applied-semantic-web.org” at the end of each reused slide a credits slide stating “These slides are partially based on “Querying the Semantic Web with SPARQL” by Emanuele Della Valle http://applied-semantic-web.org/2010/03/05_SPARQL.ppt To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/
  • 4. SPARQL in a nutshell What is SPARQL? SPARQL is the query language of the Semantic Web stays for S PARQL P rotocol a nd R DF Q uery L anguage A Query Language ...: Find names and websites of contributors to PlanetRDF : PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?website FROM <http://planetrdf.com/bloggers.rdf> WHERE { ?person foaf:weblog ?website ; foaf:name ?name . ?website a foaf:Document } ... and a Protocol. http://.../qps? query-lang=http://www.w3.org/TR/rdf-sparql-query/ &graph-id=http://planetrdf.com/bloggers.rdf &query=PREFIX foaf: <http://xmlns.com/foaf/0.1/...
  • 5. SPARQL in a nutshell Why SPARQL? SPARQL let us Pull values from structured and semi-structured data represented in RDF Explore RDF data by querying unknown relationships Perform complex joins of disparate RDF repositories in a single query Transform RDF data from one vocabulary to another Develop higher-level cross-platform application
  • 6. SPARQL in a nutshell Anatomy of a SPARQL query
  • 7. SPARQL in a nutshell Triple Pattern Syntax Turtle-like: URIs, QNames, literals, convenience syntax. Adds variables to get basic patterns ?var Variable names are a subset of NCNames (no &quot;-&quot; or &quot;.&quot;) E.g., simple ?s a dbpedia-owl:Drug . complex ?s a dbpedia-owl:Drug . ?s skos:subject category:Anxiolytics . ?s ?p dbpedia:Kidney . Adds OPTIONAL to cope with semi-structured nature of RDF FILTER to select solution according to some criteria UNION operator to get complex patterns
  • 8. SPARQL in a nutshell Writing a Simple Query Query PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?s WHERE { ?s a dbpedia-owl:Drug . } Results (click to run query on http://dbpedia.org/isparql/ ) ?s http://dbpedia.org/resource/Budesonide http://dbpedia.org/resource/Aciclovir http://dbpedia.org/resource/Salbutamol …
  • 9. SPARQL in a nutshell Writing a bit more complex query Query PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?category WHERE { ?drug a dbpedia-owl:Drug ; skos:subject ?category . } Results (click to run query on http://dbpedia.org/isparql/ ) ?drug ?category http://dbpedia.org/resource/Budesonide http://dbpedia.org/resource/Category:Glucocorticoids http://dbpedia.org/resource/Budesonide http://dbpedia.org/resource/Category:Asthma http://dbpedia.org/resource/Aciclovir http://dbpedia.org/resource/Category:Antivirals http://dbpedia.org/resource/Aciclovir http://dbpedia.org/resource/Category:Purines … …
  • 10. SPARQL in a nutshell Basic Patterns A basic Patter is a set of triple patterns, all of which must be matched . In this case m atches the graph means find a set of bindings such that the substitution of variables for values creates a subgraph that is in the set of triples making up the graph.
  • 11. SPARQL in a nutshell Matching RDF literals – Language Tag 1/2 Query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug WHERE { ?drug a dbpedia-owl:Drug ; rdfs:label &quot;Budesonide&quot; . } Results (click to run query on http://dbpedia.org/isparql/ ) 0 results!!! Explanation All RDF-S label have a language tag
  • 12. SPARQL in a nutshell Matching RDF literals – Language Tag 2/2 Query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug WHERE { ?drug a dbpedia-owl:Drug ; rdfs:label &quot;Budesonide&quot; @en . } Results (click to run query on http://dbpedia.org/isparql/ ) ?drug http://dbpedia.org/resource/Budesonide
  • 13. SPARQL in a nutshell Matching RDF literals – numerical values As in the case of language tags, if the literals are typed (i.e., &quot;259.34&quot;^^xsd:float ), they do not match if they are not given explicitly. Query PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug WHERE { ?drug a dbpedia-owl:Drug ; dbpprop:chemspiderid &quot;4777&quot; ^^xsd:integer . } Results (click to run query on http://dbpedia.org/isparql/ ) ?drug http://dbpedia.org/resource/Propranolol
  • 14. SPARQL in a nutshell RDF Term Constraints SPARQL allows restricting solutions by applying the FILTER clause. An RDF term bound to a variable appears in the results if the FILTER expression, applied to the term, evaluates to TRUE. Query PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?mp WHERE { ?drug a dbpedia-owl:Drug ; dbpprop:meltingPoint ?mp . FILTER ( ?mp < 30 ) } Results (click to run query on http://dbpedia.org/isparql/ ) ?drug ?mp http://dbpedia.org/resource/Levorphanol 23 http://dbpedia.org/resource/Streptomycin 12 … …
  • 15. SPARQL in a nutshell RDF Term Constraints – regex SPARQL FILTERs allows also restricting values of strings using the regex() Query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?c WHERE { ?drug a dbpedia-owl:Drug ; rdfs:comment ?c . FILTER( regex (?c, &quot;Asthma&quot;)) } Results (click to run query on http://dbpedia.org/isparql/ ) ?drug ?c http://dbpedia.org/resource/Budesonide … örtlichen Behandlung von Asthma bronchiale, ... http://dbpedia.org/resource/Salbutamol … Bronchospasmolytikum bei Asthma bronchiale … … …
  • 16. SPARQL in a nutshell Value Tests Notation for value comparison: <, >, =, <=, >= and != Test functions Check if a variable is bound: BOUND Check the type of resource bound: isIRI, isBLANK, isLITERAL Accessing accessories: LANG, DATATYPE Logic operators: || and && Comparing strings: REGEX, langMatches Constructor functions: bool, dbl, flt, dec, int, dT, str, IRI Extensible Value Testing E.g., FILTER ( aGeo:distance(?axLoc, ?ayLoc, ?bxLoc, ?byLoc) < 10 ) . (see http://www.w3.org/TR/rdf-sparql-query/#extensionFunctions )
  • 17. SPARQL in a nutshell More Sophisticated Graph Patterns RDF is &quot;semi structured&quot; and has no integrity constrains SPARQL addresses this issue with Group patterns match if all subpatterns match and all constraints are satisfied In SPARQL syntax, groups are { … } OPTIONAL graph patterns accommodate the need to add information to a result but without the query failing just because some information is missing. In SPARQL syntax, OPTIONAL { … } UNION graph patterns allows to match alternatives In SPARQL syntax, { … } UNION { … }
  • 18. SPARQL in a nutshell Optional Graph Patterns Query PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> SELECT ?drug ?mp WHERE { ?drug a dbpedia-owl:Drug . OPTIONAL { ?drug dbpprop:meltingPoint ?mp } } Results (click to run query on http://dbpedia.org/isparql/ ) ?drug ?mp http://dbpedia.org/resource/Budesonide http://dbpedia.org/resource/Aciclovir 256.5 … …
  • 19. SPARQL in a nutshell Matching alternatives with UNION Query PREFIX dbpprop: <http://dbpedia.org/property/> PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> PREFIX dbpedia: <http://dbpedia.org/resource/> SELECT ?drug WHERE { ?drug a dbpedia-owl:Drug . { ?drug dbpprop:metabolism dbpedia:Kidney } UNION { ?drug dbpprop:excretion dbpedia:Kidney } } Results (click to run query on http://dbpedia.org/isparql/ ) dbpprop:metabolism dbpprop:excretion ?drug http://dbpedia.org/resource/Doripenem http://dbpedia.org/resource/Alprazolam http://dbpedia.org/resource/Piroxicam http://dbpedia.org/resource/Amlodipine …
  • 20. SPARQL in a nutshell Defining the Dataset with the FROM clause A SPARQL query may specify the dataset to be used for matching by using the FROM clause and the FROM NAMED clause Ex. PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT ?label ?graph FROM <http://dbpedia.org/resource/Category:Anxiolytics> FROM NAMED <http://dbpedia.org/resource/Propranolol> WHERE { ?s rdfs:label ?label . GRAPH ?graph { ?graph skos:subject ?s . } } Results below are very different from those of the same query without FROM clauses ?label ?graph [email_address] http://dbpedia.org/resource/Propranolol
  • 21. SPARQL in a nutshell A difficult to solve problem 1/3 Image you don’t know dbpedia SPARQL endpoint, you discovered using Sindice the graph http://dbpedia.org/resource/ Category:Anxiolytics and you want to get the rdfs:labels of the anxiolytics. If you write the query PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbp-cat: <http://dbpedia.org/resource/Category:> SELECT ?label FROM <http://dbpedia.org/resource/Category:Anxiolytics> WHERE { ?s skos:subject dbp-cat:Anxiolytics . ?s rdfs:label ?label . } You get no results because the labels are in different graphs such as http://dbpedia.org/resource/Alprazolam […] http://dbpedia.org/resource/ZK-93423
  • 22. SPARQL in a nutshell A difficult to solve problem 2/3 A solution is writing the query PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbp-cat: <http://dbpedia.org/resource/Category:> SELECT ?label FROM <http://dbpedia.org/resource/Category:Anxiolytics> FROM <http://dbpedia.org/resource/Alprazolam> FROM […] FROM <http://dbpedia.org/resource/ZK-93423> WHERE { ?s skos:subject dbp-cat:Anxiolytics . ?s rdfs:label ?label . } But this means knowing the answer already
  • 23. SPARQL in a nutshell A difficult to solve problem 3/3 The Semantic Web Client provides a solution to the problem by dynamically retrieving information from the Semantic Web It dereferences HTTP URIs, It follows rdfs:seeAlso links, and it queries the Sindice search engine. Querying the Semantic Web with the following query … PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dbp-cat: <http://dbpedia.org/resource/Category:> SELECT ?label WHERE { ?s skos:subject dbp-cat:Anxiolytics . ?s rdfs:label ?label . } … gives the correct results even if no FROM clause is given and no specific SPARQL endpoint is used.
  • 24. SPARQL in a nutshell Result Forms Besides selecting tables of values, SPARQL allows three other types of queries: ASK - returns a boolean answering, does the query have any results? CONSTRUCT - uses variable bindings to return new RDF triples DESCRIBE - returns server-determined RDF about the queried resources SELECT and ASK results can be returned as XML or JSON. CONSTRUCT and DESCRIBE results can be returned via any RDF serialization (e.g. RDF/XML or Turtle).
  • 25. SPARQL in a nutshell SPARQL Resources SPARQL Frequently Asked Questions http://thefigtrees.net/lee/sw/sparql-faq SPARQL implementations - community maintained list of open-source and commercial SPARQL engines http://esw.w3.org/topic/SparqlImplementations Public SPARQL endpoints - community maintained list http://esw.w3.org/topic/SparqlEndpoints SPARQL extensions - collection of SPARQL extensions implemented in various SPARQL engines http://esw.w3.org/topic/SPARQL/Extensions
  • 26. Emanuele Della Valle [email_address] http://emanueledellavalle.org Querying the Semantic Web with SPARQL

Editor's Notes

  • #8: PREFIX skos: &lt;http://www.w3.org/2004/02/skos/core#&gt; PREFIX dbpedia: &lt;http://dbpedia.org/resource/&gt; PREFIX dbpedia-owl: &lt;http://dbpedia.org/ontology/&gt; PREFIX category: &lt;http://dbpedia.org/resource/Category:&gt; select ?s ?p where { ?s a dbpedia-owl:Drug ; skos:subject category:Anxiolytics ; ?p dbpedia:Kidney . } http://dbpedia.org/isparql/view/?query=PREFIX%20skos%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23%3E%0APREFIX%20dbpedia%3A%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%20%0APREFIX%20dbpedia-owl%3A%20%3Chttp%3A%2F%2Fdbpedia.org%2Fontology%2F%3E%20%0APREFIX%20category%3A%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FCategory%3A%3E%20%0A%0Aselect%20%3Fs%20%3Fp%20%0Awhere%20%7B%20%3Fs%20a%20dbpedia-owl%3ADrug%20%3B%0A%20%20%20%20%20%20%20%20skos%3Asubject%20category%3AAnxiolytics%20%3B%0A%20%20%20%20%20%20%20%20%3Fp%20dbpedia%3AKidney%20.%20%7D%0A%20&amp;endpoint=/sparql&amp;maxrows=50&amp;default-graph-uri=http://dbpedia.org