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 result renderer for SparqlEngine
  6. *
  7. *   @author Tobias Gauß <tobias.gauss@web.de>
  8. *   @author Christian Weiske <cweiske@cweiske.de>
  9. *
  10. *   @package sparql
  11. */
  12. class SparqlEngine_ResultRenderer_Default implements SparqlEngine_ResultRenderer
  13. {
  14.     /**
  15.     *   Converts the database results into the output format
  16.     *   and returns the result.
  17.     *
  18.     *   @param array $arVartable    Variable table
  19.     *   @param Query $query         SPARQL query object
  20.     *   @param SparqlEngine $engine Sparql Engine to query the database
  21.     *   @return mixed               Most likely an array
  22.     */
  23.     public function convertFromResult($arVartableQuery $querySparqlEngine $engine)
  24.     {
  25.         $this->query   $query;
  26.         $this->engine  $engine;
  27.         $this->dataset $engine->getDataset();
  28.  
  29.         $result false;
  30.         $qrf    $this->query->getResultForm();
  31.  
  32.         if ($arVartable != null{
  33.             switch ($qrf{
  34.                 case 'ask':
  35.                     if (count($arVartable0{
  36.                         $result true;
  37.                     else {
  38.                         $result false;
  39.                     }
  40.                     break;
  41.                 case 'count':
  42.                     $result count($arVartable);
  43.                     break;
  44.                 case 'construct':
  45.                     $result $this->constructGraph(
  46.                         $arVartable,
  47.                         $this->query->getConstructPattern()
  48.                     );
  49.                     break;
  50.                 case 'describe':
  51.                     $result $this->describeGraph($arVartable);
  52.                     break;
  53.                 default:
  54.                     $result $arVartable;
  55.                     break;
  56.             }
  57.         else if ($qrf == 'describe'){
  58.             $result $this->describeGraph(null);
  59.         else if ($qrf == 'construct'){
  60.             $result $this->constructGraph(
  61.                 false,
  62.                 $this->query->getConstructPattern()
  63.             );
  64.         }
  65.  
  66.         return $result;
  67.     }//public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
  68.  
  69.  
  70.  
  71.     /**
  72.     * Constructs a result graph.
  73.     *
  74.     * @param  array         $arVartable       A table containing the result vars and their bindings
  75.     * @param  GraphPattern  $constructPattern The CONSTRUCT pattern
  76.     * @return MemModel      The result graph which matches the CONSTRUCT pattern
  77.     */
  78.     function constructGraph($arVartable$constructPattern)
  79.     {
  80.         $resultGraph new MemModel();
  81.  
  82.         if (!$arVartable{
  83.             return $resultGraph;
  84.         }
  85.  
  86.         $tp $constructPattern->getTriplePattern();
  87.  
  88.         $bnode 0;
  89.         foreach ($arVartable as $value{
  90.             foreach ($tp as $triple{
  91.                 $sub  $triple->getSubject();
  92.                 $pred $triple->getPredicate();
  93.                 $obj  $triple->getObject();
  94.  
  95.                 if (is_string($sub)  && $sub{1== '_'{
  96.                     $sub  new BlankNode("_bN".$bnode);
  97.                 }
  98.                 if (is_string($pred&& $pred{1== '_'{
  99.                     $pred new BlankNode("_bN".$bnode);
  100.                 }
  101.                 if (is_string($obj)  && $obj{1== '_'{
  102.                     $obj  new BlankNode("_bN".$bnode);
  103.                 }
  104.  
  105.  
  106.                 if (is_string($sub)) {
  107.                     $sub  $value[$sub];
  108.                 }
  109.                 if (is_string($pred)) {
  110.                     $pred $value[$pred];
  111.                 }
  112.                 if (is_string($obj)) {
  113.                     $obj  $value[$obj];
  114.                 }
  115.  
  116.                 if ($sub !== "" && $pred !== "" && $obj !== ""{
  117.                     $resultGraph->add(new Statement($sub,$pred,$obj));
  118.                 }
  119.             }
  120.             $bnode++;
  121.         }
  122.         return $resultGraph;
  123.     }//function constructGraph($arVartable, $constructPattern)
  124.  
  125.  
  126.  
  127.     /**
  128.     * Builds a describing named graph. To define an attribute list for a
  129.     * several rdf:type look at constants.php
  130.     *
  131.     * @param  array      $arVartable 
  132.     * @return MemModel 
  133.     */
  134.     function describeGraph($arVartable)
  135.     {
  136.         // build empty named graph
  137.         $resultGraph new MemModel();
  138.         // if no where clause fill $arVartable
  139.         $vars $this->query->getResultVars();
  140.         if ($arVartable == null{
  141.             if ($vars{
  142.                 $arVartable[0array('?x' => new Resource(substr($vars[0],1,-1)));
  143.                 $vars[0'?x';
  144.             }
  145.         }
  146.         // fetch attribute list from constants.php
  147.         global $sparql_describe;
  148.         // for each resultset
  149.         foreach ($arVartable as $resultset{
  150.             foreach ($vars as $varname{
  151.                 $varvalue $resultset[$varname];
  152.                 // try to determine rdf:type of the variable
  153.                 $type $this->_determineType($varvalue$resultGraph);
  154.                 // search attribute list defined in constants.php
  155.                 $list null;
  156.                 if ($type{
  157.                     $strLuri strtolower($type->getUri());
  158.                     if (isset($sparql_describe[$strLuri])) {
  159.                         $list $sparql_describe[$strLuri;
  160.                     }
  161.                 }
  162.                 // search in dataset
  163.                 $this->_getAttributes($list$resultGraph$varvalue);
  164.             }
  165.         }
  166.  
  167.         return $resultGraph;
  168.     }//function describeGraph($arVartable)
  169.  
  170.  
  171.  
  172.     /**
  173.     * Tries to determine the rdf:type of the variable.
  174.     *
  175.     * @param  Node       $var The variable
  176.     * @param  MemModel   $resultGraph The result graph which describes the Resource
  177.     * @return String     Uri of the rdf:type
  178.     */
  179.     protected function _determineType($var$resultGraph)
  180.     {
  181.         $type null;
  182.         // find in namedGraphs
  183.         if (!$var instanceof Literal{
  184.             $iter $this->dataset->findInNamedGraphs(
  185.                 null,
  186.                 $var,
  187.                 new Resource(RDF_NAMESPACE_URI.'type'),
  188.                 null,
  189.                 true
  190.             );
  191.             while ($iter->valid()) {
  192.                 $statement $iter->current();
  193.                 $type $statement->getObject();
  194.                 $resultGraph->add($iter->current());
  195.                 break;
  196.             }
  197.         }
  198.         // if no type information found find in default graph
  199.         if (!$type{
  200.             if (!$var instanceof Literal{
  201.                 $iter1 $this->dataset->findInDefaultGraph(
  202.                     $var,
  203.                     new Resource(RDF_NAMESPACE_URI.'type'),
  204.                     null
  205.                 );
  206.                 $type null;
  207.                 while ($iter1->valid()) {
  208.                     $statement $iter1->current();
  209.                     $type      $statement->getObject();
  210.                     $resultGraph->add($iter1->current());
  211.                     break;
  212.                 }
  213.             }
  214.         }
  215.         return $type;
  216.     }//protected function _determineType($var, $resultGraph)
  217.  
  218.  
  219.  
  220.     /**
  221.     * Search the attributes listed in $list in the dataset.
  222.     * Modifies $resultGraph
  223.     *
  224.     * @param Array      $list List containing the attributes
  225.     * @param MemModel   $resultGraph The result graph which describes the Resource
  226.     * @return void 
  227.     */
  228.     protected function _getAttributes($list$resultGraph$varvalue)
  229.     {
  230.         if ($list){
  231.             foreach ($list as $attribute{
  232.                 if (!$varvalue instanceof Literal{
  233.                     $iter2 $this->dataset->findInNamedGraphs(
  234.                         null,
  235.                         $varvalue,
  236.                         new Resource($attribute),
  237.                         null,
  238.                         true
  239.                     );
  240.                     while ($iter2->valid()) {
  241.                         $resultGraph->add($iter2->current());
  242.                         $iter2->next();
  243.                     }
  244.                     $iter3 $this->dataset->findInDefaultGraph(
  245.                         $varvalue,
  246.                         new Resource($attribute),
  247.                         null
  248.                     );
  249.                     while ($iter3->valid()) {
  250.                         $resultGraph->add($iter3->current());
  251.                         $iter3->next();
  252.                     }
  253.                 }
  254.             }
  255.         }
  256.     }//protected function _getAttributes($list, $resultGraph, $varvalue)
  257.  
  258.  
  259. }//class SparqlEngine_ResultRenderer_Default implements SparqlEngine_ResultRenderer
  260.  
  261. ?>

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