290 likes | 454 Views
SPARQL. Semantic Web - Spring 2008 Computer Engineering Department Sharif University of Technology. Outline. SPARQL Syntax Semantics. Introduction. W3C recommendation 15 January 2008 for RDF query language Builds on previous RDF query languages such as rdfDB, RDQL, and SeRQL.
E N D
SPARQL Semantic Web - Spring 2008 Computer Engineering Department Sharif University of Technology
Outline • SPARQL • Syntax • Semantics
Introduction • W3C recommendation 15 January 2008 • for RDF query language • Builds on previous RDF query languages such as rdfDB, RDQL, and SeRQL. • SPARQL can be used to express queries across diverse data sources, whether the data is stored natively as RDF or viewed as RDF via middleware. • SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions.
Making simple queries • Most forms of SPARQL query contain a set of triple patterns called a basic graph pattern. • Triple patterns are like RDF triples except that each of the subject, predicate and object may be a variable. • A basic graph pattern matches a subgraph of the RDF data when RDF terms from that subgraph may be substituted for the variables and the result is RDF graph equivalent to the subgraph.
Example • Simple Query: PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?url FROM <bloggers.rdf> WHERE { ?contributor foaf:name "Jon Foobar" . ?contributor foaf:weblog ?url . }
Example (cont.) • Optional block: PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?depiction WHERE { ?person foaf:name ?name . OPTIONAL { ?person foaf:depiction ?depiction . } }
Example (cont.) • Alternative matches: PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?name ?mbox WHERE { ?person foaf:name ?name . { { ?person foaf:mbox ?mbox } UNION { ?person foaf:mbox_sha1sum ?mbox } } } • There are many other features in SparQL which is out of scope for this class. Refer to references for more information.
Semantics • A SPARQL graph pattern expression is defined recursively as follows: • (1) A tuple from (IL[V )×(I[V )×(IL[V) is a graph pattern (a triple pattern). • (2) If P1 and P2 are graph patterns, then expressions (P1 AND P2), (P1 OPT P2), and (P1 UNION P2) are graph patterns. • (3) If P is a graph pattern and R is a SPARQL built-in condition, then the expression (P FILTER R) is a graph pattern.
Semantics (cont.) • Semantics of SPARQL is defined using a mapping mechanism • A shorthand notation is used to denote the triple obtained by replacing the variables in t according to • Two mappings μ1 and μ2 are compatible when for all x in dom(μ1) intersect dom(μ2), it is the case that μ1(x) = μ2(x), i.e. when union of μ1 and μ2 is also a mapping.
Semantics (cont.) • Let and be sets of mappings. We define the join of, the union of and the difference between and as:
Example • Consider the RDF dataset D:
SPARQL vs SQL • SPARQLSELECT ?salWHERE { emps:e13954 HR:salary ?sal . } • SQL SELECT salary FROM employees WHERE emp_id = 'e13954'
SPARQL vs SQL • SPARQLSELECT ?id, ?salWHERE { ?id HR:salary ?sal } • SQL SELECT emp_id, salary FROM employees
SPARQL vs SQL • SPARQLSELECT ?hdateWHERE { ?id HR:salary ?sal . ?id HR:hire_date ?hdate . FILTER ?sal >= 21750 } • SQL SELECT hire_date FROM employees WHERE salary >= 21750
SPARQL vs SQL • SPARQL SELECT ?hdate WHERE { ?id HR:salary ?sal . ?id HR:hire_date ?hdate . FILTER ?sal >= 21750 } • SQL SELECT v.hire_date FROM emp_vars AS v, emp_consts AS c WHERE v.salary >= 21750 AND v.emp_id = c.emp_id
References • http://www.w3.org/TR/rdf-sparql-query/ • http://www-128.ibm.com/developerworks/java/library/j-sparql/ • JorgeP´erez , MarceloArenas , and ClaudioGutierrez, “Semantics and Complexity of SPARQL” , 5th International Semantic Web Conference, Athens, GA, USA, November 5-9, 2006, LNCS 4273