1 19 20 package com.hp.hpl.jena.ontology.impl.test; 23 24 25 import java.util.*; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import junit.framework.TestCase; 33 34 import com.hp.hpl.jena.ontology.*; 35 import com.hp.hpl.jena.rdf.model.ModelFactory; 36 37 38 47 public class TestOntReasoning 48 extends TestCase 49 { 50 public static final String BASE = "http://jena.hpl.hp.com/testing/ontology"; 53 public static final String NS = BASE + "#"; 54 55 58 61 64 public TestOntReasoning( String name ) { 65 super( name ); 66 } 67 68 71 public void setUp() { 72 OntDocumentManager.getInstance().reset( true ); 74 } 75 76 77 public void tearDown() { 78 } 79 80 public void testSubClassDirectTransInf1a() { 81 OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG ); 82 83 OntClass A = m.createClass( NS + "A" ); 84 OntClass B = m.createClass( NS + "B" ); 85 OntClass C = m.createClass( NS + "C" ); 86 OntClass D = m.createClass( NS + "D" ); 87 88 A.addSubClass( B ); 89 A.addSubClass( C ); 90 C.addSubClass( D ); 91 92 iteratorTest( A.listSubClasses(), new Object [] {B, C, D} ); 93 iteratorTest( A.listSubClasses( true ), new Object [] {B, C} ); 94 } 95 96 public void testSubClassDirectTransInf1b() { 97 OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG ); 98 99 OntClass A = m.createClass( NS + "A" ); 100 OntClass B = m.createClass( NS + "B" ); 101 OntClass C = m.createClass( NS + "C" ); 102 OntClass D = m.createClass( NS + "D" ); 103 104 A.addSubClass( B ); 105 A.addSubClass( C ); 106 C.addSubClass( D ); 107 A.addSubClass( D ); 109 iteratorTest( A.listSubClasses(), new Object [] {B, C, D} ); 110 iteratorTest( A.listSubClasses( true ), new Object [] {B, C} ); 111 } 112 113 public void testSubClassDirectTransInf2a() { 114 OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM ); 116 spec.setReasonerFactory( null ); 117 OntModel m = ModelFactory.createOntologyModel( spec, null ); 118 119 OntClass A = m.createClass( NS + "A" ); 120 OntClass B = m.createClass( NS + "B" ); 121 OntClass C = m.createClass( NS + "C" ); 122 OntClass D = m.createClass( NS + "D" ); 123 124 A.addSubClass( B ); 125 A.addSubClass( C ); 126 C.addSubClass( D ); 127 128 iteratorTest( A.listSubClasses(), new Object [] {B, C} ); 129 iteratorTest( A.listSubClasses( true ), new Object [] {B, C} ); 130 } 131 132 public void testSubClassDirectTransInf2b() { 133 OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM ); 135 spec.setReasonerFactory( null ); 136 OntModel m = ModelFactory.createOntologyModel( spec, null ); 137 138 OntClass A = m.createClass( NS + "A" ); 139 OntClass B = m.createClass( NS + "B" ); 140 OntClass C = m.createClass( NS + "C" ); 141 OntClass D = m.createClass( NS + "D" ); 142 143 A.addSubClass( B ); 144 A.addSubClass( C ); 145 C.addSubClass( D ); 146 A.addSubClass( D ); 148 iteratorTest( A.listSubClasses(), new Object [] {B, C, D} ); 149 iteratorTest( A.listSubClasses( true ), new Object [] {B, C} ); 150 } 151 152 public void testSubPropertyDirectTransInf1a() { 153 OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG ); 154 155 OntProperty p = m.createObjectProperty( NS + "p" ); 156 OntProperty q = m.createObjectProperty( NS + "q" ); 157 OntProperty r = m.createObjectProperty( NS + "r" ); 158 OntProperty s = m.createObjectProperty( NS + "s" ); 159 160 p.addSubProperty( q ); 161 p.addSubProperty( r ); 162 r.addSubProperty( s ); 163 164 iteratorTest( p.listSubProperties(), new Object [] {p,q,r,s} ); 165 iteratorTest( p.listSubProperties( true ), new Object [] {q,r} ); 166 } 167 168 public void testSubPropertyDirectTransInf1b() { 169 OntModel m = ModelFactory.createOntologyModel( ProfileRegistry.OWL_LITE_LANG ); 170 171 OntProperty p = m.createObjectProperty( NS + "p" ); 172 OntProperty q = m.createObjectProperty( NS + "q" ); 173 OntProperty r = m.createObjectProperty( NS + "r" ); 174 OntProperty s = m.createObjectProperty( NS + "s" ); 175 176 p.addSubProperty( q ); 177 p.addSubProperty( r ); 178 r.addSubProperty( s ); 179 p.addSubProperty( s ); 181 iteratorTest( p.listSubProperties(), new Object [] {p,q,r,s} ); 182 iteratorTest( p.listSubProperties( true ), new Object [] {q,r} ); 183 } 184 185 public void testSubPropertyDirectTransInf2a() { 186 OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM ); 188 spec.setReasonerFactory( null ); 189 OntModel m = ModelFactory.createOntologyModel( spec, null ); 190 191 OntProperty p = m.createObjectProperty( NS + "p" ); 192 OntProperty q = m.createObjectProperty( NS + "q" ); 193 OntProperty r = m.createObjectProperty( NS + "r" ); 194 OntProperty s = m.createObjectProperty( NS + "s" ); 195 196 p.addSubProperty( q ); 197 p.addSubProperty( r ); 198 r.addSubProperty( s ); 199 200 iteratorTest( p.listSubProperties(), new Object [] {q,r} ); 201 iteratorTest( p.listSubProperties( true ), new Object [] {q,r} ); 202 } 203 204 public void testSubPropertyDirectTransInf2b() { 205 OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_LITE_MEM ); 207 spec.setReasonerFactory( null ); 208 OntModel m = ModelFactory.createOntologyModel( spec, null ); 209 210 OntProperty p = m.createObjectProperty( NS + "p" ); 211 OntProperty q = m.createObjectProperty( NS + "q" ); 212 OntProperty r = m.createObjectProperty( NS + "r" ); 213 OntProperty s = m.createObjectProperty( NS + "s" ); 214 215 p.addSubProperty( q ); 216 p.addSubProperty( r ); 217 r.addSubProperty( s ); 218 p.addSubProperty( s ); 220 iteratorTest( p.listSubProperties(), new Object [] {q,r,s} ); 221 iteratorTest( p.listSubProperties( true ), new Object [] {q,r} ); 222 } 223 224 public void testListDefinedProperties() { 225 OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_RULE_INF, null ); 226 227 OntClass organism = m.createClass( NS + "Organism" ); 229 OntClass vertebrate = m.createClass( NS + "Vertebrate" ); 230 OntClass mammal = m.createClass( NS + "Mammal" ); 231 OntClass dog = m.createClass( NS + "Dog" ); 232 233 organism.addSubClass( vertebrate ); 234 vertebrate.addSubClass( mammal ); 235 mammal.addSubClass( dog ); 236 237 OntClass covering = m.createClass( NS + "Covering" ); 239 Individual hair = m.createIndividual( NS+"hair", covering ); 240 241 DatatypeProperty limbsCount = m.createDatatypeProperty( NS + "limbsCount" ); 243 DatatypeProperty hasCovering = m.createDatatypeProperty( NS + "hasCovering" ); 244 DatatypeProperty numYoung = m.createDatatypeProperty( NS + "numYoung" ); 245 246 limbsCount.addDomain( vertebrate ); 248 numYoung.addDomain( mammal ); 249 250 Restriction r = m.createRestriction( hasCovering ); 252 r.convertToHasValueRestriction( hair ); 253 mammal.addSuperClass( r ); 254 255 iteratorTest( organism.listDeclaredProperties(), new Object [] {hasCovering} ); 256 iteratorTest( vertebrate.listDeclaredProperties(), new Object [] {limbsCount, hasCovering} ); 257 iteratorTest( mammal.listDeclaredProperties(), new Object [] {limbsCount, hasCovering, numYoung} ); 258 iteratorTest( dog.listDeclaredProperties(), new Object [] {limbsCount, hasCovering, numYoung} ); 259 iteratorTest( r.listDeclaredProperties(), new Object [] {hasCovering} ); 260 261 iteratorTest( organism.listDeclaredProperties(true), new Object [] {hasCovering} ); 262 iteratorTest( vertebrate.listDeclaredProperties(true), new Object [] {limbsCount} ); 263 iteratorTest( mammal.listDeclaredProperties(true), new Object [] {numYoung} ); 264 iteratorTest( dog.listDeclaredProperties(true), new Object [] {} ); 265 iteratorTest( r.listDeclaredProperties(true), new Object [] {hasCovering} ); 266 267 iteratorTest( organism.listDeclaredProperties(false), new Object [] {hasCovering} ); 268 iteratorTest( vertebrate.listDeclaredProperties(false), new Object [] {hasCovering,limbsCount} ); 269 iteratorTest( mammal.listDeclaredProperties(false), new Object [] {hasCovering,numYoung,limbsCount} ); 270 iteratorTest( dog.listDeclaredProperties(false), new Object [] {hasCovering,numYoung,limbsCount} ); 271 iteratorTest( r.listDeclaredProperties(false), new Object [] {hasCovering} ); 272 } 273 274 277 278 protected void iteratorTest( Iterator i, Object [] expected ) { 279 Log logger = LogFactory.getLog( getClass() ); 280 List expList = new ArrayList(); 281 for (int j = 0; j < expected.length; j++) { 282 expList.add( expected[j] ); 283 } 284 285 while (i.hasNext()) { 286 Object next = i.next(); 287 288 if (!expList.contains( next )) { 290 logger.debug( getName() + " - Unexpected iterator result: " + next ); 291 } 292 293 assertTrue( "Value " + next + " was not expected as a result from this iterator ", expList.contains( next ) ); 294 assertTrue( "Value " + next + " was not removed from the list ", expList.remove( next ) ); 295 } 296 297 if (!(expList.size() == 0)) { 298 logger.debug( getName() + " Expected iterator results not found" ); 299 for (Iterator j = expList.iterator(); j.hasNext(); ) { 300 logger.debug( getName() + " - missing: " + j.next() ); 301 } 302 } 303 assertEquals( "There were expected elements from the iterator that were not found", 0, expList.size() ); 304 } 305 306 307 311 } 312 313 314 343 344 | Popular Tags |