|
From: <cw...@us...> - 2007-08-12 14:38:55
|
Revision: 488
http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=488&view=rev
Author: cweiske
Date: 2007-08-12 07:38:53 -0700 (Sun, 12 Aug 2007)
Log Message:
-----------
Dawg2 helper class and script
Added Paths:
-----------
trunk/rdfapi-php/test/unit/Sparql/Dawg2Helper.php
trunk/rdfapi-php/test/unit/Sparql/create-dawg2.php
Added: trunk/rdfapi-php/test/unit/Sparql/Dawg2Helper.php
===================================================================
--- trunk/rdfapi-php/test/unit/Sparql/Dawg2Helper.php (rev 0)
+++ trunk/rdfapi-php/test/unit/Sparql/Dawg2Helper.php 2007-08-12 14:38:53 UTC (rev 488)
@@ -0,0 +1,215 @@
+<?php
+
+require_once RDFAPI_INCLUDE_DIR . 'model/ModelFactory.php';
+require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlParser.php';
+require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngine.php';
+
+class Dawg2Helper
+{
+
+
+
+ /**
+ * Loads DAWG2 tests and adds them to the cases session array
+ * so that they can be executed with the normal tests
+ */
+ public static function loadDawg2Tests()
+ {
+ $suiteDir = dirname(__FILE__) . '/w3c-dawg2/data-r2/';
+ $suites = array(
+// 'manifest-evaluation.ttl',
+// 'extended-manifest-evaluation.ttl',
+ 'manifest-syntax.ttl'
+ );
+
+ $arTests = array();
+ foreach ($suites as $suite) {
+ $arTests = array_merge(
+ $arTests,
+ self::loadSuiteFromManifest($suiteDir . $suite)
+ );
+ }
+
+ //make relative paths
+ $strip = dirname(__FILE__) . '/';
+ foreach ($arTests as &$test) {
+ foreach ($test as $id => $value) {
+ if (substr($value, 0, strlen($strip)) == $strip) {
+ $test[$id] = substr($value, strlen($strip));
+ }
+ }
+ }
+
+ //activate it in cases.php
+ $_SESSION['sparql_dawg2_tests'] = $arTests;
+ $_SESSION['sparqlTestGroups'] = array();//remove other cases
+ $_SESSION['sparqlTestGroups']['dawg2'] = array(//'deact'=>1,
+ 'title' => 'DAWG2 tests',
+ 'tests' => 'sparql_dawg2_tests',
+ 'checkfunc' => 'resultCheck'
+ );
+
+ }//public static function loadDawg2Tests()
+
+
+
+ /**
+ * Loads all test collections from a test suite manifest file.
+ * A test suite file is an N3 rdf data file linking to other
+ * collection manifest files.
+ *
+ * @param string $strSuiteFile Test suite file
+ *
+ */
+ public static function loadSuiteFromManifest($strSuiteFile)
+ {
+ //if someone knows a proper way to select all rdf collection
+ //items with a single query, tell me
+ $res = self::queryN3($strSuiteFile,
+ 'PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>
+ SELECT ?title, ?file WHERE {
+ ?x rdfs:label ?title.
+ ?y rdf:first ?file.
+ }'
+ );
+
+ $arTests = array();
+ $dirname = dirname($strSuiteFile);
+ foreach ($res as $data) {
+ $file = $dirname . '/' . $data['?file']->uri;
+ $arTests = array_merge(
+ $arTests,
+ self::loadCollectionFromManifest($file)
+ );
+ }
+ return $arTests;
+ }//public static function loadSuiteFromManifest($strSuiteFile)
+
+
+
+ /**
+ * Loads tests from a test collection manifest file.
+ * A collection manifest contains only tests, no links to other
+ * manifest files.
+ *
+ * @param string $strCollectionFile Path of the collection file
+ *
+ * @return array Array of test definitions compatible to those in
+ * cases.php
+ */
+ public static function loadCollectionFromManifest($strCollectionFile)
+ {
+ if (!file_exists($strCollectionFile)) {
+ throw new Exception('Collection file does not exist: ' . $strCollectionFile);
+ }
+
+ //$arTests = self::loadEvaluationTests($strCollectionFile);
+ $arTests = array();
+ $arTests = array_merge($arTests, self::loadSyntaxTests($strCollectionFile));
+ return $arTests;
+ }//public static function loadCollectionFromManifest($strCollectionFile)
+
+
+
+ public static function loadEvaluationTests($strCollectionFile)
+ {
+ $res = self::queryN3($strCollectionFile,
+ 'PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>
+ PREFIX qt: <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>
+ PREFIX dawgt: <http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#>
+ SELECT ?name ?queryFile ?dataFile ?resultFile WHERE {
+ ?test rdf:type mf:QueryEvaluationTest.
+ ?test mf:name ?name.
+ ?test dawgt:approval dawgt:Approved.
+ ?test mf:action _:action.
+ _:action qt:query ?queryFile.
+ _:action qt:data ?dataFile.
+ ?test mf:result ?resultFile.
+ }'
+ );
+
+ $dirname = dirname($strCollectionFile) . '/';
+ $arTests = array();
+
+ //this is a bug in SparqlEngine and should be fixed
+ if ($res === false) {
+ return array();
+ }
+
+ foreach ($res as $test) {
+ $arTests[] = array(
+ 'title' => $test['?name']->label,
+ 'data' => $dirname . $test['?dataFile']->uri,
+ 'query' => $dirname . $test['?queryFile']->uri,
+ 'result' => $dirname . $test['?resultFile']->uri,
+ );
+ }
+
+ return $arTests;
+ }//public static function loadEvaluationTests($strCollectionFile)
+
+
+
+ public static function loadSyntaxTests($strCollectionFile)
+ {
+ $res = self::queryN3($strCollectionFile,
+ 'PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>
+ PREFIX qt: <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>
+ PREFIX dawgt: <http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#>
+ SELECT ?name ?queryFile ?type WHERE {
+ ?test rdf:type ?type.
+ ?test mf:name ?name.
+ ?test dawgt:approval dawgt:Approved.
+ ?test mf:action ?queryFile.
+ FILTER(?type = mf:NegativeSyntaxTest || ?type = mf:PositiveSyntaxTest)
+ }'
+ );
+
+ $dirname = dirname($strCollectionFile) . '/';
+ $arTests = array();
+
+ //this is a bug in SparqlEngine and should be fixed
+ if ($res === false) {
+ return array();
+ }
+ foreach ($res as $test) {
+ if ($test['?type']->uri == 'http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#PositiveSyntaxTest') {
+ $type = 'syntax-positive';
+ } else {
+ $type = 'syntax-negative';
+ }
+ $arTests[] = array(
+ 'title' => $test['?name']->label,
+ 'query' => $dirname . $test['?queryFile']->uri,
+ 'type' => $type
+ );
+ }
+
+ return $arTests;
+ }//public static function loadEvaluationTests($strCollectionFile)
+
+
+
+ /**
+ * 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);
+ }//public static function queryN3($strN3File, $strSparqlQuery)
+}
+
+?>
\ No newline at end of file
Added: trunk/rdfapi-php/test/unit/Sparql/create-dawg2.php
===================================================================
--- trunk/rdfapi-php/test/unit/Sparql/create-dawg2.php (rev 0)
+++ trunk/rdfapi-php/test/unit/Sparql/create-dawg2.php 2007-08-12 14:38:53 UTC (rev 488)
@@ -0,0 +1,24 @@
+<?php
+/**
+* Utility script to generate an array of dawg2 tests similar to
+* cases.php from the n3 tests notation.
+* We could read the n3 test definition files everytime we run the tests,
+* but this takes too long - so we cache it.
+*
+* Usage:
+* php create-dawg2.php > cases_dawg2.php
+*/
+require_once dirname(__FILE__) . '/../../config.php';
+require_once 'Dawg2Helper.php';
+
+Dawg2Helper::loadDawg2Tests();
+
+echo "<?php
+/**
+* automatically created by create-dawg2.php
+*/
+\$_SESSION['sparql_dawg2_tests'] = ";
+var_export($_SESSION['sparql_dawg2_tests']);
+echo ";
+?>";
+?>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|