001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 05-Jun-2003
009: * Filename $RCSfile: TestOntReasoning.java,v $
010: * Revision $Revision: 1.17 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:08:40 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package com.hp.hpl.jena.ontology.impl.test;
019:
020: // Imports
021: ///////////////
022: import java.util.*;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import junit.framework.TestCase;
028:
029: import com.hp.hpl.jena.ontology.*;
030: import com.hp.hpl.jena.rdf.model.ModelFactory;
031:
032: /**
033: * <p>
034: * Unit tests on ont models with reasoning
035: * </p>
036: *
037: * @author Ian Dickinson, HP Labs
038: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
039: * @version CVS $Id: TestOntReasoning.java,v 1.17 2008/01/02 12:08:40 andy_seaborne Exp $
040: */
041: public class TestOntReasoning extends TestCase {
042: // Constants
043: //////////////////////////////////
044: public static final String BASE = "http://jena.hpl.hp.com/testing/ontology";
045: public static final String NS = BASE + "#";
046:
047: // Static variables
048: //////////////////////////////////
049:
050: // Instance variables
051: //////////////////////////////////
052:
053: // Constructors
054: //////////////////////////////////
055:
056: public TestOntReasoning(String name) {
057: super (name);
058: }
059:
060: // External signature methods
061: //////////////////////////////////
062:
063: public void setUp() {
064: // ensure the ont doc manager is in a consistent state
065: OntDocumentManager.getInstance().reset(true);
066: }
067:
068: public void tearDown() {
069: }
070:
071: public void testSubClassDirectTransInf1a() {
072: OntModel m = ModelFactory
073: .createOntologyModel(ProfileRegistry.OWL_LITE_LANG);
074:
075: OntClass A = m.createClass(NS + "A");
076: OntClass B = m.createClass(NS + "B");
077: OntClass C = m.createClass(NS + "C");
078: OntClass D = m.createClass(NS + "D");
079:
080: A.addSubClass(B);
081: A.addSubClass(C);
082: C.addSubClass(D);
083:
084: iteratorTest(A.listSubClasses(), new Object[] { B, C, D });
085: iteratorTest(A.listSubClasses(true), new Object[] { B, C });
086: }
087:
088: public void testSubClassDirectTransInf1b() {
089: OntModel m = ModelFactory
090: .createOntologyModel(ProfileRegistry.OWL_LITE_LANG);
091:
092: OntClass A = m.createClass(NS + "A");
093: OntClass B = m.createClass(NS + "B");
094: OntClass C = m.createClass(NS + "C");
095: OntClass D = m.createClass(NS + "D");
096:
097: A.addSubClass(B);
098: A.addSubClass(C);
099: C.addSubClass(D);
100: A.addSubClass(D); // directly asserts a link that could be inferred
101:
102: iteratorTest(A.listSubClasses(), new Object[] { B, C, D });
103: iteratorTest(A.listSubClasses(true), new Object[] { B, C });
104: }
105:
106: public void testSubClassDirectTransInf2a() {
107: // test the code path for generating direct sc with no reasoner
108: OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_LITE_MEM);
109: spec.setReasonerFactory(null);
110: OntModel m = ModelFactory.createOntologyModel(spec, null);
111:
112: OntClass A = m.createClass(NS + "A");
113: OntClass B = m.createClass(NS + "B");
114: OntClass C = m.createClass(NS + "C");
115: OntClass D = m.createClass(NS + "D");
116:
117: A.addSubClass(B);
118: A.addSubClass(C);
119: C.addSubClass(D);
120:
121: iteratorTest(A.listSubClasses(), new Object[] { B, C });
122: iteratorTest(A.listSubClasses(true), new Object[] { B, C });
123: }
124:
125: public void testSubClassDirectTransInf2b() {
126: // test the code path for generating direct sc with no reasoner
127: OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_LITE_MEM);
128: spec.setReasonerFactory(null);
129: OntModel m = ModelFactory.createOntologyModel(spec, null);
130:
131: OntClass A = m.createClass(NS + "A");
132: OntClass B = m.createClass(NS + "B");
133: OntClass C = m.createClass(NS + "C");
134: OntClass D = m.createClass(NS + "D");
135:
136: A.addSubClass(B);
137: A.addSubClass(C);
138: C.addSubClass(D);
139: A.addSubClass(D); // directly asserts a link that could be inferred
140:
141: iteratorTest(A.listSubClasses(), new Object[] { B, C, D });
142: iteratorTest(A.listSubClasses(true), new Object[] { B, C });
143: }
144:
145: public void testSubPropertyDirectTransInf1a() {
146: OntModel m = ModelFactory
147: .createOntologyModel(ProfileRegistry.OWL_LITE_LANG);
148:
149: OntProperty p = m.createObjectProperty(NS + "p");
150: OntProperty q = m.createObjectProperty(NS + "q");
151: OntProperty r = m.createObjectProperty(NS + "r");
152: OntProperty s = m.createObjectProperty(NS + "s");
153:
154: p.addSubProperty(q);
155: p.addSubProperty(r);
156: r.addSubProperty(s);
157:
158: iteratorTest(p.listSubProperties(), new Object[] { p, q, r, s });
159: iteratorTest(p.listSubProperties(true), new Object[] { q, r });
160: }
161:
162: public void testSubPropertyDirectTransInf1b() {
163: OntModel m = ModelFactory
164: .createOntologyModel(ProfileRegistry.OWL_LITE_LANG);
165:
166: OntProperty p = m.createObjectProperty(NS + "p");
167: OntProperty q = m.createObjectProperty(NS + "q");
168: OntProperty r = m.createObjectProperty(NS + "r");
169: OntProperty s = m.createObjectProperty(NS + "s");
170:
171: p.addSubProperty(q);
172: p.addSubProperty(r);
173: r.addSubProperty(s);
174: p.addSubProperty(s); // directly asserts a link that could be inferred
175:
176: iteratorTest(p.listSubProperties(), new Object[] { p, q, r, s });
177: iteratorTest(p.listSubProperties(true), new Object[] { q, r });
178: }
179:
180: public void testSubPropertyDirectTransInf2a() {
181: // test the code path for generating direct sc with no reasoner
182: OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_LITE_MEM);
183: spec.setReasonerFactory(null);
184: OntModel m = ModelFactory.createOntologyModel(spec, null);
185:
186: OntProperty p = m.createObjectProperty(NS + "p");
187: OntProperty q = m.createObjectProperty(NS + "q");
188: OntProperty r = m.createObjectProperty(NS + "r");
189: OntProperty s = m.createObjectProperty(NS + "s");
190:
191: p.addSubProperty(q);
192: p.addSubProperty(r);
193: r.addSubProperty(s);
194:
195: iteratorTest(p.listSubProperties(), new Object[] { q, r });
196: iteratorTest(p.listSubProperties(true), new Object[] { q, r });
197: }
198:
199: public void testSubPropertyDirectTransInf2b() {
200: // test the code path for generating direct sc with no reasoner
201: OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_LITE_MEM);
202: spec.setReasonerFactory(null);
203: OntModel m = ModelFactory.createOntologyModel(spec, null);
204:
205: OntProperty p = m.createObjectProperty(NS + "p");
206: OntProperty q = m.createObjectProperty(NS + "q");
207: OntProperty r = m.createObjectProperty(NS + "r");
208: OntProperty s = m.createObjectProperty(NS + "s");
209:
210: p.addSubProperty(q);
211: p.addSubProperty(r);
212: r.addSubProperty(s);
213: p.addSubProperty(s); // directly asserts a link that could be inferred
214:
215: iteratorTest(p.listSubProperties(), new Object[] { q, r, s });
216: iteratorTest(p.listSubProperties(true), new Object[] { q, r });
217: }
218:
219: public void testListDefinedProperties() {
220: OntModel m = ModelFactory.createOntologyModel(
221: OntModelSpec.OWL_MEM_RULE_INF, null);
222:
223: // a simple class hierarchy organism -> vertebrate -> mammal -> dog
224: OntClass organism = m.createClass(NS + "Organism");
225: OntClass vertebrate = m.createClass(NS + "Vertebrate");
226: OntClass mammal = m.createClass(NS + "Mammal");
227: OntClass dog = m.createClass(NS + "Dog");
228:
229: organism.addSubClass(vertebrate);
230: vertebrate.addSubClass(mammal);
231: mammal.addSubClass(dog);
232:
233: // hair as a covering
234: OntClass covering = m.createClass(NS + "Covering");
235: Individual hair = m.createIndividual(NS + "hair", covering);
236:
237: // various properties
238: DatatypeProperty limbsCount = m.createDatatypeProperty(NS
239: + "limbsCount");
240: DatatypeProperty hasCovering = m.createDatatypeProperty(NS
241: + "hasCovering");
242: DatatypeProperty numYoung = m.createDatatypeProperty(NS
243: + "numYoung");
244:
245: // vertebrates have limbs, mammals have live young
246: limbsCount.addDomain(vertebrate);
247: numYoung.addDomain(mammal);
248:
249: // mammals have-covering = hair
250: Restriction r = m.createRestriction(hasCovering);
251: r.convertToHasValueRestriction(hair);
252: mammal.addSuperClass(r);
253:
254: iteratorTest(organism.listDeclaredProperties(),
255: new Object[] { hasCovering });
256: iteratorTest(vertebrate.listDeclaredProperties(), new Object[] {
257: limbsCount, hasCovering });
258: iteratorTest(mammal.listDeclaredProperties(), new Object[] {
259: limbsCount, hasCovering, numYoung });
260: iteratorTest(dog.listDeclaredProperties(), new Object[] {
261: limbsCount, hasCovering, numYoung });
262: iteratorTest(r.listDeclaredProperties(),
263: new Object[] { hasCovering });
264:
265: iteratorTest(organism.listDeclaredProperties(true),
266: new Object[] { hasCovering });
267: iteratorTest(vertebrate.listDeclaredProperties(true),
268: new Object[] { limbsCount });
269: iteratorTest(mammal.listDeclaredProperties(true),
270: new Object[] { numYoung });
271: iteratorTest(dog.listDeclaredProperties(true), new Object[] {});
272: iteratorTest(r.listDeclaredProperties(true),
273: new Object[] { hasCovering });
274:
275: iteratorTest(organism.listDeclaredProperties(false),
276: new Object[] { hasCovering });
277: iteratorTest(vertebrate.listDeclaredProperties(false),
278: new Object[] { hasCovering, limbsCount });
279: iteratorTest(mammal.listDeclaredProperties(false),
280: new Object[] { hasCovering, numYoung, limbsCount });
281: iteratorTest(dog.listDeclaredProperties(false), new Object[] {
282: hasCovering, numYoung, limbsCount });
283: iteratorTest(r.listDeclaredProperties(false),
284: new Object[] { hasCovering });
285: }
286:
287: // Internal implementation methods
288: //////////////////////////////////
289:
290: /** Test that an iterator delivers the expected values */
291: protected void iteratorTest(Iterator i, Object[] expected) {
292: Log logger = LogFactory.getLog(getClass());
293: List expList = new ArrayList();
294: for (int j = 0; j < expected.length; j++) {
295: expList.add(expected[j]);
296: }
297:
298: while (i.hasNext()) {
299: Object next = i.next();
300:
301: // debugging
302: if (!expList.contains(next)) {
303: logger.debug(getName()
304: + " - Unexpected iterator result: " + next);
305: }
306:
307: assertTrue(
308: "Value "
309: + next
310: + " was not expected as a result from this iterator ",
311: expList.contains(next));
312: assertTrue("Value " + next
313: + " was not removed from the list ", expList
314: .remove(next));
315: }
316:
317: if (!(expList.size() == 0)) {
318: logger.debug(getName()
319: + " Expected iterator results not found");
320: for (Iterator j = expList.iterator(); j.hasNext();) {
321: logger.debug(getName() + " - missing: " + j.next());
322: }
323: }
324: assertEquals(
325: "There were expected elements from the iterator that were not found",
326: 0, expList.size());
327: }
328:
329: //==============================================================================
330: // Inner class definitions
331: //==============================================================================
332:
333: }
334:
335: /*
336: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
337: All rights reserved.
338:
339: Redistribution and use in source and binary forms, with or without
340: modification, are permitted provided that the following conditions
341: are met:
342:
343: 1. Redistributions of source code must retain the above copyright
344: notice, this list of conditions and the following disclaimer.
345:
346: 2. Redistributions in binary form must reproduce the above copyright
347: notice, this list of conditions and the following disclaimer in the
348: documentation and/or other materials provided with the distribution.
349:
350: 3. The name of the author may not be used to endorse or promote products
351: derived from this software without specific prior written permission.
352:
353: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
354: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
355: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
356: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
357: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
358: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
359: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
360: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
361: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
362: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363: */
|