From: <cw...@us...> - 2007-08-13 13:51:50
|
Revision: 512 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=512&view=rev Author: cweiske Date: 2007-08-13 06:51:30 -0700 (Mon, 13 Aug 2007) Log Message: ----------- - Add SimpleTest EARL [1] renderer - Add nesting option to N3Serializer so that we get [ x:a x:b [ x:c [x:d x:e]]] now [1] http://www.w3.org/2001/sw/DataAccess/tests/earl Modified Paths: -------------- trunk/rdfapi-php/api/syntax/N3Serializer.php trunk/rdfapi-php/test/config.php.dist trunk/rdfapi-php/test/unit/Sparql/SparqlParserTests_test.php trunk/rdfapi-php/test/unit/sparqlParserTests.php Added Paths: ----------- trunk/rdfapi-php/test/unit/EarlReporter.php Modified: trunk/rdfapi-php/api/syntax/N3Serializer.php =================================================================== --- trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 11:58:24 UTC (rev 511) +++ trunk/rdfapi-php/api/syntax/N3Serializer.php 2007-08-13 13:51:30 UTC (rev 512) @@ -36,7 +36,8 @@ var $debug = false; - var $prefixes; + var $prefixes = array(); + var $noPrefixes = array(); var $done; // keeps track of already serialized resources var $resourcetext; @@ -47,6 +48,7 @@ var $styleCompress = false; var $stylePretty = false; + var $styleNest = false; /** * Constructor @@ -66,9 +68,9 @@ * @param string $s * @returns void **/ - function addNSPrefix( $ns, $prefix) + function addNSPrefix($ns, $prefix) { - $this->prefixes[$ns]=$prefix; + $this->prefixes[$ns] = $prefix; } @@ -81,14 +83,38 @@ $this->prefixes = array(); } + + /** + * Add a namespace that shall not get shortened by using a prefix. + * + * @param string $ns Namespace URI like "http://example.com/" + */ + function addNoNSPrefix($ns) + { + $this->noPrefixes[$ns] = true; + } + + + + /** + * Clears all previously set noNamespace prefixes + */ + function clearNoNSPrefixes() + { + $this->noPrefixes = array(); + } + + + + /** * Serializes a model to N3 syntax. * * @param object Model $model * @return string * @access public */ - function & serialize(&$m) + function &serialize(&$m) { if (is_a($m, 'DbModel')) { $m=$m->getMemModel(); @@ -111,9 +137,9 @@ } } - $namespaces =array(); - $count =array(); - $resources =array(); + $namespaces = array(); + $count = array(); + $resources = array(); foreach ($this->model->triples as $t) { $s = $t->getSubject(); if (is_a($s, 'Resource')) { @@ -127,14 +153,20 @@ if (is_a($o, 'Resource')) { $namespaces[$o->getNamespace()] = 1; } - $uri=$s->getURI(); + $uri = $s->getURI(); if (isset($count[$uri])) { $count[$uri]++; } else { - $count[$uri]=0; - $resources[$uri]=$s; + $count[$uri] = 0; + $resources[$uri] = $s; } + + if ($this->styleNest && is_a($s, 'BlankNode')) { + //make sure blank nodes are sorted *after* normal nodes + //so that they can be included + $count[$uri] -= 0.00001; + } } if (!HIDE_ADVERTISE) { @@ -155,11 +187,11 @@ } $c = 0; - foreach ( $this->resourcetext as $r=>$t) { - if ( preg_match_all('/'.MAGIC_STRING.'([^ ]+)'.MAGIC_STRING.'/', $t, $ms, PREG_SET_ORDER)) { - foreach($ms as $mseach) { - $rp=$this->resourcetext[$mseach[1]]; - $t=preg_replace('/'.MAGIC_STRING.$mseach[1].MAGIC_STRING.'/', $rp, $t); + foreach ($this->resourcetext as $r => $t) { + if (preg_match_all('/'.MAGIC_STRING.'([^ ]+)'.MAGIC_STRING.'/', $t, $ms, PREG_SET_ORDER)) { + foreach ($ms as $mseach) { + $rp = $this->resourcetext[$mseach[1]]; + $t = preg_replace('/'.MAGIC_STRING.$mseach[1].MAGIC_STRING.'/', $rp, $t); } } @@ -188,7 +220,7 @@ // } return $this->res; - } + }//function &serialize(&$m) @@ -201,7 +233,7 @@ * @return boolean * @access public */ - function saveAs(&$model, $filename) + function saveAs(&$model, $filename) { // serialize model $n3 = $this->serialize($model); @@ -217,6 +249,8 @@ }; } + + /** * Set to true, if the N3 serializer should try to compress the blank node * syntax using [] whereever possible. @@ -236,6 +270,17 @@ $this->stylePretty = $prettyPrint; } + + + /** + * Enables nesting of blank nodes with [] if + * compression is activated via @see setCompress + */ + function setNest($nest) + { + $this->styleNest = $nest; + } + /* ==================== Private Methods from here ==================== */ @@ -247,14 +292,16 @@ **/ function reset() { - $this->anon=0; - $this->done=array(); - $this->resourcetext_taken=array(); - $this->resourcetext=array(); - $this->res=''; - $this->model=NULL; + $this->anon = 0; + $this->done = array(); + $this->resourcetext_taken = array(); + $this->resourcetext = array(); + $this->res = ''; + $this->model = null; } + + /** * Makes ns0, ns1 etc. prefixes for unknown prefixes. * Outputs @prefix lines. @@ -266,11 +313,11 @@ { $c = 0; foreach ($n as $ns => $nonsense) { - if (!$ns) { + if (!$ns || isset($this->noPrefixes[$ns])) { continue; } if (isset($this->prefixes[$ns])) { - $p=$this->prefixes[$ns]; + $p = $this->prefixes[$ns]; } else { $p = 'ns' . $c; $this->prefixes[$ns] = $p; @@ -280,29 +327,31 @@ } } + + /** * Fill in $resourcetext for a single resource. * Will recurse into Objects of triples, but should never look ? (really?) - * @access private * @param object Resource $r * @returns boolean + * @access private **/ - function doResource(&$r) + function doResource(&$r, $bEmbedded = false, $strIndent = ' ') { - // print $r->getURI(); + //var_dump($r->getURI()); - $ts=$this->model->find($r, null, null); - if (count($ts->triples)==0) { + $ts = $this->model->find($r, null, null); + if (count($ts->triples) == 0) { return; } $out = ''; if (isset($this->done[$r->getURI()]) && $this->done[$r->getURI()]) { - if (is_a($r, 'BlankNode')) { + if (!$this->styleNest && is_a($r, 'BlankNode')) { if ($this->resourcetext_taken[$r->getURI()] == 1) { //Oh bother, we must use the _:blah construct. - $a=$this->resourcetext[$r->getURI()]; + $a = $this->resourcetext[$r->getURI()]; $this->resourcetext[$r->getURI()]='_:anon'.$this->anon; $this->resourcetext['_:anon'.$this->anon]=$this->fixAnon($a, '_:anon'.$this->anon); $this->resourcetext_taken[$r->getURI()]=2; @@ -319,7 +368,11 @@ if (is_a($r, 'BlankNode')) { //test, if this blanknode is referenced somewhere $rbn = $this->model->find(null, null, $r); - $compress = count($rbn->triples) == 0 && (N3SER_BNODE_SHORT || $this->styleCompress); + $compress = (N3SER_BNODE_SHORT || $this->styleCompress) + && ( + count($rbn->triples) == 0 + || (count($rbn->triples) == 1 && $bEmbedded) + ); if ($compress) { $out.='['; } else { @@ -335,14 +388,14 @@ $out .= ' '; foreach ($ts->triples as $t) { - $p=$t->getPredicate(); + $p = $t->getPredicate(); if ($p == $lastp) { - $out.=' , '; + $out .= ' , '; } else { if ($lastp!='') { if ($this->stylePretty) { - $out .= ";\n "; + $out .= ";\n" . $strIndent; } else { $out .= ' ; '; } @@ -374,11 +427,15 @@ if ($this->debug) { print 'Doing object: '.$o->getURI().LINEFEED; } - if (is_a($o,'BlankNode')) { - // $this->doResource($o); - // $out.=MAGIC_STRING.$o->getURI().MAGIC_STRING; #$this->resourcetext[$o->getURI()]; - // $this->resourcetext_taken[$o->getURI()]=1; - $out .= '_:'.$o->getLabel(); + if (is_a($o, 'BlankNode')) { + if ($this->styleNest) { + $this->doResource($o, true, $strIndent . ' '); + $out .= MAGIC_STRING . $o->getURI() . MAGIC_STRING; + //$out .= $this->resourcetext[$o->getURI()]; + $this->resourcetext_taken[$o->getURI()] = 1; + } else { + $out .= '_:'.$o->getLabel(); + } } else { $this->doURI($o, $out); } @@ -399,8 +456,8 @@ /** * Format a single URI * @param string $s + * @return void * @access private - * @return void **/ function doURI(&$r, &$out) { @@ -408,11 +465,12 @@ $out .= 'a'; return; } - if ($r->getNamespace()!='') { - $out .= $this->prefixes[$r->getNamespace()].':'.$r->getLocalName(); + $ns = $r->getNamespace(); + if ($ns != '' && !isset($this->noPrefixes[$ns])) { + $out .= $this->prefixes[$ns].':'.$r->getLocalName(); } else { - //Will this ever happen? - $out .= $r->getURI(); + //Will this ever happen? It does, now. + $out .= '<' . $r->getURI() . '>'; } } @@ -427,9 +485,8 @@ **/ function fixAnon($t,$a) { - $t=preg_replace("/( \] $|^\[ )/", '', $t); - - return $a.$t; + $t = preg_replace("/( \] $|^\[ )/", '', $t); + return $a . $t; } } Modified: trunk/rdfapi-php/test/config.php.dist =================================================================== --- trunk/rdfapi-php/test/config.php.dist 2007-08-13 11:58:24 UTC (rev 511) +++ trunk/rdfapi-php/test/config.php.dist 2007-08-13 13:51:30 UTC (rev 512) @@ -18,8 +18,17 @@ 'password' => '' ); -//enable this to get more informatin about failing unit tests +//enable this to get more information about failing unit tests //$GLOBALS['debugTests'] = true; +//used in W3C earl report serialization +$GLOBALS['earlReport'] = array( + 'reporter' => array( + 'name' => 'John Doe', + 'seeAlso' => 'http://example.org/john/johndoe.rdf', + 'homepage' => 'http://example.org/john/' + ) +); + define('LOG', false); ?> \ No newline at end of file Added: trunk/rdfapi-php/test/unit/EarlReporter.php =================================================================== --- trunk/rdfapi-php/test/unit/EarlReporter.php (rev 0) +++ trunk/rdfapi-php/test/unit/EarlReporter.php 2007-08-13 13:51:30 UTC (rev 512) @@ -0,0 +1,231 @@ +<?php +/** +* EARL reporter for SimpleTest +* +* @author Christian Weiske <cw...@cw...> +* @see http://www.w3.org/2001/sw/DataAccess/tests/earl +*/ +require_once SIMPLETEST_INCLUDE_DIR . 'simpletest.php'; +require_once RDFAPI_INCLUDE_DIR . 'model/MemModel.php'; +require_once RDFAPI_INCLUDE_DIR . 'syntax/N3Serializer.php'; + + +class EarlReporter extends SimpleReporter +{ + const EARL = 'http://www.w3.org/ns/earl#'; + const FOAF = 'http://xmlns.com/foaf/0.1/'; + const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; + const RDFS = 'http://www.w3.org/2000/01/rdf-schema#'; + const DOAP = 'http://usefulinc.com/ns/doap#'; + + protected $model = null; + protected $serializer = null; + + /** + * Set via $testcase->signal('earl:test', 'mytestname') + */ + protected $currentTestName = null; + + + + public function __construct($serializer = null, $model = null) + { + if ($serializer !== null) { + $this->serializer = $serializer; + } else { + $this->serializer = new N3Serializer(); + } + + if ($model !== null) { + $this->model = model; + } else { + $this->model = new MemModel(); + } + + if (!isset($GLOBALS['earlReport'])) { + die('Please configure $GLOBALS[\'earlReport\'] as shown in config.php.dist' . "\n"); + } + }//public function __construct($serializer = null, $model = null) + + + + function paintHeader($test_name) + { + //add personal information about the asserter + $this->assertingPerson = new Resource($GLOBALS['earlReport']['reporter']['seeAlso'] . '#me'); + $this->model->add(new Statement( + $this->assertingPerson, + new Resource(self::RDF . 'type'), + new Resource(self::FOAF . 'Person') + )); + $this->model->add(new Statement( + $this->assertingPerson, + new Resource(self::RDFS . 'seeAlso'), + new Resource($GLOBALS['earlReport']['reporter']['seeAlso']) + )); + $this->model->add(new Statement( + $this->assertingPerson, + new Resource(self::FOAF . 'homepage'), + new Resource($GLOBALS['earlReport']['reporter']['homepage']) + )); + $this->model->add(new Statement( + $this->assertingPerson, + new Resource(self::FOAF . 'name'), + new Literal($GLOBALS['earlReport']['reporter']['name']) + )); + + + //project information + $this->project = new Resource('http://rdfapi-php.sf.net/'); + $this->model->add(new Statement( + $this->project, + new Resource(self::RDF . 'type'), + new Resource(self::DOAP . 'Project') + )); + $this->model->add(new Statement( + $this->project, + new Resource(self::DOAP . 'name'), + new Literal('RDF API for PHP') + )); + $version = new BlankNode($this->model); + $this->model->add(new Statement( + $this->project, + new Resource(self::DOAP . 'release'), + $version + )); + $this->model->add(new Statement( + $version, + new Resource(self::RDF . 'type'), + new Resource(self::DOAP . 'Version') + )); + $this->model->add(new Statement( + $version, + new Resource(self::DOAP . 'created'), + new Literal(date('Y-m-d H:i'), null, 'http://www.w3.org/2001/XMLSchema#date') + )); + $this->model->add(new Statement( + $version, + new Resource(self::DOAP . 'name'), + new Literal('RAP SVN-' . date('Y-m-d\\TH:i')) + )); + }//function paintHeader($test_name) + + + + function paintFooter($test_name) + { + $this->serializer->addNSPrefix(self::DOAP, 'doap'); + $this->serializer->addNSPrefix(self::EARL, 'earl'); + $this->serializer->addNSPrefix(self::FOAF, 'foaf'); + + $this->serializer->addNoNSPrefix('http://rdfapi-php.sf.net/'); + $this->serializer->addNoNSPrefix($GLOBALS['earlReport']['reporter']['homepage']); + + $this->serializer->setCompress(true); + $this->serializer->setPrettyPrint(true); + $this->serializer->setNest(true); + + echo $this->serializer->serialize( + $this->model + ); + } + + + + /** + * We use this to keep track of test titles + */ + function paintSignal($type, $payload) + { + switch ($type) { + case 'earl:name': + $this->currentTestName = $payload; + break; + default: + echo "Unknown signal type $type\n"; + break; + } + }//function paintSignal($type, $payload) + + + function paintStart($test_name, $size) { + parent::paintStart($test_name, $size); + } + + function paintEnd($test_name, $size) { + parent::paintEnd($test_name, $size); + } + + function paintPass($message) { + $this->addTest(true); +// echo 'pass: ' . $message . "\n"; + parent::paintPass($message); + } + + function paintFail($message) { + $this->addTest(false); +// echo 'fail: ' . $message . "\n"; + parent::paintFail($message); + } + + function addTest($bPass) + { + if ($this->currentTestName === null) { +// echo "No test name set! Ignoring test\n"; + return; + } + + $assertion = new BlankNode($this->model); + $this->model->add(new Statement( + $assertion, + new Resource(self::RDF . 'type'), + new Resource(self::EARL . 'Assertion') + )); + $this->model->add(new Statement( + $assertion, + new Resource(self::EARL . 'assertedBy'), + $this->assertingPerson + )); + + $result = new BlankNode($this->model); + $this->model->add(new Statement( + $assertion, + new Resource(self::EARL . 'result'), + $result + )); + $this->model->add(new Statement( + $result, + new Resource(self::RDF . 'type'), + new Resource(self::EARL . 'TestResult') + )); + $this->model->add(new Statement( + $result, + new Resource(self::EARL . 'outcome'), + new Resource(self::EARL . ($bPass ? 'pass' : 'fail')) + )); + $this->model->add(new Statement( + $assertion, + new Resource(self::EARL . 'subject'), + $this->project + )); + $this->model->add(new Statement( + $assertion, + new Resource(self::EARL . 'test'), + new Resource($this->currentTestName) + )); + +/* + [ a earl:Assertion; + earl:assertedBy _8:chime; + earl:result [ a earl:TestResult; + earl:outcome earl:pass]; + earl:subject <http://rdflib.net>; + earl:test _7:dawg-graph-02]. +*/ + + $this->currentTestName = null; + }//function addTest($bPass) + +}//class EarlReporter extends SimpleReporter + +?> \ No newline at end of file Modified: trunk/rdfapi-php/test/unit/Sparql/SparqlParserTests_test.php =================================================================== --- trunk/rdfapi-php/test/unit/Sparql/SparqlParserTests_test.php 2007-08-13 11:58:24 UTC (rev 511) +++ trunk/rdfapi-php/test/unit/Sparql/SparqlParserTests_test.php 2007-08-13 13:51:30 UTC (rev 512) @@ -97,7 +97,7 @@ function testParseFilter() { - echo "<b>FilterParser tests</b><br/>\n"; + //echo "<b>FilterParser tests</b><br/>\n"; foreach ($GLOBALS['testSparqlParserTestsFilter'] as $arFilterTest) { list($query, $result) = $arFilterTest; @@ -122,7 +122,7 @@ function testParseNested() { - echo "<b>Nested queries tests</b><br/>\n"; + //echo "<b>Nested queries tests</b><br/>\n"; foreach ($GLOBALS['testSparqlParserTestsNested'] as $arNestedTest) { list($query, $strExpected) = $arNestedTest; @@ -162,10 +162,13 @@ } continue; } + $this->signal('earl:name', $test['title']); $qs = file_get_contents(SPARQL_TESTFILES . $test['query']); $this->runQueryParseTest($qs, $parser, $test['type'], $test['title']); + + //++$nCount; if ($nCount > 2) break; } }//function testDawg2SyntaxTests() Modified: trunk/rdfapi-php/test/unit/sparqlParserTests.php =================================================================== --- trunk/rdfapi-php/test/unit/sparqlParserTests.php 2007-08-13 11:58:24 UTC (rev 511) +++ trunk/rdfapi-php/test/unit/sparqlParserTests.php 2007-08-13 13:51:30 UTC (rev 512) @@ -4,9 +4,10 @@ die('Make a copy of test/config.php.dist, change it and save it as test/config.php'); } -require_once( SIMPLETEST_INCLUDE_DIR . 'unit_tester.php'); -require_once( SIMPLETEST_INCLUDE_DIR . 'reporter.php'); -require_once('show_passes.php'); +require_once SIMPLETEST_INCLUDE_DIR . 'unit_tester.php'; +require_once SIMPLETEST_INCLUDE_DIR . 'reporter.php'; +require_once dirname(__FILE__) . '/EarlReporter.php'; +require_once 'show_passes.php'; require(RDFAPI_INCLUDE_DIR . 'RdfAPI.php'); $_SESSION['passes'] = 0; @@ -22,6 +23,7 @@ $test_sparql->addTestFile(RDFAPI_TEST_INCLUDE_DIR. 'test/unit/Sparql/SparqlParserTests_test.php'); //$test_sparql->run(new ShowPasses()); $test_sparql->run(new TextReporter()); +//$test_sparql->run(new EarlReporter()); if(LOG){ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |