Example #1
0
 /**
  * The query engine's main method.
  *
  * @param  Dataset       $dataset    the RDF Dataset
  * @param  mixed         $query      the parsed SPARQL query
  * @param  String        $resultform the result form. If set to 'xml' the result will be
  *                                   SPARQL Query Results XML Format as described in http://www.w3.org/TR/rdf-sparql-XMLres/ .
  * @return Array/String  Type of the result depends on $resultform.
  */
 public function queryModel($dataset, Query $query, $resultform = false)
 {
     $this->query = $query;
     $this->dataset = $dataset;
     if ($this->query->isEmpty) {
         $vartable[0]['patternResult'] = null;
         return SparqlEngine_ResultConverter::convertFromResult($vartable, $this, $resultform);
     }
     $graphlist = $this->preselectGraphs();
     /// match graph patterns against the RDF Dataset
     $patternlist = $this->matchPatterns($graphlist);
     // filter results- apply inner filters
     $patternlist = $this->filterPatterns($patternlist, false);
     // join pattern results
     $vartable = $this->joinResults($patternlist);
     // filter results- apply outer filters
     $vartable = $this->filterPatterns($vartable, true);
     if ($vartable[0]['patternResult'] != null) {
         // sort vars (ORDER BY, LIMIT, OFFSET)
         $vartable = $this->sortVars($vartable[0]['patternResult']);
         $qrf = $this->query->getResultForm();
         if ($qrf == 'select' || $qrf == 'select distinct') {
             $vars = $this->query->getResultVars();
             $vartable = $this->selectVars($vartable, $vars);
             if ($qrf == 'select distinct') {
                 $vartable = $this->distinct($vartable);
             }
         }
     } else {
         $vartable = null;
     }
     return SparqlEngine_ResultConverter::convertFromResult($vartable, $this, $resultform);
 }
Example #2
0
 /**
  *   Converts the database results into the output format
  *   and returns the result.
  *
  *   @param array $arVartable    Variable table
  *   @param Query $query         SPARQL query object
  *   @param SparqlEngine $engine Sparql Engine to query the database
  *   @return string HTML result
  */
 public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
 {
     $this->query = $query;
     $this->engine = $engine;
     $this->dataset = $engine->getDataset();
     $strCode = '';
     $strResultForm = $query->getResultForm();
     switch ($strResultForm) {
         case 'select':
         case 'select distinct':
             $strCode = $this->createTableFromRecords($arVartable);
             break;
         case 'construct':
         case 'describe':
             throw new Exception('Construct and describe are currently not supported by the' . ' HTML renderer');
         case 'count':
         case 'ask':
             $nCount = count($arVartable);
             if ($strResultForm == 'ask') {
                 $strCode = 'There were results.';
             } else {
                 $strCode = 'There are ' . $nCount . ' results.';
             }
             break;
         default:
             throw new Exception('Unsupported result form: ' . $strResultForm);
     }
     return $this->wrapCode($strCode);
 }
Example #3
0
 public static function getSelect(Query $query, $arSqls, $strAdditional = '')
 {
     if (count($arSqls) == 1) {
         return implode('', $arSqls[0]) . $strAdditional;
     }
     //union
     $strUnion = 'UNION' . ($query->getResultForm() == 'select distinct' ? '' : ' ALL');
     $ar = array();
     foreach ($arSqls as $arSql) {
         $ar[] = implode('', $arSql) . $strAdditional;
     }
     return '(' . implode(') ' . $strUnion . ' (', $ar) . ')';
 }
Example #4
0
 /**
  * Adds a new variable to the query and sets result form to 'DESCRIBE'.
  *
  * @return void
  */
 protected function parseDescribe()
 {
     while (strtolower(current($this->tokens)) != 'from' & strtolower(current($this->tokens)) != 'where') {
         $this->_fastForward();
         if ($this->varCheck(current($this->tokens)) | $this->iriCheck(current($this->tokens))) {
             $this->query->addResultVar(current($this->tokens));
             if (!$this->query->getResultForm()) {
                 $this->query->setResultForm('describe');
             }
         }
         if (!current($this->tokens)) {
             break;
         }
     }
     prev($this->tokens);
 }
Example #5
0
 /**
  *   Converts the database results into JSON Format
  *
  *   @param array $arRecordSets  Array of (possibly several) SQL query results.
  *   @param Query $query     SPARQL query object
  *   @param SparqlEngineDb $engine   Sparql Engine to query the database
  *   @return mixed   HTML code
  */
 public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine)
 {
     $this->query = $query;
     $this->sg = $engine->getSqlGenerator();
     $strResultForm = $query->getResultForm();
     foreach ($this->getResultVars() as $var) {
         $ResultVarsTemp[] = substr($var, 1);
     }
     switch ($strResultForm) {
         case 'select':
         case 'select distinct':
             $results = $this->createFromRecords($arRecordSets, $strResultForm);
             $strCode = json_encode(array('head' => array('vars' => $ResultVarsTemp), 'results' => array('bindings' => $results)));
             //$strCode = str_replace(',{',','.PHP_EOL.'{',$strCode);
             break;
         case 'construct':
         case 'describe':
             throw new Exception('Construct and describe are not supported by the' . ' JSON renderer');
         case 'count':
         case 'ask':
             if (count($arRecordSets) > 1) {
                 throw new Exception('More than one result set for a ' . $strResultForm . ' query not supported by JSON Renderer');
             }
             $nCount = 0;
             $dbRecordSet = reset($arRecordSets);
             foreach ($dbRecordSet as $row) {
                 $nCount += intval($row['count']);
                 break;
             }
             if ($strResultForm == 'ask') {
                 $strcode = json_encode(array('boolean' => $nCount > 0));
             } else {
                 $strcode = json_encode(array('int' => $nCount));
             }
             break;
         default:
             throw new Exception('Error');
     }
     return $strCode;
 }
Example #6
0
 /**
  *   Converts the database results into nice HTML.
  *
  *   @param array $arRecordSets  Array of (possibly several) SQL query results.
  *   @param Query $query     SPARQL query object
  *   @param SparqlEngineDb $engine   Sparql Engine to query the database
  *   @return mixed   HTML code
  */
 public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine)
 {
     $this->query = $query;
     $this->sg = $engine->getSqlGenerator();
     $strCode = '';
     $strResultForm = $query->getResultForm();
     switch ($strResultForm) {
         case 'select':
         case 'select distinct':
             $strCode = $this->createTableFromRecords($arRecordSets);
             break;
         case 'construct':
         case 'describe':
             throw new Exception('Construct and describe are currently not supported by the' . ' HTML renderer');
         case 'count':
         case 'ask':
             if (count($arRecordSets) > 1) {
                 throw new Exception('More than one result set for a ' . $strResultForm . ' query!');
             }
             $nCount = 0;
             $dbRecordSet = reset($arRecordSets);
             foreach ($dbRecordSet as $row) {
                 $nCount += intval($row['count']);
                 break;
             }
             if ($strResultForm == 'ask') {
                 $strCode = 'There were results.';
             } else {
                 $strCode = 'There are ' . $nCount . ' results.';
             }
             break;
         default:
             throw new Exception('Unsupported result form: ' . $strResultForm);
     }
     return $this->wrapCode($strCode);
 }