예제 #1
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);
 }
예제 #2
0
파일: Dataset.php 프로젝트: komagata/plnet
 /**
  * Performs a SPARQL query against an RDF Dataset. 
  * The result can be retrived in SPARQL Query Results XML Format or
  * as an array containing the variables an their bindings.
  *
  * @param  String $query      the sparql query string
  * @param  String $resultform the result form ('xml' for SPARQL Query Results XML Format)
  * @return String/array       
  */
 function sparqlQuery($query, $resultform = false)
 {
     include_once RDFAPI_INCLUDE_DIR . PACKAGE_SPARQL;
     $parser = new SparqlParser();
     $q = $parser->parse($query);
     $eng = new SparqlEngine();
     return $eng->queryModel($this, $q, $resultform);
 }
예제 #3
0
파일: XML.php 프로젝트: p4535992/programate
 /**
  *   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 XML result
  */
 public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
 {
     $this->query = $query;
     $this->engine = $engine;
     $this->dataset = $engine->getDataset();
     if ($arVartable instanceof NamedGraphMem) {
         return $arVartable->writeRdfToString();
     }
     $result = '<sparql xmlns="http://www.w3.org/2005/sparql-results#">';
     $header = '<head>';
     // build header
     if (is_array($arVartable)) {
         $vars = $this->query->getResultVars();
         $header = '<head>';
         foreach ($vars as $value) {
             $header = $header . '<variable name="' . substr($value, 1) . '"/>';
         }
         $header = $header . '</head>';
         // build results
         $solm = $this->query->getSolutionModifier();
         $sel = $this->query->getResultForm();
         $distinct = 'false';
         if ($sel == 'select distinct') {
             $distinct = 'true';
         }
         $ordered = 'false';
         if ($solm['order by'] != 0) {
             $ordered = 'true';
         }
         $results = '<results ordered="' . $ordered . '" distinct="' . $distinct . '">';
         foreach ($arVartable as $value) {
             $results = $results . '<result>';
             foreach ($value as $varname => $varvalue) {
                 $results = $results . $this->_getBindingString(substr($varname, 1), $varvalue);
             }
             $results = $results . '</result>';
         }
         $results = $results . '</results>';
     } else {
         $results = '</head><boolean>' . $vartable . '</boolean>';
     }
     $result = $result . $header . $results . '</sparql>';
     $result = simplexml_load_string($result);
     return $result->asXML();
 }
예제 #4
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 mixed               Most likely an array
  */
 public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
 {
     $this->query = $query;
     $this->engine = $engine;
     $this->dataset = $engine->getDataset();
     $result = false;
     $qrf = $this->query->getResultForm();
     if ($arVartable != null) {
         switch ($qrf) {
             case 'ask':
                 if (count($arVartable) > 0) {
                     $result = true;
                 } else {
                     $result = false;
                 }
                 break;
             case 'count':
                 $result = count($arVartable);
                 break;
             case 'construct':
                 $result = $this->constructGraph($arVartable, $this->query->getConstructPattern());
                 break;
             case 'describe':
                 $result = $this->describeGraph($arVartable);
                 break;
             default:
                 $result = $arVartable;
                 break;
         }
     } else {
         if ($qrf == 'describe') {
             $result = $this->describeGraph(null);
         } else {
             if ($qrf == 'construct') {
                 $result = $this->constructGraph(false, $this->query->getConstructPattern());
             }
         }
     }
     return $result;
 }
예제 #5
0
 /**
  *   Converts a MemModel into a query result array.
  *   Required for the DAWG test cases.
  *
  *   @param Model $model Model object to extract data from
  *   @return array Result array
  *
  *   @see http://www.w3.org/2001/sw/DataAccess/tests/README
  *   @see http://www.w3.org/2001/sw/DataAccess/tests/result-set.n3
  */
 static function convertModelToResultArray($model)
 {
     $graphset = ModelFactory::getDatasetMem('Dataset1');
     $graphset->setDefaultGraph($model);
     $parser = new SparqlParser();
     $engine = SparqlEngine::factory($model);
     $strSparqlQuery = '
         PREFIX rs: <http://www.w3.org/2001/sw/DataAccess/tests/result-set#>
         SELECT ?varname WHERE { ?x rs:resultVariable ?varname }
     ';
     $q = $parser->parse($strSparqlQuery);
     $variables = $engine->queryModel($graphset, $q, false);
     $arVars = array();
     $strSparqlQueryPart = '';
     $nCount = 0;
     foreach ($variables as $var) {
         $varname = '?' . $var['?varname']->label;
         $name = substr($varname, 1);
         $arVars[] = $varname;
         $strSparqlQueryPart .= ' ?thing rs:binding ?binding' . $nCount . '.
              ?binding' . $nCount . ' rs:value ' . $varname . '.
              ?binding' . $nCount . ' rs:variable "' . $name . '".';
         ++$nCount;
     }
     $strSparqlQuery = '
         PREFIX rs: <http://www.w3.org/2001/sw/DataAccess/tests/result-set#>
         SELECT ' . implode($arVars, ' ') . '
         WHERE {
             ?some rs:solution ?thing.
            ' . $strSparqlQueryPart . '
         }
     ';
     //echo $strSparqlQuery;
     $q = $parser->parse($strSparqlQuery);
     $arResult = $engine->queryModel($graphset, $q, false);
     //var_dump($arResult);
     //die();
     return $arResult;
 }
예제 #6
0
 /**
  *   Prepares everything for SparqlEngine-usage
  *   Loads the files, creates instances for SparqlEngine and
  *   Dataset...
  *
  *   @return array First value is the sparql engine, second the dataset
  */
 function _prepareSparql()
 {
     require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngine.php';
     require_once RDFAPI_INCLUDE_DIR . 'dataset/DatasetMem.php';
     $dataset = new DatasetMem();
     $dataset->setDefaultGraph($this);
     return array(SparqlEngine::factory($this), $dataset);
 }
예제 #7
0
 function testArqTestcases()
 {
     foreach ($_SESSION['sparql_arq_tests'] as $name) {
         $_SESSION['test'] = $name['query'] . " test";
         $parser = new SparqlParser();
         $graphset = ModelFactory::getDatasetMem('Dataset1');
         $def = $graphset->getDefaultGraph();
         $def->load(SPARQL_TESTFILES . 'data/' . $name['data']);
         $qs = file_get_contents(SPARQL_TESTFILES . 'query/' . $name['query'] . ".rq", 'r');
         $res = file_get_contents(SPARQL_TESTFILES . 'result/' . $name['result'] . ".res", 'r');
         eval($res);
         $q = $parser->parse($qs);
         $engine = SparqlEngine::factory();
         $t = $engine->queryModel($graphset, $q, false);
         if ($t instanceof MemModel) {
             $bOk = $t->equals($result);
         } else {
             $bOk = SparqlTestHelper::resultCheck($t, $result);
         }
         $this->assertTrue($bOk);
         if (!$bOk) {
             echo $name['query'] . "\n";
         }
     }
 }
예제 #8
0
 /**
  *   Executes a SPARQL query on the data written in an
  *   file containing N3-formatted RDF data
  *
  *   @param string $strN3File      Path to file
  *   @param string $strSparqlQuery SPARQL query to execute
  *
  *   @return mixed SPARQL engine query results
  */
 public static function queryN3($strN3File, $strSparqlQuery)
 {
     $graphset = ModelFactory::getDatasetMem('Dataset1');
     $graph1 = $graphset->getDefaultGraph();
     $graph1->load($strN3File, 'n3');
     $parser = new SparqlParser();
     $q = $parser->parse($strSparqlQuery);
     $engine = SparqlEngine::factory();
     return $engine->queryModel($graphset, $q, false);
 }
예제 #9
0
파일: Model.php 프로젝트: komagata/plnet
 /**
  * Performs a SPARQL query against a model. The model is converted to
  * an RDF Dataset. The result can be retrived in SPARQL Query Results XML Format or
  * as an array containing the variables an their bindings.
  *
  * @param  String $query      the sparql query string
  * @param  String $resultform the result form ('xml' for SPARQL Query Results XML Format)
  * @return String/array       
  */
 function sparqlQuery($query, $resultform = false)
 {
     include_once RDFAPI_INCLUDE_DIR . PACKAGE_SPARQL;
     include_once RDFAPI_INCLUDE_DIR . PACKAGE_DATASET;
     $dataset = new DatasetMem();
     $dataset->setDefaultGraph($this);
     $parser = new SparqlParser();
     $q = $parser->parse($query);
     $eng = new SparqlEngine();
     return $eng->queryModel($dataset, $q, $resultform);
 }