KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > ontology > impl > test > TestOntReasoning


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 05-Jun-2003
9  * Filename $RCSfile: TestOntReasoning.java,v $
10  * Revision $Revision: 1.14 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/04/08 17:38:50 $
14  * by $Author: ian_dickinson $
15  *
16  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.ontology.impl.test;
23
24
25 // Imports
26
///////////////
27
import java.util.*;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import junit.framework.TestCase;
33
34 import com.hp.hpl.jena.ontology.*;
35 import com.hp.hpl.jena.rdf.model.ModelFactory;
36
37
38 /**
39  * <p>
40  * Unit tests on ont models with reasoning
41  * </p>
42  *
43  * @author Ian Dickinson, HP Labs
44  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
45  * @version CVS $Id: TestOntReasoning.java,v 1.14 2005/04/08 17:38:50 ian_dickinson Exp $
46  */

47 public class TestOntReasoning
48     extends TestCase
49 {
50     // Constants
51
//////////////////////////////////
52
public static final String JavaDoc BASE = "http://jena.hpl.hp.com/testing/ontology";
53     public static final String JavaDoc NS = BASE + "#";
54
55     // Static variables
56
//////////////////////////////////
57

58     // Instance variables
59
//////////////////////////////////
60

61     // Constructors
62
//////////////////////////////////
63

64     public TestOntReasoning( String JavaDoc name ) {
65         super( name );
66     }
67
68     // External signature methods
69
//////////////////////////////////
70

71     public void setUp() {
72         // ensure the ont doc manager is in a consistent state
73
OntDocumentManager.getInstance().reset( true );
74     }
75
76
77     public void tearDown() {
78     }
79
80     public void testSubClassDirectTransInf1a() {
81         OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG );
82
83         OntClass A = m.createClass( NS + "A" );
84         OntClass B = m.createClass( NS + "B" );
85         OntClass C = m.createClass( NS + "C" );
86         OntClass D = m.createClass( NS + "D" );
87
88         A.addSubClass( B );
89         A.addSubClass( C );
90         C.addSubClass( D );
91
92         iteratorTest( A.listSubClasses(), new Object JavaDoc[] {B, C, D} );
93         iteratorTest( A.listSubClasses( true ), new Object JavaDoc[] {B, C} );
94     }
95
96     public void testSubClassDirectTransInf1b() {
97         OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG );
98
99         OntClass A = m.createClass( NS + "A" );
100         OntClass B = m.createClass( NS + "B" );
101         OntClass C = m.createClass( NS + "C" );
102         OntClass D = m.createClass( NS + "D" );
103
104         A.addSubClass( B );
105         A.addSubClass( C );
106         C.addSubClass( D );
107         A.addSubClass( D ); // directly asserts a link that could be inferred
108

109         iteratorTest( A.listSubClasses(), new Object JavaDoc[] {B, C, D} );
110         iteratorTest( A.listSubClasses( true ), new Object JavaDoc[] {B, C} );
111     }
112
113     public void testSubClassDirectTransInf2a() {
114         // test the code path for generating direct sc with no reasoner
115
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM );
116         spec.setReasonerFactory( null );
117         OntModel m = ModelFactory.createOntologyModel( spec, null );
118
119         OntClass A = m.createClass( NS + "A" );
120         OntClass B = m.createClass( NS + "B" );
121         OntClass C = m.createClass( NS + "C" );
122         OntClass D = m.createClass( NS + "D" );
123
124         A.addSubClass( B );
125         A.addSubClass( C );
126         C.addSubClass( D );
127
128         iteratorTest( A.listSubClasses(), new Object JavaDoc[] {B, C} );
129         iteratorTest( A.listSubClasses( true ), new Object JavaDoc[] {B, C} );
130     }
131
132     public void testSubClassDirectTransInf2b() {
133         // test the code path for generating direct sc with no reasoner
134
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM );
135         spec.setReasonerFactory( null );
136         OntModel m = ModelFactory.createOntologyModel( spec, null );
137
138         OntClass A = m.createClass( NS + "A" );
139         OntClass B = m.createClass( NS + "B" );
140         OntClass C = m.createClass( NS + "C" );
141         OntClass D = m.createClass( NS + "D" );
142
143         A.addSubClass( B );
144         A.addSubClass( C );
145         C.addSubClass( D );
146         A.addSubClass( D ); // directly asserts a link that could be inferred
147

148         iteratorTest( A.listSubClasses(), new Object JavaDoc[] {B, C, D} );
149         iteratorTest( A.listSubClasses( true ), new Object JavaDoc[] {B, C} );
150     }
151
152     public void testSubPropertyDirectTransInf1a() {
153         OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG );
154
155         OntProperty p = m.createObjectProperty( NS + "p" );
156         OntProperty q = m.createObjectProperty( NS + "q" );
157         OntProperty r = m.createObjectProperty( NS + "r" );
158         OntProperty s = m.createObjectProperty( NS + "s" );
159
160         p.addSubProperty( q );
161         p.addSubProperty( r );
162         r.addSubProperty( s );
163
164         iteratorTest( p.listSubProperties(), new Object JavaDoc[] {p,q,r,s} );
165         iteratorTest( p.listSubProperties( true ), new Object JavaDoc[] {q,r} );
166     }
167
168     public void testSubPropertyDirectTransInf1b() {
169         OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG );
170
171         OntProperty p = m.createObjectProperty( NS + "p" );
172         OntProperty q = m.createObjectProperty( NS + "q" );
173         OntProperty r = m.createObjectProperty( NS + "r" );
174         OntProperty s = m.createObjectProperty( NS + "s" );
175
176         p.addSubProperty( q );
177         p.addSubProperty( r );
178         r.addSubProperty( s );
179         p.addSubProperty( s ); // directly asserts a link that could be inferred
180

181         iteratorTest( p.listSubProperties(), new Object JavaDoc[] {p,q,r,s} );
182         iteratorTest( p.listSubProperties( true ), new Object JavaDoc[] {q,r} );
183     }
184
185     public void testSubPropertyDirectTransInf2a() {
186         // test the code path for generating direct sc with no reasoner
187
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM );
188         spec.setReasonerFactory( null );
189         OntModel m = ModelFactory.createOntologyModel( spec, null );
190
191         OntProperty p = m.createObjectProperty( NS + "p" );
192         OntProperty q = m.createObjectProperty( NS + "q" );
193         OntProperty r = m.createObjectProperty( NS + "r" );
194         OntProperty s = m.createObjectProperty( NS + "s" );
195
196         p.addSubProperty( q );
197         p.addSubProperty( r );
198         r.addSubProperty( s );
199
200         iteratorTest( p.listSubProperties(), new Object JavaDoc[] {q,r} );
201         iteratorTest( p.listSubProperties( true ), new Object JavaDoc[] {q,r} );
202     }
203
204     public void testSubPropertyDirectTransInf2b() {
205         // test the code path for generating direct sc with no reasoner
206
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM );
207         spec.setReasonerFactory( null );
208         OntModel m = ModelFactory.createOntologyModel( spec, null );
209
210         OntProperty p = m.createObjectProperty( NS + "p" );
211         OntProperty q = m.createObjectProperty( NS + "q" );
212         OntProperty r = m.createObjectProperty( NS + "r" );
213         OntProperty s = m.createObjectProperty( NS + "s" );
214
215         p.addSubProperty( q );
216         p.addSubProperty( r );
217         r.addSubProperty( s );
218         p.addSubProperty( s ); // directly asserts a link that could be inferred
219

220         iteratorTest( p.listSubProperties(), new Object JavaDoc[] {q,r,s} );
221         iteratorTest( p.listSubProperties( true ), new Object JavaDoc[] {q,r} );
222     }
223
224     public void testListDefinedProperties() {
225         OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_RULE_INF, null );
226
227         // a simple class hierarchy organism -> vertebrate -> mammal -> dog
228
OntClass organism = m.createClass( NS + "Organism" );
229         OntClass vertebrate = m.createClass( NS + "Vertebrate" );
230         OntClass mammal = m.createClass( NS + "Mammal" );
231         OntClass dog = m.createClass( NS + "Dog" );
232
233         organism.addSubClass( vertebrate );
234         vertebrate.addSubClass( mammal );
235         mammal.addSubClass( dog );
236
237         // hair as a covering
238
OntClass covering = m.createClass( NS + "Covering" );
239         Individual hair = m.createIndividual( NS+"hair", covering );
240
241         // various properties
242
DatatypeProperty limbsCount = m.createDatatypeProperty( NS + "limbsCount" );
243         DatatypeProperty hasCovering = m.createDatatypeProperty( NS + "hasCovering" );
244         DatatypeProperty numYoung = m.createDatatypeProperty( NS + "numYoung" );
245
246         // vertebrates have limbs, mammals have live young
247
limbsCount.addDomain( vertebrate );
248         numYoung.addDomain( mammal );
249
250         // mammals have-covering = hair
251
Restriction r = m.createRestriction( hasCovering );
252         r.convertToHasValueRestriction( hair );
253         mammal.addSuperClass( r );
254
255         iteratorTest( organism.listDeclaredProperties(), new Object JavaDoc[] {hasCovering} );
256         iteratorTest( vertebrate.listDeclaredProperties(), new Object JavaDoc[] {limbsCount, hasCovering} );
257         iteratorTest( mammal.listDeclaredProperties(), new Object JavaDoc[] {limbsCount, hasCovering, numYoung} );
258         iteratorTest( dog.listDeclaredProperties(), new Object JavaDoc[] {limbsCount, hasCovering, numYoung} );
259         iteratorTest( r.listDeclaredProperties(), new Object JavaDoc[] {hasCovering} );
260
261         iteratorTest( organism.listDeclaredProperties(true), new Object JavaDoc[] {hasCovering} );
262         iteratorTest( vertebrate.listDeclaredProperties(true), new Object JavaDoc[] {limbsCount} );
263         iteratorTest( mammal.listDeclaredProperties(true), new Object JavaDoc[] {numYoung} );
264         iteratorTest( dog.listDeclaredProperties(true), new Object JavaDoc[] {} );
265         iteratorTest( r.listDeclaredProperties(true), new Object JavaDoc[] {hasCovering} );
266
267         iteratorTest( organism.listDeclaredProperties(false), new Object JavaDoc[] {hasCovering} );
268         iteratorTest( vertebrate.listDeclaredProperties(false), new Object JavaDoc[] {hasCovering,limbsCount} );
269         iteratorTest( mammal.listDeclaredProperties(false), new Object JavaDoc[] {hasCovering,numYoung,limbsCount} );
270         iteratorTest( dog.listDeclaredProperties(false), new Object JavaDoc[] {hasCovering,numYoung,limbsCount} );
271         iteratorTest( r.listDeclaredProperties(false), new Object JavaDoc[] {hasCovering} );
272     }
273
274     // Internal implementation methods
275
//////////////////////////////////
276

277     /** Test that an iterator delivers the expected values */
278     protected void iteratorTest( Iterator i, Object JavaDoc[] expected ) {
279         Log logger = LogFactory.getLog( getClass() );
280         List expList = new ArrayList();
281         for (int j = 0; j < expected.length; j++) {
282             expList.add( expected[j] );
283         }
284
285         while (i.hasNext()) {
286             Object JavaDoc next = i.next();
287
288             // debugging
289
if (!expList.contains( next )) {
290                 logger.debug( getName() + " - Unexpected iterator result: " + next );
291             }
292
293             assertTrue( "Value " + next + " was not expected as a result from this iterator ", expList.contains( next ) );
294             assertTrue( "Value " + next + " was not removed from the list ", expList.remove( next ) );
295         }
296
297         if (!(expList.size() == 0)) {
298             logger.debug( getName() + " Expected iterator results not found" );
299             for (Iterator j = expList.iterator(); j.hasNext(); ) {
300                 logger.debug( getName() + " - missing: " + j.next() );
301             }
302         }
303         assertEquals( "There were expected elements from the iterator that were not found", 0, expList.size() );
304     }
305
306
307     //==============================================================================
308
// Inner class definitions
309
//==============================================================================
310

311 }
312
313
314 /*
315     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
316     All rights reserved.
317
318     Redistribution and use in source and binary forms, with or without
319     modification, are permitted provided that the following conditions
320     are met:
321
322     1. Redistributions of source code must retain the above copyright
323        notice, this list of conditions and the following disclaimer.
324
325     2. Redistributions in binary form must reproduce the above copyright
326        notice, this list of conditions and the following disclaimer in the
327        documentation and/or other materials provided with the distribution.
328
329     3. The name of the author may not be used to endorse or promote products
330        derived from this software without specific prior written permission.
331
332     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
333     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
334     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
335     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
336     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
337     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
338     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
339     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
340     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
341     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
342 */

343
344
Popular Tags