Esempio n. 1
0
 /**
  * tests operations on defaultgraph
  */
 function testDefOperations()
 {
     $_SESSION['test'] = 'Default graph operations tests';
     $graphset = ModelFactory::getDatasetMem('NGSet1');
     $st1 = new Statement(new Resource('http://subject1'), new Resource('http://predicate1'), new Resource('http://object1'));
     $st2 = new Statement(new Resource('http://subject2'), new Resource('http://predicate2'), new Literal('http://object2'));
     $this->assertTrue($graphset->hasDefaultGraph());
     $defaultGraph =& $graphset->getDefaultGraph();
     $this->assertEqual($defaultGraph->size(), 0);
     $defaultGraph->add($st1);
     $graph2 = new NamedGraphMem('http://graph2');
     $graph2->add($st2);
     $graphset->setDefaultGraph($graph2);
     $defaultGraph2 =& $graphset->getDefaultGraph();
     $this->assertFalse($defaultGraph2->contains($st1));
     $this->assertTrue($defaultGraph2->contains($st2));
     $this->assertEqual($defaultGraph2->size(), 1);
 }
Esempio n. 2
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";
         }
     }
 }
Esempio n. 3
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;
 }
Esempio n. 4
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);
 }