Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPARQL- A QUERY LANGUAGE FOR RDF( S ) Fred Freitas - CIn/UFPE - Brazil.

Similar presentations


Presentation on theme: "SPARQL- A QUERY LANGUAGE FOR RDF( S ) Fred Freitas - CIn/UFPE - Brazil."— Presentation transcript:

1 SPARQL- A QUERY LANGUAGE FOR RDF( S ) Fred Freitas - CIn/UFPE - Brazil

2 Q UERY L ANGUAGES : SQL (SQL Q UERY L ANGUAGE ) [O RACLE 2006] A language for querying collections of tuples: SELECT SALARY, HIRE_DATE FROM EMPS WHERE EMP_ID = EMP_IDNAMEHIRE_DATESALARY 13954Joe Mary ………… 04182Bob

3 Q UERY L ANGUAGES : XQ UERY (XML Q UERY ) [O RACLE 2006] A language for querying trees of XDM nodes: for $e in document(my_employees.xml) where = return $emp/emp/salary

4 Chapter 3A Semantic Web Primer 4 W HY AN RDF Q UERY L ANGUAGE ? D IFFERENT XML R EPRESENTATIONS XML at a lower level of abstraction than RDF There are various ways of syntactically representing an RDF statement in XML Thus we would require several XQuery queries, e.g. //uni:lecturer/uni:title if uni:title element if uni:title attribute Both XML representations equivalent!

5 T WO BASIC FAMILIES OF SQL- LIKE LANGAGES FOR RDF(S) RDQL Implementations: Jena, Sesame, RDFStore,... RQL Implementations: RQL, SPARQL,...

6 6 I NTRODUCTION TO RDQL RDF Data Query Language JDBC/ODBC friendly Simple: SELECT some information FROM somewhere WHERE this match AND these constraints USING these vocabularies

7 7 E XAMPLE

8 8 q1 contains a query: SELECT ?x WHERE (?x,, "John Smith") For executing q1with a model m1.rdf: java jena.rdfquery --data m1.rdf --query q1 The outcome is: x =============================

9 9 E XAMPLE Return all the resources that have property FN and the associated values: SELECT ?x, ?fname WHERE (?x,, ?fname) The outcome is: x | fname ================================================ | "John Smith" | "Sarah Jones" | "Matt Jones"

10 10 E XAMPLE Return the first name of Jones: SELECT ?givenName WHERE (?y,, "Jones"), (?y,, ?givenName) The outcome is: givenName ========= "Matthew" "Sarah"

11 11 URI P REFIXES : USING RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?x WHERE (?x, vCard:FN, "John Smith") USING vCard FOR SELECT ?givenName WHERE (?y, vCard:Family, "Smith"), (?y, vCard:Given, ?givenName) USING vCard FOR

12 12 F ILTERS RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?resource WHERE (?resource, info:age, ?age) AND ?age >= 24 USING info FOR

13 13 L IMITATIONS Does not take into account semantics of RDF(S) For example: ex:human rdfs:subClassOf ex:animal ex:student rdfs:subClassOf ex:human ex:john rdf:type ex:student Query: “ To which class does the resource John belong?” Expected answer: ex:student, ex:human, ex:animal However, the query: SELECT ?x WHERE (, rdf:type, ?x) USING rdf FOR Yields only: Solution: Inference Engines

14 SPARQL SPARQL is a recursive acronym standing for SPARQL Protocol and RDF Query Language It provides facilities to [Staab 2006]: extract information in the form of URIs, blank nodes, plain and typed literals. extract RDF subgraphs. construct new RDF graphs based on information in the queried graphs

15 G RAPH P ATTERNS Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns.- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

16 B ASIC G RAPH P ATTERN Set of Triple Patterns Triple Pattern – similar to an RDF Triple (subject, predicate, object), but any component can be a query variable; literal subjects are allowed Matching a triple pattern to a graph: bindings between variables and RDF Terms Matching of Basic Graph Patterns A Pattern Solution of Graph Pattern GP on graph G is any substitution S such that S(GP) is a subgraph of G. xv rdf:typerdf:Property rdf:type rdf:type rdf:Property SELECT ?x ?v WHERE { ?x ?x ?v } ?book dc:title ?title

17 B ASIC G RAPH P ATTERN - M ULTIPLE M ATCHES PREFIX foaf: SELECT ?name ?mbox WHERE { ?x foaf:name ?name. ?x foaf:mbox ?mbox foaf:. _:a foaf:name "Johnny Lee Outlaw". _:a foaf:mbox. _:b foaf:name "Peter Goodguy". _:b foaf:mbox. namembox "Johnny Lee Outlaw" "Peter Goodguy" Group Graph Pattern (set of graph patterns) also! Data Query Query Result

18 B ASIC G RAPH P ATTERN - B LANK N ODES PREFIX foaf: SELECT ?x ?name WHERE { ?x foaf:name ?name foaf:. _:a foaf:name "Alice". _:b foaf:name "Bob". xname _:c“Alice“ _:d“Bob” Data Query Query Result

19 G RAPH P ATTERNS Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns.- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

20 G ROUP P ATTERN PREFIX foaf: SELECT ?name ?mbox WHERE { ?x foaf:name ?name. ?x foaf:mbox ?mbox } PREFIX foaf: SELECT ?name ?mbox WHERE { {?x foaf:name ?name; foaf:mbox ?mbox } }

21 G RAPH P ATTERNS Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns.- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

22 V ALUE C ONSTRAINTS PREFIX dc: PREFIX ns: SELECT ?title ?price WHERE { ?x ns:price ?price. FILTER ?price < 30. ?x dc:title ?title. ns:. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. titleprice "The Semantic Web"23 Data Query Query Result

23 23 R EGULAR EXPRESSIONS CAN BE USED PREFIX dc: PREFIX ldap: PREFIX foaf: SELECT ?name ?name2 { ?doc dc:title ?title. FILTER regex(?title, “SPARQL”). ?doc dc:creator ?reseacher. ?researcher ldap: ? . ?researcher ldap:name ?name } “Find the name and addresses of authors of a paper about SPARQL”

24 G RAPH P ATTERNS Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns.- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

25 O PTIONAL GRAPH PATTERNS PREFIX dc: PREFIX ns: SELECT ?title ?price WHERE { ?x dc:title ?title. OPTIONAL { ?x ns:price ?price. FILTER ?price < 30 ns:. :book1 dc:title "SPARQL Tutorial". :book1 ns:price 42. :book2 dc:title "The Semantic Web". :book2 ns:price 23. titleprice “SPARQL Tutorial“ "The Semantic Web"23 Data Query Query Result

26 M ULTIPLE O PTIONAL B LOCKS PREFIX foaf: SELECT ?name ?mbox ?hpage WHERE { ?x foaf:name ?name. OPTIONAL { ?x foaf:mbox ?mbox }. OPTIONAL { ?x foaf:homepage ?hpage } rdfs:. _:a foaf:name "Alice". _:a foaf:homepage. _:b foaf:name "Bob". _:b foaf:mbox. Data Query Query Result nameMboxhpage “Alice“ “Bob“

27 G RAPH P ATTERNS Basic Graph Patterns – set of Triple Patterns Group Patterns - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns.- additional patterns may extend the solution Alternative Graph Patterns – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

28 A LTERNATIVE G RAPH P ATTERNS PREFIX dc10: PREFIX dc11: SELECT ?x ?y WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } dc11:. _:a dc10:title "SPARQL Query Language Tutorial". _:b dc11:title "SPARQL Protocol Tutorial". _:c dc10:title "SPARQL". _:c dc11:title "SPARQL (updated)". Data Query Query Result xy "SPARQL (updated)" "SPARQL Protocol Tutorial" "SPARQL" "SPARQL Query Language Tutorial"

29 G RAPH P ATTERNS Basic Graph Pattern – set of Triple Patterns Group Pattern - a set of graph patterns must all match Value Constraints - restrict RDF terms in a solution Optional Graph Patterns.- additional patterns may extend the solution Alternative Graph Pattern – two or more possible patterns are tried Patterns on Named Graphs - patterns are matched against named graphs

30 RDF D ATASET RDF data stores may hold multiple RDF graphs: record information about each graph queries that involve information from more than one graph RDF Dataset in SPARQL terminology the background graph, which does not have a name, and zero or more named graphs, identified by URI reference the relationship between named and background graphs: (i) to have information in the background graph that includes provenance information about the named graphs (the application is not directly trusting the information in the named graphs ) (ii) to include the information in the named graphs in the background graph as well.

31 RDF D ATASET - T HE R ELATIONSHIP BETWEEN N AMED AND B ACKGROUND G RAPHS (I) # Background dc:. dc:publisher "Bob". dc:publisher "Alice". # Graph: foaf:. _:a foaf:name "Bob". _:a foaf:mbox. # Graph: foaf:. _:a foaf:name "Alice". _:a foaf:mbox.

32 RDF D ATASET - T HE R ELATIONSHIP BETWEEN N AMED AND B ACKGROUND G RAPHS (II) # Background foaf:. _:x foaf:name "Bob". _:x foaf:mbox. _:y foaf:name "Alice". _:y foaf:mbox. # Graph: foaf:. _:a foaf:name "Bob". _:a foaf:mbox. # Graph: foaf:. _:a foaf:name "Alice". _:a foaf:mbox.

33 Q UERYING THE D ATASET # Graph: rdfs:. _:a foaf:name "Alice". _:a foaf:mbox. _:a foaf:knows _:b. _:b rdfs:seeAlso. rdf:type foaf:PersonalProfileDocument. _:b foaf:name "Bob". _:b foaf:mbox. _:b foaf:age 32. # Graph: rdfs:. _:1 foaf:mbox. _:1 rdfs:seeAlso. _:1 foaf:age 35. rdf:type foaf:PersonalProfileDocument.

34 Q UERYING THE D ATASET - A CCESSING G RAPH L ABELS PREFIX foaf: SELECT ?src ?bobAge WHERE { GRAPH ?src { ?x foaf:mbox. ?x foaf:age ?bobAge } } srcbobAge 32 35

35 Q UERYING THE D ATASET - R ESTRICTING BY G RAPH L ABEL PREFIX foaf: PREFIX data: SELECT ?age WHERE { GRAPH data:bobFoaf { ?x foaf:mbox. ?x foaf:age ?age } } age 35

36 Q UERYING THE D ATASET - R ESTRICTING VIA Q UERY P ATTERN PREFIX data: PREFIX foaf: PREFIX rdf: PREFIX rdfs: SELECT ?mbox ?age ?ppd WHERE { GRAPH data:aliceFoaf { ?alice foaf:mbox ; foaf:knows ?whom. ?whom foaf:mbox ?mbox ; rdfs:seeAlso ?ppd. ?ppd a foaf:PersonalProfileDocument. }. GRAPH ?ppd { ?w foaf:mbox ?mbox ; foaf:age ?age } } mboxageppd 35

37 C ONSTRUCTING AN O UTPUT G RAPH foaf:. _:a foaf:givenname "Alice". _:a foaf:family_name "Hacker". _:b foaf:firstname "Bob". _:b foaf:surname "Hacker". Query: PREFIX foaf: PREFIX vcard: CONSTRUCT { ?x vcard:N _:v. _:v vcard:givenName ?gname. _:v vcard:familyName ?fname } WHERE { { ?x foaf:firstname ?gname } UNION { ?x foaf:givenname ?gname }. { ?x foaf:surname ?fname } UNION { ?x foaf:family_name ?fname }. } vcard:. _:v1 vcard:N _:x. _:x vcard:givenName "Alice". _:x vcard:familyName "Hacker". _:v2 vcard:N _:z. _:z vcard:givenName "Bob". _:z vcard:familyName "Hacker".

38 ASK – A B OOLEAN rdfs:. _:a foaf:name "Alice". _:a foaf:homepage. _:b foaf:name "Bob". _:b foaf:mbox. PREFIX foaf: ASK { ?x foaf:name "Alice" } TRUE.

39 C ONCLUSIONS Several query languages for RDF/RDF(S) Some (not exclusive) SPARQL advantages Inferencing Implicit and explicit joins Once the schemas are defined elsewhere in RDF/RDF(S) too, they can also be queried upon! Many facilities: regex, datatypes, functions,... Queries built similarly to SQL... But also over graphs (in opposition to flat relational DBs).

40 40 A NY Q UESTIONS ?


Download ppt "SPARQL- A QUERY LANGUAGE FOR RDF( S ) Fred Freitas - CIn/UFPE - Brazil."

Similar presentations


Ads by Google