KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > dig > test > TestDigReasoner


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 11-Sep-2003
9  * Filename $RCSfile: TestDigReasoner.java,v $
10  * Revision $Revision: 1.21 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/03/16 18:55:08 $
14  * by $Author: ian_dickinson $
15  *
16  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * [See end of file]
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.reasoner.dig.test;
23
24
25
26 // Imports
27
///////////////
28
import org.apache.commons.logging.*;
29 import org.w3c.dom.*;
30
31
32 import com.hp.hpl.jena.ontology.*;
33 import com.hp.hpl.jena.rdf.model.*;
34 import com.hp.hpl.jena.reasoner.ReasonerRegistry;
35 import com.hp.hpl.jena.reasoner.dig.*;
36 import com.hp.hpl.jena.reasoner.test.TestUtil;
37 import com.hp.hpl.jena.vocabulary.*;
38 import com.hp.hpl.jena.vocabulary.OWL;
39 import com.hp.hpl.jena.vocabulary.RDFS;
40
41 import junit.framework.*;
42
43 import java.io.*;
44 import java.util.*;
45
46 import javax.xml.parsers.*;
47 import javax.xml.parsers.DocumentBuilder JavaDoc;
48
49
50 /**
51  * <p>
52  * Unit tests for DIG reasoners
53  * </p>
54  *
55  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
56  * @version Release @release@ ($Id: TestDigReasoner.java,v 1.21 2005/03/16 18:55:08 ian_dickinson Exp $)
57  */

58 public class TestDigReasoner
59     extends TestCase
60 {
61     // Constants
62
//////////////////////////////////
63

64     // Static variables
65
//////////////////////////////////
66

67     private static Log log = LogFactory.getLog( TestDigReasoner.class );
68
69     // Instance variables
70
//////////////////////////////////
71

72     protected Model m_base;
73
74     // Constructors
75
//////////////////////////////////
76

77     public TestDigReasoner( String JavaDoc name ) {
78         super( name );
79     }
80
81
82
83     // External signature methods
84
//////////////////////////////////
85

86     public static TestSuite suite() {
87         TestSuite s = new TestSuite( "TestDigReasoner" );
88
89         buildConceptLangSuite( "testing/ontology/dig/owl/cl", OntModelSpec.OWL_MEM, s );
90         buildBasicQuerySuite( "testing/ontology/dig/owl/basicq", OntModelSpec.OWL_MEM, s );
91
92         // add the standard tests from this class
93
s.addTestSuite( TestDigReasoner.class );
94         return s;
95     }
96
97
98     public void setUp() {
99         // ensure the ont doc manager is in a consistent state
100
OntDocumentManager.getInstance().reset( true );
101     }
102
103     public void testAxioms() {
104         String JavaDoc NS = "http://example.org/foo#";
105
106         DIGReasoner r = (DIGReasoner) ReasonerRegistry.getDIGReasoner();
107         DIGReasoner ro = (DIGReasoner) ReasonerRegistry.getDIGReasoner( OWL.NAMESPACE, null );
108         DIGReasoner rd = (DIGReasoner) ReasonerRegistry.getDIGReasoner( DAML_OIL.NAMESPACE_DAML, null );
109         DIGReasoner roA = (DIGReasoner) ReasonerRegistry.getDIGReasoner( OWL.NAMESPACE, true, null );
110         DIGReasoner rdA = (DIGReasoner) ReasonerRegistry.getDIGReasoner( DAML_OIL.NAMESPACE_DAML, true, null );
111
112         axiomTestAux( r, OntModelSpec.OWL_MEM, false, false );
113         //axiomTestAux( r, OntModelSpec.DAML_MEM, false, false );
114

115         axiomTestAux( ro, OntModelSpec.OWL_MEM, false, false );
116         axiomTestAux( rd, OntModelSpec.DAML_MEM, false, false );
117
118         axiomTestAux( roA, OntModelSpec.OWL_MEM, true, false );
119         axiomTestAux( rdA, OntModelSpec.DAML_MEM, false, true );
120     }
121
122     private void axiomTestAux( DIGReasoner dr, OntModelSpec baseSpec, boolean owlResult, boolean damlResult ) {
123         OntModelSpec spec = new OntModelSpec( baseSpec );
124         spec.setReasoner( dr );
125         OntModel m = ModelFactory.createOntologyModel( spec, null );
126
127         assertEquals( "Result for owl:ObjectProperty", owlResult, m.contains( OWL.ObjectProperty, RDF.type, RDFS.Class ));
128         assertEquals( "Result for daml:ObjectProperty", damlResult, m.contains( DAML_OIL.ObjectProperty, RDF.type, RDFS.Class ));
129     }
130
131     public void testQueryAllConcepts() {
132         String JavaDoc NS = "http://example.org/foo#";
133
134         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
135
136         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
137         spec.setReasoner( r );
138         OntModel m = ModelFactory.createOntologyModel( spec, null );
139         m.read( "file:testing/ontology/dig/owl/test1.xml" );
140
141         TestUtil.assertIteratorValues( this, m.listClasses(),
142                                        new Resource[] {
143                                            m.getResource( NS + "A" ), m.getResource( NS + "B" ),
144                                            m.getResource( NS + "C"), m.getResource( NS + "D"),
145                                            m.getResource( NS + "E"), m.getResource( NS + "BB"),
146                                            m.getResource( NS + "F0"), m.getResource( NS + "F1"), m.getResource( NS + "F2"),
147                                        }, 2 );
148     }
149
150
151     public void testQuerySubsumes1() {
152         String JavaDoc NS = "http://example.org/foo#";
153
154         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
155
156         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
157         spec.setReasoner( r );
158         OntModel m = ModelFactory.createOntologyModel( spec, null );
159         m.read( "file:testing/ontology/dig/owl/test1.xml" );
160
161         OntClass A = m.getOntClass( NS + "A" );
162         OntClass B = m.getOntClass( NS + "B" );
163         assertTrue( "A should be a sub-class of B", A.hasSuperClass( B ) );
164     }
165
166     public void testQuerySubsumes2() {
167         String JavaDoc NS = "http://example.org/foo#";
168
169         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
170
171         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
172         spec.setReasoner( r );
173         OntModel m = ModelFactory.createOntologyModel( spec, null );
174         m.read( "file:testing/ontology/dig/owl/test1.xml" );
175
176         OntClass C = m.getOntClass( NS + "C" );
177         OntClass D = m.getOntClass( NS + "D" );
178         assertTrue( "D should be a sub-class of C", D.hasSuperClass( C ) );
179     }
180
181     public void testQuerySubsumes3() {
182         String JavaDoc NS = "http://example.org/foo#";
183
184         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
185
186         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
187         spec.setReasoner( r );
188         OntModel m = ModelFactory.createOntologyModel( spec, null );
189         m.read( "file:testing/ontology/dig/owl/test1.xml" );
190
191         OntClass A = m.getOntClass( NS + "A" );
192         OntClass C = m.getOntClass( NS + "C" );
193         assertFalse( "A should not be a super-class of C", C.hasSuperClass( A ) );
194         assertFalse( "C should not be a super-class of A", A.hasSuperClass( C ) );
195     }
196
197     public void testAncestors0() {
198         String JavaDoc NS = "http://example.org/foo#";
199
200         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
201
202         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
203         spec.setReasoner( r );
204         OntModel m = ModelFactory.createOntologyModel( spec, null );
205         m.read( "file:testing/ontology/dig/owl/test1.xml" );
206
207         OntClass A = m.getOntClass( NS + "A" );
208         OntClass B = m.getOntClass( NS + "B" );
209         OntClass BB = m.getOntClass( NS + "BB" );
210
211         TestUtil.assertIteratorValues( this, A.listSuperClasses(),
212                                        new Resource[] {B,BB, OWL.Thing} );
213     }
214
215     public void testAncestors1() {
216         String JavaDoc NS = "http://example.org/foo#";
217
218         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
219
220         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
221         spec.setReasoner( r );
222         OntModel m = ModelFactory.createOntologyModel( spec, null );
223         m.read( "file:testing/ontology/dig/owl/test1.xml" );
224
225         OntClass A = m.getOntClass( NS + "A" );
226         OntClass B = m.getOntClass( NS + "B" );
227         OntClass BB = m.getOntClass( NS + "BB" );
228
229         assertTrue( A.hasSuperClass( B ) );
230         assertTrue( A.hasSuperClass( BB ) );
231         assertTrue( B.hasSuperClass( BB ) );
232
233         assertTrue( BB.hasSubClass( B ) );
234         assertTrue( BB.hasSubClass( A ) );
235         assertTrue( B.hasSubClass( A ) );
236
237         assertFalse( B.hasSuperClass( A ) );
238         assertFalse( A.hasSubClass( BB ) );
239
240         assertTrue( A.hasSuperClass( OWL.Thing ));
241         assertFalse( A.hasSubClass( OWL.Thing ));
242     }
243
244     public void testDescendants() {
245         String JavaDoc NS = "http://example.org/foo#";
246
247         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
248
249         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
250         spec.setReasoner( r );
251         OntModel m = ModelFactory.createOntologyModel( spec, null );
252         m.read( "file:testing/ontology/dig/owl/test1.xml" );
253
254         OntClass A = m.getOntClass( NS + "A" );
255         OntClass B = m.getOntClass( NS + "B" );
256         OntClass BB = m.getOntClass( NS + "BB" );
257
258         TestUtil.assertIteratorValues( this, BB.listSubClasses(),
259                                        new Resource[] {B,A, OWL.Nothing} );
260     }
261
262     public void testAllClassHierarchy() {
263         String JavaDoc NS = "http://example.org/foo#";
264
265         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
266
267         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
268         spec.setReasoner( r );
269         OntModel m = ModelFactory.createOntologyModel( spec, null );
270         m.read( "file:testing/ontology/dig/owl/test1.xml" );
271
272         OntClass A = m.getOntClass( NS + "A" );
273         OntClass B = m.getOntClass( NS + "B" );
274         OntClass BB = m.getOntClass( NS + "BB" );
275         OntClass C = m.getOntClass( NS + "C" );
276         OntClass D = m.getOntClass( NS + "D" );
277         OntClass E = m.getOntClass( NS + "E" );
278         OntClass F0 = m.getOntClass( NS + "F0" );
279         OntClass F1 = m.getOntClass( NS + "F1" );
280         OntClass F2 = m.getOntClass( NS + "F2" );
281
282         TestUtil.assertIteratorValues( this, m.listStatements( null, RDFS.subClassOf, (RDFNode) null ),
283                                        new Statement[] {
284                                            m.createStatement( A, RDFS.subClassOf, A ),
285                                            m.createStatement( A, RDFS.subClassOf, B ),
286                                            m.createStatement( A, RDFS.subClassOf, BB ),
287                                            m.createStatement( B, RDFS.subClassOf, B ),
288                                            m.createStatement( B, RDFS.subClassOf, BB ),
289                                            m.createStatement( BB, RDFS.subClassOf, BB ),
290                                            m.createStatement( C, RDFS.subClassOf, C ),
291                                            m.createStatement( D, RDFS.subClassOf, D ),
292                                            m.createStatement( D, RDFS.subClassOf, C ),
293                                            m.createStatement( E, RDFS.subClassOf, C ),
294                                            m.createStatement( E, RDFS.subClassOf, E ),
295                                            m.createStatement( F0, RDFS.subClassOf, F0 ),
296                                            m.createStatement( F1, RDFS.subClassOf, F1 ),
297                                            m.createStatement( F2, RDFS.subClassOf, F2 ),
298                                            m.createStatement( A, RDFS.subClassOf, OWL.Thing ),
299                                            m.createStatement( B, RDFS.subClassOf, OWL.Thing ),
300                                            m.createStatement( BB, RDFS.subClassOf, OWL.Thing ),
301                                            m.createStatement( C, RDFS.subClassOf, OWL.Thing ),
302                                            m.createStatement( D, RDFS.subClassOf, OWL.Thing ),
303                                            m.createStatement( E, RDFS.subClassOf, OWL.Thing ),
304                                            m.createStatement( F0, RDFS.subClassOf, OWL.Thing ),
305                                            m.createStatement( F1, RDFS.subClassOf, OWL.Thing ),
306                                            m.createStatement( F2, RDFS.subClassOf, OWL.Thing ),
307                                        }, 4 );
308     }
309
310     public void testQueryDisjoint1() {
311         String JavaDoc NS = "http://example.org/foo#";
312
313         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
314
315         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
316         spec.setReasoner( r );
317         OntModel m = ModelFactory.createOntologyModel( spec, null );
318         m.read( "file:testing/ontology/dig/owl/test1.xml" );
319
320         OntClass A = m.getOntClass( NS + "A" );
321         OntClass C = m.getOntClass( NS + "C" );
322
323         assertTrue( "A should be disjoint with C", A.isDisjointWith( C ) );
324         assertTrue( "C should be disjoint with A", C.isDisjointWith( A ) );
325     }
326
327
328     public void testQueryDisjoint2() {
329         String JavaDoc NS = "http://example.org/foo#";
330
331         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
332
333         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
334         spec.setReasoner( r );
335         OntModel m = ModelFactory.createOntologyModel( spec, null );
336         m.read( "file:testing/ontology/dig/owl/test1.xml" );
337
338         OntClass F0 = m.getOntClass( NS + "F0" );
339         OntClass F1 = m.getOntClass( NS + "F1" );
340
341         assertTrue( "F0 should be disjoint with F1", F0.isDisjointWith( F1 ) );
342         assertTrue( "F1 should be disjoint with F0", F1.isDisjointWith( F0 ) );
343     }
344
345     public void testParents0() {
346         String JavaDoc NS = "http://example.org/foo#";
347
348         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
349
350         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
351         spec.setReasoner( r );
352         OntModel m = ModelFactory.createOntologyModel( spec, null );
353         m.read( "file:testing/ontology/dig/owl/test1.xml" );
354
355         OntClass A = m.getOntClass( NS + "A" );
356         OntClass B = m.getOntClass( NS + "B" );
357
358         // note - direct super class
359
TestUtil.assertIteratorValues( this, A.listSuperClasses( true ),
360                                        new Resource[] {B} );
361     }
362
363     public void testParents1() {
364         String JavaDoc NS = "http://example.org/foo#";
365
366         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
367
368         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
369         spec.setReasoner( r );
370         OntModel m = ModelFactory.createOntologyModel( spec, null );
371         m.read( "file:testing/ontology/dig/owl/test1.xml" );
372
373         OntClass A = m.getOntClass( NS + "A" );
374         OntClass B = m.getOntClass( NS + "B" );
375         OntClass BB = m.getOntClass( NS + "BB" );
376
377         // note - direct super class
378
assertTrue( A.hasSuperClass( B, true ) );
379         assertTrue( B.hasSubClass( A, true ) );
380         assertFalse( A.hasSuperClass( BB, true ) );
381
382         assertTrue( BB.hasSuperClass( OWL.Thing, true ) );
383         assertFalse( B.hasSuperClass( OWL.Thing, true ) );
384     }
385
386     public void testChildren() {
387         String JavaDoc NS = "http://example.org/foo#";
388
389         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
390
391         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
392         spec.setReasoner( r );
393         OntModel m = ModelFactory.createOntologyModel( spec, null );
394         m.read( "file:testing/ontology/dig/owl/test1.xml" );
395
396         OntClass B = m.getOntClass( NS + "B" );
397         OntClass BB = m.getOntClass( NS + "BB" );
398
399         // note direct sub-class
400
TestUtil.assertIteratorValues( this, BB.listSubClasses(true),
401                                        new Resource[] {B} );
402     }
403
404     public void testEquivalents() {
405         String JavaDoc NS = "http://example.org/foo#";
406
407         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
408
409         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
410         spec.setReasoner( r );
411         OntModel m = ModelFactory.createOntologyModel( spec, null );
412         m.read( "file:testing/ontology/dig/owl/test1.xml" );
413
414         OntClass F0 = m.getOntClass( NS + "F0" );
415         OntClass F2 = m.getOntClass( NS + "F2" );
416         OntClass F1 = m.getOntClass( NS + "F1" );
417
418         TestUtil.assertIteratorValues( this, F0.listEquivalentClasses(),
419                                        new Resource[] {F2, F0}, 1 );
420         TestUtil.assertIteratorValues( this, F2.listEquivalentClasses(),
421                                        new Resource[] {F0, F2}, 1 );
422         TestUtil.assertIteratorValues( this, F1.listEquivalentClasses(),
423                                        new Resource[] {F1}, 1 );
424     }
425
426
427     public void testIsEquivalent() {
428         String JavaDoc NS = "http://example.org/foo#";
429
430         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
431
432         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
433         spec.setReasoner( r );
434         OntModel m = ModelFactory.createOntologyModel( spec, null );
435         m.read( "file:testing/ontology/dig/owl/test1.xml" );
436
437         OntClass F0 = m.getOntClass( NS + "F0" );
438         OntClass F2 = m.getOntClass( NS + "F2" );
439         OntClass F1 = m.getOntClass( NS + "F1" );
440
441         assertTrue( "F0 should be equivalent to F2", F0.hasEquivalentClass( F2 ));
442         assertFalse( "F0 should not be equivalent to F1", F1.hasEquivalentClass( F0 ));
443     }
444
445
446     public void testRAncestors() {
447         String JavaDoc NS = "http://example.org/foo#";
448
449         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
450
451         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
452         spec.setReasoner( r );
453         OntModel m = ModelFactory.createOntologyModel( spec, null );
454         m.read( "file:testing/ontology/dig/owl/test1.xml" );
455
456         ObjectProperty p0 = m.getObjectProperty( NS + "p0" );
457         ObjectProperty p1 = m.getObjectProperty( NS + "p1" );
458         ObjectProperty p2 = m.getObjectProperty( NS + "p2" );
459
460         TestUtil.assertIteratorValues( this, p0.listSuperProperties(),
461                                        new Resource[] {p1, p2, p0} );
462     }
463
464     public void testRDescendants() {
465         String JavaDoc NS = "http://example.org/foo#";
466
467         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
468
469         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
470         spec.setReasoner( r );
471         OntModel m = ModelFactory.createOntologyModel( spec, null );
472         m.read( "file:testing/ontology/dig/owl/test1.xml" );
473
474         ObjectProperty p0 = m.getObjectProperty( NS + "p0" );
475         ObjectProperty p1 = m.getObjectProperty( NS + "p1" );
476         ObjectProperty p2 = m.getObjectProperty( NS + "p2" );
477
478         TestUtil.assertIteratorValues( this, p2.listSubProperties(),
479                                        new Resource[] {p1, p0, p2} );
480     }
481
482
483     public void testRParents() {
484         String JavaDoc NS = "http://example.org/foo#";
485
486         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
487
488         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
489         spec.setReasoner( r );
490         OntModel m = ModelFactory.createOntologyModel( spec, null );
491         m.read( "file:testing/ontology/dig/owl/test1.xml" );
492
493         ObjectProperty p0 = m.getObjectProperty( NS + "p0" );
494         ObjectProperty p1 = m.getObjectProperty( NS + "p1" );
495         //ObjectProperty p2 = m.getObjectProperty( NS + "p2" );
496

497         TestUtil.assertIteratorValues( this, p0.listSuperProperties(true),
498                                        new Resource[] {p1} );
499     }
500
501     public void testRChildren() {
502         String JavaDoc NS = "http://example.org/foo#";
503
504         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
505
506         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
507         spec.setReasoner( r );
508         OntModel m = ModelFactory.createOntologyModel( spec, null );
509         m.read( "file:testing/ontology/dig/owl/test1.xml" );
510
511         //ObjectProperty p0 = m.getObjectProperty( NS + "p0" );
512
ObjectProperty p1 = m.getObjectProperty( NS + "p1" );
513         ObjectProperty p2 = m.getObjectProperty( NS + "p2" );
514
515         TestUtil.assertIteratorValues( this, p2.listSubProperties(true),
516                                        new Resource[] {p1} );
517     }
518
519     public void testInstances() {
520         String JavaDoc NS = "http://example.org/foo#";
521
522         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
523
524         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
525         spec.setReasoner( r );
526         OntModel m = ModelFactory.createOntologyModel( spec, null );
527         m.read( "file:testing/ontology/dig/owl/test1.xml" );
528
529         OntClass F0 = m.getOntClass( NS + "F0" );
530         Resource i0 = m.getResource( NS + "i0" );
531         Resource i1 = m.getResource( NS + "i1" );
532         Resource i2 = m.getResource( NS + "i2" );
533         Resource q0 = m.getResource( NS + "q0" );
534         Resource q1 = m.getResource( NS + "q1" );
535         Resource q2 = m.getResource( NS + "q2" );
536
537         TestUtil.assertIteratorValues( this, F0.listInstances(),
538                                        new Resource[] {i0, i1, i2, q0, q2, q1} );
539     }
540
541     public void testTypes() {
542         String JavaDoc NS = "http://example.org/foo#";
543
544         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
545
546         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
547         spec.setReasoner( r );
548         OntModel m = ModelFactory.createOntologyModel( spec, null );
549         m.read( "file:testing/ontology/dig/owl/test1.xml" );
550
551         OntClass F0 = m.getOntClass( NS + "F0" );
552         OntClass F2 = m.getOntClass( NS + "F2" );
553         Individual i0 = m.getIndividual( NS + "i0" );
554
555         TestUtil.assertIteratorValues( this, i0.listRDFTypes(false),
556                                        new Resource[] {F0, F2}, 1 );
557     }
558
559     public void testInstance() {
560         String JavaDoc NS = "http://example.org/foo#";
561
562         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
563
564         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
565         spec.setReasoner( r );
566         OntModel m = ModelFactory.createOntologyModel( spec, null );
567         m.read( "file:testing/ontology/dig/owl/test1.xml" );
568
569         OntClass F0 = m.getOntClass( NS + "F0" );
570         OntClass F1 = m.getOntClass( NS + "F1" );
571         Individual i0 = m.getIndividual( NS + "i0" );
572
573         assertTrue( "i0 should be an instance of F0", i0.hasRDFType( F0 ));
574         assertFalse( "i0 should not be an instance of F1", i0.hasRDFType( F1 ));
575     }
576
577     public void testRoleFillers() {
578         String JavaDoc NS = "http://example.org/foo#";
579
580         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
581
582         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
583         spec.setReasoner( r );
584         OntModel m = ModelFactory.createOntologyModel( spec, null );
585         m.read( "file:testing/ontology/dig/owl/test1.xml" );
586
587         Individual q0 = m.getIndividual( NS + "q0" );
588         Individual q1 = m.getIndividual( NS + "q1" );
589         Individual q2 = m.getIndividual( NS + "q2" );
590         Property q = m.getProperty( NS + "q" );
591
592         TestUtil.assertIteratorValues( this, q0.listPropertyValues( q ),
593                                        new Resource[] {q1, q2}, 0 );
594     }
595
596     public void testIncrementalAdd() {
597         String JavaDoc NS = "http://example.org/foo#";
598
599         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
600
601         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
602         spec.setReasoner( r );
603         OntModel m = ModelFactory.createOntologyModel( spec, null );
604
605         OntClass F0 = m.createClass( NS + "F0" );
606         OntClass F1 = m.createClass( NS + "F1" );
607         OntClass F2 = m.createClass( NS + "F2" );
608         Individual i0 = m.createIndividual( NS + "i0", F0 );
609
610         assertFalse( "i0 is not an F2", i0.hasRDFType( F2 ) );
611         F0.addSuperClass( F1 );
612         F1.addSuperClass( F2 );
613         assertTrue( "i0 should now be an F2", i0.hasRDFType( F2 ) );
614     }
615
616
617     public void testIncrementalRemove() {
618         String JavaDoc NS = "http://example.org/foo#";
619
620         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
621
622         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
623         spec.setReasoner( r );
624         OntModel m = ModelFactory.createOntologyModel( spec, null );
625
626         OntClass F0 = m.createClass( NS + "F0" );
627         OntClass F1 = m.createClass( NS + "F1" );
628         OntClass F2 = m.createClass( NS + "F2" );
629         Individual i0 = m.createIndividual( NS + "i0", F0 );
630         F0.addSuperClass( F1 );
631         F1.addSuperClass( F2 );
632
633         assertTrue( "i0 is an F2", i0.hasRDFType( F2 ) );
634         F0.removeSuperClass( F1 );
635         assertFalse( "i0 should now not be an F2", i0.hasRDFType( F2 ) );
636     }
637
638     public void testRemoveIndividual() {
639         String JavaDoc NS = "http://example.org/foo#";
640
641         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
642
643         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
644         spec.setReasoner( r );
645         OntModel m = ModelFactory.createOntologyModel( spec, null );
646         m.read( "file:testing/ontology/dig/owl/test1.xml" );
647
648         OntClass F0 = m.createClass( NS + "F0" );
649         Individual i0 = m.getIndividual( NS + "i0" );
650
651         boolean sawI0 = false;
652         for (Iterator i = F0.listInstances(); i.hasNext(); ) {
653             Resource r0 = (Resource) i.next();
654             log.debug( "Pre remove, saw F0 instance " + r0 );
655             sawI0 = sawI0 || r0.getURI().equals( i0.getURI() );
656         }
657         assertTrue( sawI0 );
658
659         i0.remove();
660
661         sawI0 = false;
662         for (Iterator i = F0.listInstances(); i.hasNext(); ) {
663             Resource r0 = (Resource) i.next();
664             log.debug( "Post remove, saw F0 instance " + r0 );
665             sawI0 = sawI0 || r0.getURI().equals( i0.getURI() );
666         }
667         assertFalse( sawI0 );
668     }
669
670
671     // User bug reports
672

673     public void test_bug_codebaker_01() {
674         String JavaDoc NS = "http://www.owl-ontologies.com/pizza.owl#";
675
676         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
677
678         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
679         spec.setReasoner( r );
680         OntModel m = ModelFactory.createOntologyModel( spec, null );
681
682         m.read( "file:testing/ontology/bugs/test_codebaker_01.owl" );
683
684         OntClass mp = m.getOntClass( NS + "MargheritaPizza" );
685         OntClass cp = m.getOntClass( NS + "CheesyPizza" );
686
687         assertTrue( "MargheritaPizza should be cheesy", mp.hasSuperClass( cp ) );
688     }
689
690     public void test_bug_koala_01() {
691         // set up a configuration resource to connect to the reasoner
692
// on port 2004 on the local system
693
Model cModel = ModelFactory.createDefaultModel();
694         Resource conf = cModel.createResource();
695         conf.addProperty( ReasonerVocabulary.EXT_REASONER_URL, cModel.createResource( "http://localhost:8081" ) );
696
697         // create the reasoner factory and the reasoner
698
DIGReasonerFactory drf = (DIGReasonerFactory) ReasonerRegistry.theRegistry()
699                 .getFactory( DIGReasonerFactory.URI );
700         DIGReasoner r = (DIGReasoner) drf.create( conf );
701
702         // now make a model
703
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
704         spec.setReasoner( r );
705         OntModel m = ModelFactory.createOntologyModel( spec, null );
706
707         // load an input document
708
m.getDocumentManager()
709          .addAltEntry( "http://protege.stanford.edu/plugins/owl/owl-library/koala.owl",
710                        "file:testing/ontology/bugs/koala.owl" );
711         m.read( "http://protege.stanford.edu/plugins/owl/owl-library/koala.owl" );
712
713         // list the inconsistent classes
714
StmtIterator i = m.listStatements( null, OWL.equivalentClass, OWL.Nothing );
715         int unsatCount = 0;
716         while (i.hasNext()) {
717             Resource s = i.nextStatement().getSubject();
718             //System.out.println( "Class " + s + " is unsatisfiable" );
719
unsatCount++;
720         }
721         assertEquals( "Should be 4 unsatisfiable classes", 5, unsatCount );
722     }
723
724     /* Bug report by Michele Orlando - duplicate results from rdql query */
725     public void test_bug_mo_1() {
726         String JavaDoc NS = "http://example.org/foo#";
727
728         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
729
730         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
731         spec.setReasoner( r );
732         OntModel m = ModelFactory.createOntologyModel( spec, null );
733
734         OntClass a = m.createClass( NS + "A" );
735         Individual i0 = m.createIndividual( NS + "i0", a );
736
737         int iCount = 0;
738         int nonICount = 0;
739
740         for (StmtIterator j = m.listStatements( null, RDF.type, m.getResource(NS+"A")); j.hasNext(); ) {
741             if (j.nextStatement().getSubject().getURI().equals( NS + "i0") ) {
742                 iCount++;
743             }
744             else {
745                 nonICount++;
746             }
747         }
748         assertEquals( "Should be no non i0's", 0, nonICount );
749         assertEquals( "Should be only one i0", 1, iCount );
750     }
751
752     /* Bug report from Michele Orlando - failure on getLabel */
753     public void test_bug_mo_2() {
754         boolean ex0 = false;
755         boolean ex1 = false;
756         String JavaDoc NS = "http://example.org/foo#";
757
758         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
759
760         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
761         spec.setReasoner( r );
762         OntModel model = ModelFactory.createOntologyModel( spec, null );
763
764         OntClass a = model.createClass( NS + "A" );
765         Individual b = model.createIndividual( NS + "b", a );
766
767
768         Model base = model.getBaseModel();
769         Literal lbl = base.createLiteral( "fubar", "en" );
770         base.add( b, RDFS.label, lbl );
771
772         // ensure we have the model we want
773
//model.write( System.out, "N3" );
774

775         for ( Iterator instances = model.listIndividuals(); instances.hasNext(); ) {
776             Individual inst = (Individual) instances.next();
777
778             System.out.println( "Looking at " + inst );
779             try {
780                 System.out.println( "Label try #1: " + " "+inst.getLabel("en"));
781             }
782             catch (Exception JavaDoc e) {
783                 System.out.println( "First attempt failed: " + e.getMessage() );
784                 ex0 = true;
785             }
786
787             try {
788                 Resource baseInst = (Resource) inst.inModel( base );
789                 String JavaDoc label = baseInst.getProperty( RDFS.label ).getString();
790                 System.out.println("Label try #2: " + label );
791             }
792             catch (Exception JavaDoc e) {
793                 System.out.println( "Second attempt failed " + e.getMessage() );
794                 ex1 = true;
795             }
796         }
797
798         // Junit
799
assertTrue( !(ex0 || ex1) );
800     }
801
802     public void xxtestDebug1() {
803         String JavaDoc NS = "http://example.org/foo#";
804
805         OntModel base = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM, null );
806         Individual a = base.createIndividual( NS + "a", OWL.Thing );
807         Individual b = base.createIndividual( NS + "b", OWL.Thing );
808         OntClass A = base.createEnumeratedClass( NS + "A", base.createList( new Resource[] {a,b} ));
809
810         DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
811
812         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
813         spec.setReasoner( r );
814         OntModel m = ModelFactory.createOntologyModel( spec, base );
815
816         for (Iterator i = m.listClasses(); i.hasNext(); ) {
817             System.err.println( "concept " + i.next() );
818         }
819     }
820
821     public void xxtestDebug() {
822         String JavaDoc NS = "http://example.org/foo#";
823
824         OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM_RULE_INF );
825         OntModel m = ModelFactory.createOntologyModel( spec, null );
826         m.read( "file:testing/ontology/dig/owl/test1.xml" );
827
828         ObjectProperty p2 = m.getObjectProperty( NS + "p2" );
829
830         for (StmtIterator i = m.listStatements( null, RDFS.subPropertyOf, p2 ); i.hasNext(); ) {
831             System.err.println( "p2 has sub prop " + i.next() );
832         }
833     }
834
835
836     // Internal implementation methods
837
//////////////////////////////////
838

839     private static void buildConceptLangSuite( String JavaDoc root, OntModelSpec spec, TestSuite s ) {
840         int i = 0;
841         while (true) {
842             File testSource = new File( root + "/test_" + i + ".source.xml" );
843             File testTarget = new File( root + "/test_" + i + ".xml" );
844
845             if (!testSource.exists()) {
846                 break;
847             }
848             else {
849                 i++;
850             }
851
852             s.addTest( new DigTranslationTest( testSource, testTarget, spec ) );
853         }
854     }
855
856     private static void buildBasicQuerySuite( String JavaDoc root, OntModelSpec spec, TestSuite s ) {
857         int i = 0;
858         while (true) {
859             File testSource = new File( root + "/test_" + i + ".source.xml" );
860             File testQuery = new File( root + "/test_" + i + ".query.xml" );
861             File testTarget = new File( root + "/test_" + i + ".result.xml" );
862
863             if (!testSource.exists()) {
864                 break;
865             }
866             else {
867                 i++;
868             }
869
870             s.addTest( new DigBasicQueryTest( testSource, testTarget, testQuery, spec ) );
871         }
872     }
873
874
875
876     //==============================================================================
877
// Inner class definitions
878
//==============================================================================
879

880     private static class AbstractDigTest
881         extends TestCase
882     {
883         private boolean debug = true;
884
885         public AbstractDigTest( String JavaDoc name ) {
886             super( name );
887         }
888
889         /** This is a simple test that test xml structure isomorphism on elements and attributes */
890         protected void xmlEqualityTest( Document source, Document target ) {
891             // test both ways round to ensure compatability
892
boolean test = xmlEqualityTest( source.getDocumentElement(), target.getDocumentElement() );
893             if (debug && !test) {
894                 PrintWriter out = new PrintWriter( System.err );
895                 out.println( getName() + " expected:" );
896                 new DIGConnection().serialiseDocument( target, out );
897                 out.println();
898                 out.println( "Saw:" );
899                 new DIGConnection().serialiseDocument( source, out );
900                 out.println();
901             }
902             assertTrue( "Failed to match source to target documents", test );
903
904             test = xmlEqualityTest( target.getDocumentElement(), source.getDocumentElement() );
905             if (debug && !test) {
906                 PrintWriter out = new PrintWriter( System.err );
907                 out.println( getName() + " expected:" );
908                 new DIGConnection().serialiseDocument( source, out );
909                 out.println();
910                 out.println( "Saw:" );
911                 new DIGConnection().serialiseDocument( target, out );
912                 out.println();
913             }
914             assertTrue( "Failed to match target to source documents", test );
915         }
916
917         private boolean xmlEqualityTest( Element source, Element target ) {
918             boolean match = source.getNodeName().equals( target.getNodeName() );
919             NodeList children = source.getChildNodes();
920
921             for (int i = 0; match && i < children.getLength(); i++) {
922                 Node child = children.item( i );
923                 // we're only looking at structural equivalence - elements and attributes
924
if (child instanceof Element) {
925                     match = findElementMatch( (Element) child, target );
926                 }
927             }
928
929             NamedNodeMap attrs = source.getAttributes();
930
931             for (int i = 0; match && i < attrs.getLength(); i++) {
932                 match = findAttributeMatch( (Attr) attrs.item( i ), target );
933             }
934
935             return match;
936         }
937
938
939         private boolean findElementMatch( Element sourceChild, Element target ) {
940             boolean found = false;
941
942             NodeList targetChildren = target.getElementsByTagName( sourceChild.getNodeName() );
943
944             for (int i = 0; !found && i < targetChildren.getLength(); i++) {
945                 Node targetChild = targetChildren.item( i );
946
947                 if (targetChild instanceof Element && sourceChild.getNodeName().equals( targetChild.getNodeName() )) {
948                     // we have found an element with the same name - see if it matches
949
found = xmlEqualityTest( sourceChild, (Element) targetChild );
950                 }
951             }
952
953             return found;
954         }
955
956
957         private boolean findAttributeMatch( Attr child, Element target ) {
958             String JavaDoc chValue = child.getValue();
959             String JavaDoc targetValue = target.getAttribute( child.getName() );
960
961             return (chValue.startsWith( DIGAdapter.ANON_MARKER ) && targetValue.startsWith( DIGAdapter.ANON_MARKER)) ||
962                    chValue.equals( targetValue );
963         }
964     }
965
966
967     private static class DigTranslationTest
968         extends AbstractDigTest
969     {
970         private File m_source;
971         private File m_target;
972         private OntModelSpec m_spec;
973
974         DigTranslationTest( File source, File target, OntModelSpec spec ) {
975             super( "DigTranslationTest " + source.getName() );
976             m_source = source;
977             m_target = target;
978             m_spec = spec;
979         }
980
981         public void runTest()
982             throws Exception JavaDoc
983         {
984             DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
985
986             Model m = ModelFactory.createDefaultModel();
987             m.read( new FileInputStream( m_source ), null );
988             DIGAdapter da = new DIGAdapter( m_spec, m.getGraph() );
989
990             Document targetD = builder.parse( m_target );
991             Document sourceD = da.translateKbToDig();
992
993             // debug da.serialiseDocument( sourceD, new PrintWriter( System.out ));
994

995             xmlEqualityTest( sourceD, targetD );
996         }
997     }
998
999
1000    private static class DigBasicQueryTest
1001        extends AbstractDigTest
1002    {
1003        private File m_source;
1004        private File m_target;
1005        private File m_query;
1006        private OntModelSpec m_spec;
1007
1008        DigBasicQueryTest( File source, File target, File query, OntModelSpec spec ) {
1009            super( "BasicQueryTest " + source.getName() );
1010            m_source = source;
1011            m_target = target;
1012            m_query = query;
1013            m_spec = spec;
1014        }
1015
1016        public void runTest()
1017            throws Exception JavaDoc
1018        {
1019            DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
1020
1021            Model m = ModelFactory.createDefaultModel();
1022            m.read( new FileInputStream( m_source ), null );
1023            DIGAdapter da = new DIGAdapter( m_spec, m.getGraph() );
1024
1025            // upload
1026
da.resetKB();
1027            boolean warn = !da.uploadKB();
1028            if (warn) {
1029                System.err.println( "00 Warning!" );
1030                for (Iterator i = da.getConnection().getWarnings(); i.hasNext(); ) {
1031                    System.err.println( i.next() );
1032                }
1033                assertFalse( "Should not be upload warnings", warn );
1034            }
1035
1036            Document queryD = builder.parse( m_query );
1037            Document targetD = builder.parse( m_target );
1038
1039            log.debug( "DIG test " + m_source.getPath() );
1040            Document resultD = da.getConnection().sendDigVerb( queryD, da.getProfile() );
1041
1042            da.getConnection().errorCheck( resultD );
1043            assertFalse( "Should not be warnings", da.getConnection().warningCheck( resultD ) );
1044
1045            da.close();
1046            xmlEqualityTest( resultD, targetD );
1047        }
1048    }
1049}
1050
1051
1052/*
1053 * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
1054 * All rights reserved.
1055 *
1056 * Redistribution and use in source and binary forms, with or without
1057 * modification, are permitted provided that the following conditions
1058 * are met:
1059 * 1. Redistributions of source code must retain the above copyright
1060 * notice, this list of conditions and the following disclaimer.
1061 * 2. Redistributions in binary form must reproduce the above copyright
1062 * notice, this list of conditions and the following disclaimer in the
1063 * documentation and/or other materials provided with the distribution.
1064 * 3. The name of the author may not be used to endorse or promote products
1065 * derived from this software without specific prior written permission.
1066 *
1067 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1068 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1069 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1070 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1071 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1072 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1073 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1074 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1075 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1076 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1077 */

1078
Popular Tags