Source for file SparqlParser.php
Documentation is available at SparqlParser.php
// ---------------------------------------------
// ---------------------------------------------
require_once RDFAPI_INCLUDE_DIR .
'model/Literal.php';
require_once RDFAPI_INCLUDE_DIR .
'model/Resource.php';
require_once RDFAPI_INCLUDE_DIR .
'sparql/Constraint.php';
require_once RDFAPI_INCLUDE_DIR .
'sparql/Query.php';
require_once RDFAPI_INCLUDE_DIR .
'sparql/QueryTriple.php';
require_once RDFAPI_INCLUDE_DIR .
'sparql/SparqlParserException.php';
* Parses a SPARQL Query string and returns a Query Object.
* @author Tobias Gauss <tobias.gauss@web.de>
* @author Christian Weiske <cweiske@cweiske.de>
* @version $Id: fsource_sparql__sparqlSparqlParser.php.html 443 2007-06-01 16:25:38Z cax $
* Last parsed graphPattern
* Operators introduced by sparql
protected static $sops =
array(
* Which order operators are to be treated.
* (11.3 Operator Mapping)
protected static $operatorPrecedence =
array(
* Constructor of SparqlParser
* 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);
* Set all internal variables to a clear state
* before we start parsing.
// add the default prefixes defined in constants.php
global $default_prefixes;
* 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};
* 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.
* Parses the BASE part of the query.
* @throws SparqlParserException
* Adds a new namespace prefix to the query object.
* @throws SparqlParserException
* Parses the SELECT part of a query.
* @throws SparqlParserException
"Variable or '*' expected.",
"Unexpected end of File.",
* Adds a new variable to the query and sets result form to 'DESCRIBE'.
* Sets result form to 'ASK' and 'COUNT'.
* @param string $form if it's an ASK or COUNT query
* Parses the FROM clause.
* @throws SparqlParserException
* Parses the CONSTRUCT clause.
* @throws SparqlParserException
* Parses the WHERE clause.
* @throws SparqlParserException
* Checks if $token is a variable.
* @param String $token The token
* @return boolean TRUE if the token is a variable false if not
if (isset
($token[0]) &&
($token{0} ==
'$' ||
$token{0} ==
'?')) {
* 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=
"/^([^:^\<]*):([^:]*)$/";
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->addOptional($optional);
$this->tmp =
$pattern->getId();
$pattern->addUnion($union);
$pattern->setGraphname($graph);
} while ($pattern->open);
* Parses a triple pattern.
* @param GraphPattern $pattern
$pattern->addTriplePattern($trp);
* Parses a value constraint.
* @param GraphPattern $pattern
* @param boolean $outer If the constraint is an outer one.
$constraint->setOuterFilter($outer);
//for backwards compatibility with the normal sparql engine
// which does not use the tree array currently
$nEndKey -
$nBeginKey -
1
if ($expression[0] ==
'(' &&
substr($expression, -
1) ==
')') {
$constraint->addExpression($expression);
$pattern->addConstraint($constraint);
}//protected function parseConstraint(&$pattern, $outer)
* Parses a constraint string recursively.
* The result array is one "element" which may contain subelements.
* All elements have one key "type" that determines which other
* array keys the element array has. Valid types are:
* Just a plain value with a value key, nothing else
* A function has a name and an array of parameter(s). Each parameter
* An equation has an operator, and operand1 and operand2 which
* are elements themselves
* Any element may have the "negated" value set to true, which means
* that is is - negated (!).
* @internal The functionality of this method is being unit-tested
* in testSparqlParserTests::testParseFilter()
* "equation'-elements have another key "level" which is to be used
* @return array Nested tree array representing the filter
if ($chQuotes !==
null &&
$tok !=
$chQuotes) {
} else if ($tok ==
')' ||
$tok ==
'}' ||
$tok ==
'.') {
if ($chQuotes ===
null) {
$bFunc1 = isset
($part[0]['type']) &&
$part[0]['type'] ==
'value';
$bFunc2 = isset
($tree['type']) &&
$tree['type'] ==
'equation'
&& isset
($tree['operand2']) && isset
($tree['operand2']['value']);
$tree['type'] =
'function';
$tree['name'] =
$part[0]['value'];
self::fixNegationInFuncName($tree);
if (isset
($part[1]['type'])) {
$part[1] =
array($part[1]);
$tree['parameter'] =
$part[1];
$tree['operand2']['type'] =
'function';
$tree['operand2']['name'] =
$tree['operand2']['value'];
self::fixNegationInFuncName($tree['operand2']);
$tree['operand2']['parameter'] =
$part[0];
unset
($tree['operand2']['value']);
unset
($tree['operand2']['quoted']);
if (isset
($tree['type']) &&
$tree['type'] ==
'equation'
&& isset
($tree['operand2'])) {
} else if (isset
($tree['type']) &&
$tree['type'] !=
'equation') {
$tree['type'] =
'equation';
$tree['level'] =
$nLevel;
$tree['operator'] =
$tok;
$tree['operand1'] =
$part[0];
unset
($tree['operand2']);
'Unexpected "!" negation in constraint.'
if (count($part) ==
0 &&
!isset
($tree['type'])) {
} else if (substr($tok, 0, 2) ==
'^^') {
$part[count($part) -
1]['datatype']
} else if ($tok[0] ==
'@') {
if (isset
($tree['type']) &&
$tree['type'] ==
'equation' && isset
($part[0])) {
$tree['operand2'] =
$part[0];
self::balanceTree($tree);
if (!isset
($tree['type']) &&
$bParameter) {
} else if (isset
($tree['type']) &&
$tree['type'] ==
'equation'
&& isset
($tree['operand1']) &&
!isset
($tree['operand2'])
$tree['operand2'] =
$part[0];
self::balanceTree($tree);
if (!isset
($tree['type']) && isset
($part[0])) {
if (isset
($tree['negated'])) {
$part[0]['negated'] =
true;
}//protected function parseConstraintTree($nLevel = 0, $bParameter = false)
* "Balances" the filter tree in the way that operators on the same
* level are nested according to their precedence defined in
* $operatorPrecedence array.
* @param array $tree Tree to be modified
isset
($tree['type']) &&
$tree['type'] ==
'equation'
&& isset
($tree['operand1']['type']) &&
$tree['operand1']['type'] ==
'equation'
&&
$tree['level'] ==
$tree['operand1']['level']
&&
self::$operatorPrecedence[$tree['operator']] >
self::$operatorPrecedence[$tree['operand1']['operator']]
'level' =>
$tree['level'],
'operator' =>
$tree['operator'],
'operand1' =>
$tree['operand1']['operand2'],
'operand2' =>
$tree['operand2']
$tree['operator'] =
$tree['operand1']['operator'];
$tree['operand1'] =
$tree['operand1']['operand1'];
$tree['operand2'] =
$op2;
}//protected static function balanceTree(&$tree)
if ($tree['type'] ==
'function' &&
$tree['name'][0] ==
'!') {
$tree['name'] =
substr($tree['name'], 1);
if (!isset
($tree['negated'])) {
self::fixNegationInFuncName($tree);
}//protected static function fixNegationInFuncName(&$tree)
* Parses a bracketted expression.
* @param Constraint $constraint
* @throws SparqlParserException
"Unexpected end of query.",
$constraint->addExpression($exp);
* @param Constraint $constrain
* @throws SparqlParserException
"Unexpected end of query.",
$constraint->addExpression($exp);
* @param GraphPattern $pattern
* @throws SparqlParserException
* Parses the solution modifiers of a query.
* @throws SparqlParserException
* Parses order conditions of a query.
* @throws SparqlParserException
* Parses a String to an RDF node.
* @return Node The parsed RDF node
* @throws SparqlParserException
if ($node{strlen($node)-
1} ==
'.') {
} else if ($node[0] ==
'<') {
//partial IRI? loop tokens until we find a closing >
if (substr($node, -
1) ==
'>') {
if (substr($node, -
1) !=
'>') {
"Unclosed IRI: " .
$node,
$node .
" is neither a valid rdf- node nor a variable.",
* Checks if there is a datatype given and appends it to the node.
* @param String $sep used separator " or '
* Checks if the Node is a typed Literal.
* @return boolean TRUE if typed FALSE if not
$patternInt =
"/^-?[0-9]+$/";
$patternBool =
"/^(true|false)$/";
$patternDouble =
"/^-?[0-9]+.[0-9]+[e|E]?-?[0-9]*/";
* Parses an RDF collection.
* @param TriplePattern $trp
* @return Node The first parsed label
$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>
for ($i =
0; $i <
$end; $i++
) {
echo
"-><b>".
$token.
"</b><-";
}// end class: SparqlParser.php
Documentation generated on Fri, 1 Jun 2007 16:52:17 +0200 by phpDocumentor 1.3.2