KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > test > TestBugs


1 /******************************************************************
2  * File: TestBugs.java
3  * Created by: Dave Reynolds
4  * Created on: 22-Aug-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestBugs.java,v 1.33 2005/04/11 11:27:04 der Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.test;
11
12 import java.io.*;
13
14 import com.hp.hpl.jena.datatypes.TypeMapper;
15 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
16 import com.hp.hpl.jena.ontology.*;
17 import com.hp.hpl.jena.ontology.daml.DAMLModel;
18 import com.hp.hpl.jena.rdf.model.*;
19 import com.hp.hpl.jena.reasoner.*;
20 import com.hp.hpl.jena.reasoner.rulesys.*;
21 import com.hp.hpl.jena.reasoner.test.TestUtil;
22 import com.hp.hpl.jena.util.*;
23 import com.hp.hpl.jena.util.iterator.ClosableIterator;
24 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
25 import com.hp.hpl.jena.vocabulary.*;
26
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import java.util.*;
31
32 /**
33  * Unit tests for reported bugs in the rule system.
34  *
35  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
36  * @version $Revision: 1.33 $ on $Date: 2005/04/11 11:27:04 $
37  */

38 public class TestBugs extends TestCase {
39
40     /**
41      * Boilerplate for junit
42      */

43     public TestBugs( String JavaDoc name ) {
44         super( name );
45     }
46     
47     /**
48      * Boilerplate for junit.
49      * This is its own test suite
50      */

51     public static TestSuite suite() {
52         return new TestSuite( TestBugs.class );
53 // TestSuite suite = new TestSuite();
54
// suite.addTest(new TestBugs( "xxtest_oh_01" ));
55
// return suite;
56
}
57
58     public void setUp() {
59         // ensure the ont doc manager is in a consistent state
60
OntDocumentManager.getInstance().reset( true );
61     }
62     
63     /**
64      * Report of NPE during processing on an ontology with a faulty intersection list,
65      * from Hugh Winkler.
66      */

67     public void testIntersectionNPE() {
68         Model base = ModelFactory.createDefaultModel();
69         base.read("file:testing/reasoners/bugs/bad-intersection.owl");
70         boolean foundBadList = false;
71         try {
72             InfGraph infgraph = ReasonerRegistry.getOWLReasoner().bind(base.getGraph());
73             ExtendedIterator ci = infgraph.find(null, RDF.Nodes.type, OWL.Class.asNode());
74             ci.close();
75         } catch (ReasonerException e) {
76             foundBadList = true;
77         }
78         assertTrue("Correctly detected the illegal list", foundBadList);
79     }
80     
81     /**
82      * Report of problems with cardinality v. maxCardinality usage in classification,
83      * from Hugh Winkler.
84      */

85     public void testCardinality1() {
86         Model base = ModelFactory.createDefaultModel();
87         base.read("file:testing/reasoners/bugs/cardFPTest.owl");
88         InfModel test = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), base);
89         String JavaDoc NAMESPACE = "urn:foo#";
90         Resource aDocument = test.getResource(NAMESPACE + "aDocument");
91         Resource documentType = test.getResource(NAMESPACE + "Document");
92         assertTrue("Cardinality-based classification", test.contains(aDocument, RDF.type, documentType));
93     }
94     
95     /**
96      * Report of functor literals leaking out of inference graphs and raising CCE
97      * in iterators.
98      */

99     public void testFunctorCCE() {
100         Model base = ModelFactory.createDefaultModel();
101         base.read("file:testing/reasoners/bugs/cceTest.owl");
102         InfModel test = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), base);
103
104         boolean b = anyInstancesOfNothing(test);
105         ResIterator rIter = test.listSubjects();
106         while (rIter.hasNext()) {
107             Resource res = rIter.nextResource();
108         }
109     }
110     
111     /** Helper function used in testFunctorCCE */
112     private boolean anyInstancesOfNothing(Model model) {
113         boolean hasAny = false;
114         try {
115             ExtendedIterator it = model.listStatements(null, RDF.type, OWL.Nothing);
116             hasAny = it.hasNext();
117             it.close();
118         } catch (ConversionException x) {
119             hasAny = false;
120         }
121         return hasAny;
122     }
123     
124     /**
125      * Report of functor literals leaking out in DAML processing as well.
126      */

127     public void testDAMLCCE() {
128         DAMLModel m = ModelFactory.createDAMLModel();
129         m.getDocumentManager().setMetadataSearchPath( "file:etc/ont-policy-test.rdf", true );
130         m.read( "file:testing/reasoners/bugs/literalLeak.daml",
131                 "http://www.daml.org/2001/03/daml+oil-ex", null );
132         ResIterator rIter = m.listSubjects();
133         while (rIter.hasNext()) {
134             Resource res = rIter.nextResource();
135             if (res.getNode().isLiteral()) {
136                 assertTrue("Error in resource " + res, false);
137             }
138         }
139     }
140     
141     public static final String JavaDoc INPUT_SUBCLASS =
142         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
143         "" +
144         "<rdf:RDF" +
145         " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" +
146         " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"" +
147         " xmlns:daml=\"http://www.daml.org/2001/03/daml+oil#\"" +
148         " xmlns:ex=\"http://localhost:8080/axis/daml/a.daml#\"" +
149         " xml:base=\"http://localhost:8080/axis/daml/a.daml\">" +
150         " " +
151         " <daml:Ontology rdf:about=\"\">" +
152         " <daml:imports rdf:resource=\"http://www.daml.org/2001/03/daml+oil\"/>" +
153         " </daml:Ontology>" +
154         " " +
155         " <daml:Class rdf:ID=\"cls1\"/>" +
156         " <daml:Class rdf:ID=\"cls2\">" +
157         " <daml:subClassOf rdf:resource=\"#cls1\"/>" +
158         " </daml:Class>" +
159         " <ex:cls2 rdf:ID=\"test\"/>" +
160         "</rdf:RDF>";
161
162     /**
163      * This test exposes an apparent problem in the reasoners. If the input data is
164      * changed from daml:subClassOf to rdfs:subClassOf, the asserts all pass. As is,
165      * the assert for res has rdf:type cls1 fails.
166      */

167     public void testSubClass() {
168         OntModel model = ModelFactory.createOntologyModel(OntModelSpec.DAML_MEM_RDFS_INF, null);
169         model.getDocumentManager().setMetadataSearchPath( "file:etc/ont-policy-test.rdf", true );
170         
171         String JavaDoc base = "http://localhost:8080/axis/daml/a.daml#";
172         model.read( new ByteArrayInputStream( INPUT_SUBCLASS.getBytes() ), base );
173         OntResource res = (OntResource) model.getResource( base+"test").as(OntResource.class);
174             
175         OntClass cls1 = (OntClass) model.getResource(base+"cls1").as(OntClass.class);
176         OntClass cls2 = (OntClass) model.getResource(base+"cls2").as(OntClass.class);
177         
178         assertTrue( "cls2 should be a super-class of cls1", cls2.hasSuperClass( cls1 ) );
179         assertTrue( "res should have rdf:type cls1", res.hasRDFType( cls1 ) );
180         assertTrue( "res should have rdf:type cls2", res.hasRDFType( cls2 ) );
181     }
182
183     public static final String JavaDoc INPUT_SUBPROPERTY =
184         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
185         "" +
186         "<rdf:RDF" +
187         " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" +
188         " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"" +
189         " xmlns:daml=\"http://www.daml.org/2001/03/daml+oil#\"" +
190         " xmlns=\"urn:x-hp-jena:test#\"" +
191         " xml:base=\"urn:x-hp-jena:test\">" +
192         " " +
193         " <daml:Ontology rdf:about=\"\">" +
194         " <daml:imports rdf:resource=\"http://www.daml.org/2001/03/daml+oil\"/>" +
195         " </daml:Ontology>" +
196         " " +
197         " <daml:Class rdf:ID=\"A\"/>" +
198         "" +
199         " <daml:ObjectProperty rdf:ID=\"p\" />" +
200         " <daml:ObjectProperty rdf:ID=\"q\">" +
201         " <daml:subPropertyOf rdf:resource=\"#p\"/>" +
202         " </daml:ObjectProperty>" +
203         "" +
204         " <A rdf:ID=\"a0\"/>" +
205         " <A rdf:ID=\"a1\">" +
206         " <q rdf:resource=\"#a0\" />" +
207         " </A>" +
208         "</rdf:RDF>";
209
210     /**
211      * This test exposes an apparent problem in the reasoners. If the input data is
212      * changed from daml:subPropertyOf to rdfs:subPropertyOf, the asserts all pass. As is,
213      * the assert for a1 p a0 fails.
214      */

215     public void testSubProperty() {
216         OntModel model = ModelFactory.createOntologyModel(OntModelSpec.DAML_MEM_RDFS_INF, null);
217         
218         String JavaDoc base = "urn:x-hp-jena:test#";
219         model.read( new ByteArrayInputStream( INPUT_SUBPROPERTY.getBytes() ), base );
220         
221         OntResource a0 = (OntResource) model.getResource( base+"a0").as(OntResource.class);
222         OntResource a1 = (OntResource) model.getResource( base+"a1").as(OntResource.class);
223             
224         ObjectProperty p = model.getObjectProperty( base+"p" );
225         ObjectProperty q = model.getObjectProperty( base+"q" );
226         
227         assertTrue("subProp relation present", q.hasProperty(RDFS.subPropertyOf, p));
228         assertTrue( "a1 q a0", a1.hasProperty( q, a0 ) ); // asserted
229
assertTrue( "a1 p a0", a1.hasProperty( p, a0 ) ); // entailed
230
}
231
232     /**
233      * Test problems with inferring equivalence of some simple class definitions,
234      * reported by Jeffrey Hau.
235      */

236     public void testEquivalentClass1() {
237         Model base = ModelFactory.createDefaultModel();
238         base.read("file:testing/reasoners/bugs/equivalentClassTest.owl");
239         InfModel test = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), base);
240         String JavaDoc NAMESPACE = "urn:foo#";
241         Resource A = test.getResource(NAMESPACE + "A");
242         Resource B = test.getResource(NAMESPACE + "B");
243         assertTrue("hasValue equiv deduction", test.contains(A, OWL.equivalentClass, B));
244     }
245
246     /**
247      * Test reported problem with OWL property axioms.
248      */

249     public void testOWLPropertyAxioms() {
250         Model data = ModelFactory.createDefaultModel();
251         Resource fp = data.createResource("urn:x-hp:eg/fp");
252         Resource ifp = data.createResource("urn:x-hp:eg/ifp");
253         Resource tp = data.createResource("urn:x-hp:eg/tp");
254         Resource sp = data.createResource("urn:x-hp:eg/sp");
255         data.add(fp, RDF.type, OWL.FunctionalProperty);
256         data.add(ifp, RDF.type, OWL.InverseFunctionalProperty);
257         data.add(tp, RDF.type, OWL.TransitiveProperty);
258         data.add(sp, RDF.type, OWL.SymmetricProperty);
259         InfModel infmodel = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), data);
260         assertTrue("property class axioms", infmodel.contains(fp, RDF.type, RDF.Property));
261         assertTrue("property class axioms", infmodel.contains(ifp, RDF.type, RDF.Property));
262         assertTrue("property class axioms", infmodel.contains(tp, RDF.type, RDF.Property));
263         assertTrue("property class axioms", infmodel.contains(sp, RDF.type, RDF.Property));
264         assertTrue("property class axioms", infmodel.contains(ifp, RDF.type, OWL.ObjectProperty));
265         assertTrue("property class axioms", infmodel.contains(tp, RDF.type, OWL.ObjectProperty));
266         assertTrue("property class axioms", infmodel.contains(sp, RDF.type, OWL.ObjectProperty));
267     }
268     
269     /**
270      * Test for a reported bug in delete
271      */

272     public void testDeleteBug() {
273         Model modelo = ModelFactory.createDefaultModel();
274         modelo.read("file:testing/reasoners/bugs/deleteBug.owl");
275         OntModel modeloOnt = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_RULE_INF, modelo );
276         Individual indi = modeloOnt.getIndividual("http://decsai.ugr.es/~ontoserver/bacarex2.owl#JS");
277         indi.remove();
278         ClosableIterator it = modeloOnt.listStatements(indi, null, (RDFNode) null);
279         boolean ok = ! it.hasNext();
280         it.close();
281         assertTrue(ok);
282       }
283     
284     /**
285      * Test looping on recursive someValuesFrom.
286      */

287     public void hiddenTestOWLLoop() {
288         Model data = FileManager.get().loadModel("file:testing/reasoners/bugs/loop.owl");
289         InfModel infmodel = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), data);
290         ((FBRuleInfGraph)infmodel.getGraph()).setTraceOn(true);
291         String JavaDoc baseURI = "http://jena.hpl.hp.com/eg#";
292         Resource C = infmodel.getResource(baseURI + "C");
293         Resource I = infmodel.getResource(baseURI + "i");
294         Property R = infmodel.getProperty(baseURI, "R");
295 // ((FBRuleInfGraph)infmodel.getGraph()).setTraceOn(true);
296
System.out.println("Check that the instance does have an R property");
297         Statement s = I.getProperty(R);
298         System.out.println(" - " + s);
299         System.out.println("And that the type of the R property is C");
300         Statement s2 = ((Resource)s.getObject()).getProperty(RDF.type);
301         System.out.println(" - " + s2);
302         System.out.println("But does that have an R property?");
303         Statement s3 = ((Resource)s.getObject()).getProperty(R);
304         System.out.println(" - " + s3);
305         System.out.println("List all instances of C");
306         int count = 0;
307         for (Iterator i = infmodel.listStatements(null, RDF.type, C); i.hasNext(); ) {
308             Statement st = (Statement)i.next();
309             System.out.println(" - " + st);
310             count++;
311         }
312         System.out.println("OK");
313 // infmodel.write(System.out);
314
// System.out.flush();
315
}
316     
317     /**
318      * Test bug with leaking variables which results in an incorrect "range = Nothing" deduction.
319      */

320     public void testRangeBug() {
321         Model model = FileManager.get().loadModel("file:testing/reasoners/bugs/rangeBug.owl");
322         Model m = ModelFactory.createDefaultModel();
323         Reasoner r = ReasonerRegistry.getOWLReasoner();
324         InfModel omodel = ModelFactory.createInfModel(r, model);
325         String JavaDoc baseuri = "http://decsai.ugr.es/~ontoserver/bacarex2.owl#";
326         Resource js = omodel.getResource(baseuri + "JS");
327         Resource surname = omodel.getResource(baseuri + "surname");
328         Statement s = omodel.createStatement(surname, RDFS.range, OWL.Nothing);
329         assertTrue(! omodel.contains(s));
330     }
331     
332     /**
333      * Test change of RDF specs to allow plain literals w/o lang and XSD string to be the same.
334      */

335     public void testLiteralBug() {
336         Model model = FileManager.get().loadModel("file:testing/reasoners/bugs/dtValidation.owl");
337         Model m = ModelFactory.createDefaultModel();
338         Reasoner r = ReasonerRegistry.getOWLReasoner();
339         InfModel infmodel = ModelFactory.createInfModel(r, model);
340         ValidityReport validity = infmodel.validate();
341         assertTrue (validity.isValid());
342     }
343     
344     /**
345      * Test that prototype nodes are now hidden
346      */

347     public void testHide() {
348         String JavaDoc NS = "http://jena.hpl.hp.com/bugs#";
349         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, null);
350         OntClass c = m.createClass(NS + "C");
351         OntResource i = m.createIndividual(c);
352         Iterator res = m.listStatements(null, RDF.type, c);
353         TestUtil.assertIteratorValues(this, res, new Statement[] {
354             m.createStatement(i, RDF.type, c)
355         });
356     }
357     
358     /**
359      * Also want to have hidden rb:xsdRange
360      */

361     public void testHideXSDRange() {
362         OntModelSpec[] specs = new OntModelSpec[] {
363                 OntModelSpec.OWL_MEM_RULE_INF,
364                 OntModelSpec.OWL_MEM_RDFS_INF,
365                 OntModelSpec.OWL_MEM_MINI_RULE_INF,
366                 OntModelSpec.OWL_MEM_MICRO_RULE_INF
367         };
368         for (int os = 0; os < specs.length; os++) {
369             OntModelSpec spec = specs[os];
370             OntModel m = ModelFactory.createOntologyModel(spec, null);
371             Iterator i = m.listOntProperties();
372             while (i.hasNext()) {
373                 Resource r = (Resource)i.next();
374                 if (r.getURI() != null && r.getURI().startsWith(ReasonerVocabulary.RBNamespace)) {
375                     assertTrue("Rubrik internal property leaked out: " + r + "(" + os + ")", false);
376                 }
377             }
378         }
379     }
380     
381     /**
382      * Test problem with bindSchema not interacting properly with validation.
383      */

384     public void testBindSchemaValidate() {
385         Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
386         Model schema = FileManager.get().loadModel("file:testing/reasoners/bugs/sbug.owl");
387         Model data = FileManager.get().loadModel("file:testing/reasoners/bugs/sbug.rdf");
388         
389         // Union version
390
InfModel infu = ModelFactory.createInfModel(reasoner, data.union(schema));
391         ValidityReport validity = infu.validate();
392         assertTrue( ! validity.isValid());
393         // debug print
394
// for (Iterator i = validity.getReports(); i.hasNext(); ) {
395
// System.out.println(" - " + i.next());
396
// }
397

398         // bindSchema version
399
InfModel inf = ModelFactory.createInfModel(reasoner.bindSchema(schema), data);
400         validity = inf.validate();
401         assertTrue( ! validity.isValid());
402     }
403     
404     /**
405      * Delete bug in generic rule reasoner.
406      */

407     public void testGenericDeleteBug() {
408         Model data = ModelFactory.createDefaultModel();
409         String JavaDoc NS = "urn:x-hp:eg/";
410         Property p = data.createProperty(NS, "p");
411         Resource x = data.createResource(NS + "x");
412         Resource y = data.createResource(NS + "y");
413         Statement sy = data.createStatement(y, p, "foo");
414         data.add(sy);
415         data.add(x, p, "foo");
416 // String rule = "[(?x eg:p ?m) -> (?x eg:same ?x)]";
417
String JavaDoc rule = "[(?x eg:p ?m) (?y eg:p ?m) -> (?x eg:same ?y) (?y eg:same ?x)]";
418         GenericRuleReasoner reasoner = (GenericRuleReasoner) GenericRuleReasonerFactory.theInstance().create(null);
419         reasoner.setMode(GenericRuleReasoner.FORWARD_RETE);
420         reasoner.setRules(Rule.parseRules(rule));
421         InfModel inf = ModelFactory.createInfModel(reasoner, data);
422         TestUtil.assertIteratorLength(inf.listStatements(y, null, (RDFNode)null), 3);
423         inf.remove(sy);
424         TestUtil.assertIteratorLength(inf.listStatements(y, null, (RDFNode)null), 0);
425     }
426     
427     /**
428      * RETE incremental processing bug.
429      */

430     public void testRETEInc() {
431        String JavaDoc rule = "(?x ?p ?y) -> (?p rdf:type rdf:Property) .";
432        Reasoner r = new GenericRuleReasoner(Rule.parseRules(rule));
433        InfModel m = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel());
434
435        Resource source = m.createResource("urn:alfie:testResource");
436        Property prop = m.createProperty("urn:alfie:testProperty");
437        Statement s1=m.createStatement(source, prop, "value1");
438        Statement s2=m.createStatement(source, prop, "value2");
439
440        m.add(s1);
441        assertIsProperty(m, prop);
442        m.add(s2);
443        m.remove(s1);
444        assertIsProperty(m, prop);
445     }
446     
447     /**
448      * RETE incremental processing bug.
449      */

450     public void testRETEDec() {
451        String JavaDoc rule = "(?x ?p ?y) -> (?p rdf:type rdf:Property) .";
452        Reasoner r = new GenericRuleReasoner(Rule.parseRules(rule));
453        InfModel m = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel());
454
455        Resource source = m.createResource("urn:alfie:testResource");
456        Property prop = m.createProperty("urn:alfie:testProperty");
457        Statement s1=m.createStatement(source, prop, "value1");
458        Statement s2=m.createStatement(source, prop, "value2");
459
460        m.add(prop, RDF.type, RDF.Property);
461        m.add(s1);
462        m.prepare();
463        m.remove(s1);
464        assertIsProperty(m, prop);
465     }
466
467     private void assertIsProperty(Model m, Property prop) {
468         assertTrue(m.contains(prop, RDF.type, RDF.Property));
469     }
470        
471     
472     /**
473      * Bug that exposed prototypes of owl:Thing despite hiding being switched on.
474      */

475     public void testHideOnOWLThing() {
476         Reasoner r = ReasonerRegistry.getOWLReasoner();
477         Model data = ModelFactory.createDefaultModel();
478         InfModel inf = ModelFactory.createInfModel(r, data);
479         StmtIterator things = inf.listStatements(null, RDF.type, OWL.Thing);
480         TestUtil.assertIteratorLength(things, 0);
481     }
482     
483     /**
484      * Limitation of someValuesFrom applied to datatype properties.
485      */

486     public void testSomeDatatype() throws IOException {
487         String JavaDoc uri = "http://www.daml.org/2001/03/daml+oil-ex-dt";
488         String JavaDoc filename = "testing/xsd/daml+oil-ex-dt.xsd";
489         TypeMapper tm = TypeMapper.getInstance();
490         XSDDatatype.loadUserDefined(uri, new FileReader(filename), null, tm);
491         
492         Model data = ModelFactory.createDefaultModel();
493         data.read("file:testing/reasoners/bugs/userDatatypes.owl");
494         InfModel inf = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), data);
495         
496         String JavaDoc egNS = "http://jena.hpl.hp.com/eg#";
497         Resource meR = inf.getResource(egNS + "me");
498         Resource TestR = inf.getResource(egNS + "Test");
499         assertTrue("somevalues inf for datatypes", inf.contains(meR, RDF.type, TestR));
500         
501         Resource Test2R = inf.getResource(egNS + "Test2");
502         Resource me2R = inf.getResource(egNS + "me2");
503         assertTrue("somevalues inf for datatypes", inf.contains(me2R, RDF.type, Test2R));
504         assertTrue("somevalues inf for user datatypes", inf.contains(meR, RDF.type, Test2R));
505     }
506     
507     /* Report on jena-dev that DAMLMicroReasoner infmodels don't support daml:subClassOf, etc */
508     public void testDAMLMicroReasonerSupports() {
509         Reasoner r = DAMLMicroReasonerFactory.theInstance().create( null );
510         assertTrue( "Should support daml:subClassOf", r.supportsProperty( DAML_OIL.subClassOf ));
511         assertTrue( "Should support daml:subPropertyOf", r.supportsProperty( DAML_OIL.subPropertyOf));
512         assertTrue( "Should support daml:domain", r.supportsProperty( DAML_OIL.domain ));
513         assertTrue( "Should support daml:range", r.supportsProperty( DAML_OIL.range ));
514     }
515     
516     /**
517      * Utility function.
518      * Create a model from an N3 string with OWL and EG namespaces defined.
519      */

520     public static Model modelFromN3(String JavaDoc src) {
521         String JavaDoc fullSource = "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n" +
522         "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n" +
523         "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n" +
524         "@prefix eg: <http://jena.hpl.hp.com/eg#> .\n" +
525                     "@prefix : <#> .\n"+ src + "\n";
526         Model result = ModelFactory.createDefaultModel();
527         result.read(new StringReader(fullSource), "", "N3");
528         return result;
529     }
530     
531     /** Bug report from Ole Hjalmar - direct subClassOf not reporting correct result with rule reasoner */
532     public void test_oh_01() {
533         String JavaDoc NS = "http://www.idi.ntnu.no/~herje/ja/";
534         Resource[] expected = new Resource[] {
535                 ResourceFactory.createResource( NS+"reiseliv.owl#Reiseliv" ),
536                 ResourceFactory.createResource( NS+"hotell.owl#Hotell" ),
537                 ResourceFactory.createResource( NS+"restaurant.owl#Restaurant" ),
538                 ResourceFactory.createResource( NS+"restaurant.owl#UteRestaurant" ),
539                 ResourceFactory.createResource( NS+"restaurant.owl#UteBadRestaurant" ),
540                 ResourceFactory.createResource( NS+"restaurant.owl#UteDoRestaurant" ),
541                 ResourceFactory.createResource( NS+"restaurant.owl#SkogRestaurant" ),
542             };
543         
544         test_oh_01scan( OntModelSpec.OWL_MEM, "No inf", expected );
545         test_oh_01scan( OntModelSpec.OWL_MEM_MINI_RULE_INF, "Mini rule inf", expected );
546         test_oh_01scan( OntModelSpec.OWL_MEM_MICRO_RULE_INF, "Micro rule inf", expected );
547         test_oh_01scan( OntModelSpec.OWL_MEM_RULE_INF, "Full rule inf", expected );
548     }
549     
550     /** Problem with bindSchema and validation rules */
551     public void test_der_validation() {
552         Model abox = FileManager.get().loadModel("file:testing/reasoners/owl/nondetbug.rdf");
553         List rules = FBRuleReasoner.loadRules("testing/reasoners/owl/nondetbug.rules");
554         GenericRuleReasoner r = new GenericRuleReasoner(rules);
555 // r.setTraceOn(true);
556
for (int i = 0; i < 10; i++) {
557             InfModel im = ModelFactory.createInfModel(r, abox);
558             assertTrue("failed on count " + i, im.contains(null, ReasonerVocabulary.RB_VALIDATION_REPORT, (RDFNode)null));
559         }
560     }
561     
562     // Temporary for debug
563
private void test_oh_01scan( OntModelSpec s, String JavaDoc prompt, Resource[] expected ) {
564         String JavaDoc NS = "http://www.idi.ntnu.no/~herje/ja/reiseliv.owl#";
565         OntModel m = ModelFactory.createOntologyModel(s, null);
566         m.read( "file:testing/ontology/bugs/test_oh_01.owl");
567
568 // System.out.println( prompt );
569
OntClass r = m.getOntClass( NS + "Reiseliv" );
570         
571         List q = new ArrayList();
572         Set seen = new HashSet();
573         q.add( r );
574         
575         while (!q.isEmpty()) {
576             OntClass c = (OntClass) q.remove( 0 );
577             seen.add( c );
578             
579             for (Iterator i = c.listSubClasses( true ); i.hasNext(); ) {
580                 OntClass sub = (OntClass) i.next();
581                 if (!seen.contains( sub )) {
582                     q.add( sub );
583                 }
584             }
585             
586 // System.out.println( " Seen class " + c );
587
}
588
589         // check we got all classes
590
int mask = (1 << expected.length) - 1;
591         
592         for (int j = 0; j < expected.length; j++) {
593             if (seen.contains( expected[j] )) {
594                 mask &= ~(1 << j);
595             }
596             else {
597 // System.out.println( "Expected but did not see " + expected[j] );
598
}
599         }
600         
601         for (Iterator k = seen.iterator(); k.hasNext(); ) {
602             Resource res = (Resource) k.next();
603             boolean isExpected = false;
604             for (int j = 0; !isExpected && j < expected.length; j++) {
605                 isExpected = expected[j].equals( res );
606             }
607             if (!isExpected) {
608 // System.out.println( "Got unexpected result " + res );
609
}
610         }
611         
612         assertEquals( "Some expected results were not seen", 0, mask );
613     }
614     
615     /**
616      * Bug report from David A Bigwood
617      */

618     public void test_domainInf() {
619         // create an OntModel
620
OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, null );
621         // populate the model with stuff
622
String JavaDoc NS = "http://m3t4.com/ont/#";
623         OntClass c1 = m.createClass( NS + "c1" );
624         OntClass c2 = m.createClass( NS + "c2" );
625         OntClass c3 = m.createClass( NS + "c3" );
626         OntProperty p1 = m.createObjectProperty( NS + "p1" );
627         // create a union class to contain the union operands
628
UnionClass uc = m.createUnionClass(null, null);
629         // add an operand
630
uc.addOperand( c1 );
631         assertEquals( "Size should be 1", 1, uc.getOperands().size() );
632         assertTrue( "uc should have c1 as union member", uc.getOperands().contains( c1 ) );
633         // add another operand
634
uc.addOperand( c2 );
635         assertEquals( "Size should be 2", 2, uc.getOperands().size() );
636         TestUtil.assertIteratorValues(this, uc.listOperands(), new Object JavaDoc[] { c1, c2 } );
637         // add a third operand
638
uc.addOperand( c3 );
639         assertEquals( "Size should be 3", 3, uc.getOperands().size() );
640         TestUtil.assertIteratorValues(this, uc.listOperands(), new Object JavaDoc[] { c1, c2, c3} );
641         // add union class as domain of a property
642
p1.addDomain(uc);
643     }
644     
645     // debug assistant
646
private void tempList(Model m, Resource s, Property p, RDFNode o) {
647         System.out.println("Listing of " + PrintUtil.print(s) + " " + PrintUtil.print(p) + " " + PrintUtil.print(o));
648         for (StmtIterator i = m.listStatements(s, p, o); i.hasNext(); ) {
649             System.out.println(" - " + i.next());
650         }
651     }
652     
653     public static void main(String JavaDoc[] args) {
654         TestBugs test = new TestBugs("test");
655 // test.hiddenTestDeleteBug();
656
}
657 }
658
659 /*
660     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
661     All rights reserved.
662
663     Redistribution and use in source and binary forms, with or without
664     modification, are permitted provided that the following conditions
665     are met:
666
667     1. Redistributions of source code must retain the above copyright
668        notice, this list of conditions and the following disclaimer.
669
670     2. Redistributions in binary form must reproduce the above copyright
671        notice, this list of conditions and the following disclaimer in the
672        documentation and/or other materials provided with the distribution.
673
674     3. The name of the author may not be used to endorse or promote products
675        derived from this software without specific prior written permission.
676
677     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
678     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
679     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
680     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
681     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
682     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
683     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
684     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
685     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
686     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
687 */
Popular Tags