KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > test > OWLWGTester


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

10 package com.hp.hpl.jena.reasoner.rulesys.test;
11
12 import com.hp.hpl.jena.reasoner.*;
13 import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph;
14 import com.hp.hpl.jena.reasoner.test.WGReasonerTester;
15 import com.hp.hpl.jena.util.FileManager;
16 import com.hp.hpl.jena.rdf.model.*;
17 import com.hp.hpl.jena.vocabulary.*;
18 import com.hp.hpl.jena.graph.*;
19 import com.hp.hpl.jena.graph.query.*;
20
21 import com.hp.hpl.jena.shared.*;
22
23 import junit.framework.TestCase;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.io.*;
28 import java.util.*;
29
30
31 /**
32  * A test harness for running the OWL working group tests. This
33  * differs from the RDF one in several ways (separate manifest files,
34  * different namespaces, document references lack suffix ...).
35  * <p>
36  * This version is used for running the core entailment tests as part of unit testing.
37  * A separate test harness for use in reporting OWL conformance is being developed and
38  * some code rationalization might be once once that stabilizes. </p>
39  *
40  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
41  * @version $Revision: 1.23 $ on $Date: 2005/02/21 12:18:08 $
42  */

43 public class OWLWGTester {
44     /** The base URI in which the files are purported to reside */
45     public static String JavaDoc BASE_URI = "http://www.w3.org/2002/03owlt/";
46     
47     /** The base directory in which the test data is actually stored */
48     public static String JavaDoc baseDir = "testing/wg/";
49     
50     /** The namespace for the test specification schema */
51     public static final String JavaDoc NS_OTEST = "http://www.w3.org/2002/03owlt/testOntology#";
52     
53     /** The namespace for the test specification schema */
54     public static final String JavaDoc NS_RTEST = "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#";
55     
56     /** The rdf class for positive tests */
57     public static final Resource PositiveEntailmentTest;
58     
59     /** The rdf class for positive tests */
60     public static final Resource NegativeEntailmentTest;
61     
62     /** The predicate defining the description of the test */
63     public static final Property descriptionP;
64     
65     /** The predicate defining a premise for the test */
66     public static final Property premiseDocumentP;
67     
68     /** The predicate defining the conclusion from the test */
69     public static final Property conclusionDocumentP;
70     
71     /** The predicate defining the status of the test */
72     public static final Property statusP;
73     
74     /** The reasoner factory being tested */
75     protected ReasonerFactory reasonerF;
76     
77     /** The configuration information for the reasoner */
78     protected Resource configuration;
79     
80     /** The test case which has invoke this test */
81     protected TestCase testcase;
82     
83     /** The processing time used since testcase creation */
84     protected static long timeCost = 0;
85     
86     /** The total number of tests run */
87     protected static int numTests = 0;
88     
89     protected static Log logger = LogFactory.getLog(OWLWGTester.class);
90     
91     // Static initializer for the predicates
92
static {
93         PositiveEntailmentTest = ResourceFactory.createProperty(NS_OTEST, "PositiveEntailmentTest");
94         NegativeEntailmentTest = ResourceFactory.createProperty(NS_OTEST, "NegativeEntailmentTest");
95         descriptionP = ResourceFactory.createProperty(NS_RTEST, "description");
96         premiseDocumentP = ResourceFactory.createProperty(NS_RTEST, "premiseDocument");
97         conclusionDocumentP = ResourceFactory.createProperty(NS_RTEST, "conclusionDocument");
98         statusP = ResourceFactory.createProperty(NS_RTEST, "status");
99     }
100     
101     /**
102      * Constructor
103      * @param reasonerF the factory for the reasoner to be tested
104      * @param testcase the JUnit test case which is requesting this test
105      * @param configuration optional configuration information
106      */

107     public OWLWGTester(ReasonerFactory reasonerF, TestCase testcase, Resource configuration) {
108         this.reasonerF = reasonerF;
109         this.testcase = testcase;
110         this.configuration = configuration;
111     }
112     
113     /**
114      * Run all the tests in the manifest
115      * @param manifestFile the name of the manifest file relative to baseDir
116      * @param log set to true to enable derivation logging
117      * @param stats set to true to log performance statistics
118      * @return true if all the tests pass
119      * @throws IOException if one of the test files can't be found
120      */

121     public boolean runTests(String JavaDoc manifestFile, boolean log, boolean stats) throws IOException {
122         // Load up the manifest
123
Model manifest = FileManager.get().loadModel(baseDir + manifestFile);
124         ResIterator tests = manifest.listSubjectsWithProperty(RDF.type, PositiveEntailmentTest);
125         while (tests.hasNext()) {
126             Resource test = tests.nextResource();
127             if (!runTest(test, log, stats)) return false;
128         }
129         tests = manifest.listSubjectsWithProperty(RDF.type, NegativeEntailmentTest);
130         while (tests.hasNext()) {
131             Resource test = tests.nextResource();
132             if (!runTest(test, log, stats)) return false;
133         }
134         return true;
135     }
136
137      /**
138      * Run a single designated test.
139      * @param test the root node descibing the test
140      * @param log set to true to enable derivation logging
141      * @param stats set to true to log performance statistics
142      * @return true if the test passes
143      * @throws IOException if one of the test files can't be found
144      */

145     public boolean runTest(Resource test, boolean log, boolean stats) throws IOException {
146         // Find the specification for the named test
147
RDFNode testType = test.getRequiredProperty(RDF.type).getObject();
148         if (!(testType.equals(NegativeEntailmentTest) ||
149                testType.equals(PositiveEntailmentTest) ) ) {
150             throw new JenaException("Can't find test: " + test);
151         }
152
153         String JavaDoc description = test.getRequiredProperty(descriptionP).getObject().toString();
154         String JavaDoc status = test.getRequiredProperty(statusP).getObject().toString();
155         logger.debug("WG test " + test.getURI() + " - " + status);
156         
157         // Load up the premise documents
158
Model premises = ModelFactory.createNonreifyingModel();
159         for (StmtIterator premisesI = test.listProperties(premiseDocumentP); premisesI.hasNext(); ) {
160             premises.add(loadFile(premisesI.nextStatement().getObject().toString() + ".rdf"));
161         }
162
163         // Load up the conclusions document
164
Resource conclusionsRes = (Resource) test.getRequiredProperty(conclusionDocumentP).getObject();
165         Model conclusions = loadFile(conclusionsRes.toString() + ".rdf");
166         
167         // Construct the inferred graph
168
// Optional logging
169
if (log) {
170             if (configuration == null) {
171                 Model m = ModelFactory.createDefaultModel();
172                 configuration = m.createResource();
173             }
174             configuration.addProperty(ReasonerVocabulary.PROPtraceOn, "true")
175                          .addProperty(ReasonerVocabulary.PROPderivationLogging, "true");
176         }
177         Reasoner reasoner = reasonerF.create(configuration);
178         long t1 = System.currentTimeMillis();
179         InfGraph graph = reasoner.bind(premises.getGraph());
180         Model result = ModelFactory.createModelForGraph(graph);
181         
182         if (stats && graph instanceof FBRuleInfGraph) {
183 // ((FBRuleInfGraph)graph).resetLPProfile(true);
184
}
185         
186         // Check the results against the official conclusions
187
boolean correct = true;
188         if (testType.equals(PositiveEntailmentTest)) {
189             correct = testConclusions(conclusions.getGraph(), result.getGraph());
190         } else {
191             // A negative entailment check
192
correct = !testConclusions(conclusions.getGraph(), result.getGraph());
193         }
194         long t2 = System.currentTimeMillis();
195         timeCost += (t2-t1);
196         numTests++;
197         if (stats) {
198             logger.info("Time=" + (t2-t1) + "ms for " + test.getURI());
199             printStats();
200             
201             if (graph instanceof FBRuleInfGraph) {
202                 ((FBRuleInfGraph)graph).printLPProfile();
203             }
204         }
205         
206         if (!correct) {
207             // List all the forward deductions for debugging
208
// if (graph instanceof FBRuleInfGraph) {
209
// System.out.println("Error: deductions graph was ...");
210
// FBRuleInfGraph fbGraph = (FBRuleInfGraph)graph;
211
// Graph deductions = fbGraph.getDeductionsGraph();
212
// com.hp.hpl.jena.util.PrintUtil.printOut(deductions.find(null,null,null));
213
// }
214
}
215         
216         // Signal the results
217
if (testcase != null) {
218             TestCase.assertTrue("Test: " + test + "\n" + reasonerF.getURI() + "\n" + description, correct);
219         }
220         return correct;
221     }
222
223     /**
224      * Utility to load a file as a Model.
225      * Files are assumed to be relative to the BASE_URI.
226      * @param file the file name, relative to baseDir
227      * @return the loaded Model
228      */

229     public static Model loadFile(String JavaDoc file) throws IOException {
230         String JavaDoc langType = "RDF/XML";
231         if (file.endsWith(".nt")) {
232             langType = "N-TRIPLE";
233         } else if (file.endsWith("n3")) {
234             langType = "N3";
235         }
236         Model result = ModelFactory.createNonreifyingModel();
237         String JavaDoc fname = file;
238         if (fname.startsWith(BASE_URI)) {
239             fname = fname.substring(BASE_URI.length());
240         }
241         Reader reader = new BufferedReader(new FileReader(baseDir + fname));
242         result.read(reader, BASE_URI + fname, langType);
243         return result;
244     }
245     
246     /**
247      * Test a conclusions graph against a result graph. This works by
248      * translating the conclusions graph into a find query which contains one
249      * variable for each distinct bNode in the conclusions graph.
250      */

251     private boolean testConclusions(Graph conclusions, Graph result) {
252         QueryHandler qh = result.queryHandler();
253         Query query = WGReasonerTester.graphToQuery(conclusions);
254         Iterator i = qh.prepareBindings(query, new Node[] {}).executeBindings();
255         return i.hasNext();
256     }
257     
258     /**
259      * Log (info level) some summary information on the timecost of the tests.
260      */

261     public void printStats() {
262         logger.info("Ran " + numTests +" in " + timeCost +"ms = " + (timeCost/numTests) + "ms/test");
263     }
264
265 }
266
267 /*
268  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
269  * All rights reserved.
270  *
271  * Redistribution and use in source and binary forms, with or without
272  * modification, are permitted provided that the following conditions
273  * are met:
274  * 1. Redistributions of source code must retain the above copyright
275  * notice, this list of conditions and the following disclaimer.
276  * 2. Redistributions in binary form must reproduce the above copyright
277  * notice, this list of conditions and the following disclaimer in the
278  * documentation and/or other materials provided with the distribution.
279  * 3. The name of the author may not be used to endorse or promote products
280  * derived from this software without specific prior written permission.
281  *
282  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
283  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
284  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
285  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
286  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
287  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
288  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
289  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
290  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
291  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292  */

293
294
Popular Tags