Source for file Default.php

Documentation is available at Default.php

  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/ResultRenderer.php';
  3.  
  4. /**
  5. *   Default RAP result renderer.
  6. *
  7. *   @author Christian Weiske <cweiske@cweiske.de>
  8. *
  9. *   @package sparql
  10. */
  11. class SparqlEngineDb_ResultRenderer_Default implements SparqlEngineDb_ResultRenderer
  12. {
  13.  
  14.     /**
  15.     *   Defines the methods needed to create the types
  16.     *   in $arVarAssignments.
  17.     *   Key is the type (e.g. "s" for subject), and
  18.     *   value the method's name.
  19.     *
  20.     *   @see $arVarAssignments
  21.     *
  22.     *   @var array 
  23.     */
  24.     protected $arCreationMethods = array(
  25.         's' => 'createSubjectFromDbRecordSetPart',
  26.         'p' => 'createPredicateFromDbRecordSetPart',
  27.         'o' => 'createObjectFromDbRecordSetPart'
  28.     );
  29.  
  30.  
  31.  
  32.     /**
  33.     *   Converts the database results into the desired output format
  34.     *   and returns the result.
  35.     *
  36.     *   @param array $arRecordSets  Array of (possibly several) SQL query results.
  37.     *   @param Query $query     SPARQL query object
  38.     *   @param SparqlEngineDb $engine   Sparql Engine to query the database
  39.     *   @return mixed   The result as rendered by the result renderers.
  40.     */
  41.     public function convertFromDbResults($arRecordSetsQuery $querySparqlEngineDb $engine)
  42.     {
  43.         $this->query    $query;
  44.         $this->engine   $engine;
  45.         $this->sg       $engine->getSqlGenerator();
  46.  
  47.         $strResultForm $this->query->getResultForm();
  48.         switch ($strResultForm{
  49.             case 'construct':
  50.             case 'select':
  51.             case 'select distinct':
  52.                 $arResult $this->getVariableArrayFromRecordSets($arRecordSets$strResultForm);
  53.  
  54.                 //some result forms need more modification
  55.                 switch ($strResultForm{
  56.                     case 'construct';
  57.                         $arResult $this->constructGraph(
  58.                             $arResult,
  59.                             $this->query->getConstructPattern()
  60.                         );
  61.                         break;
  62.                     case 'describe';
  63.                         $arResult $this->describeGraph($arResult);
  64.                         break;
  65.                 }
  66.  
  67.                 return $arResult;
  68.                 break;
  69.  
  70.             case 'count':
  71.             case 'ask':
  72.                 if (count($arRecordSets1{
  73.                     throw new Exception(
  74.                         'More than one result set for a '
  75.                         . $strResultForm ' query!'
  76.                     );
  77.                 }
  78.  
  79.                 $nCount 0;
  80.                 $dbRecordSet reset($arRecordSets);
  81.                 foreach ($dbRecordSet as $row{
  82.                     $nCount intval($row['count']);
  83.                     break;
  84.                 }
  85.  
  86.                 if ($strResultForm == 'ask'{
  87.                     return $nCount 0;
  88.                 else {
  89.                     return $nCount;
  90.                 }
  91.                 break;
  92.  
  93.             case 'describe':
  94.             default:
  95.                 throw new Exception('Unsupported result form: ' $strResultForm);
  96.         }
  97.  
  98.     }//public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine)
  99.  
  100.  
  101.  
  102.  
  103.     protected function getVariableArrayFromRecordSets($arRecordSets$strResultForm)
  104.     {
  105.         $arResult array();
  106.         foreach ($arRecordSets as $dbRecordSet{
  107.             $arResult array_merge(
  108.                 $arResult,
  109.                 $this->getVariableArrayFromRecordSet($dbRecordSet$strResultForm)
  110.             );
  111.         }
  112.         return $arResult;
  113.     }//protected function getVariableArrayFromRecordSets($arRecordSets, $strResultForm)
  114.  
  115.  
  116.  
  117.     /**
  118.     *   Converts a ADORecordSet object into an array of "rows" that
  119.     *   are subarrays of variable => value pairs.
  120.     *
  121.     *   @param ADORecordSet $dbRecordSet    Anything ADOConnection::Execute() can return
  122.     *   @return array 
  123.     */
  124.     protected function getVariableArrayFromRecordSet(ADORecordSet $dbRecordSet$strResultForm)
  125.     {
  126.         $arResult       array();
  127.         switch ($strResultForm{
  128.             case 'construct':
  129.                 $arResultVars $this->query->getConstructPatternVariables();
  130.                 break;
  131.             default:
  132.                 $arResultVars $this->query->getResultVars();
  133.                 break;
  134.         }
  135.  
  136.         if (in_array('*'$arResultVars)) {
  137.             $arResultVars   array_keys($this->sg->arVarAssignments);
  138.         }
  139.  
  140.         //work around bug in adodb:
  141.         // ADORecordSet_empty does not implement php5 iterators
  142.         if ($dbRecordSet->RowCount(<= 0{
  143.             return array();
  144.         }
  145.  
  146.         foreach ($dbRecordSet as $row{
  147.             $arResultRow array();
  148.             foreach ($arResultVars as $strVarName{
  149.                 if (!isset($this->sg->arVarAssignments[$strVarName])) {
  150.                     //variable is in select, but not in result (test: q-select-2)
  151.                     $arResultRow[$strVarName'';
  152.                 else {
  153.                     $arVarSettings  $this->sg->arVarAssignments[$strVarName];
  154.                     $strMethod      $this->arCreationMethods[$arVarSettings[1]];
  155.                     $arResultRow[$strVarName$this->$strMethod($dbRecordSet$arVarSettings[0]$strVarName);
  156.                 }
  157.             }
  158.             $arResult[$arResultRow;
  159.         }
  160.         return $arResult;
  161.     }//function getVariableArrayFromRecordSet(ADORecordSet $dbRecordSet)
  162.  
  163.  
  164.  
  165.     /**
  166.     *   Creates an RDF Statement object for one of the variables
  167.     *   contained in the given $dbRecordSet object.
  168.     *
  169.     *   @see convertFromDbResult() to understand $strVarBase necessity
  170.     *
  171.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  172.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  173.     *
  174.     *   @return Statement   RDF statement object
  175.     */
  176.     protected function createStatementFromDbRecordSetPart(ADORecordSet $dbRecordSet$strVarBase)
  177.     {
  178.         return new Statement(
  179.             $this->createSubjectFromDbRecordSetPart  ($dbRecordSet$strVarBase),
  180.             $this->createPredicateFromDbRecordSetPart($dbRecordSet$strVarBase),
  181.             $this->createObjectFromDbRecordSetPart   ($dbRecordSet$strVarBase)
  182.         );
  183.     }//protected function createStatementFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase)
  184.  
  185.  
  186.  
  187.     /**
  188.     *   Creates an RDF subject object
  189.     *   contained in the given $dbRecordSet object.
  190.     *
  191.     *   @see convertFromDbResult() to understand $strVarBase necessity
  192.     *
  193.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  194.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  195.     *
  196.     *   @return Resource   RDF triple subject resource object
  197.     */
  198.     protected function createSubjectFromDbRecordSetPart(ADORecordSet $dbRecordSet$strVarBase$strVarName)
  199.     {
  200.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']] === null{
  201.             //FIXME: should be NULL, but doesn't pass test
  202.             return '';
  203.         }
  204.  
  205.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_is']] == 'r'
  206.             //null should be predicate which is always a resource
  207.          || $dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_is']] === null
  208.         {
  209.             $subject    new Resource ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  210.         else {
  211.             $subject    new BlankNode($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  212.         }
  213.         return $subject;
  214.     }//protected function createSubjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
  215.  
  216.  
  217.  
  218.     /**
  219.     *   Creates an RDF predicate object
  220.     *   contained in the given $dbRecordSet object.
  221.     *
  222.     *   @see convertFromDbResult() to understand $strVarBase necessity
  223.     *
  224.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  225.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  226.     *
  227.     *   @return Resource   RDF triple predicate resource object
  228.     */
  229.     protected function createPredicateFromDbRecordSetPart(ADORecordSet $dbRecordSet$strVarBase$strVarName)
  230.     {
  231.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']] === null{
  232.             //FIXME: should be NULL, but doesn't pass test
  233.             return '';
  234.         }
  235.         $predicate      new Resource ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  236.         return $predicate;
  237.     }//protected function createPredicateFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
  238.  
  239.  
  240.  
  241.     /**
  242.     *   Creates an RDF object object
  243.     *   contained in the given $dbRecordSet object.
  244.     *
  245.     *   @see convertFromDbResult() to understand $strVarBase necessity
  246.     *
  247.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  248.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  249.     *
  250.     *   @return Resource   RDF triple object resource object
  251.     */
  252.     protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet$strVarBase$strVarName)
  253.     {
  254.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']] === null{
  255.             //FIXME: should be NULL, but doesn't pass test
  256.             return '';
  257.         }
  258.         switch ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_is']]{
  259.             case 'r':
  260.                 $object new Resource ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  261.                 break;
  262.             case 'b':
  263.                 $object new BlankNode($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  264.                 break;
  265.             default:
  266.                 $object new Literal(
  267.                     $dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']],
  268.                     $dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_lang']]
  269.                 );
  270.                 if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_type']]{
  271.                     $object->setDatatype($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_type']]);
  272.                 }
  273.         }
  274.         return $object;
  275.     }//protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
  276.  
  277.  
  278.  
  279.     /**
  280.     * Constructs a result graph.
  281.     *
  282.     * @param  array         $arVartable       A table containing the result vars and their bindings
  283.     * @param  GraphPattern  $constructPattern The CONSTRUCT pattern
  284.     * @return MemModel      The result graph which matches the CONSTRUCT pattern
  285.     */
  286.     protected function constructGraph($arVartable$constructPattern)
  287.     {
  288.         $resultGraph new MemModel();
  289.  
  290.         if (!$arVartable{
  291.             return $resultGraph;
  292.         }
  293.  
  294.         $tp $constructPattern->getTriplePattern();
  295.  
  296.         $bnode 0;
  297.         foreach ($arVartable as $value{
  298.             foreach ($tp as $triple{
  299.                 $sub  $triple->getSubject();
  300.                 $pred $triple->getPredicate();
  301.                 $obj  $triple->getObject();
  302.  
  303.                 if (is_string($sub)  && $sub{1== '_'{
  304.                     $sub  new BlankNode("_bN".$bnode);
  305.                 }
  306.                 if (is_string($pred&& $pred{1== '_'{
  307.                     $pred new BlankNode("_bN".$bnode);
  308.                 }
  309.                 if (is_string($obj)  && $obj{1== '_'{
  310.                     $obj  new BlankNode("_bN".$bnode);
  311.                 }
  312.  
  313.  
  314.                 if (is_string($sub)) {
  315.                     $sub  $value[$sub];
  316.                 }
  317.                 if (is_string($pred)) {
  318.                     $pred $value[$pred];
  319.                 }
  320.                 if (is_string($obj)) {
  321.                     $obj  $value[$obj];
  322.                 }
  323.  
  324.                 if ($sub !== "" && $pred !== "" && $obj !== ""{
  325.                     $resultGraph->add(new Statement($sub,$pred,$obj));
  326.                 }
  327.             }
  328.             $bnode++;
  329.         }
  330.         return $resultGraph;
  331.     }//protected function constructGraph($arVartable, $constructPattern)
  332.  
  333. }//class SparqlEngineDb_ResultRenderer_Default implements SparqlEngineDb_ResultRenderer
  334. ?>

Documentation generated on Fri, 1 Jun 2007 16:49:00 +0200 by phpDocumentor 1.3.2