KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: OWLConsistencyTester.java
3  * Created by: Dave Reynolds
4  * Created on: 14-Feb-2005
5  *
6  * (c) Copyright 2005, Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: OWLConsistencyTest.java,v 1.3 2005/02/18 15:33:34 der Exp $
9  *****************************************************************/

10
11 package com.hp.hpl.jena.reasoner.rulesys.test;
12
13 import java.util.Iterator JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import com.hp.hpl.jena.rdf.model.InfModel;
18 import com.hp.hpl.jena.rdf.model.Model;
19 import com.hp.hpl.jena.rdf.model.ModelFactory;
20 import com.hp.hpl.jena.reasoner.Reasoner;
21 import com.hp.hpl.jena.reasoner.ReasonerFactory;
22 import com.hp.hpl.jena.reasoner.ValidityReport;
23 import com.hp.hpl.jena.util.FileManager;
24
25 /**
26  * Utility for checking OWL validation results.
27  *
28  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds </a>
29  * @version $Revision: 1.3 $
30  */

31
32 public class OWLConsistencyTest extends TestCase {
33
34     /** The base directory for finding the datafiles */
35     public static final String JavaDoc BASE_DIR = "file:testing/reasoners/owl/";
36
37     /** The tbox to be tested, relative to BASE_DIR */
38     protected String JavaDoc tbox;
39
40     /** The abox to be tested, relative to BASE_DIR */
41     protected String JavaDoc abox;
42
43     /** The expected result to check against */
44     protected int expected;
45
46     /** The factory for the reasoner to test */
47     protected ReasonerFactory rf;
48
49     /** Flag for expected result = inconsistent */
50     public static final int INCONSISTENT = 1;
51
52     /** Flag for expected result = consistent but at least 1 warning */
53     public static final int WARNINGS = 2;
54
55     /** Flag for expected result = no errors ow warnings */
56     public static final int CLEAN = 3;
57
58     /** Optional culprit object, should validate with equals */
59     protected Object JavaDoc culprit;
60
61     /**
62      * Constructor - builds a dummy test which can't be run without setting a
63      * reasoner factory
64      *
65      * @param tbox
66      * The tbox to be tested, relative to BASE_DIR
67      * @param abox
68      * The abox to be tested, relative to BASE_DIR
69      * @param expected
70      * The expected result to check against -
71      * INCONSISTENT/WARNINGS/CLEAN
72      * @param culprit
73      * Optional culprit object, should validate with equals, set to
74      * null for no test
75      */

76     public OWLConsistencyTest(String JavaDoc tbox, String JavaDoc abox, int expected,
77             Object JavaDoc culprit) {
78         super(abox);
79         this.tbox = tbox;
80         this.abox = abox;
81         this.expected = expected;
82         this.culprit = culprit;
83     }
84
85     /**
86      * Constructor builds a runnable test from a dummy test.
87      */

88     public OWLConsistencyTest(OWLConsistencyTest base, String JavaDoc reasonerName,
89             ReasonerFactory rf) {
90         super(reasonerName + ":" + base.abox);
91         this.tbox = base.tbox;
92         this.abox = base.abox;
93         this.expected = base.expected;
94         this.culprit = base.culprit;
95         this.rf = rf;
96     }
97
98     /**
99      * Define the reasoner to use for the tests.
100      */

101     public void setReasonerFactory(ReasonerFactory rf) {
102         this.rf = rf;
103     }
104
105     /**
106      * Run the consistency check, returning a ValidityReport.
107      *
108      * @param rf
109      * The factory for the reasoner to test
110      */

111     public ValidityReport testResults() {
112         Model t = FileManager.get().loadModel(BASE_DIR + tbox);
113         Model a = FileManager.get().loadModel(BASE_DIR + abox);
114         // Work around non-deterministic bug in bindSchema
115
// Reasoner r = rf.create(null).bindSchema(t);
116
Reasoner r = rf.create(null);
117         a.add(t);
118         InfModel im = ModelFactory.createInfModel(r, a);
119         return im.validate();
120     }
121
122     /**
123      * Run the consistency check and validate the result against expectations.
124      *
125      * @param rf
126      * The factory for the reasoner to test
127      */

128     public void runTest() {
129         ValidityReport report = testResults();
130         switch (expected) {
131         case INCONSISTENT:
132             assertTrue("expected inconsistent", !report.isValid());
133             break;
134         case WARNINGS:
135             assertTrue("expected just warnings but reports not valid", report
136                     .isValid());
137             assertFalse("expected warnings but reports clean", report.isClean());
138             break;
139         case CLEAN:
140             assertTrue("expected clean", report.isClean());
141         }
142         if (culprit != null) {
143             boolean foundit = false;
144             for (Iterator JavaDoc i = report.getReports(); i.hasNext();) {
145                 ValidityReport.Report r = (ValidityReport.Report) i.next();
146                 if (r.getExtension() != null
147                         && r.getExtension().equals(culprit)) {
148                     foundit = true;
149                     break;
150                 }
151             }
152             if (!foundit) {
153                 assertTrue("Expcted to find a culprint " + culprit, false);
154             }
155         }
156     }
157
158 }
159
160 /*
161  * (c) Copyright 2005 Hewlett-Packard Development Company, LP All rights
162  * reserved.
163  *
164  * Redistribution and use in source and binary forms, with or without
165  * modification, are permitted provided that the following conditions are met:
166  *
167  * 1. Redistributions of source code must retain the above copyright notice,
168  * this list of conditions and the following disclaimer.
169  *
170  * 2. Redistributions in binary form must reproduce the above copyright notice,
171  * this list of conditions and the following disclaimer in the documentation
172  * and/or other materials provided with the distribution.
173  *
174  * 3. The name of the author may not be used to endorse or promote products
175  * derived from this software without specific prior written permission.
176  *
177  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
178  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
179  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
180  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
181  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
182  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
183  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
184  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
185  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
186  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
187  */

188
Popular Tags