2017년 6월 7일 수요일

SPARQL Protocol and RDF Query Language

1. RDF Triplestores
 - Specialised databases for storing RDF data
 - Can be viewed as a database containing a single table
  1) table contains subject/predicate/object as minimum
  2) many triplestores store quads to maintain provenance

 https://en.wikipedia.org/wiki/Triplestore

 http://www.krisalexander.com/uncategorized/2013/07/16/the-difference-between-a-triplestore-and-a-relational-database/


 - Querying Triplestores
  1) the ranges of the subjects, predicates and objects define a space
  2) a triple is a point within this space
  3) query patterns define regions within this space
   * (s p ?), (s ? o), (? p o) define lines
   * (s ? ?), (? ? o), (? p ?) define planes
  4) answering queries = finding points in these regions

 - RDF Query Languages
  1) Typical query structure: <list of variables to be bound and returned> <list of triple patterns and constraints>
  2) W3C specified a single query language which combines the best features of its predecessors: SPARQL


2. SPARQL Query Language
 - SPARQL
  1) SQL-like syntax:
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>   // need to define namespaces for vocabulary terms used
  SELECT ?name ?mbox                                     // variables prefixed with '?'
  WHERE {
      ?x foaf:name ?name .                        // triple patterns expressed in N3-like syntax
      ?x foaf:mbox ?mbox .                        // intersection is defaulted between triple patterns
  }

  PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
  SELECT ?v
  WHERE {
      ?v ?p "cat"@en .                            // literal strings must have language tag, as in graph
      ?x ?q 42 .                                     // integers in SPARQL queries are implicitly typed as xsd:integer
  }

  2) Blank nodes
   ! no guarantee that blank node labels will be unchanged over repeated queries

 - SPARQL Constraints: constraints can be applied to variables
  SELECT ?title
  WHERE {
      ?x dc:title ?title .
      FILTER regax(?title, "^SPARQL")
  }

 - Optional Graph Patterns
  SELECT ?name ?mbox
  WHERE {
      ?x foaf:name ?name .
      OPTIONAL {
          ?x foaf:mbox ?mbox .
      }
  }
  => I'm interested in mbox, but it doesn't matter to be at first part

 - Union Graph Patterns
  PREFIX dc10: <http://purl.org/dc/elements/1.0/>
  PREFIX dc11: <http://purl.org/dc/elements/1.1/>
  SELECT ?title
  WHERE {
      { ?book dc10:title ?title }
      UNION
      { ?book dc11:title ?title }
  }

 - Default Graph / Named Graphs
  SELECT ?who ?g ?mbox
  FROM <http://example.org/dft.ttl>       // the default graph. unless otherwise specified, all triples are taken from that graph
  FROM NAMED <http://example.org/alice>
  FROM NAMED <http://example.org/bob>
  WHERE {
       ?g dc:publisher ?who .
       GRAPH ?g { ?who foaf:mbox ?mbox }
  }

 - Ordering
  SELECT ?name
  WHERE {
      ?x foaf:name ?name ; :empId ?emp .
  } ORDER BY ? name DESC(?emp)        // order results by ?name, and the by ?emp in descending order
                                                    // ASC() sorts in ascending order

- Limit and Offset
 SELECT ?name
 WHERE {
      ?x foaf:name ?name .
 }
 ORDER BY ? name
 LIMIT 5
 OFFSET 10    // returns five results, after skipping the first then results

- Distinct
 SELECT DISTINCT ?name        // removes duplicate results
 WHERE {
      ?x foaf:name ?name .
 }

- CONSTRUCT: returns an RDF graphs, specified by a graph template
 PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
 CONSTRUCT {
     ?x vcard:FN ?name .
     ?x vcard:EMAIL ?mail .
 }
 WHERE {
     ?x foaf:name ?name .
     ?x foaf:mbox ?mail .
 }

- ASK: checks whether a query pattern has a solution

- DESCRIBE: returns a single graph containing information about a resource or resources
 1) nature of description left unspecified: black node closure / concise bounded description
 2) structure of returned data is determined by the SPARQL query processor

https://en.wikipedia.org/wiki/SPARQL

https://www.w3.org/TR/rdf-sparql-query/

https://jena.apache.org/tutorials/sparql.html

http://www.cambridgesemantics.com/semantic-university/sparql-by-example


3. SPARQL Protocol
 : defines the method of interaction with a SPARQL processor

 - SPARQL over HTTP
  1) HTTP GET
   * query encoded as HTTP requested URI
   * limitation on query length
   GET /sparql/?query=EncodedQuery HTTP/1.1
   HOST: www.example.org
   User-agent: my-sparql-client/0.1
  2) HTTP POST
   * query encoded in HTTP request body
   * no limitation on query length

 - SPARQL results format : based on Standard XML format
 <?xml version="1.0" ?>
 <sparql xmlns="http://www.w3.org/2005/sparql-results#">
    <head>
       <variable name="book" />                    <!-- variables -->
       <variable name="who" />
    </head>
    <results distinct="false" ordered="false">   <!-- results section -->
       <result>                                                        <!-- a single result (row) -->
          <binding name="book">                         <!-- binding of the variable "book" -->
             <uri>http://www.example.org/book/book5</uri>
          <binding>
          <binding name="who">
             <bnode>r29392923r2922</bnode>
          </binding>
       </result>
       ...
    </results>
 </sparql>

 https://w3c.github.io/rdf-tests/sparql11/data-sparql11/protocol/index.html


4. SPARQL 1.1
 : extending SPARQL

 - INSERT DATA
 PREFIX dc: <http://purl.org/dc/elements/1.1/>
 INSERT DATA {
     <http://example.org/book1> dc:title "A new book" ;
                                      dc:creator "A.N.Other" .
 }

 - DELETE DATA
 PREFIX dc: <http://purl.org/dc/elements/1.1/>
 DELETE DATA {
     <http://example.org/book2> dc:title "David Copperfield" ;
                                      dc:creator "Edmund Wells" .
 }

 - UPDATE: DELETE/INSERT
 PREFIX foaf: <http://xmlns.com/foaf/0.1>
 DELETE DATA {
     ?person foaf:givenName "Bill" .
 }
 INSERT DATA {
     ?person foaf:givenName "William" .
 }

 - Aggregates
 PREFIX : <http://books.example.org/>
 SELECT (SUM(?lprice> AS ?totalPrice)
 WHERE {
     ?org :affiliates ?auth .
     ?auth :writesBook ?book .
     ?book :price ?lprice .
 }
 GROUP BY ?org
 HAVING (SUM(?lprice) > 10)

 * other aggregate functions: COUNT, MIN, MAX, GROUP_CONCAT, SAMPLE

 - Subqueries
 PREFIX : <http://people.example.org/>
 SELECT ?y ?minName
 WHERE {
     :alice :knows ?y .
     {
         SELECT ?y (MIN(?name) AS ?minName)
         WHERE {
             ?y :name ?name .
         }
         GROUP BY ?y
     }
 }

 - Alternatives: sugared syntax for UNION
 PREFIX dc: <http://purl.org/dc/elements/1.1/>
 SELECT ?displayString
 WHERE {
     :book1 dc:title|rdfs:label ?displayString .
 }

 - Sequences: sugared syntax for bnode
 SELECT ?name
 WHERE {
     ?x foaf:mbox <mailto:alice@example.org> .
     ?x foaf:knows/foaf:name ?name .
 }
  => ?x foaf:knows [ foaf:name ?name ]

 - Arbitrary Sequences
 SELECT ?name
 WHERE {
     ?x foaf:mbox <mailto:alice@example.org> .
     ?x foaf:knows+/foaf:name ?name .
 }

  * repeat operators: * (zero or more occurrences) / + (one or more occurrence)
                            {n} (exactly n occurrences) / {n,m} (between n and m occurrences)

 - Inverses
 SELECT ?name
 WHERE {
     <mailto:alice@example.org? ^foaf:mbox ?x .
     ?x foaf:knows/^foaf:name ?name .
     FILTER(?x != ?name)
 }

https://www.w3.org/TR/sparql11-protocol/

댓글 없음:

댓글 쓰기