TriQL - A Query Language for Named Graphs

Working Draft

This Version:
http://www.wiwiss.fu-berlin.de/suhl/bizer/TriQL/TriQL-20040406/
Latest Version:
http://www.wiwiss.fu-berlin.de/suhl/bizer/TriQL/
Author:
Chris Bizer (Freie Universität Berlin, Germany)

 


Abstract

This document describes TriQL, a query language for extracting information from Named Graphs.

Table of Contents


1. Introduction

This document describes TriQL, a query language for extracting information from Named Graphs.

TriQL is based on RDQL. The basic idea of TriQL is using graph patterns instead of triple pattern for querying sets of named graphs. A graph pattern consists of an optional graph name and a set of triple patterns.


2. Examples

This section describes the TriQL syntax with some examples. The examples use the set of Named Graphs serialized by the following TriG document: example.trig

# TriG Example Document
# This document encodes three graphs.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix swp: <http://www.w3.org/2004/03/trix/swp-1/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ex: <http:// www.example.org/vocabulary#> .
@prefix : <http://www.example.org/exampleDocument#> .
:G1 { :Monica ex:name "Monica Murphy" .      
      :Monica ex:homepage <http://www.monicamurphy.org> .
      :Monica ex:email <mailto:monica@monicamurphy.org> .
      :Monica ex:hasSkill ex:Management }

:G2 { :Monica rdf:type ex:Person .
      :Monica ex:hasSkill ex:Programming }

:G3 { :G1 swp:assertedBy _:w1 .
      _:w1 swp:authority :Chris .
      _:w1 dc:date "2003-10-02"^^xsd:date .   
      :G2 swp:quotedBy _:w2 .
      :G3 swp:assertedBy _:w2 .
      _:w2 dc:date "2003-09-03"^^xsd:date .
      _:w2 swp:authority :Chris .
      :Chris rdf:type ex:Person .  
      :Chris ex:email <mailto:chris@bizer.de> }

 

Example Query 1: Get all persons together with their email address.

SELECT ?person, ?email
FROM example.trig
WHERE ( ?person ex:email ?email )
( ?person rdf:type ex:Person ) USING ex FOR <http:// www.example.org/vocabulary#>
rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

Query Results 1:

No. ?person ?email
1.

Resource: http://www.example.org/exampleDocument#Monica

Resource: mailto:monica@monicamurphy.org

2.

Resource: http://www.example.org/exampleDocument#Chris

Resource: mailto:chris@bizer.de

The query contains two graph patterns. Both patterns contain one triple pattern each and specify no graph name restrictions. Thus, the query is executed against all graphs in the graph set.

Example Query 2: Get all persons together with their email address. The type and the email information should be contained in the same graph.

SELECT ?person, ?email
WHERE ( ?person ex:email ?email .
?person rdf:type ex:Person ) USING ex FOR <http:// www.example.org/vocabulary#>
rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

Query Results 2:

No. ?person ?email
1.

Resource: http://www.example.org/exampleDocument#Chris

Resource: mailto:chris@bizer.de

The query uses a graph pattern which requires graphs to match both triple patterns. Chris is included into the result set because the information that he is a person with an email address is both contained in G3. Monica isn't included into the result set because the information that she is a person with a email address is spread over the graphs G1 and G2.

 

Example Query 3: Get Monica's skills. Use only information, that has been asserted by Chris.

SELECT ?skill
WHERE ?graph ( doc:Monica ex:skill ?skill )
(?graph swp:assertedBy ?warrant)
(?warrant swp:authority doc:Chris) USING ex FOR <http://www.example.org/vocabulary#>
swp FOR <http://www.w3.org/2004/03/trix/swp-1/>
doc FOR <http://www.example.org/exampleDocument#>

Query Results 3:

No. ?skill
1.

Resource: http://www.example.org/vocabulary#Management

The query uses three graph patterns. The variable ?graph is bound to the names of all graphs that contain information about Monika's skills. The second and third pattern restrict ?graph to graphs which have been asserted by Chris. Note, that it doesn't matter if the assertedBy and the authority information in contained in the same graph or in different graphs.

 

Example Query 4: Get Monica's skills. Use only information, that has been asserted by Chris after "2003-01-01".

SELECT ?skill
WHERE ?graph ( doc:Monica ex:skill ?skill )
(?graph swp:assertedBy ?warrant .
?warrant swp:authority doc:Chris .
?warrant dc:date ?date ) AND ?date > "2003-01-01"^^xsd:date USING ex FOR <http://www.example.org/vocabulary#> xsd FOR <http://www.w3.org/2001/XMLSchema#>
swp FOR <http://www.w3.org/2004/03/trix/swp-1/>
doc FOR <http://www.example.org/exampleDocument#>
dc FOR <http://purl.org/dc/elements/1.1/>

Query Results 4:

No. ?skill
1.

Resource: http://www.example.org/vocabulary#Management

This query uses a AND condition to restrict ?date to values greater than "1/1/2003". Note, that the assertedBy, the authority and the date information has to be contained in the same graph this time.

 


3. Extended Backus-Naur Form (EBNF) Grammar

This section defined the TriQL grammar. It is extending Andy Seaborne's RDQL grammar defined in [RDQL].

Lexical Tokens

QuotedURI  ::=  '<' URI characters (from RFC 2396) '>'
NSPrefix  ::=   NCName As defined in XML Namespace v1.1 and XML 1.1
LocalPart  ::=   NCName As defined in XML Namespace v1.1 and XML 1.1
SELECT  ::=  'SELECT' Case Insensitive match
FROM  ::=  'FROM'   Case Insensitive match
SOURCE  ::=  'SOURCE' Case Insensitive match
WHERE  ::=  'WHERE' Case Insensitive match
AND  ::=  'AND' Case Insensitive match
USING  ::=  'USING' Case Insensitive match
Identifier  ::=  ([a-z][A-Z][0-9][-_.])+  
EOF  ::=  End of file
COMMA  ::=  ','
INTEGER_LITERAL   ::=  ([0-9])+
FLOATING_POINT_LITERAL  ::=  ([0-9])*'.'([0-9])+('e'('+'|'-')?([0-9])+)?
STRING_LITERAL1  ::=  '"'UTF-8 characters'"' (with escaped \")
STRING_LITERAL2  ::=  "'"UTF-8 characters"'" (with escaped \')
LPAREN  ::=  '('
RPAREN  ::=  ')'
COMMA  ::=  ','
DOT  ::=  '.'
GT  ::=  '>'
LT  ::=  '<'
BANG  ::=  '!'
TILDE  ::=  '~'
HOOK  ::=  '?'
COLON  ::=  ':'
EQ  ::=  '=='
NEQ  ::=  '!='
LE  ::=  '<='
GE  ::=  '>='
SC_OR  ::=  '||'
SC_AND  ::=  '&&'
STR_EQ  ::= 'EQ' Case Insensitive match
STR_NE  ::= 'NE' Case Insensitive match
PLUS  ::=  '+'
MINUS  ::=  '-'
STAR  ::=  '*'
SLASH  ::=  '/'
REM  ::=  '%'
STR_MATCH  ::=  '=~' | '~~'
STR_NMATCH  ::=  '!~'
DATATYPE  ::=  '^^'
AT  ::=  '@'

Grammar

References to lexical tokens are enclosed in <>.  Whitespace is skipped.

Notes: The term "literal" refers to a constant value, and not only an RDF Literal.

CompilationUnit  ::=  Query <EOF>
CommaOpt  ::=  ( <COMMA> )?
Query  ::=  SelectClause ( SourceClause )? GraphPatternClause ( ConstraintClause )? ( PrefixesClause )?
SelectClause  ::=  ( <SELECT> Var ( CommaOpt Var )* | <SELECT> <STAR> )
SourceClause  ::=  ( <SOURCE> | <FROM> ) SourceSelector ( CommaOpt SourceSelector )*
SourceSelector  ::=  QName
GraphPatternClause  ::=  <WHERE> GraphPattern ( CommaOpt GraphPattern )*
ConstraintClause  ::=  <AND> Expression ( ( <COMMA> | <AND> ) Expression )*
GraphPattern  ::=  GraphNameOpt <LPAREN> TriplePattern ( <DOT> TriplePattern )* <RPAREN>
GraphNameOpt  ::=  VarOrURIOrConst CommaOpt VarOrURI CommaOpt VarOrURIOrConst
TriplePattern  ::=  VarOrConst CommaOpt VarOrURI CommaOpt VarOrConst
VarOrURI  ::=  Var | URI
VarOrConst  ::=  Var | Const
Var  ::=  "?" Identifier
PrefixesClause  ::=  <USING> PrefixDecl ( CommaOpt PrefixDecl )*
PrefixDecl  ::=  Identifier <FOR> <QuotedURI>
Expression  ::=  ConditionalOrExpression
ConditionalOrExpression  ::=  ConditionalAndExpression ( <SC_OR> ConditionalAndExpression )*
ConditionalAndExpression  ::=  StringEqualityExpression ( <SC_AND> StringEqualityExpression )*
StringEqualityExpression  ::=  ArithmeticCondition ( <STR_EQ> ArithmeticCondition | <STR_NE> ArithmeticCondition | <STR_MATCH> PatternLiteral | <STR_NMATCH> PatternLiteral )*
ArithmeticCondition  ::=  EqualityExpression
EqualityExpression  ::=  RelationalExpression ( <EQ> RelationalExpression | <NEQ> RelationalExpression )?
RelationalExpression  ::=  AdditiveExpression ( <LT> AdditiveExpression | <GT> AdditiveExpression | <LE> AdditiveExpression | <GE> AdditiveExpression )?
AdditiveExpression  ::=  MultiplicativeExpression ( <PLUS> MultiplicativeExpression | <MINUS> MultiplicativeExpression )*
MultiplicativeExpression  ::=  UnaryExpression ( <STAR> UnaryExpression | <SLASH> UnaryExpression | <REM> UnaryExpression )*
UnaryExpression  ::=  UnaryExpressionNotPlusMinus
 | ( <PLUS> UnaryExpression | <MINUS> UnaryExpression )
UnaryExpressionNotPlusMinus  ::=  ( <TILDE> | <BANG> ) UnaryExpression
 | PrimaryExpression
PrimaryExpression  ::=  Var
 | Const
 | <LPAREN> Expression <RPAREN>
Const  ::=  URI
 | NumericLiteral
 | TextLiteral
 | BooleanLiteral
 | NullLiteral
NumericLiteral  ::=  ( <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> )
TextLiteral  ::=  ( <STRING_LITERAL1> | <STRING_LITERAL2> ) ( <AT> Identifier )? ( <DATATYPE> URI )?
PatternLiteral  ::= 
BooleanLiteral  ::=  <BOOLEAN_LITERAL>
NullLiteral  ::=  <NULL_LITERAL>
URI  ::=  <QuotedURI>
 |  QName
QName  ::=  <NSPrefix> ':' (<LocalPart>)?
Unlilke XML Namespaces, the local part is optional
Identifier  ::=  ( <IDENTIFIER> | <SELECT> | <SOURCE> | <FROM> | <WHERE> | <PREFIXES> | <FOR> | <STR_EQ> | <STR_NE> )

 


4. References

[Named-Graphs]
Named Graphs , http://www.w3.org/2004/03/trix/
[RDQL]
RDQL - A Query Language for RDF, Andy Seaborne, W3C Member Submission 9 January 2004. http://www.w3.org/Submission/2004/SUBM-RDQL-20040109/
[TriG]
The TriG Syntax, Christian Bizer, 2004. http://www.wiwiss.fu-berlin.de/suhl/bizer/TriG/
[RDF-SYNTAX]
RDF/XML Syntax Specification (Revised), Beckett D. (Editor), W3C Recommendation, 10 February 2004. This version is http://www.w3.org/TR/2004/REC-rdf-syntax-grammar-20040210/. The latest version is http://www.w3.org/TR/rdf-syntax-grammar/.


5. Change Log