Speaker: Daniel Vila-Suero The SPARQL Query Language Raúl García-Castro, Óscar Corcho, Daniel Vila-Suero Ontology Engineering Group Universidad Politécnica de Madrid, Spain
© Raúl García Castro et al. Index Introduction Syntax Graph patterns Restricting values and solutions SPARQL query forms SPARQL 1.1 2
© Raúl García Castro et al. RDF(S) query languages Languages developed to allow accessing datasets expressed in RDF(S) (and in some cases OWL) Supported by the most important language APIs -Jena (HP labs) -Sesame (Aduna) -Boca (IBM) -... There are some differences wrt. languages like SQL, such as -Combination of different sources -Trust management -Open World Assumption Relational DB Application SQL queries RDF(S) OWL Application SPARQL, RQL, etc., queries 3
© Raúl García Castro et al. Query types Selection and extraction -“Select all the essays, together with their authors and their authors’ names” -“Select everything that is related to the book ‘Bellum Civille’” Reduction: we specify what it should not be returned -“Select everything except for the ontological information and the book translators” Restructuring: the original structure is changed in the final result -“Invert the relationship ‘author’ by ‘is author of’” Aggregation -“Return all the essays together with the mean number of authors per essay” Combination and inferences -“Combine the information of a book called ‘La guerra civil’ and whose author is Julius Caesar with the book whose identifier is ‘Bellum Civille’” -“Select all the essays, together with its authors and author names”, including also the instances of the subclasses of Essay -“Obtain the relationship ‘coauthor’ among persons who have written the same book” 4
© Raúl García Castro et al. RDF(S) query language families SquishQL Family -SquishQL -rdfDB Query Language -RDQL -BRQL -TriQL XPath, XSLT, XQuery -XQuery for RDF -XsRQL -TreeHugger and RDFTwig -RDFT, Nexus Query Language -RDFPath, Rpath and RXPath -Versa RQL Family -RQL -SeRQL -eRQL Controlled natural language -Metalog Other -Algae -iTQL -N3QL -PerlRDF Query Language -RDEVICE Deductive Language -RDFQBE -RDFQL -TRIPLE -WQL SPARQL W3C Recommendation 15 January 2008 Triple database Query structure Description graphs Query semantics XML repository Query syntax 5
© Raúl García Castro et al. SPARQL SPARQL Protocol and RDF Query Language Supported by: Jena, Sesame, IBM Boca, etc. Features -It supports most of the aforementioned queries -It supports datatype reasoning (datatypes can be requested instead of actual values) -The domain vocabulary and the knowledge representation vocabulary are treated differently by the query interpreters -It allows making queries over properties with multiple values, over multiple properties of a resource and over reifications -Queries can contain optional statements -Some implementations support aggregation queries Limitations -Neither set operations nor existential or universal quantifiers can be included in the queries -It does not support recursive queries 6
© Raúl García Castro et al. SPARQL Protocol SPARQL defines a communication protocol between clients and services Provides: -Abstract interface -HTTP and SOAP bindings Mainly intended for software developers 7 SPARQL Client SPARQL Service Request Result
© Raúl García Castro et al. SparqlQuery Interface Query request -One SPARQL query string -Zero or one RDF datasets Query result -SELECT and ASK: a SPARQL Results Document (XML-based) -DESCRIBE and CONSTRUCT: an RDF graph Fault messages: -malformed-query -query-request-refused 8 SPARQL Client SPARQL Service query-request query-result
© Raúl García Castro et al. SPARQL Endpoints Implement the SPARQL protocol Programmatic access using libraries: -ARC, RAP, Jena, Sesame, Javascript SPARQL, PySPARQL, etc. List of SPARQL Endpoints - Examples: 9
© Raúl García Castro et al. Example: Querying dbpedia People who were born in Berlin before
© Raúl García Castro et al. Example: Querying dbpedia 11
© Raúl García Castro et al. A simple SPARQL :. :book1 dc:title "SPARQL Tutorial". title "SPARQL Tutorial" SELECT ?title WHERE { ?title. } Data: Query: Query result: Graph pattern (or triple pattern): RDF triples where each of the subject, predicate and object may be a variable A graph pattern is matched against the RDF data Each way a pattern can be matched yields a solution The sequence of solutions is filtered by: Project, distinct, order, limit/offset One of the result forms is applied: SELECT, CONSTRUCT, DESCRIBE, ASK 12
© Raúl García Castro et al. RDF querying. Graph matching techniques RDF inference is based on graph matching techniques Basically, the RDF inference process consists of the following steps: -Transform an RDF query into a template graph that has to be matched against the RDF graph It contains constant and variable nodes, and constant and variable edges between nodes -Match against the RDF graph, taking into account constant nodes and edges -Provide a solution for variable nodes and edges 13
© Raúl García Castro et al. RDF querying. Examples (I) Sample RDF graph Query: “Tell me who are the persons who have Raúl as a colleague” -Result: data:Asun 14 person:hasColleague data:Raul data:Asun person:hasHomePage person:hasColleague data:Oscar “Óscar Corcho García” person:hasName person:hasColleague data:Raul ? ?
© Raúl García Castro et al. RDF querying. Examples (II) Query: “Tell me which are the relationships between Oscar and Asun” -Result: person:hasColleague Query: “Tell me the homepage of Oscar colleagues” -Result: “ 15 data:Asun ? data:Oscar person:hasHomePage ? ? person:hasColleague data:Oscar
© Raúl García Castro et al. Index Introduction Syntax Graph patterns Restricting values and solutions SPARQL query forms SPARQL
© Raúl García Castro et al. Syntax for IRIs and query variables Absolute IRIs Relative IRIs (BASE) Prefixed names (PREFIX) Variables (? or $) 17 BASE PREFIX book: book:book1 PREFIX : :book1 ?name $name
© Raúl García Castro et al. Syntax for literals Literals enclosed in quotes: "... " or '...' Literals with quotation marks: """... """ or '''... ''' Language tag Datatype (optional): ^^ Interpretation of untyped literals -Integer, Decimal, Double, and Boolean 18 LiteralInterpretation 1"1"^^xsd:integer 1.3"1.3"^^xsd:decimal 1.300"1.300"^^xsd:decimal 1.0e6"1.0e6"^^xsd:double true"true"^^xsd:boolean false"false"^^xsd:boolean
© Raúl García Castro et al. Syntax for blank nodes Label form ("_:abc”) -Cannot be used in two different basic graph patterns Abbreviated form "[]” -Used only once in the graph pattern 19 [ :p "v" ]. [] :p "v". _:b57 :p "v". [ :p "v" ] :q "w". _:b57 :p "v". _:b57 :q "w". :x :q [ :p "v" ]. :x :q _:b57. _:b57 :p "v".
© Raúl García Castro et al. Syntax for triple patterns Predicate-Object lists (";") Object lists (",") 20 ?x foaf:name ?name ; foaf:mbox ?mbox. ?x foaf:name ?name. ?x foaf:mbox ?mbox. ?x foaf:nick "Alice", "Alice_". ?x foaf:nick "Alice". ?x foaf:nick "Alice_".
© Raúl García Castro et al. RDF collections and rdf:type Collection: "(element1 element2...)" rdf:nil: "()" rdf:type: "a" 21 (1 ?x 3 4) :p "w". _:b0 rdf:first 1 ; rdf:rest _:b1. _:b1 rdf:first ?x ; rdf:rest _:b2. _:b2 rdf:first 3 ; rdf:rest _:b3. _:b3 rdf:first 4 ; rdf:rest rdf:nil. _:b0 :p "w". ?x a :Class1. ?x rdf:type :Class1.
© Raúl García Castro et al. Index Introduction Syntax Graph patterns Restricting values and solutions SPARQL query forms SPARQL
© Raúl García Castro et al. Graph patterns Basic Graph Patterns, where a set of triple patterns must match Group Graph Pattern, where a set of graph patterns must all match Optional Graph patterns, where additional patterns may extend the solution Alternative Graph Pattern, where two or more possible patterns are tried Patterns on Named Graphs, where patterns are matched against named graphs 23
© Raúl García Castro et foaf:. _:a foaf:name "Johnny Lee Outlaw". _:a foaf:mbox. _:b foaf:name "Peter Goodguy". _:b foaf:mbox. _:c foaf:mbox. PREFIX foaf: SELECT ?name ?mbox WHERE { ?x foaf:name ?name. ?x foaf:mbox ?mbox } namembox "Johnny Lee Outlaw" "Peter Goodguy" Multiple matches 24
© Raúl García Castro et xsd:. :x ns:p :y ns:p "42"^^xsd:integer. :z ns:p "abc"^^dt:specialDatatype. SELECT ?v WHERE { ?v ?p "cat" } v SELECT ?v WHERE { ?v ?p } v SELECT ?v WHERE { ?v ?p 42 } v SELECT ?v WHERE { ?v ?p "abc"^^ } v Matching RDF literals 25
© Raúl García Castro et al. Creating Values with Expressions (1.1) foaf:. _:a foaf:givenName "John". _:a foaf:surname "Doe". PREFIX foaf: SELECT ( CONCAT(?G, " ", ?S) AS ?name ) WHERE { ?P foaf:givenName ?G ; foaf:surname ?S } name ”John Doe" PREFIX foaf: SELECT ?name WHERE { ?P foaf:givenName ?G ; foaf:surname ?S BIND(CONCAT(?G, " ", ?S) AS ?name) }
© Raúl García Castro et al. Group graph pattern PREFIX foaf: SELECT ?name ?mbox WHERE { { ?x foaf:name ?name. } { ?x foaf:mbox ?mbox. } } SELECT ?x WHERE {} PREFIX foaf: SELECT ?name ?mbox WHERE { { ?x foaf:name ?name. } { ?x foaf:mbox ?mbox. FILTER regex(?name, "Smith")} } 27
© Raúl García Castro et al. Optional graph rdf:. _:a rdf:type foaf:Person. _:a foaf:name "Alice". _:a foaf:mbox. _:b rdf:type foaf:Person. _:b foaf:name "Bob". PREFIX foaf: SELECT ?name ?mbox WHERE { ?x foaf:name ?name. OPTIONAL { ?x foaf:mbox ?mbox } } namembox "Alice" "Alice" “Bob" 28
© Raúl García Castro et al. Multiple optional graph foaf:. _:a foaf:name "Alice". _:a foaf:homepage. _:b foaf:name "Bob". _:b foaf:mbox. PREFIX foaf: SELECT ?name ?mbox ?hpage WHERE { ?x foaf:name ?name. OPTIONAL { ?x foaf:mbox ?mbox }. OPTIONAL { ?x foaf:homepage ?hpage } } namemboxhpage "Alice" “Bob" 29
© Raúl García Castro et al. Alternative graph dc11:. _:a dc10:title "SPARQL Query Language Tutorial". _:a dc10:creator "Alice". _:b dc11:title "SPARQL Protocol Tutorial". _:b dc11:creator "Bob". _:c dc10:title "SPARQL". _:c dc11:title "SPARQL (updated)". PREFIX dc10: PREFIX dc11: SELECT ?title WHERE { { ?book dc10:title ?title } UNION { ?book dc11:title ?title } } title "SPARQL Protocol Tutorial" "SPARQL" "SPARQL (updated)" "SPARQL Query Language Tutorial" SELECT ?x ?y WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } } SELECT ?title ?author WHERE { { ?book dc10:title ?title. ?book dc10:creator ?author } UNION { ?book dc11:title ?title. ?book dc11:creator ?author }} xy "SPARQL (updated)" "SPARQL Protocol Tutorial" "SPARQL" "SPARQL Query Language Tutorial" authortitle "Alice""SPARQL Protocol Tutorial" “Bob”"SPARQL Query Language Tutorial" 30
© Raúl García Castro et al. Patterns on named graphs # Named graph: rdfs:. _:a foaf:name "Alice". _:a foaf:mbox. _:a foaf:knows _:b. _:b foaf:name "Bob". _:b foaf:mbox. _:b foaf:nick "Bobby". _:b rdfs:seeAlso. rdf:type foaf:PersonalProfileDocument. # Named graph: rdfs:. _:z foaf:mbox. _:z rdfs:seeAlso. _:z foaf:nick "Robert". rdf:type foaf:PersonalProfileDocument. 31
© Raúl García Castro et al. Patterns on named graphs II PREFIX foaf: PREFIX data: SELECT ?nick FROM NAMED WHERE { GRAPH data:bobFoaf { ?x foaf:mbox. ?x foaf:nick ?nick } } nick "Robert" PREFIX foaf: SELECT ?src ?bobNick FROM NAMED WHERE { GRAPH ?src { ?x foaf:mbox. ?x foaf:nick ?bobNick } srcbobNick "Bobby" "Robert" 32
© Raúl García Castro et al. Index Introduction Syntax Graph patterns Restricting values and solutions SPARQL query forms SPARQL
© Raúl García Castro et al. Restricting ns:. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, "^SPARQL") } title "SPARQL Tutorial" PREFIX dc: SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, "web", "i" ) } title "The Semantic Web" PREFIX dc: PREFIX ns: SELECT ?title ?price WHERE { ?x ns:price ?price. FILTER (?price < 30.5) ?x dc:title ?title. } titleprice "The Semantic Web"23 34
© Raúl García Castro et al. Value tests Based on XQuery 1.0 and XPath 2.0 Function and Operators XSD boolean, string, integer, decimal, float, double, dateTime Notation, =, = and != for value comparison Apply to any type BOUND, isURI, isBLANK, isLITERAL REGEX, LANG, DATATYPE, STR (lexical form) Function call for casting and extensions functions 35
© Raúl García Castro et al. Solution sequences and modifiers Order modifier: put the solutions in order Projection modifier: choose certain variables Distinct modifier: ensure solutions in the sequence are unique Reduced modifier: permit elimination of some non-unique solutions Limit modifier: restrict the number of solutions Offset modifier: control where the solutions start from in the overall sequence of solutions SELECT ?name WHERE { ?x foaf:name ?name ; :empId ?emp } ORDER BY ?name DESC(?emp) SELECT ?name WHERE { ?x foaf:name ?name } SELECT DISTINCT ?name WHERE { ?x foaf:name ?name } SELECT REDUCED ?name WHERE { ?x foaf:name ?name } SELECT ?name WHERE { ?x foaf:name ?name } ORDER BY ?name LIMIT 5 OFFSET 10 SELECT ?name WHERE { ?x foaf:name ?name } LIMIT 20 36
© Raúl García Castro et al. Index Introduction Syntax Graph patterns Restricting values and solutions SPARQL query forms SPARQL
© Raúl García Castro et al. SPARQL query forms SELECT -Returns all, or a subset of, the variables bound in a query pattern match CONSTRUCT -Returns an RDF graph constructed by substituting variables in a set of triple templates ASK -Returns a boolean indicating whether a query pattern matches or not DESCRIBE -Returns an RDF graph that describes the resources found 38
© Raúl García Castro et al. SPARQL query forms: foaf:. _:a foaf:name "Alice". _:a foaf:knows _:b. _:a foaf:knows _:c. _:b foaf:name "Bob". _:c foaf:name "Clare". _:c foaf:nick "CT". PREFIX foaf: SELECT ?nameX ?nameY ?nickY WHERE { ?x foaf:knows ?y ; foaf:name ?nameX. ?y foaf:name ?nameY. OPTIONAL { ?y foaf:nick ?nickY } } nameXnameYnickY "Alice""Bob" "Alice""Clare""CT" 39
© Raúl García Castro et foaf:. _:a foaf:name "Alice". _:a foaf:mbox. PREFIX foaf: PREFIX vcard: CONSTRUCT { vcard:FN ?name } WHERE { ?x foaf:name ?name vcard:. vcard:FN "Alice". Query result: SPARQL query forms: CONSTRUCT 40
© Raúl García Castro et al. SPARQL query forms: foaf:. _:a foaf:name "Alice". _:a foaf:homepage. _:b foaf:name "Bob". _:b foaf:mbox. PREFIX foaf: ASK { ?x foaf:name "Alice" } yes Query result: 41
© Raúl García Castro et al. PREFIX ent: DESCRIBE ?x WHERE { ?x ent:employeeId "1234" owl: _:a exOrg:employeeId "1234" ; foaf:mbox_sha1sum "ABCD1234" ; vcard:N [ vcard:Family "Smith" ; vcard:Given "John" ]. foaf:mbox_sha1sum rdf:type owl:InverseFunctionalProperty. Query result: SPARQL query forms: DESCRIBE 42
© Raúl García Castro et al. Index Introduction Syntax Graph patterns Restricting values and solutions SPARQL query forms SPARQL
© Raúl García Castro et al. SPARQL 1.1 New features in Query language: -Aggregate functions: COUNT, SUM, MIN, MAX, AVG, GROUP_CONCAT, and SAMPLE. -Subqueries: Nest the results of a query within another query -Negation: Check the absence of triples in a graph -Expressions in SELECT: Introduce new variables in the SELECT clause -Property paths: Search graphs through structures that involve arbitrary-length paths -Assignment: BIND keyword and expressions in SELECT and GROUP_BY -Short form for CONSTRUCT -Expanded functions and operators: EXISTS, NOT EXISTS, SUBSTR, etc. 44
© Raúl García Castro et al. Other SPARQL 1.1 specifications Update -Language extension to express updates to an RDF graph/store Protocol -Changes related to the update operation Service description -Discover a SPARQL endpoint's capabilities and summary information of its data Graph Store HTTP Protocol -Update and fetch RDF graph content from a Graph Store over HTTP in the REST style Entailment Regimes -Define the semantics of SPARQL queries for some entailment frameworks: OWL flavors, RDFS, RIF Federation Extensions -Take a query and provide solutions based on information from many different sources Query Result JSON, CSV, TSV 45
© Raúl García Castro et al. Main References W3C SPARQL Working Group Prud’hommeaux E, Seaborne A (2008) SPARQL Query Language for RDF. W3C Recommendation 15 January Clark K G, Feigenbaum L, Torres E (2008) SPARQL Protocol for RDF. W3C Recommendation 15 January Harris S, Seaborne A (2013) SPARQL 1.1 Query Language. W3C Recommendation 21 March SPARQL validator: SPARQL implementations: SPARQL Endpoints SPARQL in Dbpedia 46
Speaker: Daniel Vila-Suero Thank you for your attention!