View
234
Download
0
Embed Size (px)
Citation preview
Overview
SPARQL: Query Language for RDF Graphs W3C Recommendation since 15 January 2008 Outline:
Basic ConceptsSELECT QuerySolution Sequence ModifiersCONSTRUCT Query
Basic Concepts
IRI: Internationalized Resource Identifier (RFC 3987). Generalization of URI – can contain characters from ISO 10646.
Basic Graph Pattern: A triple like in RDF, except that each of the subject, predicate or object may be a variable.
SELECT Query - Concept
SELECT <variables> WHERE <pattern> <variables> is a list of the form ?x ?y <pattern> is a graph pattern Result is a solution sequence There may be zero, one, or more solutions Each solution provides a set of bindings of
variables to RDF terms In a basic graph pattern, all variables must have a
binding in every solution A variable may be bound to a blank node. The
scope of the blank node is the result set.
Specifying Literals - General
Literals may be used in a basic graph pattern to match literals in data
Language tags must be specified when matching a literal with a specific language, such as “literal”@enFor typed literals, datatype must be specified using datatype IRI appended to the literal, introduced with the characters ^^May be enclosed in either single or double quotes, or three single or double quotes
Specifying Literals – Numbers and Booleans
Numbers can be specified without quotes and datatype and are interpreted as follows:
Numbers without decimal points are interpreted as xsd:integerNumbers with decimal point are interpreted as xsd:decimalNumbers with exponents are interpreted as xsd:double
Boolean values can also be written simply as true and false
Specifying Variables
Variables are prefixed with either ? or $Variable names start with a non-numeric character and can be a combination of numbers and alphabetsVariables have global scope
Specifying Blank Nodes
Blank nodes can be specified using the _:abc notationBlank nodes may also be specified using []The [] may be used in triple pattern as
[ :predicate :object]The above blank node may itself be the subject or object of a triple patternAbbreviated blank node syntax can be combined with other abbreviations
Triple Patterns - Concept
Subject, predicate, and object separated by whitespaceCommon subject for several statements can be indicated by ending the statements with ; instead of .Triples with common subject and predicate can list the objects as comma separated values
Specifying RDF Collections
RDF collections can be specified using “(element1 element2 ...)”A collection specified in this way can be the subject or object of a statementCollections can be nested() is shorthand for rdf:nil
Other Constructs
The word 'a' can be used as shorthand for rdf:typeIt must be in lower case for this notation
SELECT Query - Filters
FILTER expressions may be used to restrict which terms are bound to variablesOperators used in filter expressions are based on XPATH/XQUERY operators
Filter Expressions - Operators
SPARQL Specific: bound, isIRI, isBlank, isLiteral, str, lang, datatype, RDFterm-equal (=), sameTerm, langMatches, regexLogical Connectives: OR (||), AND (&&)XPATH Tests: =, !=, <, >, <=, >=XPATH Arithmetic: *, /, +, -XPATH Constructor Functions: xsd:boolean, xsd:double, xsd:float, xsd:decimal, xsd:integer, xsd:dateTime, xsd:string
Graph Patterns
Basic Graph Patterns: set of triple patternsGroup Graph Patterns: set of graph patternsOptional Graph Patterns: variables may not be boundAlternative Graph Patters: similar to logical ORPatterns on Named Graphs
Group Graph Patterns
Basic graph pattern delimited with {}All triple patterns in all groups must be satisfied (implicit AND)Filters apply to the group in which they are specified
Alternative Graph Patterns
Specified using the UNION keywordTriple is considered a solution if any of the alternative patterns match (OR)
Solution Sequence Modifiers
ORDER BY clause to sort in ascending (ASC) or descending (DESC) orderDISTINCT and REDUCED to select only unique solutionsOFFSET to skip a specified number of solutionsLIMIT to limit the number of solutions
CONSTRUCT Query
CONSTRUCT is a form of projection (like projection in the relational algebra)CONSTRUCT query returns an RDF graphGraph is built based on the template specified in the query
Acknowledgment
Based on http://www.w3.org/TR/2008/REC-rdf-sparql-query-20080115/Background image adapted from http://dragonartz.wordpress.com/2009/04/10/light-rays-with-sparkles-background-vector/