This document describes TriQL, a query language for extracting information from Named Graphs.
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.
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 | |
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 | |
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.
This section defined the TriQL grammar. It is extending Andy Seaborne's RDQL grammar defined in [RDQL].
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 | ::= | '@' |
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.