RDQL tutorial

RDF Resource Description Framework Icon RDQL is a query language for RDF based on SquishQL, you can find more about RDQL at this site. In this tutorial we'll cover RDQL based on the PHP RDQL implementation available at the phpxmlclasses project

Description: RDQL queries RDF documents using a SQL-alike syntax, with the PHP implementation you can query RDF documents indicating their URLs (http://) or their paths if they are in your local machine. If you query more than one document then all the RDF statements present in the documents are considered part of a single document.

You can do a lot of thing with RDQL and there're a lot of interesting queries that can be performed.

Syntax: An RDQL query is in the form:
FROM documents
WHERE Expressions
AND Filters
USING Namespace declarations
SELECT ?x,?y
FROM <http://example.com/sample.rdf>
WHERE (?x,<dc:name>,?y)
USING dc for <http://www.dc.com#>
This would return all the ?x-?y tuples indicating the resource name and the value of the dc:name property for each resource.

Syntax: SELECT The select portion of the query let's you indiate which RDQL variables you want to be returned by the query, if you use SELECT ?x,?y,?z then you will receive an array of tuples containing values for ?x,?y and ?z. You can use other variables in the query such as ?a1,?p,?foo but they won't be returned since they are not present in the select part of the query.

Syntax: FROM The from part of the query indicates the RDF souces to be queried, each source is enclosed by angle brackets (<&>). If you indicate more than one source sepparate them using commas.

In the PHP implementation sources can be URLs or paths for local RDF documents. Example:

FROM <doc.rdf>, <http://example.com/sample.rdf>, <rdfs/other.rdf>

Sytax: WHERE The where part is the most important part of the RDQL expression, in the where part you indicate constraints that RDF triples (subject, predicate, object) must accomplish in order to be returned. The where part is expressed by a list of restrictions sepparated by commas, each restriction takes the form: (subject, predicate, object) where the subject, predicate and object can be a literal value or a RDQL variable.

For the predicate you can express property names using a namespace declared in the USING section for example: <dc:name> which indicates that the predicate must match the "name" local-name for the namespace declared as "dc" in the using part.

This will match all the RDF statements where some subject "x" has a property "has" pointing to a resource and that resource has a property color. If you want to filter what color you want you use the "AND" part of the query.

Sytax: AND The AND part indicates constraints that RDQL variables must follow. In the PHP implementation the AND part is a PHP expression where variables are RDQL variables such as ?x,?y etc.

Example: Select all the subjects that have a blue item.
FROM <doc.rdf>
WHERE (?x,<foo:has>,?y),
AND ?z=="blue"
USING foo for       

Syntax: USING The using section declares all the namespaces that will be used for RDF properties, declarations are sepparated by commas and use the notation:
prefix for 
USING foo for <http://foo.org/properties#>, col for <http://props.com/catalog#>

Examples: In the following section we'll show some examples using a real RDF document and working RDQL queries (at last!).
The document describes information we know about some people. We indicate their names, age, position and other relationships between them. The RDF document to be used is the following (if you are not used to RDF docs take some time to read it):

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
            xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dt="http://foo.org#">

<rdf:Description about="http://foo.org/persons/john">

<rdf:Description about="http://foo.org/persons/Peter">
  <dt:friend rdf:resource="http://foo.org/persons/john" />
  <dt:friend rdf:resource="http://foo.org/persons/Carl" />

<rdf:Description about="http://foo.org/persons/Micky">

<rdf:Description about="http://foo.org/persons/Carl">
  <dt:friend rdf:resource="http://foo.org/persons/Peter" />

<rdf:Description about="http://foo.org/team">
    <rdf:li rdf:resource="http://foo.org/persons/Peter" />


Example 1: Indicate name and age of all the individuals older than 20.
FROM <people.rdf>
WHERE (?x,<dt:age>,?z),(?x,<dt:name>,?y)
AND ?z>20
USING dt for <http://foo.org#>, rdf for <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Result: Array ( [0] => Array ( [?y] => John ) [1] => Array ( [?y] => Peter ) [2] => Array ( [?y] => Carl ) )

Example 2: If A is friend of B and B is friend of A report them
SELECT ?x,?y
FROM <people.rdf>
WHERE (?x,<dt:friend>,?y),(?y,<dt:friend>,?x)
AND ?x<^gt;?y
USING dt for <http://foo.org#>, rdf for <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Result: Array ( [0] => Array ( [?x] => http://foo.org/persons/Carl [?y] => http://foo.org/persons/Peter ) [1] => Array ( [?x] => http://foo.org/persons/Peter [?y] => http://foo.org/persons/Carl ) )

Example 3: Show all the members of the team.
WHERE (?x,,?y),(?y,?w,?z)
AND ?z<>"http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag" && ?x=="http://foo.org/team"
USING dt for , rdf for 
Result: Array ( [0] => Array ( [?z] => http://foo.org/persons/Peter ) [1] => Array ( [?z] => Kim ) )