[64] | 1 | <?PHP
|
---|
| 2 |
|
---|
| 3 | /* RDFInt.php - RDF Interfaces for PHP
|
---|
| 4 | * Copyright 2011 netlabs.org
|
---|
| 5 | * Author: Christian Langanke, Adrian Gschwend
|
---|
| 6 | *
|
---|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 8 | * you may not use this file except in compliance with the License.
|
---|
| 9 | * You may obtain a copy of the License at
|
---|
| 10 | *
|
---|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 12 | *
|
---|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 16 | * See the License for the specific language governing permissions and
|
---|
| 17 | * limitations under the License.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | // load library setup
|
---|
[67] | 21 | if (!defined( 'RDFINT'))
|
---|
| 22 | define( 'RDFINT', getenv( 'PHP_LIBROOT_RDFINT'));
|
---|
[64] | 23 | require_once( RDFINT.'/rdfa.php');
|
---|
| 24 |
|
---|
| 25 | class RDFTestCasesManifest {
|
---|
| 26 |
|
---|
| 27 | private $rdfDataManifest;
|
---|
[65] | 28 | private $exceptions;
|
---|
[64] | 29 |
|
---|
| 30 | // --------------------------------------------------------
|
---|
| 31 |
|
---|
| 32 | public function __construct() {
|
---|
| 33 |
|
---|
| 34 | $remoteManifest = 'http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf';
|
---|
| 35 |
|
---|
| 36 | $aNamespace = array( 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
|
---|
| 37 | 'test' => 'http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#',
|
---|
| 38 | 'case' => 'http://www.w3.org/2000/10/rdf-tests/rdfcore/');
|
---|
| 39 |
|
---|
[67] | 40 |
|
---|
| 41 | // determine location of local cache file for manifest
|
---|
| 42 | $tmpdir = getenv('tmp');
|
---|
| 43 | if ($tmpdir == '')
|
---|
| 44 | $tmpdir = '/tmp';
|
---|
| 45 | $localManifest = $tmpdir.'/RDFTestcases_Manifest.rdf';
|
---|
| 46 |
|
---|
[64] | 47 | // cache manifest file first
|
---|
| 48 | if (!file_exists( $localManifest)) {
|
---|
| 49 | $data = file_get_contents( $remoteManifest);
|
---|
| 50 | file_put_contents( $localManifest, $data );
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // load RDF data
|
---|
| 54 | $this->rdfDataManifest = new \rdfa\Data();
|
---|
| 55 | foreach ( $aNamespace as $prefix => $uriNamespace) {
|
---|
| 56 | $this->rdfDataManifest->setMapping( $prefix, $uriNamespace);
|
---|
| 57 | }
|
---|
| 58 | $result = $this->rdfDataManifest->parse( $localManifest);
|
---|
| 59 |
|
---|
[65] | 60 | // load exception list
|
---|
| 61 | $this->exceptions = explode( "\r\n", file_get_contents( 'exception.lst'));
|
---|
| 62 |
|
---|
[64] | 63 | } // public function __construct
|
---|
| 64 |
|
---|
| 65 | // --------------------------------------------------------
|
---|
| 66 |
|
---|
| 67 | private function _caseNameFromSubject( $uriTestcase) {
|
---|
| 68 | $nameParts = explode( ':', $uriTestcase);
|
---|
| 69 | $name = str_replace( 'Manifest.rdf#', '', $nameParts[1]);
|
---|
| 70 | return $name;
|
---|
| 71 |
|
---|
| 72 | } // private function _caseNameFromSubject
|
---|
| 73 |
|
---|
| 74 | // --------------------------------------------------------
|
---|
| 75 |
|
---|
| 76 | // helper: get projections on approved testcases
|
---|
| 77 | private function _searchApprovedTestcases( $category) {
|
---|
| 78 |
|
---|
| 79 | // determine data
|
---|
| 80 | $templateTestcase = array( 'input' => 'test:inputDocument',
|
---|
| 81 | 'output' => 'test:outputDocument',
|
---|
| 82 | 'conclusion' => 'test:conclusionDocument');
|
---|
| 83 |
|
---|
| 84 | // assemble query and search
|
---|
| 85 | $query[ 'test:status'] = 'APPROVED';
|
---|
| 86 | $query[ 'rdf:type'] = "test:$category";
|
---|
| 87 | $testcases = $this->rdfDataManifest->query( $query, $templateTestcase);
|
---|
| 88 |
|
---|
| 89 | // create name attribute from URI
|
---|
[65] | 90 | // unshring URI values
|
---|
[64] | 91 | foreach ($testcases as $testcase) {
|
---|
| 92 | $testcase->name = $this->_caseNameFromSubject( $testcase->getSubject());
|
---|
[67] | 93 |
|
---|
| 94 | if (isset( $testcase->input))
|
---|
| 95 | $testcase->input = $this->rdfDataManifest->_resolve( $testcase->input);
|
---|
| 96 |
|
---|
| 97 | if (isset( $testcase->output))
|
---|
| 98 | $testcase->output = $this->rdfDataManifest->_resolve( $testcase->output);
|
---|
| 99 |
|
---|
| 100 | if (isset( $testcase->conclusion))
|
---|
| 101 | $testcase->conclusion = $this->rdfDataManifest->_resolve( $testcase->conclusion);
|
---|
[64] | 102 | }
|
---|
| 103 |
|
---|
| 104 | return $testcases;
|
---|
| 105 |
|
---|
| 106 | } // private function _searchApprovedTestcases
|
---|
| 107 |
|
---|
| 108 | // --------------------------------------------------------
|
---|
| 109 |
|
---|
| 110 | // helper: determine names or count of approved testcases for category
|
---|
| 111 | // NOTE: for better performance we use getSubjects() instead of query(),
|
---|
| 112 | // and check for approval in a further step
|
---|
| 113 |
|
---|
| 114 | // helper: get subjects of approved testcases
|
---|
| 115 | private function _getApprovedTestcaseSubjects( $category) {
|
---|
| 116 |
|
---|
| 117 | $uriTestcases = $this->rdfDataManifest->getSubjects( 'rdf:type', "test:$category");
|
---|
| 118 | $count = 0;
|
---|
| 119 | $testcases = array();
|
---|
| 120 | foreach ( $uriTestcases as $uriTestcase) {
|
---|
[65] | 121 |
|
---|
| 122 | // don't execute if in exception list
|
---|
| 123 | if (array_search( $this->_caseNameFromSubject( $uriTestcase), $this->exceptions) !== false)
|
---|
| 124 | continue;
|
---|
| 125 |
|
---|
| 126 |
|
---|
[64] | 127 | if ($this->rdfDataManifest->_getFirstValue( $uriTestcase, 'test:status') == 'APPROVED')
|
---|
| 128 | $testcases[] = $uriTestcase;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | return $testcases;
|
---|
| 132 |
|
---|
[65] | 133 | } // private function _getApprovedTestcaseSubjects
|
---|
[64] | 134 |
|
---|
| 135 |
|
---|
| 136 | private function _countApprovedTestcases( $category) {
|
---|
| 137 |
|
---|
[65] | 138 | return count( $this->_getApprovedTestcaseSubjects( $category));
|
---|
[64] | 139 |
|
---|
| 140 | } // private function _countApprovedTestcases
|
---|
| 141 |
|
---|
| 142 | // --------------------------------------------------------
|
---|
| 143 |
|
---|
| 144 | public function getParserTests( $category) {
|
---|
| 145 |
|
---|
| 146 | return $this->_searchApprovedTestcases( $category);
|
---|
| 147 |
|
---|
| 148 | } // public function getParserTests
|
---|
| 149 |
|
---|
| 150 | // --------------------------------------------------------
|
---|
| 151 |
|
---|
| 152 | public function getCategories() {
|
---|
| 153 |
|
---|
| 154 | // all categories - for later use
|
---|
| 155 | $validTpes = array( 'PositiveParserTest',
|
---|
| 156 | 'NegativeParserTest',
|
---|
| 157 | 'PositiveEntailmentTest',
|
---|
| 158 | 'NegativeEntailmentTest',
|
---|
| 159 | 'MiscellaneousTest');
|
---|
| 160 |
|
---|
| 161 | $validTpes = array( 'PositiveParserTest');
|
---|
| 162 |
|
---|
| 163 | return $validTpes;
|
---|
| 164 |
|
---|
| 165 | } // public function getCategories
|
---|
| 166 |
|
---|
| 167 | // --------------------------------------------------------
|
---|
| 168 |
|
---|
| 169 | public function listCases( $category = Null, $verbose = false) {
|
---|
| 170 |
|
---|
| 171 | // use all categories by default
|
---|
| 172 | if (($category == Null) || ($category == ''))
|
---|
| 173 | $categories = $this->getCategories();
|
---|
| 174 | else
|
---|
| 175 | $categories = array( $category);
|
---|
| 176 |
|
---|
| 177 | // now list cases
|
---|
| 178 | foreach ($categories as $category) {
|
---|
| 179 | if ($verbose) {
|
---|
| 180 | echo " $category\n";
|
---|
| 181 | $uriTestcases = $this->_getApprovedTestcaseSubjects( $category);
|
---|
| 182 | $count = count( $uriTestcases);
|
---|
| 183 | foreach ($uriTestcases as $uriTestcase) {
|
---|
| 184 | echo " " . $this->_caseNameFromSubject( $uriTestcase) . "\n";
|
---|
| 185 | }
|
---|
| 186 | echo "$count cases for: $category\n\n";
|
---|
| 187 | } else {
|
---|
| 188 | $count = $this->_countApprovedTestcases( $category);
|
---|
| 189 | echo " $category $count cases\n";
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | } // public function listCases
|
---|
| 194 |
|
---|
| 195 | } // class UnitTest
|
---|
| 196 |
|
---|
| 197 |
|
---|