topical media & game development

talk show tell print

server-php-xml-class-rdql-rdql.htm / htm



  <html>
  <head>
  <title>RDQL an RDF query language</title>
  <link rel="stylesheet" href="main.css" type="text/css">
  </head>
  
  <body>
  
  <div align="center">
  <table border="0" width="80%">
  <tr>
    <td bgcolor="#aaaaee" class="textblbl" align="center">RDQL tutorial</td>
  </tr>
  </table>
  <br/>
  <table border="0" width="80%">
  <tr>
    <td bgcolor="#eeeeff" class="text" width="20%"><a href="http://www.w3.org/RDF/" title="RDF Resource Description
  Framework"> <img border="0" src="http://www.w3.org/RDF/icons/rdf_w3c_icon.64"
  alt="RDF Resource Description Framework Icon"></a>
    </td>
    <td bgcolor="#eeeeff" class="text"><i>RDQL is a query language for <a href="http://www.w3.org/RDF/">RDF</a> based on SquishQL, you can find more about
    RDQL at <a href="http://www.hpl.hp.com/semweb/rdql.html">this site</a>. In this tutorial we'll cover RDQL
    based on the PHP RDQL implementation available at <a href="http://phpxmlclasses.sourceforge.net/">the phpxmlclasses project</a></i>
    </td>
  </tr>
  </table>
  <br/>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Description:</b>
    </td>
    <td bgcolor="#eeeeff" class="text">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.<br/><br/>
    You can do a lot of thing with RDQL and there're a lot of interesting queries that can be performed.
    </td>
  </tr>
  </table>
  <br/>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Syntax:</b>
    </td>
    <td bgcolor="#eeeeff" class="text">An RDQL query is in the form:<br/>
    <pre>SELECT <i>vars</i>
  FROM <i>documents</i>
  WHERE <i>Expressions</i>
  AND <i>Filters</i>
  USING <i>Namespace declarations</i></pre>
  Example:
  <pre>SELECT ?x,?y
  FROM &lt;http://example.com/sample.rdf>
  WHERE (?x,&lt;dc:name&gt;,?y)
  USING dc for &lt;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.
  </td>
  </tr>
  </table>
  <br/>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Syntax: SELECT</b>
    </td>
    <td bgcolor="#eeeeff" class="text">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.
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Syntax: FROM</b>
    </td>
    <td bgcolor="#eeeeff" class="text">The from part of the query indicates the RDF souces to be
    queried, each source is enclosed by angle brackets (&lt;&amp;&gt;). If you indicate more than
    one source sepparate them using commas.<br/><br/>
    In the PHP implementation sources can be URLs or paths for local RDF documents. Example:<br/></br/>
    <pre>FROM &lt;doc.rdf&gt;, &lt;http://example.com/sample.rdf>, &lt;rdfs/other.rdf&gt;</pre><br/><br/
    The semantic of the from part is simple, since each RDF document can be seen as a list of statements
    the from part produces the union of all the statements in the mentioned RDF documents as the set
    of statements from where the query will be processed.
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Sytax: WHERE</b>
    </td>
    <td bgcolor="#eeeeff" class="text">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.<br/><br/>
    For the predicate you can express property names using a namespace declared in the USING section for example:
    &lt;dc:name&gt; which indicates that the predicate must match the "name" local-name for the namespace declared
    as "dc" in the using part.<br/><br/>
    Example:
    <pre>(?x,&lt;foo:has^gt;,?y),
  (?y,&lt;foo:color&gt;,?z)</pre>
    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.
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Sytax: AND</b>
    </td>
    <td bgcolor="#eeeeff" class="text">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.<br/><br/>
    Example: Select all the subjects that have a blue item.
    <pre>SELECT ?x 
  FROM &lt;doc.rdf&gt;
  WHERE (?x,&lt;foo:has&gt;,?y),
        (?y,&lt;foo:color&gt;,?z)
  AND ?z=="blue"
  USING foo for <http://foo.org/properties#>      
  </pre>
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Syntax: USING</b>
    </td>
    <td bgcolor="#eeeeff" class="text">The using section declares all the namespaces that will be used
    for RDF properties, declarations are sepparated by commas and use the notation:<br/>
    <pre>prefix for <URI></pre>
    Example:<br/>
    <pre>USING foo for &lt;http://foo.org/properties#>, col for &lt;http://props.com/catalog#>
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Examples:</b>
    </td>
    <td bgcolor="#eeeeff" class="text">In the following section we'll show some examples using a real RDF
  document and working RDQL queries (at last!). <br/>
  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):
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" bgcolor="ddddff" width="80%">
  <tr>
    <td valign="top" width="100%" class="text">people.rdf
    </td>
  </tr>
  </table>
  <table border="0" bgcolor="#aaaaaa" width="80%">
  <tr>
    <td valign="top" width="100%" class="text">
  <pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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#"&gt;
  
  &lt;rdf:Description about="http://foo.org/persons/john"&gt;
    &lt;dt:name&gt;John&lt;/dt:name&gt;
    &lt;dt:age&gt;26&lt;/dt:age&gt;
  &lt;/rdf:Description&gt;
  
  &lt;rdf:Description about="http://foo.org/persons/Peter"&gt;
    &lt;dt:name&gt;Peter&lt;/dt:name&gt;
    &lt;dt:position&gt;foo2&lt;/dt:position&gt;
    &lt;dt:age&gt;25&lt;/dt:age&gt;
    &lt;dt:friend rdf:resource="http://foo.org/persons/john" /&gt;
    &lt;dt:friend rdf:resource="http://foo.org/persons/Carl" /&gt;
  &lt;/rdf:Description&gt;
  
  &lt;rdf:Description about="http://foo.org/persons/Micky"&gt;
    &lt;dt:name&gt;Micky&lt;/dt:name&gt;
    &lt;dt:position&gt;foo&lt;/dt:position&gt;
    &lt;dt:age&gt;16&lt;/dt:age&gt;
  &lt;/rdf:Description&gt;
  
  &lt;rdf:Description about="http://foo.org/persons/Carl"&gt;
    &lt;dt:name&gt;Carl&lt;/dt:name&gt;
    &lt;dt:position&gt;foo&lt;/dt:position&gt;
    &lt;dt:age&gt;28&lt;/dt:age&gt;
    &lt;dt:friend rdf:resource="http://foo.org/persons/Peter" /&gt;
  &lt;/rdf:Description&gt;
  
  &lt;rdf:Description about="http://foo.org/team"&gt;
    &lt;dt:members&gt;
     &lt;rdf:Bag&gt;
      &lt;rdf:li rdf:resource="http://foo.org/persons/Peter" /&gt;
      &lt;rdf:li&gt;Kim&lt;/rdf:li&gt;
      &lt;/rdf:Bag&gt;
    &lt;/dt:members&gt;
  &lt;/rdf:Description&gt;
  
  &lt;/rdf:RDF&gt;</pre>  
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Example 1:</b>
    </td>
    <td bgcolor="#eeeeff" class="text"><i>Indicate name and age of all the individuals older than 20.</td>
  </tr>
  </table>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Query:</b>
    </td>
    <td bgcolor="#eeeeff" class="text"><pre>SELECT ?y
  FROM &lt;people.rdf&gt;
  WHERE (?x,&lt;dt:age&gt;,?z),(?x,&lt;dt:name&gt;,?y)
  AND ?z&gt;20
  USING dt for &lt;http://foo.org#>, rdf for &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#>>
    </td>
  </tr>
  </table>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Result:</b>
    </td>
    <td bgcolor="#eeeeff" class="text">Array ( [0] => Array ( [?y] => John ) [1] => Array ( [?y] => Peter ) [2] => Array ( [?y] => Carl ) )
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Example 2:</b>
    </td>
    <td bgcolor="#eeeeff" class="text"><i>If A is friend of B and B is friend of A report them</i></td>
  </tr>
  </table>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Query:</b>
    </td>
    <td bgcolor="#eeeeff" class="text"><pre>SELECT ?x,?y
  FROM &lt;people.rdf&gt;
  WHERE (?x,&lt;dt:friend&gt;,?y),(?y,&lt;dt:friend&gt;,?x)
  AND ?x&lt;^gt;?y
  USING dt for &lt;http://foo.org#>, rdf for &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#>>
  </td>
  </tr>
  </table>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Result:</b>
    </td>
    <td bgcolor="#eeeeff" class="text">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 ) )
    </td>
  </tr>
  </table>
  <br/>
  
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Example 3:</b>
    </td>
    <td bgcolor="#eeeeff" class="text"><i>Show all the members of the team.</i></td>
  </tr>
  </table>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Query:</b>
    </td>
    <td bgcolor="#eeeeff" class="text"><pre>SELECT ?z
  FROM <people.rdf>
  WHERE (?x,<dt:members>,?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 <http://foo.org#>, rdf for <http://www.w3.org/1999/02/22-rdf-syntax-ns#></pre>
    </td>
  </tr>
  </table>
  <table border="0" width="80%">
  <tr>
    <td valign="top" bgcolor="#eeeeff" width="20%" class="text"><b>Result:</b>
    </td>
    <td bgcolor="#eeeeff" class="text">Array ( [0] => Array ( [?z] => http://foo.org/persons/Peter ) [1] => Array ( [?z] => Kim ) )
    </td>
  </tr>
  </table>
  <br/>
  
  </div>
  
  </body>
  </html>


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.