topical media & game development
server-php-xml-class-rdql-class-rdql.php / php
<?php
// # # # # # # # # # # # # # # # # # # # ###
// Title : RDQL (class_rdql.php)
// Version : 1.0
// Author : Luis Argerich (lrargerich@yahoo.com)
// Last modification date : 06-30-2002
// Description : This class implements the RDQL language
// to query RDF documents from paths or URLs.
// # # # # # # # # # # # # # # # # # # # ###
// History:
// 06-30-2002 : First release of this class
// # # # # # # # # # # # # # # # # # # # ###
// To-Dos:
// # # # # # # # # # # # # # # # # # # # ###
// How to use it: Check rdql_test.php for an example.
// rdql.html describes the RDQL language.
// class_rdql.html contains this class documentation.
// # # # # # # # # # # # # # # # # # # # ###
include_once("class_rdf_parser.php");
// A wrapper class to Query RDF documents
class RDQL_query_document {
// Queries documents passed as urls or filenames (use urls or filenames in the FROM part of the RDQL query)
function rdql_query_url(iterator = new RDF_document_iterator();
iterator);
q->parse_query(res;
}
} // end of class
// Abstract class defining methods for an RDF_iterator
// The RDF iterator is used by the RDQL_query class, the iterator
// MUST provide a find_tuples(subject,object)
// method that returns all the tuples matching subject, predicate and object
// from the designated sources (The FROM part of a RDQL expresion)
class RDF_iterator {
function init(sources,predicate,condition,condition{0}=='?') {
return true;
} else {
if(trim(tuple)) {
return true;
} else {
return false;
}
}
}
}
// This class implements the RDQL engine
class RDQL_query {
var sources,predicate,iterator) {
iterator;
}
// This parses the RDQL query returning an array of asociative arrays with the Query Results.
function parse_query(exps=query);
sources=Array();
filters=Array();
exps as exp=trim(exp,0,6)) == "SELECT") {
this->parse_select(exp,0,4)) == "FROM") {
this->parse_from(exp,0,5)) == "WHERE") {
this->parse_where(exp,0,3)) == "AND") {
this->parse_and(exp,0,5)) == "USING") {
this->parse_using(tuples = sources, ns);
foreach(filter) {
//
this->filter_tuples(filter);
}
tuples as a_result=Array();
foreach(key=>key,a_result[val;
}
}
if(count(a_result);
a_result;
}
}
if(count(query_results;
} else {
return false;
}
}
function tokenize(exprs=Array();
tok = strtok(tok) {
if(in_array(trim(strtoupper(current)>0) {
current;
current.=tok = strtok(" \n\t");
}
if(strlen(exprs[]=current=exprs;
}
function array_sql_join(v2) {
v1 as v2 as res = elemv1, res) {
res;
}
}
}
return v1, ret=Array();
foreach (array_keys(k1) {
if(isset(k1])) {
if(k1]==k1]) {
k1]=k1];
} else {
return false;
}
} else {
k1]=k1];
}
}
foreach (array_keys(k2) {
if(!isset(k2])) {
k2]=k2];
}
}
return exp) {
exp=trim(exp_parts=explode(" ",exp_parts[0]!="SELECT") {
trigger_error("Expected a SELECT token in the query",E_USER_WARNING);
}
array_shift(vars=explode(',',implode('',vars;
}
// This parses a 'FROM doc1,doc2' expression returning an array with document URIs/filenames.
function parse_from(vars=Array();
exp);
exp);
if(exp_parts);
exp_parts));
return exp) {
exp=trim(exp_parts=explode(" ",exp_parts[0]!="WHERE") {
trigger_error("Expected a WHERE token in the query",E_USER_WARNING);
}
array_shift(expr=implode('',avar='';
i=0;expr);chr=substr(i,1);
if(level++;
}
if(level--;
}
if( (level==0) ) {
if(strlen(vars[]=avar='';
}
} else {
chr;
}
}
if(strlen(vars[]=avar='';
}
return exp) {
exp=trim(exp_parts=explode(" ",exp_parts[0]!="AND") {
trigger_error("Expected a AND token in the query",E_USER_WARNING);
}
array_shift(vars=explode(',',implode('',vars;
}
// This parses a "USING" expr in the form USING prefix for URI, prefix for URI
function parse_using(vars=Array();
exp=trim(exp_parts=explode(" ",exp_parts[0]!="USING") {
trigger_error("Expected a USING token in the query",E_USER_WARNING);
}
array_shift(vars=explode(',',implode(' ',vars as var_parts=explode(' ',trim(var_parts[1])!="FOR") {
trigger_error("Expected a for token in the USING part: var_parts[2],var_parts[2]=ns[var_parts[2];
}
return tuples,toelim=Array();
for(i<count(i++) {
tuples[a_filter=a_tuple as value) {
varname,"\"a_filter);
}
a_filter.');';
//print("code:
result=eval(result) {
i;
}
}
foreach(i) {
unset(i]);
}
}
// IN:
// IN: conditions array with the coditions to be evaluated
// IN:
function find_matching_tuples(sources,ns) {
iterator=new RDF_triplets_iterator();
foreach(condition) {
condition);
//print("Condition:
preg_match("/\(([^)]*)\)/",condition,elems=explode(',',elems[0]{0}=='<') {
preg_match("/\<([^>]*)\>/",reqs);
reqs[1];
elems[0]);
ns[predicate_parts[1];
}
if(elems[1],elems[1]=predicate_parts=explode(':',elems[1]=predicate_parts[0]].elems[2]{0}=='<') {
preg_match("/\<([^>]*)\>/",reqs);
reqs[1];
elems[2]);
ns[predicate_parts[1];
}
this->iterator->find_tuples(elems[0],elems[2]);
//print_r(
if(vec) {
this->array_sql_join(vec);
} else {
a_vec;
}
}
return rdf_parser;
var object;
var tuples=Array();
function init(sources,predicate,this->subject=this->predicate=this->object=this->tuples=Array();
foreach(source) {
preg_match("/\<([^>]*)\>/",reqs);
reqs[1];
this->rdf_parser->rdf_parser_create( NULL );
this->rdf_parser->rdf_set_user_data( input=fopen(done=false;
if(!done=true;
}
done) {
input, 512 );
input);
if ( ! buf, strlen(input) ) ) {
input);
this->tuples;
}
function RDF_document_iterator() {
}
} // end of class
// This is the statement handler used by the RDF parser in the Document Iterator
function _statement_handler(&subject_type,predicate,object_type,xml_lang) {
if(user_data->subject,user_data->tuple_match(predicate) &&
user_data->object,result=Array();
if(result[subject;
}
if(result[predicate;
}
if(result[object;
}
if(count(user_data->tuples[]=var
class RDF_triplets_iterator extends RDF_iterator {
var index;
var this->index=0;
}
function find_tuples(subject,object) {
this->init();
subject;
predicate;
object;
foreach(source) {
// remove '
preg_match("/\<([^>]*)\>/",source,source=source{0}=='source=substr(this->tuples=$$source;
tuple=this->tuple_match(tuple[0]) && elems[1],this->tuple_match(tuple[2])) {
i=0;i++) {
if(i]{0}=='?') {
elems[tuple[ret[]=ret;
}
function get_tuple() {
if(this->tuples)) {
return false;
}
this->tuples[this->index++;
return
(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.