View
3
Download
0
Category
Preview:
Citation preview
M2 DAC – LODAS 2019 – – Bernd Amann
M2 – DAC - LODAS - 2019MU5IN860 Linked Open Data, Apprentisssage Symbolique
Bernd Amann
SU
1er octobre 2019
115 / 356
M2 DAC – LODAS 2019 – – Bernd Amann
SPARQL : Interrogation RDF
1 SPARQL 1.0 : Motifs de Graphes
2 SPARQL 1.1 : Aggregation, expressions de chemins, ...
116 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Bernd Amann
Outline
1 SPARQL 1.0 : Motifs de Graphes
2 SPARQL 1.1 : Aggregation, expressions de chemins, ...
117 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Introduction Bernd Amann
Linking Open Dataprojet W3C Semantic Web Education and Outreachobjectif : relier les grandes sources de données sémantiques
1,239 datasets with 16,147 links (as of March 2019)Linking Open Data cloud diagram, by Richard Cyganiak and Anja Jentzsch.http://lod-cloud.net/Exemple : https://lod-cloud.net/dataset/dbpedia-fr
118 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Introduction Bernd Amann
Interroger des graphes RDF
On chercheles noms des roisles rois sans filsles reines avec plus que troisenfantsles noms des déscendants deFrançois Ierles frères de Henry III
Un langage de requête (RDF) doitêtre déclaratif et indépendant d’une implantationavoir une sémantique formelle préciseprendre en compte le schéma (schema awareness)permettre d’interroger le schéma RDFS et les données RDF
119 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Introduction Bernd Amann
Langages de requêtes RDF
Trois familles de langages de requêtes RDF :langages ensemblistes (objet/relationnel) : RQL (chemins / objets),Squish (triplets), RDQL (triplets)langages “matching de graphes” : DQL, QEL, N3, SPARQLlangages XML étendus pour RDF : RQuery (XQuery+fonctions), RDFQuery
120 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
1 - SPARQL 1.0 : Motifs de Graphes
Motifs de graphes
121 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
SPARQL Protocol and RDF Query LanguageSPARQL Protocol and RDF Query Language (W3C Recommendation 15 January 2008) estun protocole et un langage de requête RDF qui permet la définition de motifs de graphes :
données : graphes étiquetés avec des noeuds blancsrequêtes : motifs de graphe avec des variables et des filtres
Turtle ex1
@prefix : < h t t p : / / asws . upmc . f r / exemples#> .@prefix r d f : < h t t p : / / www.w3 . org /1999/02/22− rd f−syntax−ns#> .: book1 : t i t l e " Jena SPARQL T u t o r i a l " ; : author " Apache " ;
r d f : type : Book .
Requête ex1
PREFIX : < h t t p : / / asws . upmc . f r / exemples#>SELECT ? t i t l eFROM <ex1 . t t l >WHERE { : book1 : t i t l e ? t i t l e . }
Résultat de ex1
--------------------------| title |==========================| "Jena SPARQL Tutorial" |--------------------------
122 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Motif de tripletsTerme RDF
Un terme RDF t est un élément dans l’ensemble t ∈ I ∪ L ∪ B :I : ensemble de tous les IRI (International Resource Identifiers)L : litéraux RDF (valeurs) : "val"@lang^^typeB : noeuds blancs
Motif de triplet
Soit donné un ensemble de variables V tel que V ∩ T = ∅. Un motif de triplet est un élémentde l’ensemble (T ∪ V )× (I ∪ V )× (T ∪ V ).
Exemples
:book dc:title "SPARQL Facile"@fr .:book dc:year "2007"^^xsd:integer .?book dc:title ?title .?book dc:yeard ?year ._:a foaf:name "Bob" .
123 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Motifs de graphesMotif de graphes
Un ensemble de motifs de triplets est un motif de graphe élémentaire.Si GP est un motif de graphe, { GP } est un motif de graphe de groupe.Si GP et GP’ sont des motifs de graphe :
GP FILTER (test) GP’ : sélection/filtrageGP OPTIONAL GP’ : graphes optionnelsGP UNION GP’ : union de graphesGRAPH G { GP} : graphes nommés
sont des motifs de graphe.
Exemples
Motif de graphe élémentaire :_:a foaf:age 26 . _:a foaf:mbox ?m .Motif de groupe :{ ?a foaf:age 26 . ?a foaf:mbox ?m . }Motif de groupe avec FILTRE :{ _:a foaf:age ?age .FILTER (?age < 26)_:a foaf:mbox ?m . }
Motif de groupe avec OPTIONAL :{ ?a foaf:age 26 .OPTIONAL { ?a foaf:mbox ?m . }}
Motif de graphe nommé :GRAPH ?g { ?a foaf:age 26 . }
124 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Syntaxe SPARQLRequête SPARQL
<format >FROM <source >
WHERE { <mot i f > }<transform >
<format> définit le format du résultatFROM définit la source RDF (optionnel si source par défaut)<motif> est un motif de graphe<transform> est un transformateur : ORDER, LIMIT, OFFSET
format résultatSELECT, SELECT DISTINCT table de données (bindings)CONSTRUCT graphe RDFASK valeur Booléenne (résultat non-vide)DESCRIBE “description” des ressources trouvées
125 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Une requête simpleTurtle ex2
@prefix f o a f : < h t t p : / / xmlns . com/ f o a f / 0 .1 / > ._ : a f o a f : name " A l i ce " ; f o a f : mbox <ma i l t o : alice@work . example> ._ : b f o a f : name "Bob" ; f o a f : mbox <ma i l t o : bob@work . example> ._ : c f o a f : name " A l i c e " ; f o a f : mbox <ma i l t o : alice@home . example> .
Requête ex2.1
PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0 .1 / >SELECT ?nameFROM <ex2 . t t l >WHERE { ?x f o a f : name ?name }
Résultat de ex2.1
-----------| name |===========| "Alice" || "Bob" || "Alice" |-----------
Requête ex2.2
PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0 .1 / >SELECT DISTINCT ?nameFROM <ex2 . t t l >WHERE { ?x f o a f : name ?name }
Résultat de ex2.2
-----------| name |===========| "Alice" || "Bob" |-----------
126 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
SémantiqueSémantique d’une requête
Un mapping de variables S est une fonction totale des variables V d’un motif degraphe vers l’ensemble de termes RDF (IRI, noeuds blancs, valeurs).Un mapping de variables S est une solution de motif pour une requête<format> FROM <source> WHERE { <motif> }si S(<motif>) est un sous-graphe du graphe <source>.Le résultat d’une requête est l’ensemble de toutes ses solutions de motifs.
Autre définition : Le résultat d’une requête est l’ensemble de toutes les instancesde S(< motif >) qui sont des conséquences de < source > :< source >|= S(< motif >).
127 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Motif de Graphes
Query q1
s e l e c t ?nf ?nefrom <ex1 . t t l >where { ?r :nom " Francois I " ;
: f i l s [ :nom ?nf ; : epouse : r5 ] .: r5 :nom ?ne }
Query q1ext
s e l e c t ?nf ?nefrom <ex1 . t t l >where { ?r :nom " Francois I " .
?r : f i l s ? f .? f :nom ?nf .? f : epouse : r5 .: r5 :nom ?ne }
Basic Graph Pattern (BGP)
Expression Turtle où chaque noeud ou propriété peut être remplacé par des variables.
128 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Motifs de Graphes : InterprétationMapping query Q in data-set G
M = ensemble de mappings m de Q vers G tel que m(Q) est unsous-graphe de G.La réponse est l’ensemble de bindings m(?nf , ?ne) des variables ?nf et?ne dans la clause SELECT de Q.
129 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Requête simple avec noeud blanc (exists)Turtle ex2
@prefix f o a f : < h t t p : / / xmlns . com/ f o a f / 0 .1 / > ._ : a f o a f : name " A l i ce " ; f o a f : mbox <ma i l t o : alice@work . example> ._ : b f o a f : name "Bob" ; f o a f : mbox <ma i l t o : bob@work . example> ._ : c f o a f : name " A l i c e " ; f o a f : mbox <ma i l t o : alice@home . example> .
Requête ex3
PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0 .1 / >SELECT ?nameFROM <ex2 . t t l >WHERE { _ : f f o a f : name ?name ; f o a f : mbox _ : c . }
(noeud blanc=variable avec quantification() existentielle→ ne peut pas apparaître dans SELECT !)
Résultat de ex3
-----------| name |===========| "Alice" || "Bob" || "Alice" |-----------
130 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
OPTIONALTurtle ex4
@prefix f o a f : < h t t p : / / xmlns . com/ f o a f / 0 .1 / > .[ f o a f : name " A l i c e " ;
f o a f : knows [ f o a f : name "Bob" ] ;f o a f : knows [ f o a f : name " Clare " ;
f o a f : nickname "CT" ] ]
OPTIONAL : définir des mappings « optionnels » :
Requête ex4sansopt
PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0 .1 / >SELECT ?nameX ?nameY ?nickYFROM <ex4 . t t l >WHERE { ?x f o a f : knows ?y ;
f o a f : name ?nameX .?y f o a f : name ?nameY ;
f o a f : nickname ?nickY }
Résultat de ex4sansopt
-----------------------------| nameX | nameY | nickY |=============================| "Alice" | "Clare" | "CT" |-----------------------------
Requête ex4
PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0 .1 / >SELECT ?nameX ?nameY ?nickYFROM <ex4 . t t l >WHERE { ?x f o a f : knows ?y ;
f o a f : name ?nameX .?y f o a f : name ?nameY .
OPTIONAL { ?y f o a f : nickname ?nickY } }
Résultat de ex4
-----------------------------| nameX | nameY | nickY |=============================| "Alice" | "Clare" | "CT" || "Alice" | "Bob" | |-----------------------------
131 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
FILTER: définir des prédicats / filtres sur les variables.
Turtle ex5
ns : book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 .ns : book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 .ns : book3 dc : t i t l e "RDF Framework " .
FILTER
Requête ex5.1
SELECT ? t i t l e ?pr i ceFROM <ex5 . t t l >WHERE { ?x ns : p r i c e ?pr i ce ;
dc : t i t l e ? t i t l e .FILTER ( ?p r i ce < 30) }
Résultat de ex5.1
------------------------------| title | price |==============================| "The Semantic Web" | 23 |------------------------------
Requête ex5.2
SELECT ? t i t l e ?pr i ceFROM <ex5 . t t l >WHERE { ?x dc : t i t l e ? t i t l e .
OPTIONAL { ?x ns : p r i ce ?pr i ce } .FILTER ( !BOUND( ?p r i ce ) ) }
Résultat de ex5.2
---------------------------| title | price |===========================| "RDF Framework" | |---------------------------
(les livres sans prix)OPTIONAL avec BOUND() permet d’exprimer exists/not exists.
132 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
MINUS!BOUND(var) (Sparql 1.0) : détection de liaisons absentes (avec OPTIONAL)P1 MINUS P2 (Sparql 1.1) : efface les lignes avec les liaisons correspondantes pour les variablespartagées entre P1 et P2.
Turtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i c e 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Les editeurs avec au moins un livre sans prix :
Requête ex19
SELECT ? e d i t o r FROM <ex14 . t t l >WHERE { ?y ns : e d i t o r ? e d i t o r MINUS { ?y ns : p r i ce ?pr i ce . } }
Résultat de ex19
----------------------------------| editor |==================================| <http://example.org/book/jena> |----------------------------------
Explication : Les deux motifs partagent la variable?y !
133 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
MINUSTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i c e 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex20
SELECT DISTINCT ? e d i t o rFROM <ex14 . t t l >WHERE { ?y ns : e d i t o r ? e d i t o r MINUS { ?z ns : p r i ce ?pr i ce . } }
Résultat ?
Résultat de ex20
----------------------------------| editor |==================================| <http://example.org/book/jena> || <http://example.org/book/w3c> |----------------------------------
Explication : Il n’y a pas de vairables partagées entre les deux sous-expressions⇒ MINUS est sans effet.
134 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
MINUSTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i c e 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex20
SELECT DISTINCT ? e d i t o rFROM <ex14 . t t l >WHERE { ?y ns : e d i t o r ? e d i t o r MINUS { ?z ns : p r i ce ?pr i ce . } }
Résultat ?
Résultat de ex20
----------------------------------| editor |==================================| <http://example.org/book/jena> || <http://example.org/book/w3c> |----------------------------------
Explication : Il n’y a pas de vairables partagées entre les deux sous-expressions⇒ MINUS est sans effet.
134 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
MINUSTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i c e 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Les éditeurs dont tous les livres ont un prix (division) :
Requête ex21
SELECT DISTINCT ? e d i t o rFROM <ex14 . t t l >WHERE { ?z ns : e d i t o r ? e d i t o r
MINUS { ?y ns : e d i t o r ? e d i t o rMINUS { ?y ns : p r i ce ?pr i ce . } } }
Résultat de ex21
---------------------------------| editor |=================================| <http://example.org/book/w3c> |---------------------------------
135 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
MINUSTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i c e 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Les éditeurs dont tous les livres ont un prix (division) :
Requête ex21
SELECT DISTINCT ? e d i t o rFROM <ex14 . t t l >WHERE { ?z ns : e d i t o r ? e d i t o r
MINUS { ?y ns : e d i t o r ? e d i t o rMINUS { ?y ns : p r i ce ?pr i ce . } } }
Résultat de ex21
---------------------------------| editor |=================================| <http://example.org/book/w3c> |---------------------------------
135 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
UNIONTurtle ex5
ns : book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 .ns : book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 .ns : book3 dc : t i t l e "RDF Framework " .
Requête union
SELECT ?x ?pr i ce ? t i t l eFROM <ex5 . t t l >WHERE { { ?x ns : p r i c e ?pr i ce . } UNION { ?x dc : t i t l e ? t i t l e . } }
Quel est le bon resultat ?
Résultat de union1
-----------------------------------------| x | price | title |================================| ns:book2 | 23 | || ns:book1 | 42 | || ns:book3 | | "RDF Framework" || ns:book2 | | "The Semantic Web" || ns:book1 | | "SPARQL Tutorial" |-----------------------------------------
Résultat de union2
-----------------------------------------| x | price | title |===================================| ns:book2 | 23 | "The Semantic Web" || ns:book1 | 42 | "SPARQL Tutorial" || ns:book3 | | "RDF Framework" |-----------------------------------------
Comment obtenir l’autre résultat ?
136 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
UNIONTurtle ex5
ns : book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 .ns : book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 .ns : book3 dc : t i t l e "RDF Framework " .
Requête union
SELECT ?x ?pr i ce ? t i t l eFROM <ex5 . t t l >WHERE { { ?x ns : p r i c e ?pr i ce . } UNION { ?x dc : t i t l e ? t i t l e . } }
Quel est le bon resultat ?
Résultat de union1
-----------------------------------------| x | price | title |================================| ns:book2 | 23 | || ns:book1 | 42 | || ns:book3 | | "RDF Framework" || ns:book2 | | "The Semantic Web" || ns:book1 | | "SPARQL Tutorial" |-----------------------------------------
Résultat de union2
-----------------------------------------| x | price | title |===================================| ns:book2 | 23 | "The Semantic Web" || ns:book1 | 42 | "SPARQL Tutorial" || ns:book3 | | "RDF Framework" |-----------------------------------------
Comment obtenir l’autre résultat ?
136 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
UNION avec CONSTRUCTRequête construct
PREFIX dc : < h t t p : / / p u r l . org / dc / elements /1 .1 / >PREFIX ns : < h t t p : / / asws . org / ns#>CONSTRUCT { ?x ns : p r i x ?pr i ce ;
ns : t i t r e ? t i t l e . }FROM <ex5 . t t l >WHERE { { ?x ns : p r i ce ?pr i ce . }
UNION{ ?x dc : t i t l e ? t i t l e . } }
Résultat de construct
@prefix dc: <http://purl.org/dc/elements/1.1/> .@prefix ns: <http://asws.org/ns#> .
ns:book2 ns:prix 23 ;ns:titre "The Semantic Web" .
ns:book1 ns:prix 42 ;ns:titre "SPARQL Tutorial" .
ns:book3 ns:titre "RDF Framework" .
137 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : trois-colorabilitéTurtle couleur
: rouge : e : bleu ; : e : v e r t .: b leu : e : rouge ; : e : v e r t .: v e r t : e : rouge ; : e : bleu .
Requête couleur1
SELECT DISTINCT ?a ?b ?c ?d ?f ?gFROM <couleur . t t l >WHERE {
?a : e ?b ; : e ?c ; : e ?d ; : e ? f ; : e ?g .?b : e ?a ; : e ?c ; : e ?d .?c : e ?a ; : e ?b ; : e ? f ; : e ?g .?d : e ?a ; : e ?b ; : e ?g .? f : e ?a ; : e ?c .?g : e ?a ; : e ?c ; : e ?d .
}
Résultat de couleur1
-------------------------------------------------------| a | b | c | d | f | g |=======================================================| :vert | :bleu | :rouge | :rouge | :bleu | :bleu || :vert | :rouge | :bleu | :bleu | :rouge | :rouge || :bleu | :vert | :rouge | :rouge | :vert | :vert || :bleu | :rouge | :vert | :vert | :rouge | :rouge || :rouge | :vert | :bleu | :bleu | :vert | :vert || :rouge | :bleu | :vert | :vert | :bleu | :bleu |-------------------------------------------------------
138 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : ORDER BY/LIMIT/OFFSETRequête orderby
SELECT ? t i t l e ?pr i ceFROM <ex5 . t t l >WHERE { ?x ns : p r i c e ?pr i ce ;
dc : t i t l e ? t i t l e . }ORDER BY ?pr i ce
Résultat de orderby
------------------------------| title | price |==============================| "The Semantic Web" | 23 || "SPARQL Tutorial" | 42 |------------------------------
Requête orderbyoffset
SELECT ? t i t l e ?pr i ceFROM <ex5 . t t l >WHERE { ?x ns : p r i c e ?pr i ce ;
dc : t i t l e ? t i t l e . }ORDER BY ?pr i ceLIMIT 1OFFSET 1
Résultat de orderbyoffset
-----------------------------| title | price |=============================| "SPARQL Tutorial" | 42 |-----------------------------
139 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : OPTIONAL imbriqué (1)Turtle bibliosem
: book1 : t i t l e "SPARQL T u t o r i a l " ; : p r i ce 42 ; : e d i t o r : jena .: book2 : t i t l e " The Semantic Web" ; : p r i ce 23 .: book3 : t i t l e "RDF Framework " ; : p r i x 53 .: book4 : t i t l e "SPARQL pour les Nuls " ; : e d i t o r : pour lesnu ls .: pour lesnu ls : address " Par is " .
Requête optional9a
SELECT ?x ? t i t l e ?y ?addressFROM <bib l iosem . t t l >
WHERE { ?x : t i t l e ? t i t l e .OPTIONAL { ?x : e d i t o r ?y .
OPTIONAL { ?y : address ?address } } }
Réponse?
Résultat de optional9a
------------------------------------------------------------| x | title | y | address |============================================================| :book4 | "SPARQL pour les Nuls" | :pourlesnuls | "Paris" || :book3 | "RDF Framework" | | || :book2 | "The Semantic Web" | | || :book1 | "SPARQL Tutorial" | :jena | |------------------------------------------------------------
140 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : OPTIONAL imbriqué (1)Turtle bibliosem
: book1 : t i t l e "SPARQL T u t o r i a l " ; : p r i ce 42 ; : e d i t o r : jena .: book2 : t i t l e " The Semantic Web" ; : p r i ce 23 .: book3 : t i t l e "RDF Framework " ; : p r i x 53 .: book4 : t i t l e "SPARQL pour les Nuls " ; : e d i t o r : pour lesnu ls .: pour lesnu ls : address " Par is " .
Requête optional9a
SELECT ?x ? t i t l e ?y ?addressFROM <bib l iosem . t t l >
WHERE { ?x : t i t l e ? t i t l e .OPTIONAL { ?x : e d i t o r ?y .
OPTIONAL { ?y : address ?address } } }
Réponse?
Résultat de optional9a
------------------------------------------------------------| x | title | y | address |============================================================| :book4 | "SPARQL pour les Nuls" | :pourlesnuls | "Paris" || :book3 | "RDF Framework" | | || :book2 | "The Semantic Web" | | || :book1 | "SPARQL Tutorial" | :jena | |------------------------------------------------------------
140 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : OPTIONAL imbriqué (2)Turtle bibliosem
: book1 : t i t l e "SPARQL T u t o r i a l " ; : p r i ce 42 ; : e d i t o r : jena .: book2 : t i t l e " The Semantic Web" ; : p r i ce 23 .: book3 : t i t l e "RDF Framework " ; : p r i x 53 .: book4 : t i t l e "SPARQL pour les Nuls " ; : e d i t o r : pour lesnu ls .: pour lesnu ls : address " Par is " .
Requête optional10
SELECT ?x ? t i t l e ?y ?addressFROM <bib l iosem . t t l >
WHERE { ?x : t i t l e ? t i t l e .OPTIONAL { ?x : e d i t o r : doesnotex is t
OPTIONAL { ?y : address ?address } } }
Réponse?
Résultat de optional10
-------------------------------------------------| x | title | y | address |=================================================| :book4 | "SPARQL pour les Nuls" | | || :book3 | "RDF Framework" | | || :book2 | "The Semantic Web" | | || :book1 | "SPARQL Tutorial" | | |-------------------------------------------------
La première jointure externe est vide et bloque la 2e jointure externe.
141 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : OPTIONAL imbriqué (2)Turtle bibliosem
: book1 : t i t l e "SPARQL T u t o r i a l " ; : p r i ce 42 ; : e d i t o r : jena .: book2 : t i t l e " The Semantic Web" ; : p r i ce 23 .: book3 : t i t l e "RDF Framework " ; : p r i x 53 .: book4 : t i t l e "SPARQL pour les Nuls " ; : e d i t o r : pour lesnu ls .: pour lesnu ls : address " Par is " .
Requête optional10
SELECT ?x ? t i t l e ?y ?addressFROM <bib l iosem . t t l >
WHERE { ?x : t i t l e ? t i t l e .OPTIONAL { ?x : e d i t o r : doesnotex is t
OPTIONAL { ?y : address ?address } } }
Réponse?
Résultat de optional10
-------------------------------------------------| x | title | y | address |=================================================| :book4 | "SPARQL pour les Nuls" | | || :book3 | "RDF Framework" | | || :book2 | "The Semantic Web" | | || :book1 | "SPARQL Tutorial" | | |-------------------------------------------------
La première jointure externe est vide et bloque la 2e jointure externe.
141 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : OPTIONAL imbriqué (3)Turtle bibliosem
: book1 : t i t l e "SPARQL T u t o r i a l " ; : p r i ce 42 ; : e d i t o r : jena .: book2 : t i t l e " The Semantic Web" ; : p r i ce 23 .: book3 : t i t l e "RDF Framework " ; : p r i x 53 .: book4 : t i t l e "SPARQL pour les Nuls " ; : e d i t o r : pour lesnu ls .: pour lesnu ls : address " Par is " .
Requête optional9
SELECT ?x ? t i t l e ?y ?addressFROM <bib l iosem . t t l >
WHERE { ?x : t i t l e ? t i t l e .OPTIONAL { ?y : address ?address .
OPTIONAL { ?x : e d i t o r : n o t e x i s t s } } }
Réponse?
Résultat de optional9
------------------------------------------------------------| x | title | y | address |============================================================| :book4 | "SPARQL pour les Nuls" | :pourlesnuls | "Paris" || :book3 | "RDF Framework" | :pourlesnuls | "Paris" || :book2 | "The Semantic Web" | :pourlesnuls | "Paris" || :book1 | "SPARQL Tutorial" | :pourlesnuls | "Paris" |------------------------------------------------------------
Produit cartesien entre les deux premiers motifs
142 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Exemple : OPTIONAL imbriqué (3)Turtle bibliosem
: book1 : t i t l e "SPARQL T u t o r i a l " ; : p r i ce 42 ; : e d i t o r : jena .: book2 : t i t l e " The Semantic Web" ; : p r i ce 23 .: book3 : t i t l e "RDF Framework " ; : p r i x 53 .: book4 : t i t l e "SPARQL pour les Nuls " ; : e d i t o r : pour lesnu ls .: pour lesnu ls : address " Par is " .
Requête optional9
SELECT ?x ? t i t l e ?y ?addressFROM <bib l iosem . t t l >
WHERE { ?x : t i t l e ? t i t l e .OPTIONAL { ?y : address ?address .
OPTIONAL { ?x : e d i t o r : n o t e x i s t s } } }
Réponse?
Résultat de optional9
------------------------------------------------------------| x | title | y | address |============================================================| :book4 | "SPARQL pour les Nuls" | :pourlesnuls | "Paris" || :book3 | "RDF Framework" | :pourlesnuls | "Paris" || :book2 | "The Semantic Web" | :pourlesnuls | "Paris" || :book1 | "SPARQL Tutorial" | :pourlesnuls | "Paris" |------------------------------------------------------------
Produit cartesien entre les deux premiers motifs
142 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Graphes nommés
Graphes nommés
Si n ∈ IRI est un identifiant (IRI) et e est une expression Turtle,n { e }
désigne le graphe nommé (n,G) où G est l’ensemble de triplets (graphe)décrit par e.
Dataset
Un dataset RDF est un ensemble D = {G, (n1,G1), (n2,G2), ..., (nl ,Gk )}où
G est le graphe par défaut(ni ,Gi) sont des graphes nommés.
143 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
SPARQL : Graphes nommés
Clauses FROM
pas de clause FROM :graphe par défaut choisi, par exemple, dans l’interface utilisateur.
une ou plusieurs clauses FROM <...> :graphe(s) anonyme(s) ;le graphe par défaut est défini par l’union des graphes anonymes donnésdans les clause FROM
une ou plusieurs clauses FROM NAMED <...> :graphe’s) nommé(s) qui peuvent etre référencés dans la clause WHEREavec la clause GRAPH
144 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Graphes anonymes et graphes nommésTurtle ex6
ns : book3 ns : p r i ce 15 .ns : book5 dc : t i t l e "SPARQL 1.1 " .
Requête ex6
SELECT ? t i t l e ?pr i ceFROM <ex5 . t t l >FROM <ex6 . t t l >WHERE { ?x ns : p r i c e ?pr i ce .
FILTER ( ?p r i ce < 30) .?x dc : t i t l e ? t i t l e . }
Résultat de ex6
------------------------------| title | price |==============================| "RDF Framework" | 15 || "The Semantic Web" | 23 |------------------------------
Requête ex7
SELECT ?g ? t i t l e ?pr i ceFROM NAMED <ex5 . t t l >FROM NAMED <ex6 . t t l >WHERE { GRAPH ?g
{ ?x ns : p r i c e ?pr i ce .FILTER ( ?p r i ce < 30) .?x dc : t i t l e ? t i t l e . } }
Résultat de ex7
------------------------------------------| g | title | price |==========================================| <ex5.ttl> | "The Semantic Web" | 23 |------------------------------------------
Le graphe par défaut est vide !
145 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Graphes Nommées : Union et JointureRequête ex8
SELECT ?g ? t i t l e ?pr i ceFROM NAMED <ex5 . t t l >FROM NAMED <ex6 . t t l >{ { GRAPH ?g
{ ?x ns : p r i c e ?pr i ce .FILTER ( ?p r i ce < 30) . } }
UNION{ GRAPH ?g
{ ?x dc : t i t l e ? t i t l e . } }}
Résultat de ex8
------------------------------------------| g | title | price |==========================================| <ex6.ttl> | | 15 || <ex5.ttl> | | 23 || <ex6.ttl> | "SPARQL 1.1" | || <ex5.ttl> | "RDF Framework" | || <ex5.ttl> | "The Semantic Web" | || <ex5.ttl> | "SPARQL Tutorial" | |------------------------------------------
Requête ex9
SELECT ?a ?b ? t i t l e ?pr i ceFROM NAMED <ex5 . t t l >FROM NAMED <ex6 . t t l >{ { GRAPH ?a
{ ?x ns : p r i ce ?pr i ce . } }{ GRAPH ?b
{ ?x dc : t i t l e ? t i t l e . } }}
Résultat de ex9
------------------------------------------------------| a | b | title | price |======================================================| <ex6.ttl> | <ex5.ttl> | "RDF Framework" | 15 || <ex5.ttl> | <ex5.ttl> | "The Semantic Web" | 23 || <ex5.ttl> | <ex5.ttl> | "SPARQL Tutorial" | 42 |------------------------------------------------------
146 / 356
M2 DAC – LODAS 2019 – SPARQL 1.0 : Motifs de Graphes – Motifs de graphes Bernd Amann
Graphes Nommées : Jointure ExterneRequête ex10
SELECT ?a ?b ? t i t l e ?pr i ceFROM NAMED <ex5 . t t l >FROM NAMED <ex6 . t t l >{ { GRAPH ?b { ?x dc : t i t l e ? t i t l e . } }
OPTIONAL{ GRAPH ?a { ?x ns : p r i c e ?pr i ce . } } }
Résultat de ex10
------------------------------------------------------| a | b | title | price |======================================================| | <ex6.ttl> | "SPARQL 1.1" | || <ex6.ttl> | <ex5.ttl> | "RDF Framework" | 15 || <ex5.ttl> | <ex5.ttl> | "The Semantic Web" | 23 || <ex5.ttl> | <ex5.ttl> | "SPARQL Tutorial" | 42 |------------------------------------------------------
147 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Bernd Amann
Outline
1 SPARQL 1.0 : Motifs de Graphes
2 SPARQL 1.1 : Aggregation, expressions de chemins, ...
148 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
2 - SPARQL 1.1 : Aggregation, expressions dechemins, ...
Fonctions et aggrégationExpressions de cheminsSous-requêtes et requêtes fédérées
149 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
FonctionsSPARQL 1.1 propose un grand nombre de fonctions pour
calculer : strlen, abs, round , ceil , coalesce, ...transformer : str , ucase, lcase, replace, md5, ...extraire : substr , year , month, hours, lang, ...tester : IF , isBLANK , isIRI, exists, bound , in (listes), contains (string),regex , ...composer : ||, &&, concat , ...générer : now , rand , bnode, ...
des ressources et valeurs de différents types.http://www.w3.org/TR/sparql11-query/#expressions
150 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
Exemple : REGEX et STRTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex14regex
SELECT ?x ?tFROM <ex14 . t t l >WHERE { ?x dc : t i t l e ? t .
FILTER ( regex ( s t r ( ? t ) , " ^S.∗ r .∗ l $ " , " i " ) ) }
Résultat de ex14regex
-------------------------------------------------------| x | t |=======================================================| <http://example.org/book/book1> | "SPARQL Tutorial" |-------------------------------------------------------
151 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
BIND : valeurs calculéesBIND(exp1 as ?x) lie la variable ?x au résultat fr l’expression expi.
Turtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex14bind
SELECT ? t i t l e ?pr i ce ?newpriceFROM <ex14 . t t l >WHERE { ?x ns : p r i ce ?pr i ce ; dc : t i t l e ? t i t l e .
BIND ( ?p r i ce ∗ 1.1 as ?newprice ) }
Résultat de ex14bind
-----------------------------------------| title | price | newprice |=========================================| "RDF Framework" | 15 | 16.5 || "The Semantic Web" | 23 | 25.3 || "SPARQL Tutorial" | 42 | 46.2 |-----------------------------------------
152 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
Exemple : COALESCE
COALESCE(exp1,exp2,...) retourne le résultat de la première expression expisans erreur.Si ?x = 2, !bound(?y) :
COALESCE( ?x , 1 /0 ) = 2COALESCE( 1 /0 , ?x ) = 2COALESCE(5 , ?x ) = 5COALESCE( ?y , 3) = 3COALESCE( ?y ) = e r reu r
153 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
Exemple : COALESCETurtle coalesce
: book1 : t i t l e "SPARQL T u t o r i a l " ; : p r i ce 42 ; : e d i t o r : jena .: book2 : t i t l e " The Semantic Web" ; : p r i ce 23 ; : reduc t ion 10 .: book3 : t i t l e "RDF Framework " ; : p r i ce 53 ; : reduc t ion 20.: book4 : t i t l e "SPARQL pour les Nuls " ; : e d i t o r : pour lesnu ls .: pour lesnu ls : address " Par is " .
Requête coalesce
SELECT ? t i t r e (COALESCE( ?p r i ce ∗(1.0−?reduc /100) , ?pr i ce ) as ?p r i x )FROM <coalesce . t t l >WHERE { ?x ns : t i t l e ? t i t r e ; ns : p r i ce ?pr i ce .
OPTIONAL { ?x ns : reduc t ion ?reduc } }
Résultat de coalesce
-----------------------------| titre | prix |=============================| "RDF Framework" | 42.4 || "The Semantic Web" | 20.7 || "SPARQL Tutorial" | 42 |-----------------------------
154 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
COUNTTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex14
SELECT (COUNT( ? e d i t o r ) AS ?c )FROM <ex14 . t t l >WHERE { ?x ns : e d i t o r ? e d i t o r }
Résultat de ex14
-----| c |=====| 4 |-----
155 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
Exemple GROUP BYTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex15
SELECT ? e d i t o r (AVG( ?p r i ce ) AS ?c )FROM <ex14 . t t l >WHERE { ?x ns : e d i t o r ? e d i t o r ; ns : p r i ce ?pr i ce }GROUP BY ? e d i t o r
Résultat de ex15
-----------------| editor | c |=================| :w3c | 19.0 || :jena | 42.0 |-----------------
156 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
Exemple GROUP BY/HAVINGTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex15simple
SELECT ? e d i t o r (AVG( ?p r i ce ) as ?c )FROM <ex14 . t t l >
WHERE { _ : b ns : e d i t o r ? e d i t o r ;ns : p r i ce ?pr i ce . }
GROUP BY ? e d i t o r
Résultat de ex15simple
-----------------| editor | c |=================| :w3c | 19.0 || :jena | 42.0 |-----------------
Requête ex16
SELECT ? e d i t o r (AVG( ?p r i ce ) as ?c )FROM <ex14 . t t l >WHERE { ?x ns : e d i t o r ? e d i t o r ;
ns : p r i ce ?pr i ce }GROUP BY ? e d i t o rHAVING (AVG( ?p r i ce ) > 20)
Résultat de ex16
-----------------| editor | c |=================| :jena | 42.0 |-----------------
157 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
Fonction d’aggrégation
COUNTCOUNT DISTINCTSUMAVGMINMAXSAMPLEGROUP_CONCAT
Voir http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#aggregates
158 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Fonctions et aggrégation Bernd Amann
Exemple GROUP_CONCATTurtle ex14
: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 23 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 15 ; ns : e d i t o r : w3c .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex17
SELECT ? e d i t o r(GROUP_CONCAT( ? t i t l e ; SEPARATOR = " / " ) AS ? t i t l e s )
FROM <ex14 . t t l >WHERE { ?x ns : e d i t o r ? e d i t o r ; dc : t i t l e ? t i t l e }GROUP BY ? e d i t o r
Résultat de ex17
-----------------------------------------------| editor | titles |===============================================| :w3c | "RDF Framework / The Semantic Web" || :jena | "SPARQL 1.1 / SPARQL Tutorial" |-----------------------------------------------
159 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Expressions de chemins Bernd Amann
2 - SPARQL 1.1 : Aggregation, expressions dechemins, ...
Fonctions et aggrégationExpressions de cheminsSous-requêtes et requêtes fédérées
160 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Expressions de chemins Bernd Amann
Expressions de cheminLes expressions de chemins sont des expressions régulières (étendues) sur l’alphabet des propriétés :
Syntax Form Matchesuri A URI or a prefixed name. A path of length one (property).ˆelt Inverse path (object to subject).(elt) A group path elt, brackets control precedence.elt1 / elt2 A sequence path of elt1, followed by elt2elt1 ˆ elt2 Shorthand for elt1 / ˆelt2, that is elt1 followed by the inverse of elt2.elt1 | elt2 A alternative path of elt1, or elt2 (all possibilities are tried).elt* A path of zero or more occurrences of elt.elt+ A path of one or more occurrences of elt.elt ? A path of zero or one elt.elt{n,m} A path between n and m occurrences of elt.elt{n} Exactly n occurrences of elt. A fixed length path.elt{n,} n or more occurrences of elt.elt{,n} Between 0 and n occurrences of elt.
161 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Expressions de chemins Bernd Amann
Expressions de cheminsTurtle ex11
: a l i c e f o a f : name " A l i ce " ;f o a f : knows [ f o a f : name "Bob" ; f o a f : knows [ f o a f : name "Tom" ] ] ;f o a f : knows [ f o a f : name " Clare " ; f o a f : knows : a l i c e ] .
Requête ex11
SELECT ?xn ?ynFROM <ex11 . t t l >WHERE { ?x f o a f : name ?xn ; f o a f : knows+ [ f o a f : name ?yn ] . }
Résultat de ex11
---------------------| xn | yn |=====================| "Clare" | "Alice" || "Clare" | "Clare" || "Clare" | "Bob" || "Clare" | "Tom" || "Bob" | "Tom" || "Alice" | "Clare" || "Alice" | "Alice" || "Alice" | "Bob" || "Alice" | "Tom" |---------------------
162 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Expressions de chemins Bernd Amann
Expressions de cheminTurtle ex11
: a l i c e f o a f : name " A l i ce " ;f o a f : knows [ f o a f : name "Bob" ; f o a f : knows [ f o a f : name "Tom" ] ] ;f o a f : knows [ f o a f : name " Clare " ; f o a f : knows : a l i c e ] .
Requête ex13
SELECT DISTINCT ?xn ?ynFROM <ex11 . t t l >WHERE { ?x f o a f : name ?xn ; ( f o a f : knows | ^ f o a f : knows ) [ f o a f : name ?yn ] . }
Résultat de ex13
---------------------| xn | yn |=====================| "Clare" | "Alice" || "Tom" | "Bob" || "Bob" | "Tom" || "Bob" | "Alice" || "Alice" | "Clare" || "Alice" | "Bob" |---------------------
163 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Expressions de chemins Bernd Amann
ExempleTurtle ex11
: a l i c e f o a f : name " A l i c e " ;f o a f : knows [ f o a f : name "Bob" ; f o a f : knows [ f o a f : name "Tom" ] ] ;f o a f : knows [ f o a f : name " Clare " ; f o a f : knows : a l i c e ] .
Requête ex12
PREFIX f o a f : < h t t p : / / xmlns . com/ f o a f /0 .1 / >SELECT ?xn ?ynFROM <ex11 . t t l >WHERE { ?x f o a f : name ?xn ;
( f o a f : knows+ | ^ f o a f : knows+)[ f o a f : name ?yn ] . }
Résultat de ex12
---------------------| xn | yn |=====================| "Clare" | "Alice" || "Clare" | "Clare" || "Clare" | "Bob" || "Clare" | "Tom" || "Clare" | "Alice" || "Clare" | "Clare" || "Tom" | "Bob" || "Tom" | "Alice" || "Tom" | "Clare" || "Bob" | "Tom" || "Bob" | "Alice" || "Bob" | "Clare" || "Alice" | "Clare" || "Alice" | "Alice" || "Alice" | "Bob" || "Alice" | "Tom" || "Alice" | "Clare" || "Alice" | "Alice" |---------------------
164 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Sous-requêtes et requêtes fédérées Bernd Amann
2 - SPARQL 1.1 : Aggregation, expressions dechemins, ...
Fonctions et aggrégationExpressions de cheminsSous-requêtes et requêtes fédérées
165 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Sous-requêtes et requêtes fédérées Bernd Amann
Sous-requête : ExempleTurtle ex18
: book2 dc : t i t l e " The Semantic Web" ; ns : p r i ce 24 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework " ; ns : p r i ce 16 ; ns : e d i t o r : w3c .: book3 dc : t i t l e "RDF Framework 2 " ; ns : p r i c e 20 ; ns : e d i t o r : w3c .: book1 dc : t i t l e "SPARQL T u t o r i a l " ; ns : p r i ce 42 ; ns : e d i t o r : jena .: book4 dc : t i t l e "SPARQL 1.1 " ; ns : e d i t o r : jena .
Requête ex18
SELECT ? e d i t o r ( avg ( ?pr i ce ) as ?avg2 )FROM <ex18 . t t l >WHERE { ?x ns : e d i t o r ? e d i t o r .
{ SELECT ? e d i t o r ?pr i ceWHERE { ?y ns : e d i t o r ? e d i t o r ; ns : p r i ce ?pr i ce . }ORDER BY DESC ( ?p r i ce )LIMIT 3 } }
GROUP BY ? e d i t o r
Résultat de ex18
-----------------------------------------| editor | avg2 |=========================================| <http://example.org/book/w3c> | 22.0 || <http://example.org/book/jena> | 42.0 |-----------------------------------------
166 / 356
M2 DAC – LODAS 2019 – SPARQL 1.1 : Aggregation, expressions de chemins, ... – Sous-requêtes et requêtes fédérées Bernd Amann
Requêtes fédérées : SERVICETurtle exfed
@prefix dbr : < h t t p : / / dbpedia . org / resource / >@prefix dbp : < h t t p : / / dbpedia . org / p roper ty / >
[ dbp : t i t l e dbr : President_of_France ] .
Requête exfed
PREFIX dbr : < h t t p : / / dbpedia . org / resource / >PREFIX dbp : < h t t p : / / dbpedia . org / p roper ty / >CONSTRUCT { ?a dbp : name ?na ; dbp : pa r tne r ?b }FROM <exfed . t t l >WHERE { [ ?p ?d ]
SERVICE < h t t p : / / dbpedia . org / sparq l > {?a ?p ?d ; dbp : name ?na . OPTIONAL { ?a dbp : pa r tne r ?b }
} } LIMIT 1
Résultat de exfed
@prefix dbr: <http://dbpedia.org/resource/> .@prefix dbp: <http://dbpedia.org/property/> .
dbr:François_Hollandedbp:name "François Hollande"@en ;dbp:partner "Ségolène Royal"@en .
167 / 356
Recommended