RDF and SPARQL Madalina Croitoru Representing Knowledge What type of knowledge • • • • • • Novels and short stories are books A book is a document Every book has an author A document has a title A title is a String Hugo is the author of Notre Dame de Paris How to represent this using: • Conceptual Graphs • Database Model • RDF(S) Generally speaking Facts: Conjunction of positive atoms {p(x, y), q(y, z, t), p(z, A), q(z, t, B)} Generally speaking Facts: Conjunction of positive atoms {p(x, y), q(y, z, t), p(z, A), q(z, t, B)} x 1 p x y y z A z z t t B y A q p 2 B 1 3 2 z t q Conceptual Graphs - Databases BoyId GirlId BoyId Name GirlId Name 1 2 1 John 2 Eva Conceptual Graphs - Databases BoyId GirlId BoyId Name GirlId Name 1 2 1 John 2 Eva RDF(S)??? Representing Knowledge • • • • • Conceptual Graphs Database Model RDF(S) OWL 2… … What good to represent knowledge? • Querying! • What type of queries? – Conjunctive queries – Ontological queries Facts Ontological Conjuctive Query Conjunctive Answering ╞ Facts: Conjunction of positive atoms Query Ontology Query: Conjunction of positive atoms Ontological Conjuctive Query Answering Facts Ontology Φ ╞ Conjunctive Query Facts ╞ Conjunctive Query Simple RDF entailment Database Conjunctive Query Answering Deduction in the conjunctive, positive, existential fragment of FOL Ontological Conjuctive Query Answering Facts ╞ Ontology Type Hierarchy Every cat is a vertebrate If two people are brothers then they are relatives Conjunctive Query Facts + Type Hierarchy ╞ Conjunctive Query RDF(S) entailment Conjunctive Positive Fragment of FOL Simple Conceptual Graphs Entailment Ontological Conjuctive Query Answering Facts ╞ Ontology Rules For every cat there exists a cat who is its mother Conjunctive Query Facts + Rules ╞ Conjunctive Query Simple Conceptual Graphs Rules DATALOG + Conjunction of positive atoms • Conceptual Graphs • Database Model • RDF(S) Conjunction of positive atoms • Conceptual Graphs • Database Model • RDF(S) HOW TO QUERY THIS KNOWLEDGE?!? Conjunction of positive atoms • Conceptual Graphs – projection (labelled graph homomorphism) • Database Model • RDF(S) HOW TO QUERY THIS KNOWLEDGE? Conjunction of positive atoms • Conceptual Graphs • Database Model - SQL • RDF(S) HOW TO QUERY THIS KNOWLEDGE Conjunction of positive atoms • Conceptual Graphs • Database Model • RDF(S) - SPARQL HOW TO QUERY THIS KNOWLEDGE Codd’s Relational Model Codd’s Relational Model RDF RDF The "http://en.wikipedia.org/wiki/Tony_Benn", published by Wikipedia, has for title 'Tony Benn‘ <http://en.wikipedia.org/wiki/Tony_Benn> <http://purl.org/dc/elements/1.1/publisher> "Wikipedia" <http://en.wikipedia.org/wiki/Tony_Benn> <http://purl.org/dc/elements/1.1/title> "Tony Benn" . RDF The "http://en.wikipedia.org/wiki/Tony_Benn", published by Wikipedia, has for title 'Tony Benn‘. <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> <rdf:Description rdf:about="http://en.wikipedia.org/wiki/Tony_Benn"> <dc:title>Tony Benn</dc:title> <dc:publisher>Wikipedia</dc:publisher> </rdf:Description> </rdf:RDF> SQL for Databases • SELECT: – FROM (tables) – WHERE (predicate) – GROUP BY (rows) – HAVING (predicate on GROUP BY rows) – ORDER BY (columns) Select Clause example SELECT * FROM Book WHERE price > 100.00 ORDER BY title; SPARQL SELECT ?name ?email WHERE { ?person rdf:type foaf:Person. ?person foaf:name ?name. ?person foaf:mbox ?email. } SPARQL PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?email WHERE { ?person rdf:type foaf:Person. ?person foaf:name ?name. ?person foaf:mbox ?email. } SPARQL – example 1 Data: <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" . Query: SELECT ?title WHERE { <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title . } SPARQL – example 2 Data: @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a _:a _:b _:b _:c foaf:name foaf:mbox foaf:name foaf:mbox foaf:mbox "Johnny Lee Outlaw" . <mailto:jlow@example.com> . "Peter Goodguy" . <mailto:peter@example.org> . <mailto:carol@example.org> . SPARQL – example 2 Query: PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { ?x foaf:name ?name . ?x foaf:mbox ?mbox } SPARQL – example 2 Result: name "Johnny Lee Outlaw“ "Peter Goodguy" mbox <mailto:jlow@example.com> <mailto:peter@example.org> Construct in SPARQL Data: @prefix org: <http://example.com/ns#> . _:a org:employeeName "Alice" . _:a org:employeeId 12345 . _:b org:employeeName "Bob" . _:b org:employeeId 67890 . Construct in SPARQL Query: PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX org: <http://example.com/ns#> CONSTRUCT { ?x foaf:name ?name } WHERE { ?x org:employeeName ?name } Construct in SPARQL Result: @prefix org: <http://example.com/ns#> . _:x _:y foaf:name foaf:name "Alice" . "Bob" . Construct in SPARQL Result Serialized: <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:foaf="http://xmlns.com/foaf/0.1/" > <rdf:Description> <foaf:name>Alice</foaf:name> </rdf:Description> <rdf:Description> <foaf:name>Bob</foaf:name> </rdf:Description> </rdf:RDF> SPARQL exercise • Use both SELECT and CONSTRUCT • What are all the country capitals of Africa?