|
From: <cw...@us...> - 2007-06-21 07:09:51
|
Revision: 457
http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=457&view=rev
Author: cweiske
Date: 2007-06-21 00:09:49 -0700 (Thu, 21 Jun 2007)
Log Message:
-----------
Submitting before lightning strikes my compzter
Modified Paths:
--------------
trunk/rdfapi-php/api/sparql/GraphPattern.php
trunk/rdfapi-php/api/sparql/Query.php
trunk/rdfapi-php/api/sparql/SparqlEngineDb/SqlGenerator.php
trunk/rdfapi-php/api/sparql/SparqlParser.php
Added Paths:
-----------
trunk/rdfapi-php/api/sparql/SparqlEngineDb/QuerySimplifier.php
Modified: trunk/rdfapi-php/api/sparql/GraphPattern.php
===================================================================
--- trunk/rdfapi-php/api/sparql/GraphPattern.php 2007-06-20 22:56:21 UTC (rev 456)
+++ trunk/rdfapi-php/api/sparql/GraphPattern.php 2007-06-21 07:09:49 UTC (rev 457)
@@ -97,7 +97,7 @@
*
* @return Constraint
*/
- public function getConstraint(){
+ public function getConstraint() {
return $this->constraint;
}
@@ -106,7 +106,7 @@
*
* @return integer
*/
- public function getOptional(){
+ public function getOptional() {
return $this->optional;
}
@@ -116,7 +116,7 @@
*
* @return integer
*/
- public function getSubpatternOf(){
+ public function getSubpatternOf() {
return $this->subpatternOf;
}
@@ -125,7 +125,7 @@
*
* @return integer
*/
- public function getUnion(){
+ public function getUnion() {
return $this->union;
}
@@ -227,5 +227,30 @@
return array_unique($arVars);
}//public function getVariables()
+
+
+ /**
+ * Checks if the graph pattern is empty (contains no
+ * usable data).
+ * Occurs when using "{}" without pre- or suffixes
+ * WHERE
+ * {
+ * { ?person rdf:type foaf:Person } .
+ * }
+ *
+ * @return boolean True if the pattern is empty.
+ */
+ public function isEmpty()
+ {
+ return
+ $this->getTriplePattern() === false
+ && $this->getGraphname() === null
+ && $this->getConstraint() === false
+ && $this->getOptional() === null
+ && $this->getUnion() === null
+ && $this->getSubpatternOf() === null
+ ;
+ }//public function isEmpty()
+
}// end class: GraphPattern.php
?>
\ No newline at end of file
Modified: trunk/rdfapi-php/api/sparql/Query.php
===================================================================
--- trunk/rdfapi-php/api/sparql/Query.php 2007-06-20 22:56:21 UTC (rev 456)
+++ trunk/rdfapi-php/api/sparql/Query.php 2007-06-21 07:09:49 UTC (rev 457)
@@ -289,7 +289,17 @@
$this->resultForm = strtolower($form);
}
+
/**
+ * Sets the result part.
+ *
+ * @param array Array of graph patterns
+ */
+ public function setResultPart($resultPart) {
+ $this->resultPart = $resultPart;
+ }
+
+ /**
* Adds a graph pattern to the result part.
*
* @param GraphPattern $pattern
Added: trunk/rdfapi-php/api/sparql/SparqlEngineDb/QuerySimplifier.php
===================================================================
--- trunk/rdfapi-php/api/sparql/SparqlEngineDb/QuerySimplifier.php (rev 0)
+++ trunk/rdfapi-php/api/sparql/SparqlEngineDb/QuerySimplifier.php 2007-06-21 07:09:49 UTC (rev 457)
@@ -0,0 +1,124 @@
+<?php
+
+/**
+* Simplifies ("flattens") Query objects that have graph
+* patterns which are subpatterns of other patterns.
+*
+* Example:
+* ?g ?h ?i .
+* {
+* {?person <some://typ/e> 'asd'}
+* UNION
+* {?person3 <some://typ/es2> 'three'}
+* }
+* is represented internally as three graph patterns, the latter
+* two referencing the first to be their pattern (they are subpatternOf).
+* Now this can be flattened to this which is the same:
+* {?g ?h ?i . ?person <some://typ/e> 'asd'}
+* UNION
+* {?g ?h ?i .?person3 <some://typ/es2> 'three'}
+*
+* This class does this.
+*
+* @author Christian Weiske <cw...@cw...>
+*/
+class SparqlEngineDb_QuerySimplifier
+{
+
+ /**
+ * Simplify the query by flattening out subqueries.
+ * Modifies the passed query object directly.
+ */
+ public function simplify(Query $query)
+ {
+ $arPatterns = $query->getResultPart();
+ $arPlan = $this->createPlan($arPatterns);
+ if (count($arPlan) == 0) {
+ return 0;
+ }
+
+ $this->executePlan($arPatterns, $arPlan);
+ $query->setResultPart($arPatterns);
+ }//public function simplify(Query $query)
+
+
+
+ /**
+ * Creates a plan what to do.
+ *
+ * @return array Array of arrays. Key is the parent pattern id,
+ * value is an array of subpatterns that belong to
+ * that parental pattern.
+ */
+ protected function createPlan(&$arPatterns)
+ {
+ list($arNumbers, $nNotZero) = $this->getNumbers($arPatterns);
+ if (count($arNumbers) == 0) {
+ return array();
+ }
+
+ $arPlan = array();
+
+ foreach ($arNumbers as $nId => $nNumber) {
+ $nParent = $pattern->getSubpatternOf();
+ $arPlan[$nParent][] = $nId;
+ }
+
+ return $arPlan;
+ }//protected function createPlan(&$arPatterns)
+
+
+
+ /**
+ * Returns an array of id-value pairs determining
+ * which pattern IDs (array id) are deepest nested
+ * (higher value).
+ * Array is sorted in reverse order, highest values
+ * first.
+ *
+ * @param array $arPatterns Array with GraphPatterns
+ * @return array Array with key-value pairs
+ */
+ protected function getNumbers(&$arPatterns)
+ {
+ $arNumbers = array();
+ foreach ($arPatterns as $nId => &$pattern) {
+ $nParent = $pattern->getSubpatternOf();
+ if ($nParent !== null) {
+ $arNumbers[$nId] = $arNumbers[$nParent] + 1;
+ } else {
+ $arNumbers[$nId] = 0;
+ }
+ }
+ //remove the not so interesting ones
+ foreach ($arNumbers as $nId => $nNumber) {
+ if ($nNumber == 0) {
+ unset($arNumbers[$nId]);
+ }
+ }
+
+ rsort($arNumbers);
+
+ return $arNumbers;
+ }//protected function getNumbers(&$arPatterns)
+
+
+
+ /**
+ * Removes all empty graph patterns from the
+ * array.
+ * Modifies it directly
+ */
+ protected static function dropEmpty(&$arPatterns)
+ {
+ foreach ($arPatterns as $nId => &$pattern) {
+ if ($pattern->isEmpty()) {
+ unset($arPatterns[$nId]);
+ }
+ }
+ //FIXME: continued indexes?
+ }//protected static function dropEmpty(&$arPatterns)
+
+}//class SparqlEngineDb_QuerySimplifier
+
+?>
\ No newline at end of file
Modified: trunk/rdfapi-php/api/sparql/SparqlEngineDb/SqlGenerator.php
===================================================================
--- trunk/rdfapi-php/api/sparql/SparqlEngineDb/SqlGenerator.php 2007-06-20 22:56:21 UTC (rev 456)
+++ trunk/rdfapi-php/api/sparql/SparqlEngineDb/SqlGenerator.php 2007-06-21 07:09:49 UTC (rev 457)
@@ -172,7 +172,7 @@
$this->arUnionVarAssignments[0] = array();
foreach ($this->query->getResultPart() as $graphPattern) {
- if (self::isEmptyPattern($graphPattern)) {
+ if ($graphPattern->isEmpty()) {
continue;
}
if ($graphPattern->getUnion() !== null) {
@@ -815,31 +815,6 @@
/**
- * Checks if the graph pattern is empty (contains no
- * usable data).
- * Occurs when using "{}" without pre- or suffixes
- * WHERE
- * {
- * { ?person rdf:type foaf:Person } .
- * }
- *
- * @param GraphPattern $graphPattern Graph pattern to check
- * @return boolean True if the pattern is empty.
- */
- protected static function isEmptyPattern(GraphPattern $graphPattern)
- {
- return
- $graphPattern->getTriplePattern() === false
- && $graphPattern->getGraphname() === null
- && $graphPattern->getConstraint() === false
- && $graphPattern->getOptional() === null
- && $graphPattern->getUnion() === null
- ;
- }//protected static function isEmptyPattern(GraphPattern $graphPattern)
-
-
-
- /**
* Removes all NULL values from an array and returns it.
*
* @param array $array Some array
Modified: trunk/rdfapi-php/api/sparql/SparqlParser.php
===================================================================
--- trunk/rdfapi-php/api/sparql/SparqlParser.php 2007-06-20 22:56:21 UTC (rev 456)
+++ trunk/rdfapi-php/api/sparql/SparqlParser.php 2007-06-21 07:09:49 UTC (rev 457)
@@ -640,6 +640,7 @@
$tmpPred = "";
$obj = "";
do {
+//echo strtolower(current($this->tokens)) . "\n";
switch (strtolower(current($this->tokens))) {
case false:
$cont = false;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|