001: /******************************************************************
002: * File: OWLConsistencyTester.java
003: * Created by: Dave Reynolds
004: * Created on: 14-Feb-2005
005: *
006: * (c) Copyright 2005, Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: OWLConsistencyTest.java,v 1.6 2008/01/02 12:08:20 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.test;
010:
011: import java.util.Iterator;
012:
013: import junit.framework.TestCase;
014:
015: import com.hp.hpl.jena.rdf.model.InfModel;
016: import com.hp.hpl.jena.rdf.model.Model;
017: import com.hp.hpl.jena.rdf.model.ModelFactory;
018: import com.hp.hpl.jena.reasoner.Reasoner;
019: import com.hp.hpl.jena.reasoner.ReasonerFactory;
020: import com.hp.hpl.jena.reasoner.ValidityReport;
021: import com.hp.hpl.jena.util.FileManager;
022:
023: /**
024: * Utility for checking OWL validation results.
025: *
026: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds </a>
027: * @version $Revision: 1.6 $
028: */
029:
030: public class OWLConsistencyTest extends TestCase {
031:
032: /** The base directory for finding the datafiles */
033: public static final String BASE_DIR = "file:testing/reasoners/owl/";
034:
035: /** The tbox to be tested, relative to BASE_DIR */
036: protected String tbox;
037:
038: /** The abox to be tested, relative to BASE_DIR */
039: protected String abox;
040:
041: /** The expected result to check against */
042: protected int expected;
043:
044: /** The factory for the reasoner to test */
045: protected ReasonerFactory rf;
046:
047: /** Flag for expected result = inconsistent */
048: public static final int INCONSISTENT = 1;
049:
050: /** Flag for expected result = consistent but at least 1 warning */
051: public static final int WARNINGS = 2;
052:
053: /** Flag for expected result = no errors ow warnings */
054: public static final int CLEAN = 3;
055:
056: /** Optional culprit object, should validate with equals */
057: protected Object culprit;
058:
059: /**
060: * Constructor - builds a dummy test which can't be run without setting a
061: * reasoner factory
062: *
063: * @param tbox
064: * The tbox to be tested, relative to BASE_DIR
065: * @param abox
066: * The abox to be tested, relative to BASE_DIR
067: * @param expected
068: * The expected result to check against -
069: * INCONSISTENT/WARNINGS/CLEAN
070: * @param culprit
071: * Optional culprit object, should validate with equals, set to
072: * null for no test
073: */
074: public OWLConsistencyTest(String tbox, String abox, int expected,
075: Object culprit) {
076: super (abox);
077: this .tbox = tbox;
078: this .abox = abox;
079: this .expected = expected;
080: this .culprit = culprit;
081: }
082:
083: /**
084: * Constructor builds a runnable test from a dummy test.
085: */
086: public OWLConsistencyTest(OWLConsistencyTest base,
087: String reasonerName, ReasonerFactory rf) {
088: super (reasonerName + ":" + base.abox);
089: this .tbox = base.tbox;
090: this .abox = base.abox;
091: this .expected = base.expected;
092: this .culprit = base.culprit;
093: this .rf = rf;
094: }
095:
096: /**
097: * Define the reasoner to use for the tests.
098: */
099: public void setReasonerFactory(ReasonerFactory rf) {
100: this .rf = rf;
101: }
102:
103: /**
104: * Run the consistency check, returning a ValidityReport.
105: *
106: * @param rf
107: * The factory for the reasoner to test
108: */
109: public ValidityReport testResults() {
110: Model t = FileManager.get().loadModel(BASE_DIR + tbox);
111: Model a = FileManager.get().loadModel(BASE_DIR + abox);
112: // Work around non-deterministic bug in bindSchema
113: // Reasoner r = rf.create(null).bindSchema(t);
114: Reasoner r = rf.create(null);
115: a.add(t);
116: InfModel im = ModelFactory.createInfModel(r, a);
117: return im.validate();
118: }
119:
120: /**
121: * Run the consistency check and validate the result against expectations.
122: *
123: * @param rf
124: * The factory for the reasoner to test
125: */
126: public void runTest() {
127: ValidityReport report = testResults();
128: switch (expected) {
129: case INCONSISTENT:
130: assertTrue("expected inconsistent", !report.isValid());
131: break;
132: case WARNINGS:
133: assertTrue("expected just warnings but reports not valid",
134: report.isValid());
135: assertFalse("expected warnings but reports clean", report
136: .isClean());
137: break;
138: case CLEAN:
139: assertTrue("expected clean", report.isClean());
140: }
141: if (culprit != null) {
142: boolean foundit = false;
143: for (Iterator i = report.getReports(); i.hasNext();) {
144: ValidityReport.Report r = (ValidityReport.Report) i
145: .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,
154: false);
155: }
156: }
157: }
158:
159: }
160:
161: /*
162: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
163: * reserved.
164: *
165: * Redistribution and use in source and binary forms, with or without
166: * modification, are permitted provided that the following conditions are met:
167: *
168: * 1. Redistributions of source code must retain the above copyright notice,
169: * this list of conditions and the following disclaimer.
170: *
171: * 2. Redistributions in binary form must reproduce the above copyright notice,
172: * this list of conditions and the following disclaimer in the documentation
173: * and/or other materials provided with the distribution.
174: *
175: * 3. The name of the author may not be used to endorse or promote products
176: * derived from this software without specific prior written permission.
177: *
178: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
179: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
181: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
182: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
183: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
184: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
185: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
186: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
187: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
188: */
|