Source for file SparqlParser.php
Documentation is available at SparqlParser.php
// ---------------------------------------------
// ---------------------------------------------
* Parses a SPARQL Query string and returns a Query Object.
* @author Tobias Gauss <tobias.gauss@web.de>
* @version $Id: fsource_sparql__sparqlSparqlParser.php.html,v 1.7 2006/06/26 12:34:18 tgauss Exp $
* @var Query The query Object
* @var string The Querystring
* @var array The tokenized Query
private $tokens =
array();
* @var int Last parsed graphPattern
* @var array Operators introduced by sparql
private $sops =
array('regex','bound','isuri','isblank','isliteral','str','lang','datatype','langmatches');
* Constructor of SparqlParser
$this->query =
new Query();
$this->querystring =
null;
// add the default prefixes defined in constants.php
global $default_prefixes;
$this->query->prefixes =
$default_prefixes;
* Main function of SparqlParser. Parses a query string.
* @param String $queryString The SPARQL query
* @return Query The query object
* @throws SparqlParserException
public function parse($queryString =
false){
$uncommentedQuery =
$this->uncomment($queryString);
$this->querystring =
$uncommentedQuery;
$this->query->isEmpty =
true;
* Tokenizes the querystring.
* @param String $queryString
protected function tokenize($queryString){
$queryString =
trim($queryString);
$specialChars =
array (" ", "\t", "\r", "\n", ",", "(", ")","{","}",'"',"'",";","[","]");
for ($i=
0; $i<
$len; ++
$i) {
if (!in_array($queryString{$i}, $specialChars))
$this->tokens[$n] .=
$queryString{$i};
if ($this->tokens[$n] !=
'')
$this->tokens[$n] =
$queryString{$i};
$this->tokens[++
$n] =
'';
* Removes comments in the query string. Comments are
* @param String $queryString
* @return String The uncommented query string
// php appears to escape quotes, so unescape them
$regex =
"/((\"[^\"]*\")|(\'[^\']*\')|(\<[^\>]*\>))|(#.*)/";
* Starts parsing the tokenized SPARQL Query.
}while(next($this->tokens));
* Parses the BASE part of the query.
* @throws SparqlParserException
$this->query->setBase(current($this->tokens));
* Adds a new namespace prefix to the query object.
* @throws SparqlParserException
$this->query->addPrefix($prefix,$uri);
* Parses the SELECT part of a query.
* @throws SparqlParserException
$this->query->addVariable(current($this->tokens));
if(!$this->query->getResultForm())
$this->query->setResultForm('select');
$this->query->setResultForm('select distinct');
$this->query->addVariable(current($this->tokens));
* Adds a new variable to the query and sets result form to 'DESCRIBE'.
$this->query->addVariable(current($this->tokens));
if(!$this->query->getResultForm())
$this->query->setResultForm('describe');
* Sets result form to 'ASK'.
$this->query->setResultForm('ask');
* Parses the FROM clause.
* @throws SparqlParserException
$this->query->addFrom(current($this->tokens));
$this->query->addFrom(current($this->tokens));
$this->query->addFromNamed(current($this->tokens));
* Parses the CONSTRUCT clause.
* @throws SparqlParserException
$this->query->setResultForm('construct');
* Parses the WHERE clause.
* @throws SparqlParserException
* Checks if $token is a variable.
* @param String $token The token
* @return boolean TRUE if the token is ja variable false if not
if($token{0}==
'$'|
$token{0}==
'?'){
$this->query->addVar($token);
* Checks if $token is an IRI.
* @param String $token The token
* @return boolean TRUE if the token is an IRI false if not
$pattern=
"/^<[^>]*>\.?$/";
* Checks if $token is a Blanknode.
* @param String $token The token
* @return boolean TRUE if the token is BNode false if not
* Checks if $token is a qname.
* @param String $token The token
* @return boolean TRUE if the token is a qname false if not
* @throws SparqlParserException
$pattern=
"/^([^:^\<]*):([^:]*)$/";
$prefs =
$this->query->getPrefixes();
if(isset
($prefs{$hits{1}}))
* Checks if $token is a Literal.
* @param String $token The token
* @return boolean TRUE if the token is a Literal false if not
* FastForward until next token which is not blank.
* Rewind until next token which is not blank.
* Parses a graph pattern.
* @param int $optional Optional graph pattern
* @param int $union Union graph pattern
* @param string $graph Graphname
* @param boolean $constr TRUE if the pattern is a construct pattern
protected function parseGraphPattern($optional =
false, $union =
false, $graph =
false,$constr =
false, $external =
false){
$pattern =
$this->query->getNewPattern($constr);
$pattern->addOptional($optional);
$this->tmp =
$pattern->getId();
$pattern->addUnion($union);
$pattern->setGraphname($graph);
* Parses a triple pattern.
* @param GraphPattern $pattern
$tmp =
$this->parseNode($this->query->getBlanknodeLabel());
$tmp =
$this->parseNode($this->query->getBlanknodeLabel());
}else if(current($this->tokens)==
"("){
$pattern->addTriplePattern($trp);
* Parses a value constraint.
* @param GraphPattern $pattern
$constraint->setOuterFilter($outer);
$pattern->addConstraint(&$constraint);
* Parses a bracketted expression.
* @param Constraint $constraint
* @throws SparqlParserException
while($open !=
0 & current($this->tokens)!=
false){
$exp =
$exp.
current($this->tokens);
$exp =
$exp.
current($this->tokens);
$exp =
$exp.
current($this->tokens);
$constraint->addExpression($exp);
* @param Constraint $constrain
* @throws SparqlParserException
while(current($this->tokens)!=
false &&
current($this->tokens)!=
"}"){
$exp =
$exp.
current($this->tokens);
$constraint->addExpression($exp);
* @param GraphPattern $pattern
* @throws SparqlParserException
* Parses the solution modifiers of a query.
* @throws SparqlParserException
$this->query->setSolutionModifier('limit',$val);
$this->query->setSolutionModifier('offset',$val);
}while(next($this->tokens));
* Parses order conditions of a query.
* @throws SparqlParserException
$val['val'] =
current($this->tokens);
$val['val'] =
current($this->tokens);
$val['val'] =
current($this->tokens);
$this->query->setSolutionModifier('order by',$valList);
* Parses a String to an RDF node.
* @return Node The parsed RDF node
* @throws SparqlParserException
if($node{strlen($node)-
1} ==
'.')
$this->query->addVar($node);
$node =
'?'.
substr($this->query->getBlanknodeLabel(),1);
$this->query->addVar($node);
$base =
$this->query->getBase();
$node =
$this->getFN($node);
* Checks if there is a datatype given and appends it to the node.
* @param String $sep used separator " or '
$node =
$node.
current($this->tokens);
}while(current($this->tokens)!=
$sep);
* Checks if the Node is a typed Literal.
* @return boolean TRUE if typed FALSE if not
$patternInt =
"/^-?[0-9]+$/";
$node->setDatatype(XML_SCHEMA.
'integer');
$patternBool =
"/^(true|false)$/";
$node->setDatatype(XML_SCHEMA.
'boolean');
$node =
new Resource(RDF_NAMESPACE_URI.
'type');
$patternDouble =
"/^-?[0-9]+.[0-9]+[e|E]?-?[0-9]*/";
$node->setDatatype(XML_SCHEMA.
'double');
* Parses an RDF collection.
* @param TriplePattern $trp
* @return Node The first parsed label
$tmpLabel =
$this->query->getBlanknodeLabel();
while(current($this->tokens)!=
")"){
$trp[] =
new QueryTriple($this->parseNode($tmpLabel),new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"),new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"));
* @param SparqlException $e
protected function error($e){
echo
"<b>SPARQL PARSER ERROR: </b>".
$e->getMessage().
"<br>
$end =
count($this->tokens)-
1;
for($i =
0;$i<
$end;$i++
){
echo
"-><b>".
$token.
"</b><-";
* Gets the full URI of a qname token.
* @return String The complete URI of a given token
protected function getFN($token){
$pattern=
"/^([^:]*):([^:]*)$/";
$prefs =
$this->query->getPrefixes();
$base =
$this->query->getBase();
if(isset
($prefs{$hits{1}}))
return substr($base,1,-
1).
$prefs{$hits{1}}.
$hits{2};
$base =
$this->query->getBase();
}// end class: SparqlParser.php
Documentation generated on Mon, 26 Jun 2006 14:26:23 +0200 by phpDocumentor 1.3.0RC6