source: php/trunk/unittest/rdftestcases/rdftestcases.php@ 64

Last change on this file since 64 was 64, checked in by cla, 14 years ago

rdfint

  • added code to read list of RDF testcases from RDF data
  • TODO: execution of testcases!
  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
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
21define( 'RDFINT', getenv( 'PHP_LIBROOT_RDFINT'));
22require_once( RDFINT.'/rdfa.php');
23
24class RDFTestCasesManifest {
25
26 private $rdfDataManifest;
27
28 // --------------------------------------------------------
29
30 public function __construct() {
31
32 $remoteManifest = 'http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf';
33 $localManifest = 'Manifest.rdf';
34
35 $aNamespace = array( 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
36 'test' => 'http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#',
37 'case' => 'http://www.w3.org/2000/10/rdf-tests/rdfcore/');
38
39 // cache manifest file first
40 if (!file_exists( $localManifest)) {
41 $data = file_get_contents( $remoteManifest);
42 file_put_contents( $localManifest, $data );
43 }
44
45 // load RDF data
46 $this->rdfDataManifest = new \rdfa\Data();
47 foreach ( $aNamespace as $prefix => $uriNamespace) {
48 $this->rdfDataManifest->setMapping( $prefix, $uriNamespace);
49 }
50 $result = $this->rdfDataManifest->parse( $localManifest);
51
52 } // public function __construct
53
54 // --------------------------------------------------------
55
56 private function _caseNameFromSubject( $uriTestcase) {
57 $nameParts = explode( ':', $uriTestcase);
58 $name = str_replace( 'Manifest.rdf#', '', $nameParts[1]);
59 return $name;
60
61 } // private function _caseNameFromSubject
62
63 // --------------------------------------------------------
64
65 // helper: get projections on approved testcases
66 private function _searchApprovedTestcases( $category) {
67
68 // determine data
69 $templateTestcase = array( 'input' => 'test:inputDocument',
70 'output' => 'test:outputDocument',
71 'conclusion' => 'test:conclusionDocument');
72
73 // assemble query and search
74 $query[ 'test:status'] = 'APPROVED';
75 $query[ 'rdf:type'] = "test:$category";
76 $testcases = $this->rdfDataManifest->query( $query, $templateTestcase);
77
78 // create name attribute from URI
79 foreach ($testcases as $testcase) {
80 $testcase->name = $this->_caseNameFromSubject( $testcase->getSubject());
81 }
82
83 return $testcases;
84
85 } // private function _searchApprovedTestcases
86
87 // --------------------------------------------------------
88
89 // helper: determine names or count of approved testcases for category
90 // NOTE: for better performance we use getSubjects() instead of query(),
91 // and check for approval in a further step
92
93 // helper: get subjects of approved testcases
94 private function _getApprovedTestcaseSubjects( $category) {
95
96 $uriTestcases = $this->rdfDataManifest->getSubjects( 'rdf:type', "test:$category");
97 $count = 0;
98 $testcases = array();
99 foreach ( $uriTestcases as $uriTestcase) {
100 if ($this->rdfDataManifest->_getFirstValue( $uriTestcase, 'test:status') == 'APPROVED')
101 $testcases[] = $uriTestcase;
102 }
103
104 return $testcases;
105
106 } // private function _searchApprovedTestcases
107
108
109 private function _countApprovedTestcases( $category) {
110
111 $uriTestcases = $this->rdfDataManifest->getSubjects( 'rdf:type', "test:$category");
112 $count = 0;
113 foreach ( $uriTestcases as $uriTestcase) {
114 if ($this->rdfDataManifest->_getFirstValue( $uriTestcase, 'test:status') == 'APPROVED')
115 $count += 1;
116 }
117
118 return $count;
119
120 } // private function _countApprovedTestcases
121
122 // --------------------------------------------------------
123
124 public function getParserTests( $category) {
125
126 return $this->_searchApprovedTestcases( $category);
127
128 } // public function getParserTests
129
130 // --------------------------------------------------------
131
132 public function getCategories() {
133
134 // all categories - for later use
135 $validTpes = array( 'PositiveParserTest',
136 'NegativeParserTest',
137 'PositiveEntailmentTest',
138 'NegativeEntailmentTest',
139 'MiscellaneousTest');
140
141 $validTpes = array( 'PositiveParserTest');
142
143 return $validTpes;
144
145 } // public function getCategories
146
147 // --------------------------------------------------------
148
149 public function listCases( $category = Null, $verbose = false) {
150
151 // use all categories by default
152 if (($category == Null) || ($category == ''))
153 $categories = $this->getCategories();
154 else
155 $categories = array( $category);
156
157 // now list cases
158 echo "Categories of RDF Test Cases:\n";
159 foreach ($categories as $category) {
160 if ($verbose) {
161 echo " $category\n";
162 $uriTestcases = $this->_getApprovedTestcaseSubjects( $category);
163 $count = count( $uriTestcases);
164 foreach ($uriTestcases as $uriTestcase) {
165 echo " " . $this->_caseNameFromSubject( $uriTestcase) . "\n";
166 }
167 echo "$count cases for: $category\n\n";
168
169 } else {
170 $count = $this->_countApprovedTestcases( $category);
171 echo " $category $count cases\n";
172 }
173
174
175 }
176
177 } // public function listCases
178
179} // class UnitTest
180
181
Note: See TracBrowser for help on using the repository browser.