1 19 20 package com.hp.hpl.jena.ontology; 23 24 25 26 29 import com.hp.hpl.jena.rdf.model.*; 30 import com.hp.hpl.jena.rdf.model.impl.*; 31 import com.hp.hpl.jena.reasoner.*; 32 import com.hp.hpl.jena.reasoner.rulesys.*; 33 import com.hp.hpl.jena.ontology.impl.*; 34 import com.hp.hpl.jena.vocabulary.*; 35 import com.hp.hpl.jena.reasoner.transitiveReasoner.TransitiveReasonerFactory; 36 37 38 48 public class OntModelSpec extends ModelSpecImpl implements ModelSpec { 49 54 55 public static final OntModelSpec OWL_MEM = new OntModelSpec( ModelFactory.createMemModelMaker(), null, null, ProfileRegistry.OWL_LANG ); 56 57 58 public static final OntModelSpec OWL_MEM_RDFS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, RDFSRuleReasonerFactory.theInstance(), ProfileRegistry.OWL_LANG ); 59 60 61 public static final OntModelSpec OWL_MEM_TRANS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, TransitiveReasonerFactory.theInstance(), ProfileRegistry.OWL_LANG ); 62 63 64 public static final OntModelSpec OWL_MEM_RULE_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, OWLFBRuleReasonerFactory.theInstance(), ProfileRegistry.OWL_LANG ); 65 66 67 public static final OntModelSpec OWL_MEM_MICRO_RULE_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, OWLMicroReasonerFactory.theInstance(), ProfileRegistry.OWL_LANG ); 68 69 70 public static final OntModelSpec OWL_MEM_MINI_RULE_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, OWLMiniReasonerFactory.theInstance(), ProfileRegistry.OWL_LANG ); 71 72 73 public static final OntModelSpec OWL_DL_MEM = new OntModelSpec( ModelFactory.createMemModelMaker(), null, null, ProfileRegistry.OWL_DL_LANG ); 74 75 76 public static final OntModelSpec OWL_DL_MEM_RDFS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, RDFSRuleReasonerFactory.theInstance(), ProfileRegistry.OWL_DL_LANG ); 77 78 79 public static final OntModelSpec OWL_DL_MEM_TRANS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, TransitiveReasonerFactory.theInstance(), ProfileRegistry.OWL_DL_LANG ); 80 81 82 public static final OntModelSpec OWL_DL_MEM_RULE_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, OWLFBRuleReasonerFactory.theInstance(), ProfileRegistry.OWL_DL_LANG ); 83 84 85 public static final OntModelSpec OWL_LITE_MEM = new OntModelSpec( ModelFactory.createMemModelMaker(), null, null, ProfileRegistry.OWL_LITE_LANG ); 86 87 88 public static final OntModelSpec OWL_LITE_MEM_TRANS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, TransitiveReasonerFactory.theInstance(), ProfileRegistry.OWL_LITE_LANG ); 89 90 91 public static final OntModelSpec OWL_LITE_MEM_RDFS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, RDFSRuleReasonerFactory.theInstance(), ProfileRegistry.OWL_LITE_LANG ); 92 93 94 public static final OntModelSpec OWL_LITE_MEM_RULES_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, OWLFBRuleReasonerFactory.theInstance(), ProfileRegistry.OWL_LITE_LANG ); 95 96 97 public static final OntModelSpec DAML_MEM = new OntModelSpec( ModelFactory.createMemModelMaker(), null, null, ProfileRegistry.DAML_LANG ); 98 99 100 public static final OntModelSpec DAML_MEM_TRANS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, TransitiveReasonerFactory.theInstance(), ProfileRegistry.DAML_LANG ); 101 102 103 public static final OntModelSpec DAML_MEM_RDFS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, RDFSRuleReasonerFactory.theInstance(), ProfileRegistry.DAML_LANG ); 104 105 106 public static final OntModelSpec DAML_MEM_RULE_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, DAMLMicroReasonerFactory.theInstance(), ProfileRegistry.DAML_LANG ); 107 108 109 public static final OntModelSpec RDFS_MEM = new OntModelSpec( ModelFactory.createMemModelMaker(), null, null, ProfileRegistry.RDFS_LANG ); 110 111 112 public static final OntModelSpec RDFS_MEM_TRANS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, TransitiveReasonerFactory.theInstance(), ProfileRegistry.RDFS_LANG ); 113 114 115 public static final OntModelSpec RDFS_MEM_RDFS_INF = new OntModelSpec( ModelFactory.createMemModelMaker(), null, RDFSRuleReasonerFactory.theInstance(), ProfileRegistry.RDFS_LANG ); 116 117 118 121 122 protected OntDocumentManager m_docManager = null; 123 124 125 protected Reasoner m_reasoner = null; 126 127 128 protected String m_languageURI; 129 130 131 protected Profile m_profile = null; 132 133 134 protected ReasonerFactory m_rFactory = null; 135 136 137 protected ModelMaker importsMaker; 138 139 140 protected String baseModelName; 141 142 145 153 public OntModelSpec( ModelMaker importsMaker, OntDocumentManager docMgr, ReasonerFactory rFactory, String languageURI ) { 154 this( ModelFactory.createMemModelMaker(), importsMaker, docMgr, rFactory, languageURI ); 155 } 156 157 165 public OntModelSpec( ModelMaker baseMaker, ModelMaker importsMaker, OntDocumentManager docMgr, 166 ReasonerFactory rFactory, String languageURI ) 167 { this( null, baseMaker, importsMaker, docMgr, rFactory, languageURI ); } 168 169 170 179 public OntModelSpec( String baseModelName, ModelMaker baseMaker, 180 ModelMaker importsMaker, OntDocumentManager docMgr, 181 ReasonerFactory rFactory, String languageURI ) { 182 super( baseMaker ); 183 this.baseModelName = baseModelName; 184 this.importsMaker = importsMaker == null ? ModelFactory.createMemModelMaker(): importsMaker; 185 setDocumentManager( docMgr ); 186 setReasonerFactory( rFactory ); 187 188 if (languageURI == null) { 189 throw new IllegalArgumentException ( "Cannot create OntModelSpec with a null ontology language" ); 190 } 191 setLanguage( languageURI ); 192 } 193 194 195 209 public OntModelSpec( OntModelSpec spec ) { 210 this( spec.getBaseModelMaker(), spec.getImportModelMaker(), spec.getDocumentManager(), 211 spec.getReasonerFactory(), spec.getLanguage() ); 212 } 213 214 218 public ModelMaker getModelMaker() { 219 if (false) throw new RuntimeException ( "get rid of calls to this method" ); 220 return getImportModelMaker(); 221 } 222 223 226 public ModelMaker getImportModelMaker() { 227 return importsMaker; 228 } 229 230 233 public ModelMaker getBaseModelMaker() { 234 return super.getModelMaker(); 235 } 236 237 244 public OntModelSpec( Model description ) { 245 this( findRootByType( description, JenaModelSpec.OntModelSpec ), description ); 246 } 247 248 257 public OntModelSpec( Resource root, Model description ) { 258 this( getBaseModelName( description, root ), 259 getBaseMaker( description, root ), 260 getImportMaker( description, root ), 261 getDocumentManager( description, root ), 262 getReasonerFactory( description, root ), 263 getLanguage( description, root ) ); 264 265 } 266 267 282 public static OntModelSpec getDefaultSpec( String languageURI ) { 283 if (languageURI.equals( ProfileRegistry.OWL_LANG )) { 284 return OWL_MEM_RDFS_INF; 285 } 286 else if (languageURI.equals( ProfileRegistry.OWL_DL_LANG )) { 287 return OWL_DL_MEM_RDFS_INF; 288 } 289 else if (languageURI.equals( ProfileRegistry.OWL_LITE_LANG )) { 290 return OWL_LITE_MEM_RDFS_INF; 291 } 292 else if (languageURI.equals( ProfileRegistry.DAML_LANG )) { 293 return DAML_MEM_RULE_INF; 294 } 295 else if (languageURI.equals( ProfileRegistry.RDFS_LANG )) { 296 return RDFS_MEM_RDFS_INF; 297 } 298 else { 299 throw new OntologyException( "Did not recognise this language URI, so cannot determine default model spec: " + languageURI ); 300 } 301 } 302 303 304 309 public OntDocumentManager getDocumentManager() { 310 if (m_docManager == null) { 311 m_docManager = OntDocumentManager.getInstance(); 313 } 314 315 return m_docManager; 316 } 317 318 319 323 public void setDocumentManager( OntDocumentManager docMgr ) { 324 m_docManager = docMgr; 325 } 326 327 332 public void setImportModelMaker( ModelMaker maker ) { 333 this.maker = maker; 334 } 335 336 343 public void setModelMaker( ModelMaker m ) { 344 setImportModelMaker( m ); 345 } 346 347 350 public void setBaseModelMaker( ModelMaker m ) { 351 this.maker = m; 352 } 353 354 359 public Reasoner getReasoner() { 360 if (m_reasoner == null && m_rFactory != null) { 361 m_reasoner = m_rFactory.create( null ); 363 } 364 365 return m_reasoner; 366 } 367 368 369 378 public void setReasoner( Reasoner reasoner ) { 379 m_reasoner = reasoner; 380 } 381 382 383 389 public void setReasonerFactory( ReasonerFactory rFactory ) { 390 m_rFactory = rFactory; 391 } 392 393 397 public ReasonerFactory getReasonerFactory() { 398 return m_rFactory; 399 } 400 401 402 408 public String getLanguage() { 409 return m_languageURI; 410 } 411 412 413 419 public void setLanguage( String languageURI ) { 420 m_languageURI = languageURI; 421 m_profile = ProfileRegistry.getInstance().getProfile( m_languageURI ); 422 423 if (m_profile == null) { 424 throw new OntologyException( "Could not determine an ontology language profile for URI " + m_languageURI ); 425 } 426 } 427 428 429 433 public Profile getProfile() { 434 return m_profile; 435 } 436 437 442 public Model createModel() { 443 Model m = baseModelName == null ? maker.createModel() : maker.createModel( baseModelName ); 444 return new OntModelImpl( this, m ); 445 } 446 447 453 public Model createModelOver( String name ) { 454 return new OntModelImpl( this, maker.createModel( name, false ) ); 455 } 456 457 467 public static ModelMaker getImportMaker( Model description, Resource root ) { 468 return getMaker( description, root, JenaModelSpec.importMaker ); 469 } 470 471 481 public static ModelMaker getBaseMaker( Model description, Resource root ) { 482 return getMaker( description, root, JenaModelSpec.maker ); 483 } 484 485 489 protected static String getBaseModelName( Model description, Resource root ) { 490 Statement s = description.getProperty( root, JenaModelSpec.modelName ); 491 return s == null ? null : s.getString(); 492 } 493 494 499 protected static ModelMaker getMaker( Model description, Resource root, Property makerProperty ) { 500 Statement mStatement = description.getProperty( root, makerProperty ); 501 return mStatement == null 502 ? ModelFactory.createMemModelMaker() 503 : createMaker( mStatement.getResource(), description ); 504 } 505 506 513 public static String getLanguage( Model description, Resource root ) { 514 Statement langStatement = description.getRequiredProperty( root, JenaModelSpec.ontLanguage ); 515 return langStatement.getResource().getURI(); 516 } 517 518 528 public static OntDocumentManager getDocumentManager( Model description, 529 Resource root ) { 530 Statement docStatement = description.getProperty( root, JenaModelSpec.docManager ); 531 if (docStatement == null) return null; 532 Resource manager = docStatement.getResource(); 533 Statement policy = description.getProperty( manager, JenaModelSpec.policyPath ); 534 if (policy == null) 535 return (OntDocumentManager) getValue( manager ); 536 else 537 return new OntDocumentManager( policy.getString() ); 538 } 539 540 548 public static ReasonerFactory getReasonerFactory( Model description, Resource root ) { 549 Statement factStatement = description.getProperty( root, JenaModelSpec.reasonsWith ); 550 if (factStatement == null) return null; 551 return InfModelSpec.getReasonerFactory( factStatement.getResource(), description ); 552 } 553 554 561 public Model addDescription( Model d, Resource self ) { 562 super.addDescription( d, self ); 563 addImportsDescription( d, self, importsMaker ); 564 addLanguageDescription( d, self, m_languageURI ); 565 addManagerDescription( d, self, getDocumentManager() ); 566 addReasonerDescription( d, self, getReasonerFactory() ); 567 return d; 568 } 569 570 576 public Property getMakerProperty() { 577 return JenaModelSpec.importMaker; 578 } 579 580 586 protected void addLanguageDescription( Model d, Resource me, String langURI ) { 587 d.add( me, JenaModelSpec.ontLanguage, d.createResource( langURI ) ); 588 } 589 590 protected void addImportsDescription( Model d, Resource me, ModelMaker m ) { 591 Resource importSelf = d.createResource(); 592 d.add( me, JenaModelSpec.importMaker, importSelf ); 593 m.addDescription( d, importSelf ); 594 } 595 596 602 protected void addManagerDescription( Model d, Resource me, 603 OntDocumentManager man ) { 604 d.add( me, JenaModelSpec.docManager, createValue( man ) ); 605 } 606 607 613 protected void addReasonerDescription( Model d, Resource me, ReasonerFactory rf ) { 614 Resource reasonerSelf = d.createResource(); 615 d.add( me, JenaModelSpec.reasonsWith, reasonerSelf ); 616 if (rf != null) { 617 d.add( reasonerSelf, JenaModelSpec.reasoner, d.createResource( rf.getURI() ) ); 618 } 619 } 620 621 625 public Model createBaseModel() { 626 return ModelFactory.createDefaultModel(); 627 } 628 629 } 630 631 632 661 662 | Popular Tags |