server-php-xml-class-rdql-db-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(// Now process the query result return 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 // Constructor receives a RDF_iterator object // that must implement a find_tuples(sources,predicate, // returning all the tuples in the RDF sources matching the provided arguments. // There're two RDF_Iterator classes provided: RDF_document_iterator AND // RDF_mysql_iterator // The first one is used to query a set of RDF documents passed as filepaths or URLs // The second one can be used to query a document stored in MySQL using the RDF_store class function RDQL_query(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(// Now everything is parsed and the query can be processed. // The next step will parse all the conditions against the // supplied source's tuples returning an array of asociative // arrays with all the variables involved in the conditions 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 // This parses a 'SELECT ?x,?y,?z' expression returning an array with variable names. function parse_select(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 // This parses a where construction in the form 'WHERE (x1,x2,x3),(z1,z2,z3)' returning and array of conditions function parse_where(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;<strlen(expr);chr=substr(i,1); if(level++; } if(level--; } if( (level==0) ) { if(strlen(vars[]=avar=''; } } else { chr; } } if(strlen(vars[]=avar=''; } return // This parses and AND condition function parse_and(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 // This function filters the tuples passed as arguments according to the filter function filter_tuples(&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: <br/>"); preg_match("/\(([^)]*)\)/",condition,elems=explode(',',// Check each element, if it is <something:foo> then replace it by the // namespace if(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(<br/>"); if(vec) { this->array_sql_join(vec); } else { a_vec; } } return //end of class // This class implements an iterator for RDF documents // using URLs or filenames (paths) to locate the documents. class RDF_document_iterator extends RDF_iterator { var 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[]=// This is an iterator for RDF triplets the sources in the // FROM part of the RDQL expression must be PHP vars in the // form 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 // end of class ?>
(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.