Semantic Web in Depth SPARQL Protocol and RDF
Semantic Web in Depth SPARQL Protocol and RDF Query Language Dr Nicholas Gibbins – nmg@ecs. soton. ac. uk 2013 -2014
Semantic Web Applications Technologies considered so far allow us to: – create representation schemes (RDFS, OWL) – represent data (RDF) We can put data in – how do we get it back out?
The Semantic Web Application User Interface Application Data Sources RDF RDF
The Semantic Web Application User Interface Client Server Application Logic Query Language RDF Database Data Sources RDF RDF
RDF Triplestores Specialised databases for storing RDF data Can be viewed as a database containing a single table – Table contains subject/predicate/object as minimum – Many triplestores store quads to maintain provenance – May store other ancillary information Subject Predicate Object Graph URI URI/Literal URI … …
RDF Query Languages • RDF query languages were not standardised until 2008 • Large number of early RDF query languages: – Ser. QL – Squish. QL – RDQL – Xs. RQL – RDFQL
RDF Query Languages Early query languages mostly represented queries in the same way, as a set of triple patterns: (? s ? p ? o) Typical query structure: <list of variables to be bound and returned> <list of triple patterns and constraints> W 3 C specified a single query language which combines the best features of its predecessors: SPARQL
SPARQL Query Language
SPARQL in Brief SQL-like syntax: SELECT <variables> FROM <graphs> WHERE { <triple patterns> } – Triple patterns expressed in N 3 -like syntax – Variables prefixed with ‘? ’: ? who foaf: knows <http: //example. org/person/fred>. – Queries return a result set of bindings for the variables
Example Query _: a foaf: name _: a foaf: mbox _: b foaf: name _: b foaf: mbox _: c foaf: mbox "Johnny Lee Outlaw". <mailto: jlow@example. com>. "Peter Goodguy". <mailto: peter@example. org>. <mailto: carol@example. org>. SELECT ? name ? mbox WHERE { ? x foaf: name ? name. ? x foaf: mbox ? mbox. }
Example Query _: a foaf: name "Johnny Lee Outlaw". _: a foaf: mbox <mailto: jlow@example. com>. _: b foaf: name "Peter Goodguy". _: b foaf: mbox <mailto: peter@example. org>. ? name ? mbox "Johnny Lee Outlaw" <mailto: jlow@example. com > "Peter Goodguy" <mailto: peter@example. org
Namespaces in Queries The query given in the previous example was over-simplified – Need to define namespaces for vocabulary terms used: PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? name ? mbox WHERE { ? x foaf: name ? name. ? x foaf: mbox ? mbox. }
Matching RDF Literals @prefix ns: @prefix xsd: : x ns: p : y ns: p <http: //example. org/ns#>. <http: //www. w 3. org/2001/XMLSchema#>. "cat"@en. "42"^^xsd: integer.
Matching RDF String Literals The following query returns no bindings: SELECT ? V WHERE { ? v ? p "cat". } “cat” must have language tag, as in graph: “cat”@en SELECT ? v WHERE { ? v ? p "cat"@en. } – Returns a single binding
Matching RDF Numeric Literals SELECT ? v WHERE { ? v ? p 42. } – Returns a single binding Integers in SPARQL queries are implicitly typed as xsd: integer: "42"^^<http: //www. w 3. org/2001/XMLSchema#integer>
Blank Nodes Blank nodes in a result set are scoped to that result set – No guarantee that blank node labels will be unchanged over repeated queries _: a foaf: name "Alice". _: b foaf: name "Bob". SELECT ? x ? name WHERE { ? x foaf: name ? name. }
Blank Nodes ? x ? name _: a “Alice” _: b “Bob” ? x ? name _: x “Alice” _: y “Bob”
SPARQL Constraints can be applied to variables: SELECT ? title WHERE { ? x dc: title ? title. FILTER regex(? title, "^SPARQL") } SELECT ? title ? price WHERE { ? x ns: price ? price. FILTER (? price < 30. 5) ? x dc: title ? title. }
Group Graph Patterns Set of patterns enclosed in { } – Determines scoping for FILTER operators (and other operators)
Optional Graph Patterns _: a _: b rdf: type foaf: name foaf: mbox rdf: type foaf: name foaf: Person. "Alice". <mailto: alice@example. com>. <mailto: alice@work. example. com>. foaf: Person. "Bob". SELECT ? name ? mbox WHERE { ? x foaf: name ? name. OPTIONAL { ? x foaf: mbox ? mbox } }
Optional Graph Patterns ? name ? mbox “Alice” <mailto: alice@example. com> “Alice” <mailto: alice@work. example. com> “Bob”
Union Graph Patterns PREFIX dc 10: <http: //purl. org/dc/elements/1. 0/> PREFIX dc 11: <http: //purl. org/dc/elements/1. 1/> SELECT ? title WHERE { { ? book dc 10: title ? title } UNION { ? book dc 11: title ? title } }
Specifying Datasets RDF data may be published as multiple graphs (serialised in multiple RDF/XML documents) Sometimes we wish to be able to restrict a query to certain sources (graphs or datasets)
Default Graph PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? name FROM <http: //example. org/foaf/alice. Foaf> WHERE { ? x foaf: name ? name } • http: //example. org/foaf/alice. Foaf is the default graph • Unless otherwise specified, all triples are taken from that graph
Named Graphs PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> PREFIX dc: <http: //purl. org/dc/elements/1. 1/> SELECT ? who ? g ? mbox FROM <http: //example. org/dft. ttl> FROM NAMED <http: //example. org/alice> FROM NAMED <http: //example. org/bob> WHERE { ? g dc: publisher ? who. GRAPH ? g { ? who foaf: mbox ? mbox } }
Ordering PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? name WHERE { ? x foaf: name ? name ; : emp. Id ? emp } ORDER BY ? name DESC(? emp) • Order results by ? name, and then by ? emp in descending order • ASC() sorts in ascending order
Limit and Offset PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? name WHERE { ? x foaf: name ? name } ORDER BY ? name LIMIT 5 OFFSET 10 • Returns five results, after skipping the first ten results
Distinct PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT DISTINCT ? name WHERE { ? x foaf: name ? name } • Removes duplicate results
Other SPARQL Verbs SELECT is not the only verb – CONSTRUCT – ASK – DESCRIBE
CONSTRUCT A CONSTRUCT query returns an RDF graph, specified by a graph template – For each row in the result set, substitute the variables in the template with the values in that row – Combine the resulting graphs into a single graph (set union of triples)
CONSTRUCT PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> PREFIX vcard: <http: //www. w 3. org/2001/vcard-rdf/3. 0#> CONSTRUCT { ? x vcard: FN ? name. ? x vcard: EMAIL ? mail. } WHERE { ? x foaf: name ? name. ? x foaf: mbox ? mail. }
CONSTRUCT and b. Nodes CONSTRUCT can create graphs with b. Nodes b. Node labels are scoped to the template for each solution – If the same label occurs twice in a template, then there will be one blank node created for each query solution – There will be different blank nodes for triples generated by different query solutions
CONSTRUCT and b. Nodes CONSTRUCT { ? x vcard: N _: v vcard: given. Name ? gname. _: v vcard: family. Name ? fname. } WHERE { { ? x foaf: firstname ? gname } UNION { ? x foaf: givenname ? gname }. { ? x foaf: surname ? fname } UNION { ? x foaf: family_name ? fname }. }
CONSTRUCT and b. Nodes _: a _: b foaf: givenname "Alice". foaf: family_name "Hacker". foaf: firstname "Bob". foaf: surname "Hacker”. _: v 1 vcard: N _: x vcard: given. Name "Alice". _: x vcard: family. Name "Hacker". _: v 2 vcard: N _: z vcard: given. Name "Bob". _: z vcard: family. Name "Hacker".
CONSTRUCT is not a rule language CONSTRUCT matches graph patterns and returns a new graph We could implement a simple rule-based system using CONSTRUCT queries to represent rules
CONSTRUCT is not a rule language From the Data Access Working Group Charter: “While it is hoped that the product of the RDF Data Access Working Group will be useful in later development of a rules language, development of such a rules language is out of scope for this working group. However, any serializations of a query language must not preclude extension to, or inclusion in, a rules language. The group should expend minimal effort assuring that such an extension be intuitive and consistent with any query language produced by the group. ”
Accessing graphs with CONSTRUCT { ? s ? p ? o. } WHERE { GRAPH <http: //example. org/a. Graph> { ? s ? p ? o }. }
Accessing graphs with CONSTRUCT { ? s ? p ? o. } WHERE { GRAPH ? g { ? s ? p ? o }. { ? g dc: publisher <http: //www. w 3. org/> }. { ? g dc: date ? date }. FILTER ( ? date > "2005 -02 -28 T 00: 00 Z"^^xsd: date. Time ). }
ASK An ASK query is used to check whether a query pattern has a solution. – No information is returned about query solutions (no variable bindings), just a boolean value
ASK Example _: a _: b foaf: name "Alice". foaf: homepage <http: //work. example. org/alice/>. foaf: name "Bob". foaf: mbox <mailto: bob@work. example>. ASK { ? x foaf: name "Alice" } (returns true)
DESCRIBE Returns a single graph containing information about a resource or resources Nature of description left unspecified – Blank node closure – Concise Bounded Description Structure of returned data is determined by the SPARQL query processor
Describe Examples DESCRIBE <http: //example. org/> DESCRIBE ? x WHERE { ? x foaf: name "Alice". } DESCRIBE ? x ? y <http: //example. org/> WHERE { ? x foaf: knows ? y. }
Concise Bounded Description Algorithm for extracting a subgraph from an RDF graph, given a resource of interest – Produces a graph where the object nodes are either URI references, literals, or blank nodes not serving as the subject of any statement in the graph – Asymmetric view of a resource – follows only outgoing links
Concise Bounded Description Algorithm 1. Include in the subgraph all statements in the source graph where the subject of the statement is the starting node; 2. Recursively, for all statements identified in the subgraph thus far having a blank node object, include in the subgraph all statements in the source graph where the subject of the statement is the blank node in question and which are not already included in the subgraph. 3. Recursively, for all statements included in the subgraph thus far, for all reifications of each statement in the source graph, include the concise bounded description beginning from the rdf: Statement node of each reification.
Concise Bounded Description Example _: a foaf: name "Alice". _: a foaf: homepage <http: //work. example. org/alice/>. _: b foaf: name "Bob". _: b foaf: mbox <mailto: bob@example. org>. <http: //example. org/> dc: creator _: a. <http: //example. org/> dc: title “Example Inc. Website”. <http: //example. org/> dc: date “ 2005 -05 -23”.
Concise Bounded Description Example DESCRIBE <http: //example. org/> _: a foaf: name "Alice". _: a foaf: homepage <http: //work. example. org/alice/>. _: b foaf: name "Bob". _: b foaf: mbox <mailto: bob@example. org>. <http: //example. org/> dc: creator _: a. <http: //example. org/> dc: title “Example Inc. Website”. <http: //example. org/> dc: date “ 2005 -05 -23”.
SPARQL Protocol
SPARQL Protocol SPARQL Query Language is protocol-independent – SPARQL Protocol defines the method of interaction with a SPARQL processor Two standard bindings: – HTTP – SOAP Standard XML results format
SPARQL over HTTP Two methods of submitting a query to a processor: HTTP GET – Query encoded as HTTP request URI – Limitation on query length HTTP POST – Query encoded in HTTP request body – No limitation on query length
HTTP GET /sparql/? query=Encoded. Query HTTP/1. 1 Host: www. example. org User-agent: my-sparql-client/0. 1
SPARQL Results Format <? xml version="1. 0"? > <sparql xmlns="http: //www. w 3. org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false”> <result> <binding name="book"> <uri>http: //www. example/book 5</uri> </binding> <binding name="who"> <bnode>r 29392923 r 2922</bnode> </binding> </result> … </results> </sparql>
SPARQL Results Format <? xml version="1. 0"? > <sparql xmlns="http: //www. w 3. org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false”> <result> <binding name="book"> <uri>http: //www. example/book 5</uri> </binding> <binding name="who"> <bnode>r 29392923 r 2922</bnode> </binding> </result> … </results> </sparql> variables
SPARQL Results Format <? xml version="1. 0"? > <sparql xmlns="http: //www. w 3. org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false”> <result> <binding name="book"> <uri>http: //www. example/book 5</uri> </binding> <binding name="who"> <bnode>r 29392923 r 2922</bnode> </binding> </result> … </results> </sparql> results section
SPARQL Results Format <? xml version="1. 0"? > <sparql xmlns="http: //www. w 3. org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false”> <result> <binding name="book"> <uri>http: //www. example/book 5</uri> </binding> <binding name="who"> <bnode>r 29392923 r 2922</bnode> </binding> </result> … </results> </sparql> a single result (row)
SPARQL Results Format <? xml version="1. 0"? > <sparql xmlns="http: //www. w 3. org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false”> <result> <binding name="book"> <uri>http: //www. example/book 5</uri> </binding> <binding name="who"> <bnode>r 29392923 r 2922</bnode> </binding> </result> … </results> </sparql> binding of the variable “book”
SPARQL Results Format <? xml version="1. 0"? > <sparql xmlns="http: //www. w 3. org/2005/sparql-results#"> <head> <variable name="book"/> <variable name="who"/> </head> <results distinct="false" ordered="false”> <result> <binding name="book"> <uri>http: //www. example/book 5</uri> </binding> <binding name="who"> <bnode>r 29392923 r 2922</bnode> </binding> </result> … </results> </sparql> binding of the variable “who”
SPARQL Results Format • CONSTRUCT and DESCRIBE queries return RDF data – application/rdf+xml or similar • ASK queries return a boolean value: <? xml version="1. 0"? > <sparql xmlns="http: //www. w 3. org/2005/sparql-results#"> <head></head> <boolean>true</boolean> </sparql>
Further Reading SPARQL Query Language for RDF http: //www. w 3. org/TR/rdf-sparql-query/ SPARQL Protocol for RDF http: //www. w 3. org/TR/rdf-sparql-protocol/ SPARQL Query XML Results Format http: //www. w 3. org/TR/rdf-sparql-XMLres/
SPARQL 1. 1
Extending SPARQL Current version of SPARQL became a W 3 C Recommendation in 2008 Other features requested by community: – Update – Aggregates – Subqueries – SELECT expressions – Property paths – Federation – Entailment
SPARQL 1. 1 W 3 C SPARQL Working Group started in 2009 Standards became W 3 C Recommendations in March 2013
Update SPARQL only provides a means for querying a database Other capabilities required by many applications: – Create – (Read) – Update – Delete SPARQL 1. 1 Update derived from earlier SPARUL proposal
Update: INSERT DATA PREFIX dc: <http: //purl. org/dc/elements/1. 1/> INSERT DATA { <http: //example/book 1> dc: title "A new book" ; dc: creator "A. N. Other". }
Update: DELETE DATA PREFIX dc: <http: //purl. org/dc/elements/1. 1/> DELETE DATA { <http: //example/book 2> dc: title "David Copperfield" ; dc: creator "Edmund Wells". }
Update: DELETE/INSERT PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> WITH <http: //example/addresses> DELETE { ? person foaf: given. Name 'Bill' } INSERT { ? person foaf: given. Name 'William' } WHERE { ? person a foaf: Person. ? person foaf: given. Name 'Bill' }
Update: Graph Operations • LOAD: load triples from URI into graph • CLEAR: clear all triples from graph • CREATE: create new graph in store • DROP: remove graph from store • COPY: copy all triples from one graph to another • MOVE: move all triples from one graph to another (remove from source) • ADD: add all triples in one graph to another
Aggregates PREFIX : <http: //books. example/> SELECT (SUM(? lprice) AS ? total. Price) WHERE { ? org : affiliates ? auth : writes. Book ? book : price ? lprice. } GROUP BY ? org HAVING (SUM(? lprice) > 10) Other aggregate functions: – COUNT, MIN, MAX, GROUP_CONCAT, SAMPLE
Subqueries PREFIX : <http: //people. example/> SELECT ? y ? min. Name WHERE { : alice : knows ? y. { SELECT ? y (MIN(? name) AS ? min. Name) WHERE { ? y : name ? name. } GROUP BY ? y } }
SELECT Expressions PREFIX dc: <http: //purl. org/dc/elements/1. 1/> PREFIX ns: <http: //example. org/ns#> SELECT ? title (? p*(1 -? discount) AS ? price) WHERE { ? x ns: price ? p. ? x dc: title ? title. ? x ns: discount ? discount }
Property Paths: Alternatives PREFIX dc: <http: //purl. org/dc/elements/1. 1/> SELECT ? display. String WHERE { : book 1 dc: title|rdfs: label ? display. String } Sugared syntax for UNION
Property Paths: Sequences PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? name WHERE { ? x foaf: mbox <mailto: alice@example>. ? x foaf: knows/foaf: name ? name. } Sugared syntax for ? x foaf: knows [ foaf: name ? name ]
Property Paths: Arbitrary Sequences PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? name WHERE { ? x foaf: mbox <mailto: alice@example>. ? x foaf: knows+/foaf: name ? name. } Repeat operators • * (zero or more occurrences) • + (one or more occurrences) • {n} (exactly n occurrences) • {n, m} (between n and m occurrences)
Property Paths: Inverses PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? y WHERE { <mailto: alice@example> ^foaf: mbox ? x foaf: knows/^foaf: knows ? y. FILTER(? x != ? y) }
Further Reading SPARQL 1. 1 Query Language http: //www. w 3. org/TR/sparql 11 -query/ SPARQL 1. 1 Update http: //www. w 3. org/TR/sparql 11 -update/ SPARQL Query XML Results Format http: //www. w 3. org/TR/rdf-sparql-XMLres/
- Slides: 74