REPRESENTATION DES CONNAISSANCES Cours préparé pour l Université de Stendhal, Grenoble (France) Cédric Lopez http://www.viseo.net/cedric-lopez clopez@objetdirect.com Frédérique Segond http://www.viseo.net/frederique-segond fsegond@objetdirect.com 1
Rappel 1/2 Dessinez un graphe RDF représentant la situation suivante : Pauline a 22 ans. Pauline connaît Lucie. Pauline et Lucie sont des étudiantes. Les étudiantes sont des adultes. http://www.grenoble-univ.fr/ex#adulte http://www.grenoble-univ.fr/ex#pauline http://www.w3org/2000/01/rdf-schema#subclassof http://www.w3org/1999/02/22-rdf-syntax-ns#type http://www.grenoble-univ.fr/ex#age 22 http://www.grenoble-univ.fr/ex#lucie http://www.grenoble-univ.fr/ex#etudiante http://www.grenoble-univ.fr/ex#connait http://www.w3org/1999/02/22-rdf-syntax-ns#type 2
Rappel (2/2) RDF/XML <?xml version="1.0"?> <rdf:rdf xmlns:local="http://www.polymtl.ca/vocabulary#" xmlns:ing="http://www.viseo.net/ing#" <rdf:description rdf:about="http://www.viseo.net/ing#cedriclopez"> <local:worksat rdf:resource="http://www.viseo.net/vocabulary#dgi"/> <local:hasname>cédric Lopez</local:hasName> <local:hashomepage rdf:resource="http://www.viseo.net/cedric-lopez"/> </rdf:description> </rdf:rdf> 3
5. Interrogation des descriptions RDF avec SPARQL Cours préparé pour l Université de Stendhal, Grenoble (France) Cédric Lopez http://www.viseo.net/cedric-lopez clopez@objetdirect.com VISEO Objet Direct 4, avenue Doyen Louis Weil 38000 Grenoble http://www.viseo.net/ 4
Vers le Web sémantique 2011 5
SPARQL («étincelle») = Protocol and RDF Query Language Langage de requête qui permet de rechercher, d'ajouter, de modifier ou de supprimer des données RDF (triplets) disponibles à travers Internet. SPARQL est le langage développé par le W3C (http://www.w3.org/tr/rdf-sparqlquery/) Recommandation officielle du W3C depuis 2008 Equivalent de SQL SQL SPARQL Base de données Web des données Vers un accès à toutes les données du Web!!! 6
Matching de littéraux 7
Données : <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> "SPARQL Tutorial" SELECT?title Requête SPARQL : WHERE { <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title>?title Résultats : title "SPARQL Tutorial" 8
@prefix foaf: <http://xmlns.com/foaf/0.1/>. _: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>. _:c foaf:mbox <mailto:carol@example.org>. PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT?name?mbox WHERE {?x foaf:name?name.?x foaf:mbox?mbox name "Johnny Lee Outlaw" "Peter Goodguy" mbox <mailto:jlow@example.com> <mailto:peter@example.org> 9
@prefix dt: <http://example.org/datatype#>. @prefix ns: <http://example.org/ns#>. @prefix : <http://example.org/ns#>. @prefix xsd: <http://www.w3.org/2001/xmlschema#>. :x ns:p "cat"@en. :y ns:p "42"^^xsd:integer. :z ns:p "abc"^^dt:specialdatatype. SELECT?v WHERE {?v?p "cat" v 10
@prefix dt: <http://example.org/datatype#>. @prefix ns: <http://example.org/ns#>. @prefix : <http://example.org/ns#>. @prefix xsd: <http://www.w3.org/2001/xmlschema#>. :x ns:p "cat"@en. :y ns:p "42"^^xsd:integer. :z ns:p "abc"^^dt:specialdatatype. SELECT?v WHERE {?v?p "cat"@en v <http://example.org/ns#x> 11
@prefix dt: <http://example.org/datatype#>. @prefix ns: <http://example.org/ns#>. @prefix : <http://example.org/ns#>. @prefix xsd: <http://www.w3.org/2001/xmlschema#>. :x ns:p "cat"@en. :y ns:p "42"^^xsd:integer. :z ns:p "abc"^^dt:specialdatatype. SELECT?v WHERE {?v?p 42 v <http://example.org/ns#y> 12
Construction de graphes RDF par SPARQL 13
@prefix org: <http://example.com/ns#>. _:a org:employeename "Alice". _:a org:employeeid 12345. _:b org:employeename "Bob". _:b org:employeeid 67890. PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX org: <http://example.com/ns#> CONSTRUCT {?x foaf:name?name WHERE {?x org:employeename?name @prefix org: <http://example.com/ns#>. _:a foaf:name "Alice". _:b foaf:name "Bob". 14
Filtres sur les chaines de caractères, sur les valeurs, Opérateurs possibles: Comparaison : < <= = >= >!= Opération : + * / - Booléen : && (and) (or)! (not) Fonction : isblank(?x) regex(?x, «string") 15
@prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix : <http://example.org/book/>. @prefix ns: <http://example.org/ns#>. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT?title WHERE {?x dc:title?title FILTER regex(?title, "^SPARQL") 16
@prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix : <http://example.org/book/>. @prefix ns: <http://example.org/ns#>. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT?title WHERE {?x dc:title?title FILTER regex(?title, "^SPARQL") 17
@prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix : <http://example.org/book/>. @prefix ns: <http://example.org/ns#>. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT?title WHERE {?x dc:title?title FILTER regex(?title, "^SPARQL") 18
@prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix : <http://example.org/book/>. @prefix ns: <http://example.org/ns#>. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT?title WHERE {?x dc:title?title FILTER regex(?title, "^SPARQL") 19
@prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix : <http://example.org/book/>. @prefix ns: <http://example.org/ns#>. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT?title WHERE {?x dc:title?title FILTER regex(?title, "^SPARQL") title "SPARQL Tutorial" 20
@prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix : <http://example.org/book/>. @prefix ns: <http://example.org/ns#>. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ns: <http://example.org/ns#> SELECT?title?price WHERE {?x ns:price?price. FILTER (?price < 30.5)?x dc:title?title. Autre exemple : filtre sur les valeurs title price "The Semantic Web" 23 21
OPTIONS 22
@prefix foaf: <http://xmlns.com/foaf/0.1/>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. _:a rdf:type foaf:person. _:a foaf:name "Alice". _:a foaf:mbox <mailto:alice@example.com>. _:a foaf:mbox <mailto:alice@work.example>. _:b rdf:type foaf:person. _:b foaf:name "Bob". PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT?name?mbox WHERE {?x foaf:name?name. OPTIONAL {?x foaf:mbox?mbox name "Alice" "Alice" "Bob" mbox <mailto:alice@example.com> <mailto:alice@work.example> 23
FILTRE dans les OPTIONS 24
@prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix : <http://example.org/book/>. @prefix ns: <http://example.org/ns#>. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ns: <http://example.org/ns#> SELECT?title?price WHERE {?x dc:title?title. OPTIONAL{?x ns:price?price. FILTER (?price < 30) title "SPARQL Tutorial" price "The Semantic Web" 23 25
UNION 26
@prefix foaf: <http://xmlns.com/foaf/0.1/>. @prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>. _:a foaf:name «Philippe". _:b foaf:name «Frédérique". _:c vcard:fn «Vasiliki". _:d vcard:fn «Cédric". PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT?name WHERE { []?p?name FILTER (?p = foaf:name?p = vcard:fn ) name Philippe Frédérique Vasiliki Cédric 27
SPARQL sur Dbpedia : http://fr.dbpedia.org/sparql Exemples de requêtes : http://wimmics.inria.fr/projects/dbpedia/doc/index.php/exemples_sparql 28