KickJava   Java API By Example, From Geeks To Geeks.

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


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 21-Jun-2003
9  * Filename $RCSfile: TestOntModel.java,v $
10  * Revision $Revision: 1.20 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/04/11 16:38:44 $
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.io.*;
28 import java.util.*;
29
30 import com.hp.hpl.jena.graph.Graph;
31 import com.hp.hpl.jena.ontology.*;
32 import com.hp.hpl.jena.ontology.impl.*;
33 import com.hp.hpl.jena.rdf.model.*;
34 import com.hp.hpl.jena.rdf.model.test.*;
35 import com.hp.hpl.jena.reasoner.test.TestUtil;
36 import com.hp.hpl.jena.shared.PrefixMapping;
37 import com.hp.hpl.jena.vocabulary.RDF;
38
39
40
41 /**
42  * <p>
43  * Unit tests on OntModel capabilities. Many of OntModel's methods are tested by the other
44  * abstractions' unit tests.
45  * </p>
46  *
47  * @author Ian Dickinson, HP Labs
48  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
49  * @version CVS $Id: TestOntModel.java,v 1.20 2005/04/11 16:38:44 ian_dickinson Exp $
50  */

51 public class TestOntModel
52     extends ModelTestBase
53 {
54     // Constants
55
//////////////////////////////////
56

57     // Static variables
58
//////////////////////////////////
59

60     public static final String JavaDoc BASE = "http://www.hp.com/test";
61     public static final String JavaDoc NS = BASE + "#";
62
63     public static final String JavaDoc DOC = "<rdf:RDF" +
64                                      " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" +
65                                      " xmlns:owl=\"http://www.w3.org/2002/07/owl#\"" +
66                                      " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">" +
67                                      " <owl:Class rdf:about=\"http://www.hp.com/test#D\">" +
68                                      " <rdfs:subClassOf>" +
69                                      " <owl:Class rdf:about=\"http://www.hp.com/test#B\"/>" +
70                                      " </rdfs:subClassOf>" +
71                                      " </owl:Class>" +
72                                      " <owl:Class rdf:about=\"http://www.hp.com/test#B\">" +
73                                      " <rdfs:subClassOf rdf:resource=\"http://www.hp.com/test#A\"" +
74                                      " rdf:type=\"http://www.w3.org/2002/07/owl#Class\"/>" +
75                                      " </owl:Class>" +
76                                      " <owl:Class rdf:about=\"http://www.hp.com/test#C\">" +
77                                      " <rdfs:subClassOf rdf:resource=\"http://www.hp.com/test#B\"/>" +
78                                      " </owl:Class>" +
79                                      " <owl:ObjectProperty rdf:about=\"http://www.hp.com/test#p\">" +
80                                      " <rdfs:domain rdf:resource=\"http://www.hp.com/test#A\"/>" +
81                                      " <rdfs:range rdf:resource=\"http://www.hp.com/test#B\"/>" +
82                                      " <rdfs:range rdf:resource=\"http://www.hp.com/test#C\"/>" +
83                                      " </owl:ObjectProperty>" +
84                                      "</rdf:RDF>";
85
86     // Instance variables
87
//////////////////////////////////
88

89     // Constructors
90
//////////////////////////////////
91

92     public TestOntModel( String JavaDoc name ) {
93         super( name );
94     }
95
96     // External signature methods
97
//////////////////////////////////
98

99     public void setUp() {
100         // ensure the ont doc manager is in a consistent state
101
OntDocumentManager.getInstance().reset( true );
102     }
103
104
105     /** Test writing the base model to an output stream */
106     public void testWriteOutputStream() {
107         OntModel m = ModelFactory.createOntologyModel();
108
109         // set up the model
110
OntClass A = m.createClass( NS + "A" );
111         OntClass B = m.createClass( NS + "B" );
112         OntClass C = m.createClass( NS + "C" );
113         OntClass D = m.createClass( NS + "D" );
114
115         A.addSubClass( B );
116         B.addSubClass( C );
117         B.addSubClass( D );
118
119         ObjectProperty p = m.createObjectProperty( NS + "p" );
120
121         p.addDomain( A );
122         p.addRange( B );
123         p.addRange( C );
124
125         // write to a stream
126
ByteArrayOutputStream out = new ByteArrayOutputStream();
127         m.write( out );
128
129         String JavaDoc s = out.toString();
130         ByteArrayInputStream in = new ByteArrayInputStream( s.getBytes() );
131
132         // read it back again
133
Model mIn1 = ModelFactory.createDefaultModel();
134         mIn1.read( in, BASE );
135
136         Model mIn2 = ModelFactory.createDefaultModel();
137         mIn2.read( new ByteArrayInputStream( DOC.getBytes() ), BASE );
138
139         // should be the same
140
assertTrue( "InputStream write/read cycle failed (1)", mIn1.isIsomorphicWith( m.getBaseModel() ) );
141         assertTrue( "InputStream write/read cycle failed (2)", mIn2.isIsomorphicWith( m.getBaseModel() ) );
142     }
143
144     public void testGetBaseModelPrefixes()
145         {
146         OntModel om = ModelFactory.createOntologyModel();
147         om.setNsPrefix( "bill", "http://bill.and.ben/flowerpot#" );
148         om.setNsPrefix( "grue", "ftp://grue.and.bleen/2000#" );
149         assertEquals( om.getNsPrefixMap(), om.getBaseModel().getNsPrefixMap() );
150         }
151
152     /**
153          The prefixes of an OntModel should be the prefixes of its base model,
154          plus any non-clashing ones from the document manager's prefix mapping.
155          (which this test assume includes rdfs and daml).
156     */

157     public void testPrefixDefaulting()
158         {
159         Model base = ModelFactory.createDefaultModel();
160         base.setNsPrefix( "hedgehog", "http://hedgehog.hog/" );
161         base.setNsPrefix( "daml", "not:the-DAML-URI/" );
162         base.setNsPrefix( "mine", RDF.getURI() );
163         OntModel m = new OntModelImpl( OntModelSpec.RDFS_MEM, base );
164         PrefixMapping given = m.getDocumentManager().getDeclaredPrefixMapping();
165         assertEquals( "http://hedgehog.hog/", m.getNsPrefixURI( "hedgehog" ) );
166         assertEquals( "not:the-DAML-URI/", m.getNsPrefixURI( "daml" ) );
167         assertEquals( RDF.getURI(), m.getNsPrefixURI( "mine" ) );
168         assertEquals( null, m.getNsPrefixURI( "rdf" ) );
169         assertEquals( given.getNsPrefixURI( "rdfs" ), m.getNsPrefixURI( "rdfs" ) );
170         }
171
172     public void testWritesPrefixes()
173         {
174         OntModel om = ModelFactory.createOntologyModel();
175         om.setNsPrefix( "spoo", "http://spoo.spoo.com/spoo#" );
176         om.add( statement( om, "ping http://spoo.spoo.com/spoo#pang pilly" ) );
177         om.add( statement( om, "gg http://www.daml.org/2001/03/daml+oil#hh ii" ) );
178         StringWriter sw = new StringWriter();
179         om.write( sw );
180         String JavaDoc s = sw.getBuffer().toString();
181         assertTrue( s.indexOf( "xmlns:spoo=\"http://spoo.spoo.com/spoo#\"" ) > 0 );
182         assertTrue( s.indexOf( "xmlns:daml=\"http://www.daml.org/2001/03/daml+oil#\"" ) > 0 );
183         }
184
185     /** Test writing the base model to an output stream */
186     public void testWriteWriter() {
187         OntModel m = ModelFactory.createOntologyModel();
188
189         // set up the model
190
OntClass A = m.createClass( NS + "A" );
191         OntClass B = m.createClass( NS + "B" );
192         OntClass C = m.createClass( NS + "C" );
193         OntClass D = m.createClass( NS + "D" );
194
195         A.addSubClass( B );
196         B.addSubClass( C );
197         B.addSubClass( D );
198
199         ObjectProperty p = m.createObjectProperty( NS + "p" );
200
201         p.addDomain( A );
202         p.addRange( B );
203         p.addRange( C );
204
205         // write to a stream
206
StringWriter out = new StringWriter();
207         m.write( out );
208
209         String JavaDoc s = out.toString();
210
211         // read it back again
212
Model mIn1 = ModelFactory.createDefaultModel();
213         mIn1.read( new StringReader( s ), BASE );
214
215         Model mIn2 = ModelFactory.createDefaultModel();
216         mIn2.read( new StringReader( DOC ), BASE );
217
218         // should be the same
219
assertTrue( "Writer write/read cycle failed (1)", mIn1.isIsomorphicWith( m.getBaseModel() ) );
220         assertTrue( "Writer write/read cycle failed (2)", mIn2.isIsomorphicWith( m.getBaseModel() ) );
221     }
222
223     public void testGetOntology() {
224         OntModel m = ModelFactory.createOntologyModel();
225         Resource r = m.getResource( NS + "r" );
226         m.add( r, RDF.type, r );
227         Resource s = m.createOntology( NS + "s" );
228         assertEquals( "Result of get s", s, m.getOntology( NS + "s" ) );
229         assertNull( "result of get q", m.getOntology( NS+"q") );
230         assertNull( "result of get r", m.getOntology( NS+"r"));
231     }
232
233
234     public void testGetIndividual() {
235         OntModel m = ModelFactory.createOntologyModel();
236         OntClass c = m.createClass( NS +"c" );
237         Resource r = m.getResource( NS + "r" );
238         m.add( r, RDF.type, r );
239         Resource s = m.createIndividual( NS + "s", c );
240         assertEquals( "Result of get s", s, m.getIndividual( NS + "s" ) );
241         assertNull( "result of get q", m.getIndividual( NS+"q") );
242     }
243
244
245     public void testGetOntProperty() {
246         OntModel m = ModelFactory.createOntologyModel();
247         Resource r = m.getResource( NS + "r" );
248         m.add( r, RDF.type, r );
249         Resource s = m.createOntProperty( NS + "s" );
250         assertEquals( "Result of get s", s, m.getOntProperty( NS + "s" ) );
251         assertNull( "result of get q", m.getOntProperty( NS+"q") );
252         assertNull( "result of get r", m.getOntProperty( NS+"r"));
253     }
254
255
256     public void testGetObjectProperty() {
257         OntModel m = ModelFactory.createOntologyModel();
258         Resource r = m.getResource( NS + "r" );
259         m.add( r, RDF.type, r );
260         Resource s = m.createObjectProperty( NS + "s" );
261         assertEquals( "Result of get s", s, m.getObjectProperty( NS + "s" ) );
262         assertNull( "result of get q", m.getObjectProperty( NS+"q") );
263         assertNull( "result of get r", m.getObjectProperty( NS+"r"));
264     }
265
266
267     public void testGetTransitiveProperty() {
268         OntModel m = ModelFactory.createOntologyModel();
269         Resource r = m.getResource( NS + "r" );
270         m.add( r, RDF.type, r );
271         Resource s = m.createTransitiveProperty( NS + "s" );
272         assertEquals( "Result of get s", s, m.getTransitiveProperty( NS + "s" ) );
273         assertNull( "result of get q", m.getTransitiveProperty( NS+"q") );
274         assertNull( "result of get r", m.getTransitiveProperty( NS+"r"));
275     }
276
277
278     public void testGetSymmetricProperty() {
279         OntModel m = ModelFactory.createOntologyModel();
280         Resource r = m.getResource( NS + "r" );
281         m.add( r, RDF.type, r );
282         Resource s = m.createSymmetricProperty( NS + "s" );
283         assertEquals( "Result of get s", s, m.getSymmetricProperty( NS + "s" ) );
284         assertNull( "result of get q", m.getSymmetricProperty( NS+"q") );
285         assertNull( "result of get r", m.getSymmetricProperty( NS+"r"));
286     }
287
288
289     public void testGetInverseFunctionalProperty() {
290         OntModel m = ModelFactory.createOntologyModel();
291         Resource r = m.getResource( NS + "r" );
292         m.add( r, RDF.type, r );
293         Resource s = m.createInverseFunctionalProperty( NS + "s" );
294         assertEquals( "Result of get s", s, m.getInverseFunctionalProperty( NS + "s" ) );
295         assertNull( "result of get q", m.getInverseFunctionalProperty( NS+"q") );
296         assertNull( "result of get r", m.getInverseFunctionalProperty( NS+"r"));
297     }
298
299
300     public void testGetDatatypeProperty() {
301         OntModel m = ModelFactory.createOntologyModel();
302         Resource r = m.getResource( NS + "r" );
303         m.add( r, RDF.type, r );
304         Resource s = m.createDatatypeProperty( NS + "s" );
305         assertEquals( "Result of get s", s, m.getDatatypeProperty( NS + "s" ) );
306         assertNull( "result of get q", m.getDatatypeProperty( NS+"q") );
307         assertNull( "result of get r", m.getDatatypeProperty( NS+"r"));
308     }
309
310
311     public void testGetAnnotationProperty() {
312         OntModel m = ModelFactory.createOntologyModel();
313         Resource r = m.getResource( NS + "r" );
314         m.add( r, RDF.type, r );
315         Resource s = m.createAnnotationProperty( NS + "s" );
316         assertEquals( "Result of get s", s, m.getAnnotationProperty( NS + "s" ) );
317         assertNull( "result of get q", m.getAnnotationProperty( NS+"q") );
318         assertNull( "result of get r", m.getAnnotationProperty( NS+"r"));
319     }
320
321     public void testGetOntResource() {
322         OntModel m = ModelFactory.createOntologyModel();
323         OntResource r0 = m.getOntResource( NS + "a" );
324         assertNull( r0 );
325         OntResource r1 = m.createOntResource( NS + "aaa" );
326         assertTrue( r1 instanceof OntResource );
327         Resource r2a = m.getResource( NS + "a" );
328         Resource r2b = m.getResource( NS + "b" );
329         Property p = m.getProperty( NS + "p" );
330         m.add( r2a, p, r2b );
331         r0 = m.getOntResource( NS + "a" );
332         assertTrue( r0 instanceof OntResource );
333         OntResource r3 = m.getOntResource( r2b );
334         assertTrue( r3 instanceof OntResource );
335     }
336
337     public void testGetOntClass() {
338         OntModel m = ModelFactory.createOntologyModel();
339         Resource r = m.getResource( NS + "r" );
340         Resource r0 = m.getResource( NS + "r0" );
341         m.add( r, RDF.type, r0 );
342         Resource s = m.createClass( NS + "s" );
343         assertEquals( "Result of get s", s, m.getOntClass( NS + "s" ) );
344         assertNull( "result of get q", m.getOntClass( NS+"q") );
345         assertNull( "result of get r", m.getOntClass( NS+"r"));
346     }
347
348
349     public void testGetComplementClass() {
350         OntModel m = ModelFactory.createOntologyModel();
351         OntClass c = m.createClass( NS +"c" );
352         Resource r = m.getResource( NS + "r" );
353         m.add( r, RDF.type, r );
354         Resource s = m.createComplementClass( NS + "s", c );
355         assertEquals( "Result of get s", s, m.getComplementClass( NS + "s" ) );
356         assertNull( "result of get q", m.getComplementClass( NS+"q") );
357         assertNull( "result of get r", m.getComplementClass( NS+"r"));
358     }
359
360
361     public void testGetEnumeratedClass() {
362         OntModel m = ModelFactory.createOntologyModel();
363         RDFList l = m.createList();
364         Resource r = m.getResource( NS + "r" );
365         m.add( r, RDF.type, r );
366         Resource s = m.createEnumeratedClass( NS + "s", l );
367         assertEquals( "Result of get s", s, m.getEnumeratedClass( NS + "s" ) );
368         assertNull( "result of get q", m.getEnumeratedClass( NS+"q") );
369         assertNull( "result of get r", m.getEnumeratedClass( NS+"r"));
370     }
371
372
373     public void testGetUnionClass() {
374         OntModel m = ModelFactory.createOntologyModel();
375         RDFList l = m.createList();
376         Resource r = m.getResource( NS + "r" );
377         m.add( r, RDF.type, r );
378         Resource s = m.createUnionClass( NS + "s", l );
379         assertEquals( "Result of get s", s, m.getUnionClass( NS + "s" ) );
380         assertNull( "result of get q", m.getUnionClass( NS+"q") );
381         assertNull( "result of get r", m.getUnionClass( NS+"r"));
382     }
383
384
385     public void testGetIntersectionClass() {
386         OntModel m = ModelFactory.createOntologyModel();
387         RDFList l = m.createList();
388         Resource r = m.getResource( NS + "r" );
389         m.add( r, RDF.type, r );
390         Resource s = m.createIntersectionClass( NS + "s", l );
391         assertEquals( "Result of get s", s, m.getIntersectionClass( NS + "s" ) );
392         assertNull( "result of get q", m.getIntersectionClass( NS+"q") );
393         assertNull( "result of get r", m.getIntersectionClass( NS+"r"));
394     }
395
396
397     public void testGetRestriction() {
398         OntModel m = ModelFactory.createOntologyModel();
399         Property p = m.createProperty( NS + "p" );
400         Resource r = m.getResource( NS + "r" );
401         m.add( r, RDF.type, r );
402         Resource s = m.createRestriction( NS + "s", p );
403         assertEquals( "Result of get s", s, m.getRestriction( NS + "s" ) );
404         assertNull( "result of get q", m.getRestriction( NS+"q") );
405         assertNull( "result of get r", m.getRestriction( NS+"r"));
406     }
407
408
409     public void testGetHasValueRestriction() {
410         OntModel m = ModelFactory.createOntologyModel();
411         Property p = m.createProperty( NS + "p" );
412         OntClass c = m.createClass( NS + "c" );
413         Resource r = m.getResource( NS + "r" );
414         m.add( r, RDF.type, r );
415         Resource s = m.createHasValueRestriction( NS + "s", p, c );
416         assertEquals( "Result of get s", s, m.getHasValueRestriction( NS + "s" ) );
417         assertNull( "result of get q", m.getHasValueRestriction( NS+"q") );
418         assertNull( "result of get r", m.getHasValueRestriction( NS+"r"));
419     }
420
421
422     public void testGetSomeValuesFromRestriction() {
423         OntModel m = ModelFactory.createOntologyModel();
424         Property p = m.createProperty( NS + "p" );
425         OntClass c = m.createClass( NS + "c" );
426         Resource r = m.getResource( NS + "r" );
427         m.add( r, RDF.type, r );
428         Resource s = m.createSomeValuesFromRestriction( NS + "s", p, c );
429         assertEquals( "Result of get s", s, m.getSomeValuesFromRestriction( NS + "s" ) );
430         assertNull( "result of get q", m.getSomeValuesFromRestriction( NS+"q") );
431         assertNull( "result of get r", m.getSomeValuesFromRestriction( NS+"r"));
432     }
433
434
435     public void testGetAllValuesFromRestriction() {
436         OntModel m = ModelFactory.createOntologyModel();
437         Property p = m.createProperty( NS + "p" );
438         OntClass c = m.createClass( NS + "c" );
439         Resource r = m.getResource( NS + "r" );
440         m.add( r, RDF.type, r );
441         Resource s = m.createAllValuesFromRestriction( NS + "s", p, c );
442         assertEquals( "Result of get s", s, m.getAllValuesFromRestriction( NS + "s" ) );
443         assertNull( "result of get q", m.getAllValuesFromRestriction( NS+"q") );
444         assertNull( "result of get r", m.getAllValuesFromRestriction( NS+"r"));
445     }
446
447
448     public void testGetCardinalityRestriction() {
449         OntModel m = ModelFactory.createOntologyModel();
450         Property p = m.createProperty( NS + "p" );
451         Resource r = m.getResource( NS + "r" );
452         m.add( r, RDF.type, r );
453         Resource s = m.createCardinalityRestriction( NS + "s", p, 1 );
454         assertEquals( "Result of get s", s, m.getCardinalityRestriction( NS + "s" ) );
455         assertNull( "result of get q", m.getCardinalityRestriction( NS+"q") );
456         assertNull( "result of get r", m.getCardinalityRestriction( NS+"r"));
457     }
458
459
460     public void testGetMinCardinalityRestriction() {
461         OntModel m = ModelFactory.createOntologyModel();
462         Property p = m.createProperty( NS + "p" );
463         Resource r = m.getResource( NS + "r" );
464         m.add( r, RDF.type, r );
465         Resource s = m.createMinCardinalityRestriction( NS + "s", p, 1 );
466         assertEquals( "Result of get s", s, m.getMinCardinalityRestriction( NS + "s" ) );
467         assertNull( "result of get q", m.getMinCardinalityRestriction( NS+"q") );
468         assertNull( "result of get r", m.getMinCardinalityRestriction( NS+"r"));
469     }
470
471
472     public void testGetMaxCardinalityRestriction() {
473         OntModel m = ModelFactory.createOntologyModel();
474         Property p = m.createProperty( NS + "p" );
475         Resource r = m.getResource( NS + "r" );
476         m.add( r, RDF.type, r );
477         Resource s = m.createMaxCardinalityRestriction( NS + "s", p, 1 );
478         assertEquals( "Result of get s", s, m.getMaxCardinalityRestriction( NS + "s" ) );
479         assertNull( "result of get q", m.getMaxCardinalityRestriction( NS+"q") );
480         assertNull( "result of get r", m.getMaxCardinalityRestriction( NS+"r"));
481     }
482
483     public void testGetSubgraphs() {
484         OntModel m = ModelFactory.createOntologyModel();
485         m.read( "file:testing/ontology/testImport6/a.owl" );
486         assertEquals( "Marker count not correct", 4, TestOntDocumentManager.countMarkers( m ) );
487
488         List subs = m.getSubGraphs();
489
490         assertEquals( "n subgraphs should be ", 3, subs.size() );
491
492         boolean isGraph = true;
493         for (Iterator i = subs.iterator(); i.hasNext(); ) {
494             Object JavaDoc x = i.next();
495             if (!(x instanceof Graph)) {
496                 isGraph = false;
497             }
498         }
499         assertTrue( "All sub-graphs should be graphs", isGraph );
500
501     }
502
503
504     public void testListImportURIs() {
505         OntModel m = ModelFactory.createOntologyModel();
506         m.read( "file:testing/ontology/testImport6/a.owl" );
507         Collection c = m.listImportedOntologyURIs();
508
509         assertEquals( "Should be two non-closed import URI's", 2, c.size() );
510         assertTrue( "b should be imported ", c.contains( "file:testing/ontology/testImport6/b.owl" ));
511         assertFalse( "c should not be imported ", c.contains( "file:testing/ontology/testImport6/c.owl" ));
512         assertTrue( "d should be imported ", c.contains( "file:testing/ontology/testImport6/d.owl" ));
513
514         c = m.listImportedOntologyURIs( true );
515
516         assertEquals( "Should be two non-closed import URI's", 3, c.size() );
517         assertTrue( "b should be imported ", c.contains( "file:testing/ontology/testImport6/b.owl" ));
518         assertTrue( "c should be imported ", c.contains( "file:testing/ontology/testImport6/c.owl" ));
519         assertTrue( "d should be imported ", c.contains( "file:testing/ontology/testImport6/d.owl" ));
520     }
521
522     public void testListImportedModels() {
523         OntModel m = ModelFactory.createOntologyModel();
524         m.read( "file:testing/ontology/testImport6/a.owl" );
525         assertEquals( "Marker count not correct", 4, TestOntDocumentManager.countMarkers( m ) );
526
527         List importModels = new ArrayList();
528         for (Iterator j = m.listImportedModels(); j.hasNext(); importModels.add( j.next() ));
529
530         assertEquals( "n import models should be ", 3, importModels.size() );
531
532         boolean isOntModel = true;
533         int nImports = 0;
534
535         for (Iterator i = importModels.iterator(); i.hasNext(); ) {
536             Object JavaDoc x = i.next();
537             if (!(x instanceof OntModel)) {
538                 isOntModel = false;
539             }
540             else {
541                 // count the number of imports of each sub-model
542
OntModel mi = (OntModel) x;
543                 nImports += mi.listImportedOntologyURIs().size();
544             }
545         }
546
547         assertTrue( "All import models should be OntModels", isOntModel );
548         assertEquals( "Wrong number of sub-model imports", 2, nImports );
549     }
550
551     public void testGetImportedModel() {
552         OntModel m = ModelFactory.createOntologyModel();
553         m.read( "file:testing/ontology/testImport6/a.owl" );
554
555         OntModel m0 = m.getImportedModel( "file:testing/ontology/testImport6/b.owl" );
556         OntModel m1 = m.getImportedModel( "file:testing/ontology/testImport6/c.owl" );
557         OntModel m2 = m.getImportedModel( "file:testing/ontology/testImport6/d.owl" );
558         OntModel m3 = m.getImportedModel( "file:testing/ontology/testImport6/b.owl" )
559                        .getImportedModel( "file:testing/ontology/testImport6/c.owl" );
560         OntModel m4 = m.getImportedModel( "file:testing/ontology/testImport6/a.owl" );
561
562         assertNotNull( "Import model b should not be null", m0 );
563         assertNotNull( "Import model c should not be null", m1 );
564         assertNotNull( "Import model d should not be null", m2 );
565         assertNotNull( "Import model b-c should not be null", m3 );
566         assertNull( "Import model a should be null", m4 );
567     }
568
569     /**
570      * Test that the supports checks that are defined in the OWL full profile are not
571      * missing in the DL and Lite profiles, unless by design.
572      * Not strictly a model test, but it has to go somewhere */

573     public void testProfiles() {
574         List notInDL = Arrays.asList( new Class JavaDoc[] {} );
575         List notInLite = Arrays.asList( new Class JavaDoc[] {DataRange.class, HasValueRestriction.class} );
576
577         Map fullProfileMap = new OWLProfileExt().getSupportsMap();
578         Map dlProfileMap = new OWLDLProfileExt().getSupportsMap();
579         Map liteProfileMap = new OWLLiteProfileExt().getSupportsMap();
580
581         for (Iterator i = fullProfileMap.entrySet().iterator(); i.hasNext(); ) {
582             Map.Entry kv = (Map.Entry) i.next();
583             Class JavaDoc c = (Class JavaDoc) kv.getKey();
584             assertTrue( "Key in OWL DL profile: " + c.getName(), dlProfileMap.containsKey( c ) || notInDL.contains( c ));
585             assertTrue( "Key in OWL lite profile: " + c.getName(), liteProfileMap.containsKey( c ) || notInLite.contains( c ));
586         }
587     }
588
589
590     /**
591         Added by kers to ensure that bulk update works; should really be a test
592         of the ontology Graph using AbstractTestGraph, but that fails because there
593         are too many things that don't pass those tests.
594     <p>
595         <b>Yet</b>.
596     */

597     public void testBulkAddWorks()
598         {
599         OntModel om1= ModelFactory.createOntologyModel();
600         OntModel om2 = ModelFactory.createOntologyModel();
601         om1.add( om2 );
602         }
603
604     public void testRead() {
605         String JavaDoc base0 = "http://example.com/test0";
606         String JavaDoc ns0 = base0 + "#";
607         String JavaDoc base1 = "http://example.com/test1";
608         String JavaDoc ns1 = base1 + "#";
609
610         OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
611         m.getDocumentManager().reset();
612         m.getDocumentManager().addAltEntry( base0, "file:testing/ontology/relativenames.rdf" );
613         m.read( base0, "RDF/XML" );
614         assertNotNull( "Should be a class ns0:A", m.getOntClass( ns0 + "A" ) );
615         assertNull( "Should not be a class ns1:A", m.getOntClass( ns1 + "A" ) );
616
617         m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
618         m.getDocumentManager().reset();
619         m.getDocumentManager().addAltEntry( base0, "file:testing/ontology/relativenames.rdf" );
620         m.read( base0, base1, "RDF/XML" );
621         assertNull( "Should not be a class ns0:A", m.getOntClass( ns0 + "A" ) );
622         assertNotNull( "Should be a class ns1:A", m.getOntClass( ns1 + "A" ) );
623     }
624
625     public void testListDataRange() {
626         String JavaDoc base = "http://jena.hpl.hp.com/test#";
627         String JavaDoc doc =
628                  "<?xml version='1.0'?>"
629                 + "<!DOCTYPE owl ["
630                 + " <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' >"
631                 + " <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#' >"
632                 + " <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#' >"
633                 + " <!ENTITY owl 'http://www.w3.org/2002/07/owl#' >"
634                 + " <!ENTITY dc 'http://purl.org/dc/elements/1.1/' >"
635                 + " <!ENTITY base 'http://jena.hpl.hp.com/test' >"
636                 + " ]>"
637                 + "<rdf:RDF"
638                 + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
639                 + " xmlns:owl='http://www.w3.org/2002/07/owl#'>"
640                 + " <owl:DataRange>"
641                 + " <owl:oneOf>"
642                 + " <rdf:List>"
643                 + " <rdf:first rdf:datatype='&xsd;integer'>0</rdf:first>"
644                 + " <rdf:rest rdf:resource='&rdf;nil' />"
645                 + " </rdf:List>"
646                 + " </owl:oneOf>"
647                 + " </owl:DataRange>"
648                 + "</rdf:RDF>";
649
650         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
651         m.read(new StringReader(doc), base);
652
653         Iterator i = m.listDataRanges();
654         assertTrue( "Should be at least one DataRange", i.hasNext() );
655         Object JavaDoc dr = i.next();
656         assertTrue( dr instanceof DataRange );
657         assertFalse( "Should no more DataRange", i.hasNext() );
658     }
659
660
661
662     public void testListHierarchyRoots0() {
663         OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
664         assertFalse( m.listHierarchyRootClasses().hasNext() );
665         m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
666         assertFalse( m.listHierarchyRootClasses().hasNext() );
667     }
668
669     public void testListHierarchyRoots1() {
670         String JavaDoc doc =
671                   "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. "
672                 + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. "
673                 + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. "
674                 + "@prefix owl: <http://www.w3.org/2002/07/owl#>. "
675                 + "@prefix : <" + NS + ">. "
676                 + ":A a owl:Class. "
677                 ;
678
679         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
680         m.read( new StringReader(doc), NS, "N3" );
681
682         OntClass a = m.getOntClass(NS+"A");
683         TestUtil.assertIteratorValues( this, m.listHierarchyRootClasses(),
684                                        new Object JavaDoc[] {a} );
685     }
686
687
688     public void testListHierarchyRoots2() {
689         String JavaDoc doc =
690                   "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. "
691                 + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. "
692                 + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. "
693                 + "@prefix owl: <http://www.w3.org/2002/07/owl#>. "
694                 + "@prefix : <" + NS + ">. "
695                 + ":A a owl:Class. "
696                 ;
697
698         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, null);
699         m.read( new StringReader(doc), NS, "N3" );
700
701         OntClass a = m.getOntClass(NS+"A");
702         TestUtil.assertIteratorValues( this, m.listHierarchyRootClasses(),
703                                        new Object JavaDoc[] {a} );
704     }
705
706
707     public void testListHierarchyRoots3() {
708         String JavaDoc doc =
709                   "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. "
710                 + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. "
711                 + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. "
712                 + "@prefix owl: <http://www.w3.org/2002/07/owl#>. "
713                 + "@prefix : <" + NS + ">. "
714                 + ":A a owl:Class. "
715                 + ":B a owl:Class ; rdfs:subClassOf :A . "
716                 ;
717
718         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MINI_RULE_INF, null);
719         m.read( new StringReader(doc), NS, "N3" );
720
721         OntClass a = m.getOntClass(NS+"A");
722         TestUtil.assertIteratorValues( this, m.listHierarchyRootClasses(),
723                                        new Object JavaDoc[] {a} );
724     }
725
726     public void testListHierarchyRoots4() {
727         String JavaDoc doc =
728                   "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. "
729                 + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. "
730                 + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. "
731                 + "@prefix owl: <http://www.w3.org/2002/07/owl#>. "
732                 + "@prefix : <" + NS + ">. "
733                 + ":A a rdfs:Class. "
734                 + ":C a rdfs:Class. "
735                 + ":B a rdfs:Class ; rdfs:subClassOf :A . "
736                 ;
737
738         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF, null);
739         m.read( new StringReader(doc), NS, "N3" );
740
741         OntClass a = m.getOntClass(NS+"A");
742         OntClass c = m.getOntClass(NS+"C");
743         TestUtil.assertIteratorValues( this, m.listHierarchyRootClasses(),
744                                        new Object JavaDoc[] {a,c} );
745     }
746
747
748     // Internal implementation methods
749
//////////////////////////////////
750

751     //==============================================================================
752
// Inner class definitions
753
//==============================================================================
754

755     protected class OWLProfileExt extends OWLProfile
756     {
757         public Map getSupportsMap() {
758             return getCheckTable();
759         }
760     }
761
762     protected class OWLDLProfileExt extends OWLDLProfile
763     {
764         public Map getSupportsMap() {
765             return getCheckTable();
766         }
767     }
768
769     protected class OWLLiteProfileExt extends OWLLiteProfile
770     {
771         public Map getSupportsMap() {
772             return getCheckTable();
773         }
774     }
775 }
776
777
778 /*
779     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
780     All rights reserved.
781
782     Redistribution and use in source and binary forms, with or without
783     modification, are permitted provided that the following conditions
784     are met:
785
786     1. Redistributions of source code must retain the above copyright
787        notice, this list of conditions and the following disclaimer.
788
789     2. Redistributions in binary form must reproduce the above copyright
790        notice, this list of conditions and the following disclaimer in the
791        documentation and/or other materials provided with the distribution.
792
793     3. The name of the author may not be used to endorse or promote products
794        derived from this software without specific prior written permission.
795
796     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
797     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
798     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
799     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
800     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
801     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
802     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
803     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
804     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
805     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
806 */

807
808
Popular Tags