1 19 20 package com.hp.hpl.jena.ontology.impl; 23 24 25 import java.util.*; 28 29 import com.hp.hpl.jena.enhanced.*; 30 import com.hp.hpl.jena.graph.*; 31 import com.hp.hpl.jena.ontology.*; 32 import com.hp.hpl.jena.rdf.model.*; 33 import com.hp.hpl.jena.util.iterator.*; 34 import com.hp.hpl.jena.util.iterator.ExtendedIterator; 35 import com.hp.hpl.jena.util.iterator.Filter; 36 37 38 39 48 public class OntPropertyImpl 49 extends OntResourceImpl 50 implements OntProperty 51 { 52 55 58 63 public static Implementation factory = new Implementation() { 64 public EnhNode wrap( Node n, EnhGraph eg ) { 65 if (canWrap( n, eg )) { 66 return new OntPropertyImpl( n, eg ); 67 } 68 else { 69 throw new ConversionException( "Cannot convert node " + n + " to OntProperty"); 70 } 71 } 72 73 public boolean canWrap( Node node, EnhGraph eg ) { 74 Profile profile = (eg instanceof OntModel) ? ((OntModel) eg).getProfile() : null; 76 return (profile != null) && profile.isSupported( node, eg, OntProperty.class ); 77 } 78 }; 79 80 81 84 87 95 public OntPropertyImpl( Node n, EnhGraph g ) { 96 super( n, g ); 97 } 98 99 100 103 110 public boolean isProperty() { 111 return true; 112 } 113 114 115 118 public int getOrdinal() { 119 return ((Property) as( Property.class )).getOrdinal(); 120 } 121 122 123 125 131 public void setSuperProperty( Property prop ) { 132 setPropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop ); 133 } 134 135 140 public void addSuperProperty( Property prop ) { 141 addPropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop ); 142 } 143 144 150 public OntProperty getSuperProperty() { 151 return objectAsProperty( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF" ); 152 } 153 154 160 public ExtendedIterator listSuperProperties() { 161 return listSuperProperties( false ); 162 } 163 164 173 public ExtendedIterator listSuperProperties( boolean direct ) { 174 return listDirectPropertyValues( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", OntProperty.class, getProfile().SUB_PROPERTY_OF(), direct, false ); 175 } 176 177 184 public boolean hasSuperProperty( Property prop, boolean direct ) { 185 return hasPropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop ); 186 } 187 188 194 public void removeSuperProperty( Property prop ) { 195 removePropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop ); 196 } 197 198 199 205 public void setSubProperty( Property prop ) { 206 checkProfile( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF" ); 208 for (StmtIterator i = getModel().listStatements( null, getProfile().SUB_PROPERTY_OF(), this ); i.hasNext(); ) { 209 i.removeNext(); 210 } 211 212 ((OntProperty) prop.as( OntProperty.class )).addSuperProperty( this ); 213 } 214 215 220 public void addSubProperty( Property prop ) { 221 ((OntProperty) prop.as( OntProperty.class )).addSuperProperty( this ); 222 } 223 224 230 public OntProperty getSubProperty() { 231 checkProfile( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF" ); 232 return (OntProperty) getModel().listStatements( null, getProfile().SUB_PROPERTY_OF(), this ) 233 .nextStatement() 234 .getSubject() 235 .as( OntProperty.class ); 236 } 237 238 244 public ExtendedIterator listSubProperties() { 245 return listSubProperties( false ); 246 } 247 248 257 public ExtendedIterator listSubProperties( boolean direct ) { 258 return listDirectPropertyValues( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", OntProperty.class, getProfile().SUB_PROPERTY_OF(), direct, true ); 259 } 260 261 268 public boolean hasSubProperty( Property prop, boolean direct ) { 269 return ((OntProperty) prop.as( OntProperty.class )).hasSuperProperty( this, direct ); 270 } 271 272 278 public void removeSubProperty( Property prop ) { 279 ((OntProperty) prop.as( OntProperty.class )).removeSuperProperty( this ); 280 } 281 282 284 290 public void setDomain( Resource res ) { 291 setPropertyValue( getProfile().DOMAIN(), "DOMAIN", res ); 292 } 293 294 299 public void addDomain( Resource res ) { 300 addPropertyValue( getProfile().DOMAIN(), "DOMAIN", res ); 301 } 302 303 309 public OntResource getDomain() { 310 return objectAsResource( getProfile().DOMAIN(), "DOMAIN" ); 311 } 312 313 319 public ExtendedIterator listDomain() { 320 return listAs( getProfile().DOMAIN(), "DOMAIN", OntClass.class ); 321 } 322 323 328 public boolean hasDomain( Resource res ) { 329 return hasPropertyValue( getProfile().DOMAIN(), "DOMAIN", res ); 330 } 331 332 338 public void removeDomain( Resource cls ) { 339 removePropertyValue( getProfile().DOMAIN(), "DOMAIN", cls ); 340 } 341 342 343 345 351 public void setRange( Resource res ) { 352 setPropertyValue( getProfile().RANGE(), "RANGE", res ); 353 } 354 355 360 public void addRange( Resource res ) { 361 addPropertyValue( getProfile().RANGE(), "RANGE", res ); 362 } 363 364 370 public OntResource getRange() { 371 return objectAsResource( getProfile().RANGE(), "RANGE" ); 372 } 373 374 380 public ExtendedIterator listRange() { 381 return listAs( getProfile().RANGE(), "RANGE", OntClass.class ); 382 } 383 384 389 public boolean hasRange( Resource res ) { 390 return hasPropertyValue( getProfile().RANGE(), "RANGE", res ); 391 } 392 393 399 public void removeRange( Resource cls ) { 400 removePropertyValue( getProfile().RANGE(), "RANGE", cls ); 401 } 402 403 404 406 408 414 public void setEquivalentProperty( Property prop ) { 415 setPropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop ); 416 } 417 418 423 public void addEquivalentProperty( Property prop ) { 424 addPropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop ); 425 } 426 427 433 public OntProperty getEquivalentProperty() { 434 return objectAsProperty( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY" ); 435 } 436 437 443 public ExtendedIterator listEquivalentProperties() { 444 return listAs( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", OntProperty.class ); 445 } 446 447 452 public boolean hasEquivalentProperty( Property prop ) { 453 return hasPropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop ); 454 } 455 456 463 public void removeEquivalentProperty( Property prop ) { 464 removePropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop ); 465 } 466 467 469 475 public void setInverseOf( Property prop ) { 476 setPropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop ); 477 } 478 479 484 public void addInverseOf( Property prop ) { 485 addPropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop ); 486 } 487 488 494 public OntProperty getInverseOf() { 495 return objectAsProperty( getProfile().INVERSE_OF(), "INVERSE_OF" ); 496 } 497 498 504 public ExtendedIterator listInverseOf() { 505 return listAs( getProfile().INVERSE_OF(), "INVERSE_OF", OntProperty.class ); 506 } 507 508 513 public boolean isInverseOf( Property prop ) { 514 return hasPropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop ); 515 } 516 517 523 public void removeInverseProperty( Property prop ) { 524 removePropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop ); 525 } 526 527 528 534 public FunctionalProperty asFunctionalProperty() { 535 return (FunctionalProperty) as( FunctionalProperty.class ); 536 } 537 538 544 public DatatypeProperty asDatatypeProperty() { 545 return (DatatypeProperty) as( DatatypeProperty.class ); 546 } 547 548 554 public ObjectProperty asObjectProperty() { 555 return (ObjectProperty) as( ObjectProperty.class ); 556 } 557 558 564 public TransitiveProperty asTransitiveProperty() { 565 return (TransitiveProperty) as( TransitiveProperty.class ); 566 } 567 568 574 public InverseFunctionalProperty asInverseFunctionalProperty() { 575 return (InverseFunctionalProperty) as( InverseFunctionalProperty.class ); 576 } 577 578 584 public SymmetricProperty asSymmetricProperty() { 585 return (SymmetricProperty) as( SymmetricProperty.class ); 586 } 587 588 590 594 public FunctionalProperty convertToFunctionalProperty() { 595 return (FunctionalProperty) convertToType( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY", FunctionalProperty.class ); 596 } 597 598 602 public DatatypeProperty convertToDatatypeProperty() { 603 return (DatatypeProperty) convertToType( getProfile().DATATYPE_PROPERTY(), "DATATYPE_PROPERTY", DatatypeProperty.class ); 604 } 605 606 610 public ObjectProperty convertToObjectProperty() { 611 return (ObjectProperty) convertToType( getProfile().OBJECT_PROPERTY(), "OBJECT_PROPERTY", ObjectProperty.class ); 612 } 613 614 618 public TransitiveProperty convertToTransitiveProperty() { 619 return (TransitiveProperty) convertToType( getProfile().TRANSITIVE_PROPERTY(), "TRANSITIVE_PROPERTY", TransitiveProperty.class ); 620 } 621 622 626 public InverseFunctionalProperty convertToInverseFunctionalProperty() { 627 return (InverseFunctionalProperty) convertToType( getProfile().INVERSE_FUNCTIONAL_PROPERTY(), "INVERSE_FUNCTIONAL_PROPERTY", InverseFunctionalProperty.class ); 628 } 629 630 634 public SymmetricProperty convertToSymmetricProperty() { 635 return (SymmetricProperty) convertToType( getProfile().SYMMETRIC_PROPERTY(), "SYMMETRIC_PROPERTY", SymmetricProperty.class ); 636 } 637 638 639 641 645 public boolean isFunctionalProperty() { 646 return hasRDFType( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY", false ); 647 } 648 649 653 public boolean isDatatypeProperty() { 654 return hasRDFType( getProfile().DATATYPE_PROPERTY(), "DATATYPE_PROPERTY", false ); 655 } 656 657 661 public boolean isObjectProperty() { 662 return hasRDFType( getProfile().OBJECT_PROPERTY(), "OBJECT_PROPERTY", false ); 663 } 664 665 669 public boolean isTransitiveProperty() { 670 return hasRDFType( getProfile().TRANSITIVE_PROPERTY(), "TRANSITIVE_PROPERTY", false ); 671 } 672 673 677 public boolean isInverseFunctionalProperty() { 678 return hasRDFType( getProfile().INVERSE_FUNCTIONAL_PROPERTY(), "INVERSE_FUNCTIONAL_PROPERTY", false ); 679 } 680 681 685 public boolean isSymmetricProperty() { 686 return hasRDFType( getProfile().SYMMETRIC_PROPERTY(), "SYMMETRIC_PROPERTY", false ); 687 } 688 689 690 695 public OntProperty getInverse() { 696 ExtendedIterator i = listInverse(); 697 OntProperty p = i.hasNext() ? ((OntProperty) i.next()) : null; 698 i.close(); 699 700 return p; 701 } 702 703 707 public ExtendedIterator listInverse() { 708 return getModel().listStatements( null, getProfile().INVERSE_OF(), this ).mapWith( new SubjectAsMapper( OntProperty.class ) ); 709 } 710 711 715 public boolean hasInverse() { 716 ExtendedIterator i = listInverse(); 717 boolean hasInv = i.hasNext(); 718 i.close(); 719 720 return hasInv; 721 } 722 723 724 734 public ExtendedIterator listDeclaringClasses() { 735 return listDeclaringClasses( false ); 736 } 737 738 750 public ExtendedIterator listDeclaringClasses( boolean direct ) { 751 Set cands = new HashSet(); 754 for (Iterator i = listDomain(); i.hasNext(); ) { 755 List q = new ArrayList(); 757 q.add( i.next() ); 758 759 while (!q.isEmpty()) { 760 OntClass c = (OntClass) q.remove( 0 ); 761 762 if (!c.isOntLanguageTerm() && !cands.contains( c )) { 763 cands.add( c ); 765 for (Iterator j = c.listSubClasses(); j.hasNext(); ) { 766 q.add( j.next() ); 767 } 768 } 769 } 770 } 771 772 if (cands.isEmpty()) { 773 if (!direct) { 775 return ((OntModel) getModel()).listClasses() 778 .filterDrop( new Filter() { 779 public boolean accept( Object o ) { 780 return ((OntClass) o).isOntLanguageTerm(); 781 }} ); 782 } 783 else { 784 return ((OntModel) getModel()).listHierarchyRootClasses(); 787 } 788 } 789 else { 790 return WrappedIterator.create( cands.iterator() ) 793 .filterKeep( new FilterDeclaringClass( this, direct )); 794 } 795 } 796 797 798 799 802 810 public RDFNode inModel( Model m ) { 811 return (getModel() == m) ? this : m.createProperty( getURI() ); 812 } 813 814 815 816 820 824 private class FilterDeclaringClass 825 implements Filter 826 { 827 private boolean m_direct; 828 private Property m_prop; 829 830 private FilterDeclaringClass( Property prop, boolean direct ) { 831 m_prop = prop; 832 m_direct = direct; 833 } 834 835 public boolean accept( Object o ) { 836 return ((OntClass) o).hasDeclaredProperty( m_prop, m_direct ); 837 } 838 839 } 840 } 841 842 843 872 873 | Popular Tags |