KickJava   Java API By Example, From Geeks To Geeks.

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


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 16-Jun-2003
9  * Filename $RCSfile: TestBugReports.java,v $
10  * Revision $Revision: 1.62 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/04/11 13:55:54 $
14  * by $Author: jeremy_carroll $
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 // Imports
25
///////////////
26
import java.io.*;
27 import java.lang.reflect.Field JavaDoc;
28 import java.lang.reflect.Modifier JavaDoc;
29 import java.util.*;
30
31 import com.hp.hpl.jena.enhanced.EnhGraph;
32 import com.hp.hpl.jena.graph.*;
33 import com.hp.hpl.jena.graph.impl.*;
34 import com.hp.hpl.jena.mem.GraphMem;
35 import com.hp.hpl.jena.ontology.*;
36 import com.hp.hpl.jena.ontology.daml.*;
37 import com.hp.hpl.jena.ontology.daml.DAMLModel;
38 import com.hp.hpl.jena.ontology.impl.OntClassImpl;
39 import com.hp.hpl.jena.rdf.model.*;
40 import com.hp.hpl.jena.rdf.model.impl.ModelMakerImpl;
41 import com.hp.hpl.jena.reasoner.*;
42 import com.hp.hpl.jena.reasoner.ReasonerRegistry;
43 import com.hp.hpl.jena.reasoner.dig.*;
44 import com.hp.hpl.jena.reasoner.dig.DIGReasoner;
45 import com.hp.hpl.jena.reasoner.dig.DIGReasonerFactory;
46 import com.hp.hpl.jena.reasoner.test.TestUtil;
47 import com.hp.hpl.jena.util.FileUtils;
48 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
49 import com.hp.hpl.jena.vocabulary.*;
50 import com.hp.hpl.jena.vocabulary.OWL;
51
52 import junit.framework.*;
53
54 /**
55  * <p>
56  * Unit tests that are derived from user bug reports
57  * </p>
58  *
59  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com" >
60  * email</a>)
61  * @version CVS $Id: TestBugReports.java,v 1.23 2003/11/20 17:53:10
62  * ian_dickinson Exp $
63  */

64 public class TestBugReports
65     extends TestCase
66 {
67     // Constants
68
//////////////////////////////////
69

70     public static String JavaDoc NS = "http://example.org/test#";
71
72     // Static variables
73
//////////////////////////////////
74

75     // Instance variables
76
//////////////////////////////////
77

78     public TestBugReports(String JavaDoc name) {
79         super(name);
80     }
81
82     // Constructors
83
//////////////////////////////////
84

85     // External signature methods
86
//////////////////////////////////
87

88     public void setUp() {
89         // ensure the ont doc manager is in a consistent state
90
OntDocumentManager.getInstance().reset( true );
91     }
92
93
94     /** Bug report by Danah Nada - listIndividuals returning too many results */
95     public void test_dn_0() {
96         OntModel schema = ModelFactory.createOntologyModel( OntModelSpec.OWL_LITE_MEM_RULES_INF, null );
97
98         schema.read( "file:doc/inference/data/owlDemoSchema.xml", null );
99
100         int count = 0;
101         for (Iterator i = schema.listIndividuals(); i.hasNext(); ) {
102             //Resource r = (Resource) i.next();
103
i.next();
104             count++;
105             /* Debugging * /
106             for (StmtIterator j = r.listProperties(RDF.type); j.hasNext(); ) {
107                 System.out.println( "ind - " + r + " rdf:type = " + j.nextStatement().getObject() );
108             }
109             System.out.println("----------"); /**/

110         }
111
112         assertEquals( "Expecting 6 individuals", 6, count );
113     }
114
115
116     /* Bug report by Danah Nada - duplicate elements in property domain */
117     public void test_dn_01() {
118         // direct reading for the model method 1
119
OntModel m0 = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM_RULE_INF, null );
120         m0.read( "file:testing/ontology/bugs/test_hk_07B.owl" );
121
122         OntProperty p0 = m0.getOntProperty( "file:testing/ontology/bugs/test_hk_07B.owl#PropB" );
123         int count = 0;
124         for (Iterator i = p0.listDomain(); i.hasNext();) {
125             count++;
126             i.next();
127         }
128         assertEquals( 3, count );
129
130         // repeat test - thus using previously cached model for import
131

132         OntModel m1 = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM_RULE_INF, null );
133         m1.read( "file:testing/ontology/bugs/test_hk_07B.owl" );
134
135         OntProperty p1 = m1.getOntProperty( "file:testing/ontology/bugs/test_hk_07B.owl#PropB" );
136         count = 0;
137         for (Iterator i = p1.listDomain(); i.hasNext();) {
138             count++;
139             i.next();
140         }
141         assertEquals( 3, count );
142     }
143
144     /** Bug report by Danah Nada - cannot remove import */
145     public void test_dn_02() {
146         OntModel mymod = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );
147         mymod.read( "file:testing/ontology/testImport3/a.owl" );
148
149         assertEquals( "Graph count..", 2, mymod.getSubGraphs().size() );
150
151         for (Iterator it = mymod.listImportedModels(); it.hasNext();) {
152                 mymod.removeSubModel( (Model) it.next() );
153         }
154
155         assertEquals( "Graph count..", 0, mymod.getSubGraphs().size() );
156     }
157
158
159     /**
160      * Bug report by Mariano Rico Almod�var [Mariano.Rico@uam.es] on June 16th.
161      * Said to raise exception.
162      */

163     public void test_mra_01() {
164         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.DAML_MEM, null, null);
165         String JavaDoc myDicURI = "http://somewhere/myDictionaries/1.0#";
166         String JavaDoc damlURI = "http://www.daml.org/2001/03/daml+oil#";
167         m.setNsPrefix("DAML", damlURI);
168
169         String JavaDoc c1_uri = myDicURI + "C1";
170         OntClass c1 = m.createClass(c1_uri);
171
172         DatatypeProperty p1 = m.createDatatypeProperty(myDicURI + "P1");
173         p1.setDomain(c1);
174
175         ByteArrayOutputStream strOut = new ByteArrayOutputStream();
176
177         m.write(strOut, "RDF/XML-ABBREV", myDicURI);
178         //m.write(System.out,"RDF/XML-ABBREV", myDicURI);
179

180     }
181
182     /**
183      * Bug report from Holger Knublauch on July 25th 2003. Cannot convert
184      * owl:Class to an OntClass
185      */

186     public void test_hk_01() {
187         // synthesise a mini-document
188
String JavaDoc base = "http://jena.hpl.hp.com/test#";
189         String JavaDoc doc =
190             "<rdf:RDF"
191                 + " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
192                 + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\">"
193                 + " <owl:Ontology rdf:about=\"\">"
194                 + " <owl:imports rdf:resource=\"http://www.w3.org/2002/07/owl\" />"
195                 + " </owl:Ontology>"
196                 + "</rdf:RDF>";
197
198         // read in the base ontology, which includes the owl language
199
// definition
200
// note OWL_MEM => no reasoner is used
201
OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
202         m.getDocumentManager().setMetadataSearchPath( "file:etc/ont-policy-test.rdf", true );
203         m.read(new ByteArrayInputStream(doc.getBytes()), base);
204
205         // we need a resource corresponding to OWL Class but in m
206
Resource owlClassRes = m.getResource(OWL.Class.getURI());
207
208         // now can we see this as an OntClass?
209
OntClass c = (OntClass) owlClassRes.as(OntClass.class);
210         assertNotNull("OntClass c should not be null", c);
211
212         //(OntClass) (ontModel.getProfile().CLASS()).as(OntClass.class);
213

214     }
215
216     /**
217      * Bug report from Hoger Knublauch on Aug 19th 2003. NPE when setting all
218      * distinct members
219      */

220     public void test_hk_02() {
221         OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
222         spec.setReasoner(null);
223         OntModel ontModel = ModelFactory.createOntologyModel(spec, null); // ProfileRegistry.OWL_LANG);
224
ontModel.createAllDifferent();
225         assertTrue(ontModel.listAllDifferent().hasNext());
226         AllDifferent allDifferent = (AllDifferent) ontModel.listAllDifferent().next();
227         //allDifferent.setDistinct(ontModel.createList());
228
assertFalse(allDifferent.listDistinctMembers().hasNext());
229     }
230
231     /** Bug report from Holger Knublauch on Aug 19th, 2003. Initialisation error */
232     public void test_hk_03() {
233         OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
234         spec.setReasoner(null);
235         OntModel ontModel = ModelFactory.createOntologyModel(spec, null);
236         OntProperty property = ontModel.createObjectProperty("http://www.aldi.de#property");
237         /* MinCardinalityRestriction testClass = */
238         ontModel.createMinCardinalityRestriction(null, property, 42);
239
240     }
241
242     /**
243      * Bug report from Holger Knublauch on Aug 19th, 2003. Document manager alt
244      * mechanism breaks relative name translation
245      */

246     public void test_hk_04() {
247         OntModel m = ModelFactory.createOntologyModel();
248         m.getDocumentManager().addAltEntry(
249             "http://jena.hpl.hp.com/testing/ontology/relativenames",
250             "file:testing/ontology/relativenames.rdf");
251
252         m.read("http://jena.hpl.hp.com/testing/ontology/relativenames");
253         assertTrue(
254             "#A should be a class",
255             m.getResource("http://jena.hpl.hp.com/testing/ontology/relativenames#A").canAs(OntClass.class));
256         assertFalse(
257             "file: #A should not be a class",
258             m.getResource("file:testing/ontology/relativenames.rdf#A").canAs(OntClass.class));
259     }
260
261     /** Bug report from Holger Knublach: not all elements of a union are removed */
262     public void test_hk_05() {
263         OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
264         spec.setReasoner(null);
265         OntModel ontModel = ModelFactory.createOntologyModel(spec, null);
266         String JavaDoc ns = "http://foo.bar/fu#";
267         OntClass a = ontModel.createClass(ns + "A");
268         OntClass b = ontModel.createClass(ns + "B");
269
270         int oldCount = getStatementCount(ontModel);
271
272         RDFList members = ontModel.createList(new RDFNode[] { a, b });
273         IntersectionClass intersectionClass = ontModel.createIntersectionClass(null, members);
274         intersectionClass.remove();
275
276         assertEquals("Before and after statement counts are different", oldCount, getStatementCount(ontModel));
277     }
278
279     /**
280      * Bug report from Holger Knublach: moving between ontology models - comes
281      * down to a test for a resource being in the base model
282      */

283     public void test_hk_06() throws Exception JavaDoc {
284         OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
285         ontModel.read("file:testing/ontology/bugs/test_hk_06/a.owl");
286
287         String JavaDoc NSa = "http://jena.hpl.hp.com/2003/03/testont/a#";
288         String JavaDoc NSb = "http://jena.hpl.hp.com/2003/03/testont/b#";
289
290         OntClass A = ontModel.getOntClass(NSa + "A");
291         assertTrue("class A should be in the base model", ontModel.isInBaseModel(A));
292
293         OntClass B = ontModel.getOntClass(NSb + "B");
294         assertFalse("class B should not be in the base model", ontModel.isInBaseModel(B));
295
296         assertTrue(
297             "A rdf:type owl:Class should be in the base model",
298             ontModel.isInBaseModel(ontModel.createStatement(A, RDF.type, OWL.Class)));
299         assertFalse(
300             "B rdf:type owl:Class should not be in the base model",
301             ontModel.isInBaseModel(ontModel.createStatement(B, RDF.type, OWL.Class)));
302     }
303
304     public void test_hk_importCache() {
305         final String JavaDoc BASE = "http://protege.stanford.edu/plugins/owl/testdata/";
306         OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
307         spec.setReasoner(null);
308         OntDocumentManager dm = OntDocumentManager.getInstance();
309         dm.reset();
310         dm.setCacheModels(false);
311         dm.addAltEntry( "http://protege.stanford.edu/plugins/owl/testdata/Import-normalizerBug.owl",
312                         "file:testing/ontology/bugs/test_hk_import/Import-normalizerBug.owl" );
313         dm.addAltEntry( "http://protege.stanford.edu/plugins/owl/testdata/normalizerBug.owl",
314                         "file:testing/ontology/bugs/test_hk_import/normalizerBug.owl" );
315         spec.setDocumentManager(dm);
316
317         OntModel oldOntModel = ModelFactory.createOntologyModel(spec, null);
318         oldOntModel.read(BASE + "Import-normalizerBug.owl", FileUtils.langXMLAbbrev);
319         Graph oldSubGraph = (Graph) oldOntModel.getSubGraphs().iterator().next();
320         final int oldTripleCount = getTripleCount(oldSubGraph);
321         OntClass ontClass = oldOntModel.getOntClass(BASE + "normalizerBug.owl#SuperClass");
322         oldSubGraph.add(new Triple(ontClass.getNode(), RDF.type.getNode(), OWL.DeprecatedClass.getNode()));
323         assertEquals(oldTripleCount + 1, getTripleCount(oldSubGraph));
324
325         // TODO this workaround to be removed
326
SimpleGraphMaker sgm = (SimpleGraphMaker) ((ModelMakerImpl) spec.getImportModelMaker()).getGraphMaker();
327         List toGo = new ArrayList();
328         for (Iterator i = sgm.listGraphs(); i.hasNext(); toGo.add( i.next() ));
329         for (Iterator i = toGo.iterator(); i.hasNext(); sgm.removeGraph( (String JavaDoc) i.next() ));
330         dm.clearCache();
331
332         OntModel newOntModel = ModelFactory.createOntologyModel(spec, null);
333         newOntModel.read(BASE + "Import-normalizerBug.owl", FileUtils.langXMLAbbrev);
334         Graph newSubGraph = (Graph) newOntModel.getSubGraphs().iterator().next();
335         assertFalse(newOntModel == oldOntModel); // OK!
336
assertFalse(newSubGraph == oldSubGraph); // FAILS!
337
final int newTripleCount = getTripleCount(newSubGraph);
338         assertEquals(oldTripleCount, newTripleCount);
339     }
340
341
342     private int getTripleCount(Graph graph) {
343         int count = 0;
344         for (Iterator it = graph.find(null, null, null); it.hasNext();) {
345             it.next();
346             count++;
347         }
348         return count;
349     }
350
351     /**
352      * Bug report by federico.carbone@bt.com, 30-July-2003. A literal can be
353      * turned into an individual.
354      */

355     public void test_fc_01() {
356         OntModel m = ModelFactory.createOntologyModel();
357
358         ObjectProperty p = m.createObjectProperty(NS + "p");
359         Restriction r = m.createRestriction(p);
360         HasValueRestriction hv = r.convertToHasValueRestriction(m.createLiteral(1));
361
362         RDFNode n = hv.getHasValue();
363         assertFalse("Should not be able to convert literal to individual", n.canAs(Individual.class));
364     }
365
366     /**
367      * Bug report by Christoph Kunze (Christoph.Kunz@iao.fhg.de). 18/Aug/03 No
368      * transaction support in ontmodel.
369      */

370     public void test_ck_01() {
371         Graph g = new GraphMem() {
372             TransactionHandler m_t = new MockTransactionHandler();
373             public TransactionHandler getTransactionHandler() {
374                 return m_t;
375             }
376         };
377         Model m0 = ModelFactory.createModelForGraph(g);
378         OntModel m1 = ModelFactory.createOntologyModel(OntModelSpec.OWL_LITE_MEM, m0);
379
380         assertFalse(
381             "Transaction not started yet",
382             ((MockTransactionHandler) m1.getGraph().getTransactionHandler()).m_inTransaction);
383         m1.begin();
384         assertTrue(
385             "Transaction started",
386             ((MockTransactionHandler) m1.getGraph().getTransactionHandler()).m_inTransaction);
387         m1.abort();
388         assertFalse(
389             "Transaction aborted",
390             ((MockTransactionHandler) m1.getGraph().getTransactionHandler()).m_inTransaction);
391         assertTrue("Transaction aborted", ((MockTransactionHandler) m1.getGraph().getTransactionHandler()).m_aborted);
392         m1.begin();
393         assertTrue(
394             "Transaction started",
395             ((MockTransactionHandler) m1.getGraph().getTransactionHandler()).m_inTransaction);
396         m1.commit();
397         assertFalse(
398             "Transaction committed",
399             ((MockTransactionHandler) m1.getGraph().getTransactionHandler()).m_inTransaction);
400         assertTrue(
401             "Transaction committed",
402             ((MockTransactionHandler) m1.getGraph().getTransactionHandler()).m_committed);
403     }
404
405     /**
406      * Bug report by Christoph Kunz, 26/Aug/03. CCE when creating a statement
407      * from a vocabulary
408      *
409      */

410     public void test_ck_02() {
411         OntModel vocabModel = ModelFactory.createOntologyModel();
412         ObjectProperty p = vocabModel.createObjectProperty("p");
413         OntClass A = vocabModel.createClass("A");
414
415         OntModel workModel = ModelFactory.createOntologyModel();
416         Individual sub = workModel.createIndividual("uri1", A);
417         Individual obj = workModel.createIndividual("uri2", A);
418         workModel.createStatement(sub, p, obj);
419     }
420
421     /**
422      * Bug report from Christoph Kunz - reification problems and
423      * UnsupportedOperationException
424      */

425     public void test_ck_03() {
426         // part A - surprising reification
427
OntModel model1 = ModelFactory.createOntologyModel(OntModelSpec.DAML_MEM, null);
428         OntModel model2 = ModelFactory.createOntologyModel(OntModelSpec.DAML_MEM_RULE_INF, null);
429
430         Individual sub = model1.createIndividual("http://mytest#i1", model1.getProfile().CLASS());
431         OntProperty pred = model1.createOntProperty("http://mytest#");
432         Individual obj = model1.createIndividual("http://mytest#i2", model1.getProfile().CLASS());
433         OntProperty probabilityP = model1.createOntProperty("http://mytest#prob");
434
435         Statement st = model1.createStatement(sub, pred, obj);
436         model1.add(st);
437         st.createReifiedStatement().addProperty(probabilityP, 0.9);
438         assertTrue("st should be reified", st.isReified());
439
440         Statement st2 = model2.createStatement(sub, pred, obj);
441         model2.add(st2);
442         st2.createReifiedStatement().addProperty(probabilityP, 0.3);
443         assertTrue("st2 should be reified", st2.isReified());
444
445         sub.addProperty(probabilityP, 0.3);
446         sub.removeAll(probabilityP).addProperty(probabilityP, 0.3); //!!!
447
// exception
448

449         // Part B - exception in remove All
450
Individual sub2 = model2.createIndividual("http://mytest#i1", model1.getProfile().CLASS());
451
452         sub.addProperty(probabilityP, 0.3);
453         sub.removeAll(probabilityP); //!!! exception
454

455         sub2.addProperty(probabilityP, 0.3);
456         sub2.removeAll(probabilityP); //!!! exception
457

458     }
459
460     /**
461      * Bug report by sjooseng [sjooseng@hotmail.com]. CCE in listOneOf in
462      * Enumerated Class with DAML profile.
463      */

464     public void test_sjooseng_01() {
465         String JavaDoc source =
466             "<rdf:RDF xmlns:daml='http://www.daml.org/2001/03/daml+oil#'"
467                 + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
468                 + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#' >"
469                 + " <daml:Class rdf:about='http://localhost:8080/kc2c#C1'>"
470                 + " <daml:subClassOf>"
471                 + " <daml:Restriction>"
472                 + " <daml:onProperty rdf:resource='http://localhost:8080/kc2c#p1'/>"
473                 + " <daml:hasClass>"
474                 + " <daml:Class>"
475                 + " <daml:oneOf rdf:parseType=\"daml:collection\">"
476                 + " <daml:Thing rdf:about='http://localhost:8080/kc2c#i1'/>"
477                 + " <daml:Thing rdf:about='http://localhost:8080/kc2c#i2'/>"
478                 + " </daml:oneOf>"
479                 + " </daml:Class>"
480                 + " </daml:hasClass>"
481                 + " </daml:Restriction>"
482                 + " </daml:subClassOf>"
483                 + " </daml:Class>"
484                 + " <daml:ObjectProperty rdf:about='http://localhost:8080/kc2c#p1'>"
485                 + " <rdfs:label>p1</rdfs:label>"
486                 + " </daml:ObjectProperty>"
487                 + "</rdf:RDF>";
488
489         OntModel m = ModelFactory.createOntologyModel(ProfileRegistry.DAML_LANG);
490         m.read(new ByteArrayInputStream(source.getBytes()), "http://localhost:8080/kc2c");
491
492         OntClass kc1 = m.getOntClass("http://localhost:8080/kc2c#C1");
493
494         boolean found = false;
495
496         Iterator it = kc1.listSuperClasses(false);
497         while (it.hasNext()) {
498             OntClass oc = (OntClass) it.next();
499             if (oc.isRestriction()) {
500                 Restriction r = oc.asRestriction();
501                 if (r.isSomeValuesFromRestriction()) {
502                     SomeValuesFromRestriction sr = r.asSomeValuesFromRestriction();
503                     OntClass sc = (OntClass) sr.getSomeValuesFrom();
504                     if (sc.isEnumeratedClass()) {
505                         EnumeratedClass ec = sc.asEnumeratedClass();
506                         assertEquals("Enumeration size should be 2", 2, ec.getOneOf().size());
507                         found = true;
508                     }
509                 }
510             }
511         }
512
513         assertTrue(found);
514     }
515
516     /**
517      * Problem reported by Andy Seaborne - combine abox and tbox in RDFS with
518      * ontmodel
519      */

520     public void test_afs_01() {
521         String JavaDoc sourceT =
522             "<rdf:RDF "
523                 + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
524                 + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'"
525                 + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\">"
526                 + " <owl:Class rdf:about='http://example.org/foo#A'>"
527                 + " </owl:Class>"
528                 + "</rdf:RDF>";
529
530         String JavaDoc sourceA =
531             "<rdf:RDF "
532                 + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
533                 + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#' "
534                 + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\">"
535                 + " <rdf:Description rdf:about='http://example.org/foo#x'>"
536                 + " <rdf:type rdf:resource='http://example.org/foo#A' />"
537                 + " </rdf:Description>"
538                 + "</rdf:RDF>";
539
540         Model tBox = ModelFactory.createDefaultModel();
541         tBox.read(new ByteArrayInputStream(sourceT.getBytes()), "http://example.org/foo");
542
543         Model aBox = ModelFactory.createDefaultModel();
544         aBox.read(new ByteArrayInputStream(sourceA.getBytes()), "http://example.org/foo");
545
546         Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
547         reasoner = reasoner.bindSchema(tBox);
548
549         OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM_RULE_INF);
550         spec.setReasoner(reasoner);
551
552         OntModel m = ModelFactory.createOntologyModel(spec, aBox);
553
554         List inds = new ArrayList();
555         for (Iterator i = m.listIndividuals(); i.hasNext();) {
556             inds.add(i.next());
557         }
558
559         assertTrue("x should be an individual", inds.contains(m.getResource("http://example.org/foo#x")));
560
561     }
562
563     /**
564      * Bug report by Thorsten Ottmann [Thorsten.Ottmann@rwth-aachen.de] -
565      * problem accessing elements of DAML list
566      */

567     public void test_to_01() {
568         String JavaDoc sourceT =
569             "<rdf:RDF "
570                 + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
571                 + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'"
572                 + " xmlns:daml='http://www.daml.org/2001/03/daml+oil#'>"
573                 + " <daml:Class rdf:about='http://example.org/foo#A'>"
574                 + " <daml:intersectionOf rdf:parseType=\"daml:collection\">"
575                 + " <daml:Class rdf:ID=\"B\" />"
576                 + " <daml:Class rdf:ID=\"C\" />"
577                 + " </daml:intersectionOf>"
578                 + " </daml:Class>"
579                 + "</rdf:RDF>";
580
581         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.DAML_MEM, null);
582         m.read(new ByteArrayInputStream(sourceT.getBytes()), "http://example.org/foo");
583
584         OntClass A = m.getOntClass("http://example.org/foo#A");
585         assertNotNull(A);
586
587         IntersectionClass iA = A.asIntersectionClass();
588         assertNotNull(iA);
589
590         RDFList intersection = iA.getOperands();
591         assertNotNull(intersection);
592
593         assertEquals(2, intersection.size());
594         assertTrue(intersection.contains(m.getOntClass("http://example.org/foo#B")));
595         assertTrue(intersection.contains(m.getOntClass("http://example.org/foo#C")));
596     }
597
598     /**
599      * Bug report by Thorsten Liebig [liebig@informatik.uni-ulm.de] -
600      * SymmetricProperty etc not visible in list ont properties
601      */

602     public void test_tl_01() {
603         String JavaDoc sourceT =
604             "<rdf:RDF "
605                 + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
606                 + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'"
607                 + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\">"
608                 + " <owl:SymmetricProperty rdf:about='http://example.org/foo#p1'>"
609                 + " </owl:SymmetricProperty>"
610                 + " <owl:TransitiveProperty rdf:about='http://example.org/foo#p2'>"
611                 + " </owl:TransitiveProperty>"
612                 + " <owl:InverseFunctionalProperty rdf:about='http://example.org/foo#p3'>"
613                 + " </owl:InverseFunctionalProperty>"
614                 + "</rdf:RDF>";
615
616         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, null);
617         m.read(new ByteArrayInputStream(sourceT.getBytes()), "http://example.org/foo");
618
619         boolean foundP1 = false;
620         boolean foundP2 = false;
621         boolean foundP3 = false;
622
623         // iterator of properties should include p1-3
624
for (Iterator i = m.listOntProperties(); i.hasNext();) {
625             Resource r = (Resource) i.next();
626             foundP1 = foundP1 || r.getURI().equals("http://example.org/foo#p1");
627             foundP2 = foundP2 || r.getURI().equals("http://example.org/foo#p2");
628             foundP3 = foundP3 || r.getURI().equals("http://example.org/foo#p3");
629         }
630
631         assertTrue("p1 not listed", foundP1);
632         assertTrue("p2 not listed", foundP2);
633         assertTrue("p3 not listed", foundP3);
634
635         foundP1 = false;
636         foundP2 = false;
637         foundP3 = false;
638
639         // iterator of object properties should include p1-3
640
for (Iterator i = m.listObjectProperties(); i.hasNext();) {
641             Resource r = (Resource) i.next();
642             foundP1 = foundP1 || r.getURI().equals("http://example.org/foo#p1");
643             foundP2 = foundP2 || r.getURI().equals("http://example.org/foo#p2");
644             foundP3 = foundP3 || r.getURI().equals("http://example.org/foo#p3");
645         }
646
647         assertTrue("p1 not listed", foundP1);
648         assertTrue("p2 not listed", foundP2);
649         assertTrue("p3 not listed", foundP3);
650     }
651
652     /** Bug report by Dave Reynolds - SF bug report 810492 */
653     public void test_der_01() {
654         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF, null);
655         Resource a = m.createResource("http://example.org#A");
656         Resource b = m.createResource("http://example.org#B");
657         OntClass A = new OntClassImpl(a.getNode(), (EnhGraph) m) {
658             protected boolean hasSuperClassDirect(Resource cls) {
659                 throw new RuntimeException JavaDoc("did not find direct reasoner");
660             }
661         };
662
663         // will throw an exception if the wrong code path is taken
664
A.hasSuperClass(b, true);
665     }
666
667     /**
668      * Bug report by Ivan Ferrari (ivan_ferrari_75 [ivan_ferrari_75@yahoo.it]) -
669      * duplicate nodes in output
670      */

671     public void test_if_01() {
672         //create a new default model
673
OntModel m = ModelFactory.createOntologyModel();
674
675         m.getDocumentManager().addAltEntry(
676             "http://www.w3.org/2001/sw/WebOnt/guide-src/wine",
677             "file:testing/ontology/bugs/oldwine.owl");
678         m.getDocumentManager().addAltEntry(
679             "http://www.w3.org/2001/sw/WebOnt/guide-src/food",
680             "file:testing/ontology/bugs/oldfood.owl");
681
682         // note: due to bug in the Wine example, we have to manually read the
683
// imported food document
684
m.getDocumentManager().setProcessImports(false);
685         m.read("http://www.w3.org/2001/sw/WebOnt/guide-src/wine");
686         m.getDocumentManager().setProcessImports(true);
687         m.getDocumentManager().loadImport(m, "http://www.w3.org/2001/sw/WebOnt/guide-src/food");
688
689         OntClass ontclass = m.getOntClass("http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine");
690
691         int nNamed = 0;
692         int nRestriction = 0;
693         int nAnon = 0;
694
695         for (ExtendedIterator iter2 = ontclass.listSuperClasses(true); iter2.hasNext();) {
696             OntClass ontsuperclass = (OntClass) iter2.next();
697
698             //this is to view different anonymous IDs
699
if (!ontsuperclass.isAnon()) {
700                 nNamed++;
701             }
702             else if (ontsuperclass.canAs(Restriction.class)) {
703                 ontsuperclass.asRestriction();
704                 nRestriction++;
705             }
706             else {
707                 //System.out.println("anon. super: " + ontsuperclass.getId());
708
nAnon++;
709             }
710         }
711
712         assertEquals("Should be two named super classes ", 2, nNamed);
713         assertEquals("Should be nine named super classes ", 9, nRestriction);
714         assertEquals("Should be no named super classes ", 0, nAnon);
715     }
716
717     /** Bug report by Lawrence Tay - missing datatype property */
718     public void test_lt_01() {
719         OntModel m = ModelFactory.createOntologyModel();
720
721         DatatypeProperty p = m.createDatatypeProperty(NS + "p");
722         OntClass c = m.createClass(NS + "A");
723
724         Individual i = m.createIndividual(NS + "i", c);
725         i.addProperty(p, "testData");
726
727         int count = 0;
728
729         for (Iterator j = i.listPropertyValues(p); j.hasNext();) {
730             //System.err.println("Individual i has p value: " + j.next());
731
j.next();
732             count++;
733         }
734
735         assertEquals("i should have one property", 1, count);
736     }
737
738
739     /** Bug report by David Kensche [david.kensche@post.rwth-aachen.de] - NPE in listDeclaredProperties */
740     public void test_dk_01() {
741         OntModel m = ModelFactory.createOntologyModel();
742         m.read( "file:testing/ontology/bugs/test_dk_01.xml" );
743
744         String JavaDoc NS = "http://localhost:8080/Repository/QueryAgent/UserOntology/qgen-example-1#";
745         String JavaDoc[] classes = new String JavaDoc[] {NS+"C1", NS+"C3", NS+"C2"};
746
747         for (int i = 0; i < classes.length; i++) {
748             OntClass c = m.getOntClass( classes[i] );
749             for (Iterator j = c.listDeclaredProperties(); j.hasNext(); j.next() );
750         }
751     }
752
753     /** Bug report by Paulo Pinheiro da Silva [pp@ksl.stanford.edu] - exception while accessing PropertyAccessor.getDAMLValue */
754     public void test_ppds_01() {
755         DAMLModel m = ModelFactory.createDAMLModel();
756         DAMLClass c = m.createDAMLClass( NS + "C" );
757         DAMLInstance x = m.createDAMLInstance( c, NS + "x" );
758         DAMLProperty p = m.createDAMLProperty( NS + "p" );
759
760         x.addProperty( p, "(s (s 0))" );
761
762         PropertyAccessor a = x.accessProperty( p );
763         assertNull( "Property accessor value should be null", a.getDAMLValue() );
764     }
765
766     /** Bug report by anon at SourceForge - Bug ID 887409 */
767     public void test_anon_0() {
768         String JavaDoc NS = "http://example.org/foo#";
769         String JavaDoc sourceT =
770             "<rdf:RDF "
771             + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'"
772             + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'"
773             + " xmlns:ex='http://example.org/foo#'"
774             + " xmlns:owl='http://www.w3.org/2002/07/owl#'>"
775             + " <owl:ObjectProperty rdf:about='http://example.org/foo#p' />"
776             + " <owl:Class rdf:about='http://example.org/foo#A' />"
777             + " <ex:A rdf:about='http://example.org/foo#x' />"
778             + " <owl:Class rdf:about='http://example.org/foo#B'>"
779             + " <owl:equivalentClass>"
780             + " <owl:Restriction>"
781             + " <owl:onProperty rdf:resource='http://example.org/foo#p' />"
782             + " <owl:hasValue rdf:resource='http://example.org/foo#x' />"
783             + " </owl:Restriction>"
784             + " </owl:equivalentClass>"
785             + " </owl:Class>"
786             + "</rdf:RDF>";
787
788         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
789         m.read(new ByteArrayInputStream(sourceT.getBytes()), "http://example.org/foo");
790
791         OntClass B = m.getOntClass( NS + "B");
792         Restriction r = B.getEquivalentClass().asRestriction();
793         HasValueRestriction hvr = r.asHasValueRestriction();
794         RDFNode n = hvr.getHasValue();
795
796         assertTrue( "Should be an individual", n instanceof Individual );
797     }
798
799     /** Bug report by Zhao Jun [jeff@seu.edu.cn] - throws no such element exception */
800     public void test_zj_0() {
801         String JavaDoc NS = "file:/C:/orel/orel0_5.owl#";
802         String JavaDoc sourceT =
803             "<rdf:RDF " +
804             " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'" +
805             " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'" +
806             " xmlns:ex='http://example.org/foo#'" +
807             " xmlns:owl='http://www.w3.org/2002/07/owl#'" +
808             " xmlns:orel='file:/C:/orel/orel0_5.owl#'" +
809             " xml:base='file:/C:/orel/orel0_5.owl#'" +
810             " xmlns='file:/C:/orel/orel0_5.owl#'>" +
811             " <owl:ObjectProperty rdf:ID='hasAgent' />" +
812             " <owl:ObjectProperty rdf:ID='hasResource' />" +
813             " <owl:Class rdf:ID='MyPlay'>" +
814             " <rdfs:subClassOf>" +
815             " <owl:Restriction>" +
816             " <owl:onProperty rdf:resource='file:/C:/orel/orel0_5.owl#hasResource'/>" +
817             " <owl:hasValue>" +
818             " <orel:Resource rdf:ID='myResource'>" +
819             " <orel:resourceURI>http://mp3.com/newcd/sample.mp3</orel:resourceURI>" +
820             " </orel:Resource>" +
821             " </owl:hasValue>" +
822             " </owl:Restriction>" +
823             " </rdfs:subClassOf>" +
824             " <rdfs:subClassOf rdf:resource='http://www.w3.org/2002/07/owl#Thing'/>" +
825             " <rdfs:subClassOf>" +
826             " <owl:Restriction>" +
827             " <owl:onProperty rdf:resource='file:/C:/orel/orel0_5.owl#hasAgent'/>" +
828             " <owl:hasValue>" +
829             " <orel:Agent rdf:ID='myAgent'>" +
830             " <orel:agentPK>123456789</orel:agentPK>" +
831             " </orel:Agent>" +
832             " </owl:hasValue>" +
833             " </owl:Restriction>" +
834             " </rdfs:subClassOf>" +
835             " <rdfs:subClassOf rdf:resource='file:/C:/orel/orel0_5.owl#Play'/>" +
836             " </owl:Class>" +
837             "</rdf:RDF>";
838
839         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, null);
840         m.read(new ByteArrayInputStream(sourceT.getBytes()), "file:/C:/orel/orel0_5.owl");
841
842         OntClass myPlay = m.getOntClass( NS + "MyPlay");
843         for (Iterator i = myPlay.listDeclaredProperties(); i.hasNext(); ) {
844             //System.err.println( "prop " + i.next() );
845
i.next();
846         }
847     }
848
849     /* Bug reprort by E. Johnson ejohnson@carolina.rr.com - ill formed list in writer */
850     public void test_ej_01() {
851         String JavaDoc BASE = "http://jena.hpl.hp.com/testing/ontology";
852         String JavaDoc NS = BASE + "#";
853
854         DAMLModel m = ModelFactory.createDAMLModel();
855         DAMLClass A = m.createDAMLClass(NS + "A");
856         DAMLClass B = m.createDAMLClass(NS + "B");
857         DAMLClass C = m.createDAMLClass(NS + "C");
858         DAMLList l = m.createDAMLList(new RDFNode[] {A, B, C});
859
860         assertTrue( l.isValid() );
861
862         Model baseModel = m.getBaseModel();
863         RDFWriter writer = baseModel.getWriter("RDF/XML-ABBREV");
864
865         // will generate warnings, so suppress until Jeremy has fixed
866
ByteArrayOutputStream out = new ByteArrayOutputStream();
867         //writer.write(baseModel, out, BASE );
868
}
869
870     /** Bug report by Harry Chen - closed exception when reading many models */
871     public void test_hc_01()
872         throws Exception JavaDoc
873     {
874         for (int i = 0; i < 5; i++) {
875
876             OntModel m = ModelFactory.createOntologyModel();
877
878             FileInputStream ifs = new FileInputStream("testing/ontology/relativenames.rdf");
879
880             //System.out.println("Start reading...");
881
m.read(ifs, "http://example.org/foo");
882             //System.out.println("Done reading...");
883

884             ifs.close();
885             //System.out.println("Closed ifs");
886
m.close();
887             //System.out.println("Closed model");
888
}
889     }
890
891     /** Bug report by sinclair bain (slbain) SF bugID 912202 - NPE in createOntResource() when 2nd param is null */
892     public void test_sb_01() {
893         OntModel model= ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RDFS_INF, null);
894
895         Resource result= null;
896         Resource nullValueForResourceType= null;
897
898         result= model.createOntResource( OntResource.class, nullValueForResourceType, "http://www.somewhere.com/models#SomeResourceName" );
899         assertNotNull( result );
900     }
901
902     /* Bug report from Dave Reynolds: listDeclaredProperties not complete */
903     public void test_der_02() {
904         String JavaDoc SOURCE=
905         "<?xml version='1.0'?>" +
906         "<!DOCTYPE owl [" +
907         " <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' >" +
908         " <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#' >" +
909         " <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#' >" +
910         " <!ENTITY owl 'http://www.w3.org/2002/07/owl#' >" +
911         " <!ENTITY dc 'http://purl.org/dc/elements/1.1/' >" +
912         " <!ENTITY base 'http://jena.hpl.hp.com/test' >" +
913         " ]>" +
914         "<rdf:RDF xmlns:owl ='&owl;' xmlns:rdf='&rdf;' xmlns:rdfs='&rdfs;' xmlns:dc='&dc;' xmlns='&base;#' xml:base='&base;'>" +
915         " <owl:ObjectProperty rdf:ID='hasPublications'>" +
916         " <rdfs:domain>" +
917         " <owl:Class>" +
918         " <owl:unionOf rdf:parseType='Collection'>" +
919         " <owl:Class rdf:about='#Project'/>" +
920         " <owl:Class rdf:about='#Task'/>" +
921         " </owl:unionOf>" +
922         " </owl:Class>" +
923         " </rdfs:domain>" +
924         " <rdfs:domain rdf:resource='#Dummy' />" +
925         " <rdfs:range rdf:resource='#Publications'/>" +
926         " </owl:ObjectProperty>" +
927         " <owl:Class rdf:ID='Dummy'>" +
928         " </owl:Class>" +
929         "</rdf:RDF>";
930         String JavaDoc NS = "http://jena.hpl.hp.com/test#";
931         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
932         m.read(new ByteArrayInputStream( SOURCE.getBytes()), NS );
933
934         OntClass dummy = m.getOntClass( NS + "Dummy" );
935         // assert commented out - bug not accepted -ijd
936
//TestUtil.assertIteratorValues( this, dummy.listDeclaredProperties(),
937
// new Object[] {m.getObjectProperty( NS+"hasPublications")} );
938
}
939
940     /** Bug report from Dave - cycles checking code still not correct */
941     public void test_der_03() {
942         String JavaDoc NS = "http://jena.hpl.hp.com/test#";
943         OntModel om = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
944         OntClass A = om.createClass(NS+"A");
945         OntClass B = om.createClass(NS+"B");
946         OntClass C = om.createClass(NS+"C");
947         A.addSuperClass(B);
948         A.addSuperClass(C);
949         B.addSuperClass(C);
950         C.addSuperClass(B);
951
952         TestUtil.assertIteratorValues( this, A.listSuperClasses( true ), new Object JavaDoc[] {B,C} );
953     }
954
955
956     /**
957      * Bug report by pierluigi.damadio@katamail.com: raises conversion exception
958      */

959     public void test_pd_01() {
960         String JavaDoc SOURCE =
961             "<?xml version='1.0'?>" +
962             "<rdf:RDF" +
963             " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'" +
964             " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'" +
965             " xmlns:owl='http://www.w3.org/2002/07/owl#'" +
966             " xml:base='http://iasi.cnr.it/leks/localSchema1#'" +
967             " xmlns:test='http://iasi.cnr.it/test/test1#'" +
968             " xmlns='http://iasi.cnr.it/test/test1#'>" +
969             " <owl:Ontology rdf:about=''/>" +
970             " <owl:Class rdf:ID='Hotel'/>" +
971             " <owl:Class rdf:ID='Hotel5Stars'>" +
972             " <rdfs:subClassOf>" +
973             " <owl:Restriction>" +
974             " <owl:onProperty rdf:resource='#hasCategory'/>" +
975             " <owl:hasValue rdf:resource='#Category5'/>" +
976             " </owl:Restriction>" +
977             " </rdfs:subClassOf>" +
978             " </owl:Class>" +
979             " <owl:DatatypeProperty rdf:ID='hasCategory'>" +
980             " <rdfs:range rdf:resource='http://www.w3.org/2001/XMLSchema#string'/>" +
981             " <rdfs:domain rdf:resource='#Hotel'/>" +
982             " <rdf:type rdf:resource='http://www.w3.org/2002/07/owl#FunctionalProperty'/>" +
983             " </owl:DatatypeProperty>" +
984             " <owl:Thing rdf:ID='Category5'/>" +
985             "</rdf:RDF>";
986         String JavaDoc NS = "http://iasi.cnr.it/leks/localSchema1#";
987         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
988         m.read(new ByteArrayInputStream( SOURCE.getBytes()), NS );
989
990         for (ExtendedIterator j = m.listRestrictions(); j.hasNext(); ) {
991               Restriction r = (Restriction) j.next();
992               if (r.isHasValueRestriction()) {
993                   HasValueRestriction hv = r.asHasValueRestriction();
994                   String JavaDoc s = hv.getHasValue().toString();
995                   //System.out.println( s );
996
}
997         }
998     }
999
1000    /** Bug report from Ole Hjalmar - direct subClassOf not reporting correct result with rule reasoner */
1001    public void xxtest_oh_01() {
1002        String JavaDoc NS = "http://www.idi.ntnu.no/~herje/ja/";
1003        Resource[] expected = new Resource[] {
1004            ResourceFactory.createResource( NS+"reiseliv.owl#Reiseliv" ),
1005            ResourceFactory.createResource( NS+"hotell.owl#Hotell" ),
1006            ResourceFactory.createResource( NS+"restaurant.owl#Restaurant" ),
1007            ResourceFactory.createResource( NS+"restaurant.owl#UteRestaurant" ),
1008            ResourceFactory.createResource( NS+"restaurant.owl#UteBadRestaurant" ),
1009            ResourceFactory.createResource( NS+"restaurant.owl#UteDoRestaurant" ),
1010            ResourceFactory.createResource( NS+"restaurant.owl#SkogRestaurant" ),
1011        };
1012
1013        test_oh_01scan( OntModelSpec.OWL_MEM, "No inf", expected );
1014        test_oh_01scan( OntModelSpec.OWL_MEM_MINI_RULE_INF, "Mini rule inf", expected );
1015        test_oh_01scan( OntModelSpec.OWL_MEM_RULE_INF, "Full rule inf", expected );
1016        test_oh_01scan( OntModelSpec.OWL_MEM_MICRO_RULE_INF, "Micro rule inf", expected );
1017    }
1018
1019    private void test_oh_01scan( OntModelSpec s, String JavaDoc prompt, Resource[] expected ) {
1020        String JavaDoc NS = "http://www.idi.ntnu.no/~herje/ja/reiseliv.owl#";
1021        OntModel m = ModelFactory.createOntologyModel(s, null);
1022        m.read( "file:testing/ontology/bugs/test_oh_01.owl");
1023
1024        System.out.println( prompt );
1025        OntClass r = m.getOntClass( NS + "Reiseliv" );
1026        List q = new ArrayList();
1027        Set seen = new HashSet();
1028        q.add( r );
1029
1030        while (!q.isEmpty()) {
1031            OntClass c = (OntClass) q.remove( 0 );
1032            seen.add( c );
1033
1034            for (Iterator i = c.listSubClasses( true ); i.hasNext(); ) {
1035                OntClass sub = (OntClass) i.next();
1036                if (!seen.contains( sub )) {
1037                    q.add( sub );
1038                }
1039            }
1040
1041            System.out.println( " Seen class " + c );
1042        }
1043
1044        // check we got all classes
1045
int mask = (1 << expected.length) - 1;
1046
1047        for (int j = 0; j < expected.length; j++) {
1048            if (seen.contains( expected[j] )) {
1049                mask &= ~(1 << j);
1050            }
1051            else {
1052                System.out.println( "Expected but did not see " + expected[j] );
1053            }
1054        }
1055
1056        for (Iterator k = seen.iterator(); k.hasNext(); ) {
1057            Resource res = (Resource) k.next();
1058            boolean isExpected = false;
1059            for (int j = 0; !isExpected && j < expected.length; j++) {
1060                isExpected = expected[j].equals( res );
1061            }
1062            if (!isExpected) {
1063                System.out.println( "Got unexpected result " + res );
1064            }
1065        }
1066
1067        assertEquals( "Some expected results were not seen", 0, mask );
1068    }
1069
1070    /** Test case for SF bug 927641 - list direct subclasses */
1071    public void test_sf_927641() {
1072        String JavaDoc NS = "http://example.org/test#";
1073        OntModel m0 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
1074        OntClass c0 = m0.createClass( NS + "C0" );
1075        OntClass c1 = m0.createClass( NS + "C1" );
1076        OntClass c2 = m0.createClass( NS + "C2" );
1077        OntClass c3 = m0.createClass( NS + "C3" );
1078
1079        c0.addSubClass( c1 );
1080        c1.addSubClass( c2 );
1081        c2.addEquivalentClass( c3 );
1082
1083        // now c1 is the direct super-class of c2, even allowing for the equiv with c3
1084
assertFalse( "pass 1: c0 should not be a direct super of c2", c2.hasSuperClass( c0, true ) );
1085        assertFalse( "pass 1: c3 should not be a direct super of c2", c2.hasSuperClass( c3, true ) );
1086        assertFalse( "pass 1: c2 should not be a direct super of c2", c2.hasSuperClass( c2, true ) );
1087        assertTrue( "pass 1: c1 should be a direct super of c2", c2.hasSuperClass( c1, true ) );
1088
1089        // second pass - with inference
1090
m0 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_RULE_INF );
1091        c0 = m0.createClass( NS + "C0" );
1092        c1 = m0.createClass( NS + "C1" );
1093        c2 = m0.createClass( NS + "C2" );
1094        c3 = m0.createClass( NS + "C3" );
1095
1096        c0.addSubClass( c1 );
1097        c1.addSubClass( c2 );
1098        c2.addEquivalentClass( c3 );
1099
1100        // now c1 is the direct super-class of c2, even allowing for the equiv with c3
1101
assertFalse( "pass 2: c0 should not be a direct super of c2", c2.hasSuperClass( c0, true ) );
1102        assertFalse( "pass 2: c3 should not be a direct super of c2", c2.hasSuperClass( c3, true ) );
1103        assertFalse( "pass 2: c2 should not be a direct super of c2", c2.hasSuperClass( c2, true ) );
1104        assertTrue( "pass 2: c1 should be a direct super of c2", c2.hasSuperClass( c1, true ) );
1105    }
1106
1107
1108    /** Test case for SF bug 934528 - conversion exception with owl:Thing and owl:Nothing when no reasoner */
1109    public void test_sf_934528() {
1110        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
1111
1112        Resource r = (Resource) OWL.Thing.inModel( m );
1113        OntClass thingClass = (OntClass) r.as( OntClass.class );
1114        assertNotNull( thingClass );
1115
1116        r = (Resource) OWL.Nothing.inModel( m );
1117        OntClass nothingClass = (OntClass) r.as( OntClass.class );
1118        assertNotNull( nothingClass );
1119    }
1120
1121    /** Test case for SF bug 937810 - NPE from ModelSpec.getDescription() */
1122    public void test_sf_937810() throws IllegalAccessException JavaDoc {
1123        Field JavaDoc[] specs = OntModelSpec.class.getDeclaredFields();
1124
1125        for (int i = 0; i < specs.length; i++) {
1126            if (Modifier.isPublic( specs[i].getModifiers()) &&
1127                Modifier.isStatic( specs[i].getModifiers()) &&
1128                specs[i].getType().equals( OntModelSpec.class )) {
1129                OntModelSpec s = (OntModelSpec) specs[i].get( null );
1130                assertNotNull( s.getDescription() );
1131            }
1132        }
1133    }
1134
1135    /** Test case for SF bug 940570 - listIndividuals not working with RDFS_INF
1136     */

1137    public void test_sf_940570() {
1138        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_RDFS_INF );
1139        OntClass C = m.createClass( NS + "C" );
1140        Resource a = m.createResource( NS + "a", C );
1141
1142        TestUtil.assertIteratorValues( this, m.listIndividuals(), new Object JavaDoc[] {a} );
1143
1144        OntModel dm = ModelFactory.createOntologyModel( OntModelSpec.DAML_MEM_RULE_INF );
1145        OntClass D = dm.createClass( NS + "D" );
1146        Resource b = dm.createResource( NS + "b", D );
1147
1148        TestUtil.assertIteratorValues( this, dm.listIndividuals(), new Object JavaDoc[] {b} );
1149    }
1150
1151    /** Test case for SF bug 940570 - listIndividuals not working with RDFS_INF (rdfs case)
1152     */

1153    public void test_sf_940570_rdfs() {
1154        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_RDFS_INF );
1155        OntClass C = m.createClass( NS + "C" );
1156        Resource a = m.createResource( NS + "a", C );
1157
1158        TestUtil.assertIteratorValues( this, m.listIndividuals(), new Object JavaDoc[] {a} );
1159    }
1160
1161    /** Test case for SF bug 940570 - listIndividuals not working with RDFS_INF (daml case)
1162     */

1163    public void test_sf_940570_daml() {
1164        OntModel dm = ModelFactory.createOntologyModel( OntModelSpec.DAML_MEM_RULE_INF );
1165        OntClass D = dm.createClass( NS + "D" );
1166        Resource b = dm.createResource( NS + "b", D );
1167
1168        TestUtil.assertIteratorValues( this, dm.listIndividuals(), new Object JavaDoc[] {b} );
1169    }
1170
1171    /** Test case for SF bug 945436 - a xml:lang='' in the dataset causes sring index exception in getLabel() */
1172    public void test_sf_945436() {
1173        String JavaDoc SOURCE=
1174            "<?xml version='1.0'?>" +
1175            "<!DOCTYPE owl [" +
1176            " <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' >" +
1177            " <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#' >" +
1178            " <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#' >" +
1179            " <!ENTITY owl 'http://www.w3.org/2002/07/owl#' >" +
1180            " <!ENTITY dc 'http://purl.org/dc/elements/1.1/' >" +
1181            " <!ENTITY base 'http://jena.hpl.hp.com/test' >" +
1182            " ]>" +
1183            "<rdf:RDF xmlns:owl ='&owl;' xmlns:rdf='&rdf;' xmlns:rdfs='&rdfs;' xmlns:dc='&dc;' xmlns='&base;#' xml:base='&base;'>" +
1184            " <C rdf:ID='x'>" +
1185            " <rdfs:label xml:lang=''>a_label</rdfs:label>" +
1186            " </C>" +
1187            " <owl:Class rdf:ID='C'>" +
1188            " </owl:Class>" +
1189            "</rdf:RDF>";
1190        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
1191        m.read( new StringReader( SOURCE ), null );
1192        Individual x = m.getIndividual( "http://jena.hpl.hp.com/test#x" );
1193        assertEquals( "Label on resource x", "a_label", x.getLabel( null) );
1194        assertEquals( "Label on resource x", "a_label", x.getLabel( "" ) );
1195        assertSame( "fr label on resource x", null, x.getLabel( "fr" ) );
1196    }
1197
1198    /** Test case for SF bug 948995 - OWL full should allow inverse functional datatype properties */
1199    public void test_sf_948995() {
1200        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM ); // OWL dl
1201
DatatypeProperty dp = m.createDatatypeProperty( NS + "dp" );
1202        dp.addRDFType( OWL.InverseFunctionalProperty );
1203
1204        boolean ex = false;
1205        try {
1206            dp.as( InverseFunctionalProperty.class );
1207        }
1208        catch (ConversionException e) {
1209            ex = true;
1210        }
1211        assertTrue( "Should have been a conversion exception", ex );
1212
1213        m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); // OWL full
1214
dp = m.createDatatypeProperty( NS + "dp" );
1215        dp.addRDFType( OWL.InverseFunctionalProperty );
1216
1217        ex = false;
1218        try {
1219            dp.as( InverseFunctionalProperty.class );
1220        }
1221        catch (ConversionException e) {
1222            ex = true;
1223        }
1224        assertFalse( "Should not have been a conversion exception", ex );
1225    }
1226
1227    /** Test case for SF bug 969475 - the return value for getInverse() on an ObjectProperty should be an object property */
1228    public void test_sf_969475() {
1229        String JavaDoc SOURCE=
1230            "<?xml version='1.0'?>" +
1231            "<!DOCTYPE owl [" +
1232            " <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' >" +
1233            " <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#' >" +
1234            " <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#' >" +
1235            " <!ENTITY owl 'http://www.w3.org/2002/07/owl#' >" +
1236            " <!ENTITY dc 'http://purl.org/dc/elements/1.1/' >" +
1237            " <!ENTITY base 'http://jena.hpl.hp.com/test' >" +
1238            " ]>" +
1239            "<rdf:RDF xmlns:owl ='&owl;' xmlns:rdf='&rdf;' xmlns:rdfs='&rdfs;' xmlns:dc='&dc;' xmlns='&base;#' xml:base='&base;'>" +
1240            " <owl:ObjectProperty rdf:ID='p0'>" +
1241            " <owl:inverseOf>" +
1242            " <owl:ObjectProperty rdf:ID='q0' />" +
1243            " </owl:inverseOf>" +
1244            " </owl:ObjectProperty>" +
1245            " <owl:ObjectProperty rdf:ID='p1'>" +
1246            " <owl:inverseOf>" +
1247            " <owl:ObjectProperty rdf:ID='q1' />" +
1248            " </owl:inverseOf>" +
1249            " </owl:ObjectProperty>" +
1250            "</rdf:RDF>";
1251        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
1252        m.read( new StringReader( SOURCE ), null );
1253
1254        ObjectProperty p0 = m.getObjectProperty( "http://jena.hpl.hp.com/test#p0");
1255        Object JavaDoc invP0 = p0.getInverseOf();
1256
1257        assertEquals( m.getResource( "http://jena.hpl.hp.com/test#q0"), invP0 );
1258        assertTrue( "Should be an ObjectProperty facet", invP0 instanceof ObjectProperty );
1259
1260        ObjectProperty q1 = m.getObjectProperty( "http://jena.hpl.hp.com/test#q1");
1261        Object JavaDoc invQ1 = q1.getInverse();
1262
1263        assertEquals( m.getResource( "http://jena.hpl.hp.com/test#p1"), invQ1 );
1264        assertTrue( "Should be an ObjectProperty facet", invQ1 instanceof ObjectProperty );
1265    }
1266
1267    /** Test case for SF bug 978259 - missing supports() checks in OWL DL and Lite profiles */
1268    public void test_sf_978259() {
1269        OntModel md = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
1270        OntModel ml = ModelFactory.createOntologyModel( OntModelSpec.OWL_LITE_MEM );
1271
1272        DataRange drd = md.createDataRange( md.createList( new Resource[] {OWL.Thing}) );
1273
1274        assertNotNull( drd );
1275
1276        HasValueRestriction hvrd = md.createHasValueRestriction( null, RDFS.seeAlso, OWL.Thing );
1277
1278        assertNotNull( hvrd );
1279    }
1280
1281    public void test_ijd_01() {
1282        String JavaDoc SOURCE=
1283            "<!DOCTYPE rdf:RDF [" +
1284            " <!ENTITY nuin 'http://www.nuin.org'>" +
1285            " <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" +
1286            " <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#'>" +
1287            " <!ENTITY owl 'http://www.w3.org/2002/07/owl#'>" +
1288            " <!ENTITY jms 'http://jena.hpl.hp.com/2003/08/jms#'>" +
1289            " <!ENTITY reasoner 'http://jena.hpl.hp.com/2003/'>" +
1290            " <!ENTITY base '&nuin;/demo/kma'>" +
1291            " <!ENTITY kma '&base;#'>" +
1292            "]>" +
1293            "<rdf:RDF" +
1294            " xmlns:rdf ='&rdf;'" +
1295            " xmlns:xsd ='&xsd;'" +
1296            " xmlns:owl ='&owl;'" +
1297            " xmlns:jms ='&jms;'" +
1298            " xmlns:kma ='&kma;'" +
1299            " xml:base ='&base;'" +
1300            ">" +
1301            " <kma:AgentConfiguration rdf:about='&kma;ijdTest'>" +
1302            " <kma:rdfModelSpec>" +
1303            " <jms:OntModelSpec>" +
1304            " <jms:ontLanguage rdf:resource='&owl;' />" +
1305            " <jms:reasonsWith>" +
1306            " <jms:Reasoner>" +
1307            " <jms:reasoner rdf:resource='&reasoner;OWLFBRuleReasoner' />" +
1308            " </jms:Reasoner>" +
1309            " </jms:reasonsWith>" +
1310            " </jms:OntModelSpec>" +
1311            " </kma:rdfModelSpec>" +
1312            " </kma:AgentConfiguration>" +
1313            "</rdf:RDF>";
1314
1315        Model m = ModelFactory.createDefaultModel();
1316        m.read( new StringReader( SOURCE ), null );
1317
1318        Resource root = m.getResource( "http://www.nuin.org/demo/kma#ijdTest" );
1319        Property rms = m.getProperty( "http://www.nuin.org/demo/kma#rdfModelSpec");
1320        Resource conf = root.getProperty(rms).getResource();
1321        OntModel om = (OntModel) ModelFactory.createSpec(conf,m)
1322                                             .createModel();
1323
1324        OntClass A = om.createClass( "A" );
1325        OntClass B = om.createClass( "B" );
1326        OntClass C = om.createClass( "C" );
1327        C.addSuperClass(B);
1328        B.addSuperClass(A);
1329        assertTrue( C.hasSuperClass(A) );
1330    }
1331
1332    /**
1333     * Bug report by James Tizard - failure in listIndividuals with DIGexception causes
1334     * blocked thread
1335     */

1336    public void test_jt_01() {
1337        // set up a configuration resource to connect to the reasoner
1338
// on port 2004 on the local system
1339
Model cModel = ModelFactory.createDefaultModel();
1340        Resource conf = cModel.createResource();
1341        conf.addProperty( ReasonerVocabulary.EXT_REASONER_URL, cModel.createResource( "http://localhost:2004" ) );
1342
1343        // create the reasoner factory and the reasoner
1344
DIGReasonerFactory drf = (DIGReasonerFactory) ReasonerRegistry.theRegistry()
1345                .getFactory( DIGReasonerFactory.URI );
1346        DIGReasoner r = (DIGReasoner) drf.create( conf );
1347
1348        // now make a model
1349
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
1350        spec.setReasoner( r );
1351        OntModel m = ModelFactory.createOntologyModel( spec, null );
1352
1353        boolean ex = false;
1354        try {
1355            Iterator i = m.listIndividuals(); // if this throws a DIG exception
1356
System.out.println( i.hasNext() ); // then this doesn't return
1357
}
1358        catch (DIGWrappedException e) {
1359            ex = true;
1360        }
1361        assertTrue( "Should have seen a dig wrapped exception for connection fail", ex );
1362    }
1363
1364
1365    /**
1366     * Bug report by David Bigwood - listDeclaredProps(false) fails when props
1367     * are defined in an imported model
1368     */

1369    public void test_dab_01() {
1370        OntModel m0 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
1371
1372        // in model M0, p0 has class c0 in the domain
1373
OntClass c0 = m0.createClass( NS + "c0" );
1374        ObjectProperty p0 = m0.createObjectProperty( NS + "p0" );
1375        p0.setDomain( c0 );
1376
1377        // in model M1, class c1 is a subClass of c0
1378
OntModel m1 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
1379        OntClass c1 = m1.createClass( NS + "c1" );
1380        c1.addSuperClass( c0 );
1381
1382        // simulate imports
1383
m1.addSubModel( m0 );
1384
1385        // get a c0 reference from m1
1386
OntClass cc0 = m1.getOntClass( NS + "c0" );
1387        assertNotNull( cc0 );
1388
1389        TestUtil.assertIteratorValues( this, c1.listDeclaredProperties(), new Object JavaDoc[] {p0} );
1390        TestUtil.assertIteratorValues( this, c0.listDeclaredProperties(false), new Object JavaDoc[] {p0} );
1391
1392        // this is the one that fails per David's bug report
1393
TestUtil.assertIteratorValues( this, cc0.listDeclaredProperties(false), new Object JavaDoc[] {p0} );
1394    }
1395
1396
1397    // Internal implementation methods
1398
//////////////////////////////////
1399

1400    private int getStatementCount(OntModel ontModel) {
1401        int count = 0;
1402        for (Iterator it = ontModel.listStatements(); it.hasNext(); it.next()) {
1403            count++;
1404        }
1405        return count;
1406    }
1407
1408    //==============================================================================
1409
// Inner class definitions
1410
//==============================================================================
1411

1412    class MockTransactionHandler extends SimpleTransactionHandler {
1413        boolean m_inTransaction = false;
1414        boolean m_aborted = false;
1415        boolean m_committed = false;
1416
1417        public void begin() {
1418            m_inTransaction = true;
1419        }
1420        public void abort() {
1421            m_inTransaction = false;
1422            m_aborted = true;
1423        }
1424        public void commit() {
1425            m_inTransaction = false;
1426            m_committed = true;
1427        }
1428    }
1429}
1430
1431/*
1432 * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP All rights
1433 * reserved.
1434 *
1435 * Redistribution and use in source and binary forms, with or without
1436 * modification, are permitted provided that the following conditions are met:
1437 * 1. Redistributions of source code must retain the above copyright notice,
1438 * this list of conditions and the following disclaimer.
1439 * 2. Redistributions in binary form must reproduce the above copyright
1440 * notice, this list of conditions and the following disclaimer in the
1441 * documentation and/or other materials provided with the distribution.
1442 * 3. The name of the author may not be used to endorse or promote products
1443 * derived from this software without specific prior written permission.
1444 *
1445 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
1446 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1447 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
1448 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1449 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1450 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
1451 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
1452 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
1453 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1454 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1455 */

1456
Popular Tags