KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > test > TestRDFSReasoners


1 /******************************************************************
2  * File: TestRDFSReasoner.java
3  * Created by: Dave Reynolds
4  * Created on: 19-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestRDFSReasoners.java,v 1.14 2005/02/21 12:18:16 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.test;
11
12 //import com.hp.hpl.jena.reasoner.rdfsReasoner1.*;
13
import com.hp.hpl.jena.reasoner.rulesys.RDFSRuleReasonerFactory;
14 import com.hp.hpl.jena.reasoner.rulesys.RDFSFBRuleReasonerFactory;
15 import com.hp.hpl.jena.reasoner.*;
16 import com.hp.hpl.jena.rdf.model.*;
17 import com.hp.hpl.jena.vocabulary.*;
18
19 import java.io.*;
20 import java.util.Iterator JavaDoc;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29  * Test the set of admissable RDFS reasoners.
30  *
31  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
32  * @version $Revision: 1.14 $ on $Date: 2005/02/21 12:18:16 $
33  */

34 public class TestRDFSReasoners extends ReasonerTestBase {
35     
36     /** Base URI for the test names */
37     public static final String JavaDoc NAMESPACE = "http://www.hpl.hp.com/semweb/2003/query_tester/";
38     
39     protected static Log logger = LogFactory.getLog(TestReasoners.class);
40
41     /**
42      * Boilerplate for junit
43      */

44     public TestRDFSReasoners( String JavaDoc name ) {
45         super( name );
46     }
47     
48     /**
49      * Boilerplate for junit.
50      * This is its own test suite
51      */

52     public static TestSuite suite() {
53         TestSuite suite = new TestSuite();
54         try {
55             // Even though it is deprecated, maintain the tests for now
56
// constructRDFWGtests(suite, RDFSReasonerFactory.theInstance(), null);
57
// constructQuerytests(suite, "rdfs/manifest.rdf", RDFSReasonerFactory.theInstance(), null);
58

59             // FB reasoner doesn't support validation so the full set of wg tests are
60
// commented out
61
// constructRDFWGtests(suite, RDFSFBRuleReasonerFactory.theInstance(), null);
62
constructQuerytests(suite, "rdfs/manifest-nodirect-noresource.rdf", RDFSFBRuleReasonerFactory.theInstance(), null);
63             
64             Resource config = newResource().addProperty(ReasonerVocabulary.PROPenableCMPScan, true);
65 // config.addProperty(ReasonerVocabulary.PROPtraceOn, true);
66
constructRDFWGtests(suite, RDFSRuleReasonerFactory.theInstance(), null);
67             constructQuerytests(suite, "rdfs/manifest-standard.rdf", RDFSRuleReasonerFactory.theInstance(), config);
68             
69             suite.addTest(new TestRDFSMisc(RDFSRuleReasonerFactory.theInstance(), null));
70
71             Resource configFull = newResource().addProperty(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_FULL);
72             constructQuerytests(suite, "rdfs/manifest.rdf", RDFSRuleReasonerFactory.theInstance(), configFull);
73             
74             // This test was needed for the brief time the rdfs12 rules might have been in the standard
75
// That's no longer true but left comment out because we might want them for OWL someday
76
// constructQuerytests(suite, "rdfs/manifest-rdfs12.rdf", RDFSRuleReasonerFactory.theInstance(), configFull);
77

78             Resource configSimple = newResource().addProperty(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_SIMPLE);
79             constructQuerytests(suite, "rdfs/manifest-simple.rdf", RDFSRuleReasonerFactory.theInstance(), configSimple);
80
81             // Single test case used in debugging, subsumed by above
82
// constructSingleQuerytests(suite,
83
// "rdfs/manifest.rdf",
84
// "http://www.hpl.hp.com/semweb/2003/query_tester/rdfs/test13",
85
// RDFSRuleReasonerFactory.theInstance(),
86
// configFull);
87

88         } catch (IOException e) {
89             // failed to even built the test harness
90
logger.error("Failed to construct RDFS test harness", e);
91         }
92         return suite;
93     }
94     
95     /**
96      * Build a single named query test
97      */

98     private static void constructSingleQuerytests(TestSuite suite, String JavaDoc manifest, String JavaDoc test, ReasonerFactory rf, Resource config) throws IOException {
99         ReasonerTester tester = new ReasonerTester(manifest);
100         Reasoner r = rf.create(config);
101         suite.addTest(new TestReasonerFromManifest(tester, test, r));
102     }
103     
104     /**
105      * Build the query tests for the given reasoner.
106      */

107     private static void constructQuerytests(TestSuite suite, String JavaDoc manifest, ReasonerFactory rf, Resource config) throws IOException {
108         ReasonerTester tester = new ReasonerTester(manifest);
109         Reasoner r = rf.create(config);
110         for (Iterator JavaDoc i = tester.listTests().iterator(); i.hasNext(); ) {
111             String JavaDoc test = (String JavaDoc)i.next();
112             suite.addTest(new TestReasonerFromManifest(tester, test, r));
113         }
114     }
115     
116     /**
117      * Build the working group tests for the given reasoner.
118      */

119     private static void constructRDFWGtests(TestSuite suite, ReasonerFactory rf, Resource config) throws IOException {
120         WGReasonerTester tester = new WGReasonerTester("Manifest.rdf");
121         for (Iterator JavaDoc i = tester.listTests().iterator(); i.hasNext(); ) {
122             String JavaDoc test = (String JavaDoc)i.next();
123             suite.addTest(new TestReasonerWG(tester, test, rf, config));
124         }
125     }
126         
127     
128     /**
129      * Build the query tests for the given reasoner.
130      */

131     public static void constructQuerytests(TestSuite suite, String JavaDoc manifest, Reasoner reasoner) throws IOException {
132         ReasonerTester tester = new ReasonerTester(manifest);
133         for (Iterator JavaDoc i = tester.listTests().iterator(); i.hasNext(); ) {
134             String JavaDoc test = (String JavaDoc)i.next();
135             suite.addTest(new TestReasonerFromManifest(tester, test, reasoner));
136         }
137     }
138     
139     /**
140      * Inner class defining a test framework for invoking a single locally
141      * defined query-over-inference test.
142      */

143     static class TestReasonerFromManifest extends TestCase {
144         
145         /** The tester which already has the test manifest loaded */
146         ReasonerTester tester;
147         
148         /** The name of the specific test to run */
149         String JavaDoc test;
150         
151         /** The factory for the reasoner type under test */
152         Reasoner reasoner;
153         
154         /** Constructor */
155         TestReasonerFromManifest(ReasonerTester tester, String JavaDoc test, Reasoner reasoner) {
156             super(test);
157             this.tester = tester;
158             this.test = test;
159             this.reasoner = reasoner;
160         }
161         
162     
163         /**
164          * The test runner
165          */

166         public void runTest() throws IOException {
167             tester.runTest(test, reasoner, this);
168         }
169
170     }
171
172     /**
173      * Inner class defining a test framework for invoking a single
174      * RDFCore working group test.
175      */

176     static class TestReasonerWG extends TestCase {
177         
178         /** The tester which already has the test manifest loaded */
179         WGReasonerTester tester;
180         
181         /** The name of the specific test to run */
182         String JavaDoc test;
183         
184         /** The factory for the reasoner type under test */
185         ReasonerFactory reasonerFactory;
186         
187         /** An optional configuration model */
188         Resource config;
189         
190         /** Constructor */
191         TestReasonerWG(WGReasonerTester tester, String JavaDoc test,
192                                  ReasonerFactory reasonerFactory, Resource config) {
193             super(test);
194             this.tester = tester;
195             this.test = test;
196             this.reasonerFactory = reasonerFactory;
197             this.config = config;
198         }
199         
200         /**
201          * The test runner
202          */

203         public void runTest() throws IOException {
204             tester.runTest(test, reasonerFactory, this, config);
205         }
206
207     }
208     
209     /**
210      * Inner class defining the misc extra tests needed to check out a
211      * candidate RDFS reasoner.
212      */

213     static class TestRDFSMisc extends TestCase {
214         
215         /** The factory for the reasoner type under test */
216         ReasonerFactory reasonerFactory;
217         
218         /** An optional configuration model */
219         Resource config;
220         
221         /** Constructor */
222         TestRDFSMisc(ReasonerFactory reasonerFactory, Resource config) {
223             super("TestRDFSMisc");
224             this.reasonerFactory = reasonerFactory;
225             this.config = config;
226         }
227
228         /**
229          * The test runner
230          */

231         public void runTest() throws IOException {
232             ReasonerTester tester = new ReasonerTester("rdfs/manifest.rdf");
233             // Test effect of switching off property scan - should break container property test case
234
Resource configuration = newResource();
235             if (config != null) {
236                 for (StmtIterator i = config.listProperties(); i.hasNext();) {
237                     Statement s = i.nextStatement();
238                     configuration.addProperty(s.getPredicate(), s.getObject());
239                 }
240             }
241             configuration.addProperty(ReasonerVocabulary.PROPenableCMPScan, "false");
242             assertTrue("scanproperties off",
243                         !tester.runTest(NAMESPACE + "rdfs/test17", reasonerFactory, null, configuration));
244         
245             // Check capabilities description
246
Reasoner r = reasonerFactory.create(null);
247             assertTrue(r.supportsProperty(RDFS.subClassOf));
248             assertTrue(r.supportsProperty(RDFS.domain));
249             assertTrue(r.supportsProperty(RDFS.range));
250
251             // Datatype tests
252
assertTrue( ! doTestRDFSDTRange("dttest1.nt", reasonerFactory));
253             assertTrue( ! doTestRDFSDTRange("dttest2.nt", reasonerFactory));
254             assertTrue( doTestRDFSDTRange("dttest3.nt", reasonerFactory));
255         }
256
257         /**
258          * Helper for dt range testing - loads a file, validates it using RDFS/DT
259          * and returns error status of the result
260          */

261         private boolean doTestRDFSDTRange(String JavaDoc file, ReasonerFactory rf) throws IOException {
262             String JavaDoc langType = "RDF/XML";
263             if (file.endsWith(".nt")) {
264                 langType = "N-TRIPLE";
265             } else if (file.endsWith("n3")) {
266                 langType = "N3";
267             }
268             Model m = ModelFactory.createNonreifyingModel();
269             Reader reader = new BufferedReader(new FileReader("testing/reasoners/rdfs/"+file));
270             m.read(reader, WGReasonerTester.BASE_URI + file, langType);
271             InfGraph g = rf.create(null).bind(m.getGraph());
272             ValidityReport report = g.validate();
273             if (!report.isValid()) {
274                 logger.debug("Validation error report:");
275                 for (Iterator JavaDoc i = report.getReports(); i.hasNext(); ) {
276                     logger.debug(i.next().toString());
277                 }
278             }
279             return report.isValid();
280         }
281           
282     }
283     
284 }
285
286
287
288 /*
289     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
290     All rights reserved.
291
292     Redistribution and use in source and binary forms, with or without
293     modification, are permitted provided that the following conditions
294     are met:
295
296     1. Redistributions of source code must retain the above copyright
297        notice, this list of conditions and the following disclaimer.
298
299     2. Redistributions in binary form must reproduce the above copyright
300        notice, this list of conditions and the following disclaimer in the
301        documentation and/or other materials provided with the distribution.
302
303     3. The name of the author may not be used to endorse or promote products
304        derived from this software without specific prior written permission.
305
306     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
307     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
308     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
309     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
310     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
311     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
312     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
313     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
314     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
315     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
316 */
Popular Tags