1 6 7 package com.hp.hpl.jena.rdf.model.test; 8 9 import java.util.*; 10 11 import com.hp.hpl.jena.rdf.model.*; 12 import com.hp.hpl.jena.vocabulary.*; 13 import com.hp.hpl.jena.graph.impl.*; 14 import com.hp.hpl.jena.rdf.model.impl.*; 15 import com.hp.hpl.jena.reasoner.*; 16 import com.hp.hpl.jena.reasoner.rulesys.*; 17 import com.hp.hpl.jena.shared.*; 18 import com.hp.hpl.jena.mem.*; 19 import com.hp.hpl.jena.ontology.*; 20 21 import junit.framework.*; 22 23 29 30 public class TestModelSpec extends ModelTestBase 31 { 32 public TestModelSpec( String name ) 33 { super( name ); } 34 35 public static TestSuite suite() 36 { return new TestSuite( TestModelSpec.class ); } 37 41 public void testNotFindMaker() 42 { 43 Resource type = resource( "jms:xyz" ); 44 assertSame( null, ModelMakerCreatorRegistry.findCreator( type ) ); 45 } 46 47 public void testFindMakerChoice() 48 { 49 Resource type1 = resource( "jms:type1" ), type2 = resource( "jms:type2" ); 50 ModelMakerCreator mmc1 = new ModelMakerCreator() 51 { public ModelMaker create( Model desc, Resource root ) { return null; } }; 52 ModelMakerCreator mmc2 = new ModelMakerCreator() 53 { public ModelMaker create( Model desc, Resource root ) { return null; } }; 54 ModelMakerCreatorRegistry.register( type1, mmc1 ); 55 ModelMakerCreatorRegistry.register( type2, mmc2 ); 56 assertSame( mmc1, ModelMakerCreatorRegistry.findCreator( type1 ) ); 57 assertSame( mmc2, ModelMakerCreatorRegistry.findCreator( type2 ) ); 58 } 59 60 public void testFindStandardMakers() 61 { 62 assertNotNull( ModelMakerCreatorRegistry.findCreator( JenaModelSpec.FileMakerSpec ) ); 63 assertNotNull( ModelMakerCreatorRegistry.findCreator( JenaModelSpec.MemMakerSpec ) ); 64 assertNotNull( ModelMakerCreatorRegistry.findCreator( JenaModelSpec.RDBMakerSpec ) ); 65 } 66 67 public void testDefaultMaker() 68 { 69 Model spec = modelWithStatements( "_x jms:maker _y; _y jms:reificationMode jms:rsMinimal" ); 70 ModelSpec ms = ModelFactory.createSpec( spec ); 71 Model m = ModelFactory.createModel( ms ) ; 72 assertTrue( m.getGraph() instanceof GraphMem ); 73 } 74 75 public void testAbsentDefaultMaker() 76 { 77 Model spec = modelWithStatements( "_x rdf:type jms:DefaultModelSpec" ); 78 ModelSpec ms = ModelFactory.createSpec( spec ); 79 Model m = ModelFactory.createModel( ms ) ; 80 assertTrue( m.getGraph() instanceof GraphMem ); 81 } 82 83 94 public void testNotFindCreator() 95 { 96 Resource type = resource( "jms:SomeType" ); 97 assertSame( null, ModelSpecCreatorRegistry.findCreator( type ) ); 98 } 99 100 public void testFindCreator() 101 { 102 Resource type = resource( "jms:SomeType" ); 103 ModelSpecCreator c = new ModelSpecCreator() 104 { public ModelSpec create( Resource root, Model m ) { return null; } }; 105 ModelSpecCreatorRegistry.register( type, c ); 106 assertSame( c, ModelSpecCreatorRegistry.findCreator( type ) ); 107 } 108 109 public void testFindCreatorChoice() 110 { 111 Resource type1 = resource( "jms:SomeType1" ); 112 Resource type2 = resource( "jms:SomeType2" ); 113 ModelSpecCreator c1 = new ModelSpecCreator() 114 { public ModelSpec create( Resource root, Model m ) { return null; } }; 115 ModelSpecCreator c2 = new ModelSpecCreator() 116 { public ModelSpec create( Resource root, Model m ) { return null; } }; 117 ModelSpecCreatorRegistry.register( type1, c1 ); 118 ModelSpecCreatorRegistry.register( type2, c2 ); 119 assertSame( c1, ModelSpecCreatorRegistry.findCreator( type1 ) ); 120 assertSame( c2, ModelSpecCreatorRegistry.findCreator( type2 ) ); 121 } 122 123 public void testHasStandardCreators() 124 { 125 assertNotNull( ModelSpecCreatorRegistry.findCreator( JenaModelSpec.InfModelSpec ) ); 126 assertNotNull( ModelSpecCreatorRegistry.findCreator( JenaModelSpec.PlainModelSpec ) ); 127 assertNotNull( ModelSpecCreatorRegistry.findCreator( JenaModelSpec.OntModelSpec ) ); 128 } 129 130 public void testNamedCreatePlain() 131 { 132 ModelSpec ms = ModelSpecFactory.createSpec( createPlainModelDesc() ); 133 Model m = ms.createModelOver( "aName" ); 134 assertTrue( m.getGraph() instanceof GraphMem ); 135 } 136 137 public void testNamedCreateInf() 138 { 139 String URI = DAMLMicroReasonerFactory.URI; 140 ModelSpec ms = ModelSpecFactory.createSpec( createInfModelDesc( URI ) ); 141 Model m = ms.createModelOver( "iName" ); 142 assertTrue( m.getGraph() instanceof InfGraph ); 143 } 144 145 public void testDetectRootAmbiguity() 146 { 147 Model desc = createPlainModelDesc().add( createPlainModelDesc() ); 148 try { ModelSpecFactory.createSpec( desc ); fail( "must trap ambiguous description" ); } 149 catch (BadDescriptionException b) { pass(); } 150 } 151 152 public void testCreateByName() 153 { 154 Resource plain = resource(); 155 Model desc = createPlainModelDesc( plain ); 156 ModelSpec ms = ModelSpecFactory.createSpec( ModelSpecFactory.withSchema( desc ), plain ); 157 assertTrue( ms.createModel().getGraph() instanceof GraphMem ); 158 } 159 160 public void testCreateByNameChoice() 161 { 162 Resource plain = resource(); 163 Resource inf = resource(); 164 String URI = DAMLMicroReasonerFactory.URI; 165 Model desc = createPlainModelDesc( plain ).add( createInfModelDesc( inf, URI ) ); 166 ModelSpec ms = ModelSpecFactory.createSpec( ModelSpecFactory.withSchema( desc ), plain ); 167 assertTrue( ms.createModel().getGraph() instanceof GraphMem ); 168 } 169 170 public void testOntModeSpecIsaModelSpec() 171 { 172 assertTrue( OntModelSpec.DAML_MEM_RULE_INF instanceof ModelSpec ); 173 } 174 175 public void testOntModelSpecCreatesOntModels() 176 { 177 Model m = OntModelSpec.DAML_MEM_RULE_INF.createModel(); 178 assertTrue( m instanceof OntModel ); 179 } 180 181 public void testOntModelSpecDescription() 182 { 183 OntModelSpec oms = OntModelSpec.DAML_MEM_RULE_INF; 184 Model d = oms.getDescription(); 185 186 assertTrue( "", d.contains( null, JenaModelSpec.ontLanguage, TestModelFactory.DAMLLangResource ) ); 187 188 StmtIterator si = d.listStatements( null, JenaModelSpec.docManager, (RDFNode) null ); 189 Resource manager = si.nextStatement().getResource(); 190 assertSame( oms.getDocumentManager(), ModelSpecImpl.getValue( manager ) ); 191 } 192 193 public void testOntModelSpecMaker() 194 { 195 OntModelSpec oms = OntModelSpec.DAML_MEM_RULE_INF; 196 Model d = oms.getDescription(); 197 Statement s = d.getRequiredProperty( null, JenaModelSpec.importMaker ); 198 Model makerSpec = oms.getImportModelMaker().getDescription(); 199 assertNotNull( s ); 200 assertIsoModels( makerSpec, subModel( d, s.getObject() ) ); 201 } 202 203 public void testOntModelReasoner() 204 { 205 OntModelSpec oms = OntModelSpec.DAML_MEM_RULE_INF; 206 Model d = oms.getDescription(); 207 Resource reasonerURI = d.createResource( oms.getReasonerFactory().getURI() ); 208 Statement s = d.getRequiredProperty( null, JenaModelSpec.reasonsWith ); 209 Model reasonerSpec = ModelFactory.createDefaultModel() 210 .add( d.createResource(), JenaModelSpec.reasoner, reasonerURI ); 211 assertIsoModels( reasonerSpec, subModel( d, s.getObject() ) ); 212 } 213 214 public Model subModel( Model m, RDFNode root ) 215 { 216 Model result = ModelFactory.createDefaultModel(); 217 if (root instanceof Resource) 218 result.add( m.listStatements( (Resource) root, null, (RDFNode) null ) ); 219 return result; 220 } 221 222 public void testCreateOntSpec() 223 { 224 OntModelSpec oms = OntModelSpec.OWL_MEM_RULE_INF; 225 Model spec = ModelFactory.createDefaultModel(); 226 Resource lang = spec.createResource( oms.getLanguage() ); 227 Resource me = resource(); 228 Resource factory = spec.createResource( oms.getReasonerFactory().getURI() ); 229 spec.add( me, JenaModelSpec.ontLanguage, lang ); 230 Resource r = spec.createResource(); 231 spec.add( r, JenaModelSpec.reasoner, factory ); 232 spec.add( me, JenaModelSpec.reasonsWith, r ); 233 Resource m = spec.createResource(); 234 Model modelMaker = ModelFactory.createDefaultModel(); 235 modelMaker.add( m, RDF.type, JenaModelSpec.MemMakerSpec ); 236 modelMaker.add( m, JenaModelSpec.reificationMode, JenaModelSpec.rsStandard ); 237 spec.add( me, JenaModelSpec.importMaker, m ); 238 spec.add( modelMaker ); 239 OntDocumentManager odm = oms.getDocumentManager(); 240 Resource dm = ModelSpecImpl.createValue( odm ); 241 spec.add( me, JenaModelSpec.docManager, dm ); 242 243 OntModelSpec ms = new OntModelSpec( spec ); 244 assertEquals( lang.getURI(), ms.getLanguage() ); 245 assertEquals( factory.getURI(), ms.getReasonerFactory().getURI() ); 246 assertIsoModels( modelMaker, ms.getImportModelMaker().getDescription() ); 247 assertSame( odm, ms.getDocumentManager() ); 248 } 249 250 public void testCreateOntSpecWithoutMaker() 251 { 252 OntModelSpec oms = OntModelSpec.OWL_MEM_RULE_INF; 253 Model spec = ModelFactory.createDefaultModel(); 254 Resource lang = spec.createResource( oms.getLanguage() ); 255 Resource me = resource(); 256 Resource factory = spec.createResource( oms.getReasonerFactory().getURI() ); 257 spec.add( me, JenaModelSpec.ontLanguage, lang ); 258 Resource r = spec.createResource(); 259 spec.add( r, JenaModelSpec.reasoner, factory ); 260 spec.add( me, JenaModelSpec.reasonsWith, r ); 261 OntDocumentManager odm = oms.getDocumentManager(); 262 Resource dm = ModelSpecImpl.createValue( odm ); 263 spec.add( me, JenaModelSpec.docManager, dm ); 264 265 OntModelSpec ms = new OntModelSpec( spec ); 266 assertEquals( lang.getURI(), ms.getLanguage() ); 267 assertEquals( factory.getURI(), ms.getReasonerFactory().getURI() ); 268 assertSame( odm, ms.getDocumentManager() ); 269 } 270 271 public void testCreateOntSpecWithoutDocmanager() 272 { 273 OntModelSpec oms = OntModelSpec.OWL_MEM_RULE_INF; 274 Model spec = ModelFactory.createDefaultModel(); 275 Resource lang = spec.createResource( oms.getLanguage() ); 276 Resource me = resource(); 277 Resource factory = spec.createResource( oms.getReasonerFactory().getURI() ); 278 spec.add( me, JenaModelSpec.ontLanguage, lang ); 279 Resource r = spec.createResource(); 280 spec.add( r, JenaModelSpec.reasoner, factory ); 281 spec.add( me, JenaModelSpec.reasonsWith, r ); 282 Resource m = spec.createResource(); 283 Model modelMaker = ModelFactory.createDefaultModel(); 284 modelMaker.add( m, RDF.type, JenaModelSpec.MemMakerSpec ); 285 modelMaker.add( m, JenaModelSpec.reificationMode, JenaModelSpec.rsStandard ); 286 spec.add( me, JenaModelSpec.importMaker, m ); 287 spec.add( modelMaker ); 288 289 OntModelSpec ms = new OntModelSpec( spec ); 290 assertEquals( lang.getURI(), ms.getLanguage() ); 291 assertEquals( factory.getURI(), ms.getReasonerFactory().getURI() ); 292 assertIsoModels( modelMaker, ms.getImportModelMaker().getDescription() ); 293 } 294 295 public void testCreateOntSpecWithoutReasoner() 296 { 297 OntModelSpec oms = OntModelSpec.OWL_MEM_RULE_INF; 298 Model spec = ModelFactory.createDefaultModel(); 299 Resource lang = spec.createResource( oms.getLanguage() ); 300 Resource me = resource(); 301 spec.add( me, JenaModelSpec.ontLanguage, lang ); 302 Resource m = spec.createResource(); 303 Model modelMaker = ModelFactory.createDefaultModel(); 304 modelMaker.add( m, RDF.type, JenaModelSpec.MemMakerSpec ); 305 modelMaker.add( m, JenaModelSpec.reificationMode, JenaModelSpec.rsStandard ); 306 spec.add( me, JenaModelSpec.importMaker, m ); 307 spec.add( modelMaker ); 308 OntDocumentManager odm = oms.getDocumentManager(); 309 Resource dm = ModelSpecImpl.createValue( odm ); 310 spec.add( me, JenaModelSpec.docManager, dm ); 311 312 OntModelSpec ms = new OntModelSpec( spec ); 313 assertEquals( lang.getURI(), ms.getLanguage() ); 314 assertIsoModels( modelMaker, ms.getImportModelMaker().getDescription() ); 315 assertSame( odm, ms.getDocumentManager() ); 316 } 317 318 public void testOntModelSpecWithModelName() 319 { 320 final List record = new ArrayList(); 321 ModelMaker tracker = new ModelMakerImpl( new SimpleGraphMaker( ) ) 322 { 323 public Model createModel( String name, boolean strict ) 324 { 325 record.add( name ); 326 return super.createModel( name, strict ); 327 } 328 }; 329 Model x = modelWithStatements 330 ( 331 "_this jms:ontLanguage http://www.w3.org/TR/owl-features/#term_OWLLite" 332 + "; _this jms:modelName 'cranberry'" 333 + "; _this jms:docManager _DM" 334 + "; _this jms:reasonsWith _R" 335 + "; _R jms:reasoner http://jena.hpl.hp.com/2003/RDFSExptRuleReasoner" 336 ); 337 OntModelSpec s = (OntModelSpec) ModelSpecFactory.createSpec( x ); 338 s.setBaseModelMaker( tracker ); 339 Model m = s.createModel(); 340 assertEquals( list( "cranberry" ), record ); 341 } 342 343 protected List list( String element ) 344 { 345 List result = new ArrayList(); 346 result.add( element ); 347 return result; 348 } 349 350 public void testCreateFailingMaker() 351 { 352 try 353 { ModelSpecImpl.createMaker( modelWithStatements( "" ) ); 354 fail( "should generate BadDescriptionException" ); } 355 catch (BadDescriptionException e) 356 { pass(); } 357 } 358 359 public void testCreateMemModelMaker() 360 { 361 Resource mem = JenaModelSpec.MemMakerSpec; 362 testCreateModelMaker( JenaModelSpec.rsStandard, mem, SimpleGraphMaker.class ); 363 testCreateModelMaker( JenaModelSpec.rsMinimal, mem, SimpleGraphMaker.class ); 364 testCreateModelMaker( JenaModelSpec.rsConvenient, mem, SimpleGraphMaker.class ); 365 } 366 367 public void testCreateFileModelMaker() 368 { 369 Resource file =JenaModelSpec.FileMakerSpec; 370 testCreateModelMaker( JenaModelSpec.rsStandard, file, FileGraphMaker.class ); 371 testCreateModelMaker( JenaModelSpec.rsMinimal, file, FileGraphMaker.class ); 372 testCreateModelMaker( JenaModelSpec.rsConvenient, file, FileGraphMaker.class ); 373 } 374 375 public void testCreateFileModelMakerRooted() 376 { 377 String fileBase = "/somewhere"; 378 Resource me = resource(); 379 Model spec = ModelFactory.createDefaultModel() 380 .add( me, RDF.type, JenaModelSpec.FileMakerSpec ) 381 .add( me, JenaModelSpec.fileBase, fileBase ) 382 ; 383 ModelMaker maker = ModelSpecImpl.createMaker( spec ); 384 FileGraphMaker fgm = (FileGraphMaker) maker.getGraphMaker(); 385 assertEquals( fileBase, fgm.getFileBase() ); 386 387 Model desc = ModelFactory.createModelForGraph( fgm.getDescription() ); 388 assertTrue( desc.listStatements( null, JenaModelSpec.fileBase, fileBase ).hasNext() ); 389 } 390 391 public void testCreateModelMaker( Resource style, Resource cl, Class required ) 392 { 393 Resource me = resource(); 394 ReificationStyle wanted = JenaModelSpec.findStyle( style ); 395 Model spec = modelWithStatements( "" ) 396 .add( me, RDF.type, cl ) 397 .add( me, JenaModelSpec.reificationMode, style ); 398 ModelMaker maker = ModelSpecImpl.createMaker( spec ); 399 assertTrue( required.isInstance( maker.getGraphMaker() ) ); 400 assertEquals( wanted, maker.getGraphMaker().getReificationStyle() ); 401 } 402 403 public void testCreatePlainMemModel() 404 { 405 Resource me = resource(); 406 Model spec = createPlainModelDesc( me ); 407 PlainModelSpec pms = new PlainModelSpec( me, spec ); 408 ModelMaker mm = pms.getModelMaker(); 409 Model desc = mm.getDescription( me ); 410 assertTrue( desc.contains( me, RDF.type, JenaModelSpec.MemMakerSpec ) ); 411 assertTrue( desc.listStatements( null, JenaModelSpec.reificationMode, JenaModelSpec.rsMinimal ).hasNext() ); 412 assertTrue( mm.getGraphMaker() instanceof SimpleGraphMaker ); 413 assertEquals( ReificationStyle.Minimal , mm.getGraphMaker().getReificationStyle() ); 414 } 415 416 public void testCreatePlainFileModel() 417 { 418 Resource me = resource(); 419 Resource maker = resource(); 420 Model spec = createPlainModelDesc( me, maker, JenaModelSpec.FileMakerSpec ); 421 PlainModelSpec pms = new PlainModelSpec( me, spec ); 422 ModelMaker mm = pms.getModelMaker(); 423 Model desc = mm.getDescription( me ); 424 assertTrue( desc.listStatements( null, RDF.type, JenaModelSpec.FileMakerSpec ).hasNext() ); 425 assertTrue( desc.listStatements( null, JenaModelSpec.reificationMode, JenaModelSpec.rsMinimal ).hasNext() ); 426 assertTrue( mm.getGraphMaker() instanceof FileGraphMaker ); 427 assertEquals( ReificationStyle.Minimal , mm.getGraphMaker().getReificationStyle() ); 428 } 429 430 434 public static Model createPlainModelDesc() 435 { return createPlainModelDesc( resource() ); } 436 437 441 public static Model createPlainModelDesc( Resource root ) 442 { return createPlainModelDesc( root, resource() ); } 443 444 public static Model createPlainModelDesc( Resource root, Resource maker ) 445 { return createPlainModelDesc( root, maker, JenaModelSpec.MemMakerSpec ); } 446 447 public static Model createPlainModelDesc( Resource root, Resource maker, Resource spec ) 448 { 449 return ModelFactory.createDefaultModel() 450 .add( root, JenaModelSpec.maker, maker ) 451 .add( maker, RDF.type, spec ) 452 .add( maker, JenaModelSpec.reificationMode, JenaModelSpec.rsMinimal ); 453 } 454 455 public static Model createInfModelDesc( String URI ) 456 { return createInfModelDesc( resource(), URI ); } 457 458 public static Model createInfModelDesc( Resource root, String URI ) 459 { 460 Resource maker = resource(); 461 Resource reasoner = resource(); 462 Resource res = resource( URI ); 463 return ModelFactory.createDefaultModel() 464 .add( root, JenaModelSpec.reasonsWith, reasoner ) 465 .add( reasoner, JenaModelSpec.reasoner, res ) 466 .add( root, JenaModelSpec.maker, maker ) 467 .add( maker, RDF.type, JenaModelSpec.MemMakerSpec ) 468 .add( maker, JenaModelSpec.reificationMode, JenaModelSpec.rsMinimal ) 469 ; 470 } 471 } 472 473 | Popular Tags |