1 18 19 package com.hp.hpl.jena.ontology.daml.impl; 22 23 24 import java.net.*; 27 import java.io.*; 28 import java.util.*; 29 30 import com.hp.hpl.jena.rdf.model.*; 31 import com.hp.hpl.jena.shared.JenaException; 32 import com.hp.hpl.jena.util.iterator.*; 33 import com.hp.hpl.jena.datatypes.*; 34 import com.hp.hpl.jena.datatypes.TypeMapper; 35 import com.hp.hpl.jena.graph.Node; 36 import com.hp.hpl.jena.ontology.daml.*; 37 import com.hp.hpl.jena.ontology.*; 38 import com.hp.hpl.jena.ontology.impl.*; 39 import com.hp.hpl.jena.vocabulary.*; 40 41 42 43 55 public class DAMLModelImpl 56 extends OntModelImpl 57 implements DAMLModel 58 { 59 62 63 66 67 protected static Object [][] DAML_CLASS_TABLE = new Object [][] { 68 { DAML_OIL.Class, DAMLClassImpl.class }, 70 { RDFS.Class, DAMLClassImpl.class }, 71 72 { DAML_OIL.Restriction, DAMLRestrictionImpl.class }, 73 74 { DAML_OIL.List, DAMLListImpl.class }, 75 76 { DAML_OIL.Ontology, DAMLOntologyImpl.class }, 77 78 { DAML_OIL.Property, DAMLPropertyImpl.class }, 79 { RDF.Property, DAMLPropertyImpl.class }, 80 81 { DAML_OIL.DatatypeProperty, DAMLDatatypePropertyImpl.class }, 82 { DAML_OIL.ObjectProperty, DAMLObjectPropertyImpl.class }, 83 84 { DAML_OIL.UniqueProperty, DAMLPropertyImpl.class }, 85 { DAML_OIL.TransitiveProperty, DAMLObjectPropertyImpl.class }, 86 { DAML_OIL.UnambiguousProperty, DAMLObjectPropertyImpl.class } 87 }; 88 89 90 93 94 private DAMLLoader m_loader = new DAMLLoader( this ); 95 96 97 98 101 104 public DAMLModelImpl( OntModelSpec spec, Model m ) { 105 super( spec, m ); 106 107 initStore(); 109 } 110 111 112 113 116 135 public DAMLOntology createDAMLOntology( String uri ) { 136 return (DAMLOntology) createOntResource( DAMLOntology.class, getProfile().ONTOLOGY(), uri ); 137 } 138 139 140 147 public DAMLInstance createDAMLInstance( DAMLClass damlClass, String uri ) { 148 return (DAMLInstance) createOntResource( DAMLInstance.class, damlClass, uri ); 149 } 150 151 152 158 public DAMLDataInstance createDAMLDataInstance( Resource datatype, Object value ) { 159 return createDAMLDataInstance( TypeMapper.getInstance().getTypeByName( datatype.getURI() ), value ); 160 } 161 162 163 169 public DAMLDataInstance createDAMLDataInstance( RDFDatatype datatype, Object value ) { 170 Resource bNode = createResource( getResource( datatype.getURI() ) ); 171 bNode.addProperty( RDF.value, createTypedLiteral( value, datatype ) ); 172 return (DAMLDataInstance) bNode.as( DAMLDataInstance.class ); 173 } 174 175 176 181 public DAMLDataInstance createDAMLDataInstance( Object value ) { 182 RDFDatatype datatype = TypeMapper.getInstance().getTypeByValue( value ); 183 if (datatype == null) { 184 throw new JenaException( "Could not determine an appropriate datatype for value " + value ); 185 } 186 else { 187 return createDAMLDataInstance( datatype, value ); 188 } 189 } 190 191 192 199 public DAMLClass createDAMLClass( String uri ) { 200 return (DAMLClass) createOntResource( DAMLClass.class, getProfile().CLASS(), uri ); 201 } 202 203 204 213 public DAMLProperty createDAMLProperty( String uri ) { 214 return (DAMLProperty) createOntResource( DAMLProperty.class, getProfile().PROPERTY(), uri ); 215 } 216 217 218 226 public DAMLObjectProperty createDAMLObjectProperty( String uri ) { 227 return (DAMLObjectProperty) createOntResource( DAMLObjectProperty.class, getProfile().OBJECT_PROPERTY(), uri ); 228 } 229 230 231 240 public DAMLDatatypeProperty createDAMLDatatypeProperty( String uri ) { 241 return (DAMLDatatypeProperty) createOntResource( DAMLDatatypeProperty.class, getProfile().DATATYPE_PROPERTY(), uri ); 242 } 243 244 249 public DAMLList createDAMLList() { 250 return (DAMLList) getResource( DAML_OIL.nil.getURI() ).as( DAMLList.class ); 251 } 252 253 254 260 public DAMLList createDAMLList( Iterator elements ) { 261 DAMLList l = createDAMLList(); 262 if (elements.hasNext()) { 263 RDFNode n = (RDFNode) elements.next(); 265 l = (DAMLList) l.cons( n ); 266 267 while (elements.hasNext()) { 269 l.add( (RDFNode) elements.next() ); 270 } 271 } 272 273 return l; 274 } 275 276 277 283 public DAMLList createDAMLList( RDFNode[] elements ) { 284 return createDAMLList( Arrays.asList( elements ).iterator() ); 285 } 286 287 288 295 public DAMLRestriction createDAMLRestriction( String uri ) { 296 return (DAMLRestriction) createOntResource( DAMLRestriction.class, getProfile().RESTRICTION(), uri ); 297 } 298 299 300 307 public DAMLDatatype createDAMLDatatype( String uri ) { 308 Resource dt = getResource( uri ); 309 dt.addProperty( RDF.type, DAML_OIL.Datatype ); 310 return (DAMLDatatype) dt.as( DAMLDatatype.class ); 311 } 312 313 314 326 public DAMLCommon createDAMLValue( String uri, Resource damlClass ) { 327 Class javaClass = DAMLInstance.class; 328 329 for (int i = 0; i < DAML_CLASS_TABLE.length; i++) { 331 if (DAML_CLASS_TABLE[i][0].equals( damlClass )) { 332 javaClass = (Class ) DAML_CLASS_TABLE[i][1]; 333 break; 334 } 335 } 336 337 return (DAMLCommon) createOntResource( javaClass, damlClass, uri ); 338 } 339 340 341 350 public DAMLCommon getDAMLValue( String uri ) { 351 Node n = Node.createURI( uri ); 352 if (getGraph().queryHandler().containsNode( n )) { 353 return (DAMLCommon) ((Resource) asRDFNode( n )).as( DAMLCommon.class ); 354 } 355 else { 356 return null; 357 } 358 } 359 360 361 373 public DAMLCommon getDAMLValue( String uri, DAMLClass damlClass ) { 374 DAMLCommon res = getDAMLValue( uri ); 375 376 return (res == null && damlClass != null) ? createDAMLValue( uri, damlClass ) : res; 377 } 378 379 380 385 public ExtendedIterator listDAMLClasses() { 386 return UniqueExtendedIterator.create( findByTypeAs( getProfile().CLASS(), null, DAMLClass.class ) ); 387 } 388 389 390 396 public ExtendedIterator listDAMLProperties() { 397 return UniqueExtendedIterator.create( findByTypeAs( getProfile().PROPERTY(), null, DAMLProperty.class ) ); 398 } 399 400 401 406 public ExtendedIterator listDAMLInstances() { 407 return UniqueExtendedIterator.create( listIndividuals().mapWith( new Map1() { 408 public Object map1(Object x){ return ((Resource) x).as( DAMLInstance.class );} } ) ); 409 } 410 411 412 417 public DAMLClass getDAMLClass( String uri ) { 418 Node n = Node.createURI( uri ); 419 if (getGraph().queryHandler().containsNode( n )) { 420 return (DAMLClass) ((Resource) asRDFNode( n )).as( DAMLClass.class ); 421 } 422 else { 423 return null; 424 } 425 } 426 427 432 public DAMLProperty getDAMLProperty( String uri ) { 433 Node n = Node.createURI( uri ); 434 if (getGraph().queryHandler().containsNode( n )) { 435 return (DAMLProperty) ((Resource) asRDFNode( n )).as( DAMLProperty.class ); 436 } 437 else { 438 return null; 439 } 440 } 441 442 447 public DAMLInstance getDAMLInstance( String uri ) { 448 Node n = Node.createURI( uri ); 449 if (getGraph().queryHandler().containsNode( n )) { 450 return (DAMLInstance) ((Resource) asRDFNode( n )).as( DAMLInstance.class ); 451 } 452 else { 453 return null; 454 } 455 } 456 457 458 469 public Model read( String uri, String base, String lang ) { 470 try { 471 URL url = new URL( uri ); 472 return read( url.openStream(), base, lang ); 473 } 474 catch (IOException e) { 475 throw new OntologyException( "I/O error while reading from uri " + uri ); 476 } 477 } 478 479 480 485 public DAMLLoader getLoader() { 486 return m_loader; 487 } 488 489 490 496 public boolean getLoadSuccessful() { 497 return getLoader().getStatus() == DAMLLoader.STATUS_OK; 498 } 499 500 501 511 public TypeMapper getDatatypeRegistry() { 512 return TypeMapper.getInstance(); 513 } 514 515 516 527 public void setUseEquivalence( boolean useEquivalence ) { 528 } 529 530 531 541 public boolean getUseEquivalence() { 542 return true; 543 } 544 545 546 547 548 551 554 protected void initStore() { 555 } 556 557 558 564 protected boolean containsResource( String uri ) { 565 return containsResource( getResource( uri ) ); 566 } 567 568 569 570 574 575 } 576 577 578 607 | Popular Tags |