1 19 20 package com.hp.hpl.jena.ontology.impl; 23 24 25 26 import com.hp.hpl.jena.ontology.*; 29 import com.hp.hpl.jena.enhanced.*; 30 import com.hp.hpl.jena.graph.*; 31 import com.hp.hpl.jena.graph.query.*; 32 import com.hp.hpl.jena.rdf.model.*; 33 import com.hp.hpl.jena.reasoner.*; 34 import com.hp.hpl.jena.util.iterator.*; 35 import com.hp.hpl.jena.vocabulary.*; 36 37 import java.util.*; 38 39 40 49 public class OntClassImpl 50 extends OntResourceImpl 51 implements OntClass 52 { 53 56 57 private static final String [] IGNORE_NAMESPACES = new String [] { 58 OWL.NS, 59 DAMLVocabulary.NAMESPACE_DAML_2001_03_URI, 60 RDF.getURI(), 61 RDFS.getURI(), 62 ReasonerVocabulary.RBNamespace 63 }; 64 65 66 69 74 public static Implementation factory = new Implementation() { 75 public EnhNode wrap( Node n, EnhGraph eg ) { 76 if (canWrap( n, eg )) { 77 return new OntClassImpl( n, eg ); 78 } 79 else { 80 throw new ConversionException( "Cannot convert node " + n.toString() + " to OntClass: it does not have rdf:type owl:Class or equivalent"); 81 } 82 } 83 84 public boolean canWrap( Node node, EnhGraph eg ) { 85 Profile profile = (eg instanceof OntModel) ? ((OntModel) eg).getProfile() : null; 87 return (profile != null) && profile.isSupported( node, eg, OntClass.class ); 88 } 89 }; 90 91 92 95 96 protected BindingQueryPlan m_domainQuery; 97 98 99 protected BindingQueryPlan m_restrictionPropQuery = null; 100 101 102 105 113 public OntClassImpl( Node n, EnhGraph g ) { 114 super( n, g ); 115 116 Query q = new Query(); 119 q.addMatch( Query.X, getProfile().DOMAIN().asNode(), asNode() ); 120 121 m_domainQuery = getModel().queryHandler().prepareBindings( q, new Node[] {Query.X} ); 122 123 if (getProfile().ON_PROPERTY() != null) { 125 q = new Query(); 126 q.addMatch( asNode(), getProfile().SUB_CLASS_OF().asNode(), Query.X ); 127 q.addMatch( Query.X, getProfile().ON_PROPERTY().asNode(), Query.Y ); 128 129 m_restrictionPropQuery = getModel().queryHandler().prepareBindings( q, new Node[] {Query.Y} ); 130 } 131 } 132 133 134 137 139 145 public void setSuperClass( Resource cls ) { 146 setPropertyValue( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF", cls ); 147 } 148 149 154 public void addSuperClass( Resource cls ) { 155 addPropertyValue( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF", cls ); 156 } 157 158 164 public OntClass getSuperClass() { 165 return (OntClass) objectAs( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF", OntClass.class ); 166 } 167 168 174 public ExtendedIterator listSuperClasses() { 175 return listSuperClasses( false ); 176 } 177 178 191 public ExtendedIterator listSuperClasses( boolean direct ) { 192 return UniqueExtendedIterator.create( 193 listDirectPropertyValues( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF", OntClass.class, getProfile().SUB_CLASS_OF(), direct, false ) 194 .filterDrop( new SingleEqualityFilter( this ) ) ); 195 } 196 197 203 public boolean hasSuperClass( Resource cls ) { 204 return hasSuperClass( cls, false ); 205 } 206 207 213 public boolean hasSuperClass() { 214 return getSuperClass() != null; 215 } 216 217 228 public boolean hasSuperClass( Resource cls, boolean direct ) { 229 if (!direct) { 230 return hasPropertyValue( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF", cls ); 232 } 233 else { 234 InfGraph ig = null; 237 if (getGraph() instanceof InfGraph) { 238 ig = (InfGraph) getGraph(); 239 } 240 else if (getGraph() instanceof OntModel) { 241 OntModel m = (OntModel) getGraph(); 242 if (m.getGraph() instanceof InfGraph) { 243 ig = (InfGraph) m.getGraph(); 244 } 245 } 246 247 if (ig != null && ig.getReasoner().supportsProperty( ReasonerVocabulary.directSubClassOf )) { 248 return hasPropertyValue( ReasonerVocabulary.directSubClassOf, "direct sub-class", cls ); 250 } 251 else { 252 return hasSuperClassDirect(cls); 254 } 255 } 256 } 257 258 264 public void removeSuperClass( Resource cls ) { 265 removePropertyValue( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF", cls ); 266 } 267 268 274 public void setSubClass( Resource cls ) { 275 checkProfile( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF" ); 277 for (StmtIterator i = getModel().listStatements( null, getProfile().SUB_CLASS_OF(), this ); i.hasNext(); ) { 278 i.removeNext(); 279 } 280 281 ((OntClass) cls.as( OntClass.class )).addSuperClass( this ); 282 } 283 284 289 public void addSubClass( Resource cls ) { 290 ((OntClass) cls.as( OntClass.class )).addSuperClass( this ); 291 } 292 293 301 public OntClass getSubClass() { 302 checkProfile( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF" ); 303 StmtIterator i = getModel().listStatements( null, getProfile().SUB_CLASS_OF(), this ); 304 try { 305 if (i.hasNext()) { 306 return (OntClass) i.nextStatement() 307 .getSubject() 308 .as( OntClass.class ); 309 } 310 else { 311 return null; 312 } 313 } 314 finally { 315 i.close(); 316 } 317 } 318 319 325 public ExtendedIterator listSubClasses() { 326 return listSubClasses( false ); 327 } 328 329 373 public ExtendedIterator listSubClasses( boolean direct ) { 374 return UniqueExtendedIterator.create( 375 listDirectPropertyValues( getProfile().SUB_CLASS_OF(), "SUB_CLASS_OF", OntClass.class, getProfile().SUB_CLASS_OF(), direct, true ) 376 .filterDrop( new SingleEqualityFilter( this ) ) ); 377 } 378 379 380 386 public boolean hasSubClass( Resource cls ) { 387 return hasSubClass( cls, false ); 388 } 389 390 396 public boolean hasSubClass() { 397 return getSubClass() != null; 398 } 399 400 411 public boolean hasSubClass( Resource cls, boolean direct ) { 412 if (getModel() instanceof OntModel && 413 (cls.getModel() == null || !(cls.getModel() instanceof OntModel))) 414 { 415 cls = (Resource) cls.inModel( getModel() ); 417 } 418 return ((OntClass) cls.as( OntClass.class )).hasSuperClass( this, direct ); 419 } 420 421 427 public void removeSubClass( Resource cls ) { 428 ((OntClass) cls.as( OntClass.class)).removeSuperClass( this ); 429 } 430 431 432 434 440 public void setEquivalentClass( Resource cls ) { 441 setPropertyValue( getProfile().EQUIVALENT_CLASS(), "EQUIVALENT_CLASS", cls ); 442 } 443 444 449 public void addEquivalentClass( Resource cls ) { 450 addPropertyValue( getProfile().EQUIVALENT_CLASS(), "EQUIVALENT_CLASS", cls ); 451 } 452 453 459 public OntClass getEquivalentClass() { 460 return (OntClass) objectAs( getProfile().EQUIVALENT_CLASS(), "EQUIVALENT_CLASS", OntClass.class ); 461 } 462 463 469 public ExtendedIterator listEquivalentClasses() { 470 return UniqueExtendedIterator.create( listAs( getProfile().EQUIVALENT_CLASS(), "EQUIVALENT_CLASS", OntClass.class ) ); 471 } 472 473 479 public boolean hasEquivalentClass( Resource cls ) { 480 return hasPropertyValue( getProfile().EQUIVALENT_CLASS(), "EQUIVALENT_CLASS", cls ); 481 } 482 483 490 public void removeEquivalentClass( Resource cls ) { 491 removePropertyValue( getProfile().EQUIVALENT_CLASS(), "EQUIVALENT_CLASS", cls ); 492 } 493 494 496 502 public void setDisjointWith( Resource cls ) { 503 setPropertyValue( getProfile().DISJOINT_WITH(), "DISJOINT_WITH", cls ); 504 } 505 506 511 public void addDisjointWith( Resource cls ) { 512 addPropertyValue( getProfile().DISJOINT_WITH(), "DISJOINT_WITH", cls ); 513 } 514 515 521 public OntClass getDisjointWith() { 522 return (OntClass) objectAs( getProfile().DISJOINT_WITH(), "DISJOINT_WITH", OntClass.class ); 523 } 524 525 531 public ExtendedIterator listDisjointWith() { 532 return UniqueExtendedIterator.create( listAs( getProfile().DISJOINT_WITH(), "DISJOINT_WITH", OntClass.class ) ); 533 } 534 535 541 public boolean isDisjointWith( Resource cls ) { 542 return hasPropertyValue( getProfile().DISJOINT_WITH(), "DISJOINT_WITH", cls ); 543 } 544 545 552 public void removeDisjointWith( Resource cls ) { 553 removePropertyValue( getProfile().DISJOINT_WITH(), "DISJOINT_WITH", cls ); 554 } 555 556 557 559 571 public ExtendedIterator listDeclaredProperties() { 572 return listDeclaredProperties( false ); 573 } 574 575 576 589 public ExtendedIterator listDeclaredProperties( boolean direct ) { 590 Set candSet = new HashSet(); 592 593 for (Iterator i = listAllProperties(); i.hasNext(); ) { 596 candSet.add( ((Statement) i.next()).getSubject().as( Property.class ) ); 597 } 598 599 List cands = new ArrayList(); 601 cands.addAll( candSet ); 602 for (int j = cands.size() -1; j >= 0; j--) { 603 Property cand = (Property) cands.get( j ); 604 if (!hasDeclaredProperty( cand, direct )) { 605 cands.remove( j ); 606 } 607 } 608 609 return WrappedIterator.create( cands.iterator() ) 611 .mapWith( new AsMapper( OntProperty.class ) ); 612 } 613 614 615 624 public boolean hasDeclaredProperty( Property p, boolean direct ) { 625 return testDomain( p, direct ); 626 } 627 628 629 636 public ExtendedIterator listInstances() { 637 return UniqueExtendedIterator.create( 638 getModel() 639 .listStatements( null, RDF.type, this ) 640 .mapWith( new SubjectAsMapper( Individual.class ) ) 641 ); 642 } 643 644 645 649 public Individual createIndividual() { 650 return ((OntModel) getModel()).createIndividual( this ); 651 } 652 653 654 659 public Individual createIndividual( String uri ) { 660 return ((OntModel) getModel()).createIndividual( uri, this ); 661 } 662 663 664 672 public boolean isHierarchyRoot() { 673 if (equals( getProfile().NOTHING() )) { 675 return false; 676 } 677 678 ExtendedIterator i = null; 681 try { 682 i = listSuperClasses( true ); 683 684 while (i.hasNext()) { 685 Resource sup = (Resource) i.next(); 686 if (!(sup.equals( getProfile().THING() ) || 687 sup.equals( RDFS.Resource ) || 688 sup.equals( this ))) 689 { 690 return false; 692 } 693 } 694 } 695 finally { 696 i.close(); 697 } 698 699 return true; 700 } 701 702 703 710 public EnumeratedClass asEnumeratedClass() { 711 return (EnumeratedClass) as( EnumeratedClass.class ); 712 } 713 714 720 public UnionClass asUnionClass() { 721 return (UnionClass) as( UnionClass.class ); 722 } 723 724 730 public IntersectionClass asIntersectionClass() { 731 return (IntersectionClass) as( IntersectionClass.class ); 732 } 733 734 740 public ComplementClass asComplementClass() { 741 return (ComplementClass) as( ComplementClass.class ); 742 } 743 744 750 public Restriction asRestriction() { 751 return (Restriction) as( Restriction.class ); 752 } 753 754 755 757 761 public boolean isEnumeratedClass() { 762 checkProfile( getProfile().ONE_OF(), "ONE_OF" ); 763 return hasProperty( getProfile().ONE_OF() ); 764 } 765 766 770 public boolean isUnionClass() { 771 checkProfile( getProfile().UNION_OF(), "UNION_OF" ); 772 return hasProperty( getProfile().UNION_OF() ); 773 } 774 775 779 public boolean isIntersectionClass() { 780 checkProfile( getProfile().INTERSECTION_OF(), "INTERSECTION_OF" ); 781 return hasProperty( getProfile().INTERSECTION_OF() ); 782 } 783 784 788 public boolean isComplementClass() { 789 checkProfile( getProfile().COMPLEMENT_OF(), "COMPLEMENT_OF" ); 790 return hasProperty( getProfile().COMPLEMENT_OF() ); 791 } 792 793 797 public boolean isRestriction() { 798 checkProfile( getProfile().RESTRICTION(), "RESTRICTION" ); 799 return hasProperty( getProfile().ON_PROPERTY() ) || 800 hasProperty( RDF.type, getProfile().RESTRICTION() ); 801 } 802 803 804 806 812 public EnumeratedClass convertToEnumeratedClass( RDFList individuals ) { 813 setPropertyValue( getProfile().ONE_OF(), "ONE_OF", individuals ); 814 return (EnumeratedClass) as( EnumeratedClass.class ); 815 } 816 817 822 public IntersectionClass convertToIntersectionClass( RDFList classes ) { 823 setPropertyValue( getProfile().INTERSECTION_OF(), "INTERSECTION_OF", classes ); 824 return (IntersectionClass) as( IntersectionClass.class ); 825 } 826 827 832 public UnionClass convertToUnionClass( RDFList classes ) { 833 setPropertyValue( getProfile().UNION_OF(), "UNION_OF", classes ); 834 return (UnionClass) as( UnionClass.class ); 835 } 836 837 842 public ComplementClass convertToComplementClass( Resource cls ) { 843 setPropertyValue( getProfile().COMPLEMENT_OF(), "COMPLEMENT_OF", cls ); 844 return (ComplementClass) as( ComplementClass.class ); 845 } 846 847 852 public Restriction convertToRestriction( Property prop ) { 853 if (!hasRDFType( getProfile().RESTRICTION(), "RESTRICTION", false )) { 854 setRDFType( getProfile().RESTRICTION() ); 855 } 856 setPropertyValue( getProfile().ON_PROPERTY(), "ON_PROPERTY", prop ); 857 return (Restriction) as( Restriction.class ); 858 } 859 860 861 864 865 private void collectProperty( Property p, Set props, OntModel m ) { 866 props.add( m.getProperty( p.getURI() ) ); 868 } 869 870 876 protected boolean hasSuperClassDirect(Resource cls) { 877 880 ExtendedIterator i = listDirectPropertyValues( getProfile().SUB_CLASS_OF(), "subClassOf", OntClass.class, 881 getProfile().SUB_CLASS_OF(), true, false ); 882 try { 883 while (i.hasNext()) { 884 if (cls.equals( i.next() )) { 885 return true; 886 } 887 } 888 } 889 finally { 890 i.close(); 891 } 892 893 return false; 894 } 895 896 897 903 protected boolean testDomain( Property p, boolean direct ) { 904 String namespace = p.getNameSpace(); 906 for (int i = 0; i < IGNORE_NAMESPACES.length; i++) { 907 if (namespace.equals( IGNORE_NAMESPACES[i] )) { 908 return false; 909 } 910 } 911 912 boolean isGlobal = true; 914 915 boolean seenDirect = false; 917 918 for (StmtIterator i = getModel().listStatements( p, getProfile().DOMAIN(), (RDFNode) null ); i.hasNext(); ) { 919 Resource domain = i.nextStatement().getResource(); 920 921 if (!(domain.equals( getProfile().THING() ) || domain.equals( RDFS.Resource ))) { 923 isGlobal = false; 925 926 if (domain.equals( this )) { 927 seenDirect = true; 930 } 931 else if (!canProveSuperClass( domain )) { 932 return false; 934 } 935 } 936 } 937 938 if (direct) { 939 return seenDirect || (isGlobal && isHierarchyRoot()); 942 } 943 else { 944 return true; 947 } 948 } 949 950 951 955 protected ExtendedIterator listAllProperties() { 956 OntModel mOnt = (OntModel) getModel(); 957 Profile prof = mOnt.getProfile(); 958 959 ExtendedIterator pi = mOnt.listStatements( null, RDF.type, getProfile().PROPERTY() ); 960 961 if (mOnt.getReasoner() != null) { 963 Model caps = mOnt.getReasoner().getReasonerCapabilities(); 964 if (caps.contains( null, ReasonerVocabulary.supportsP, OWL.ObjectProperty) || 965 caps.contains( null, ReasonerVocabulary.supportsP, DAML_OIL.ObjectProperty)) 966 { 967 return pi; 970 } 971 } 972 973 if (prof.OBJECT_PROPERTY() != null) { 975 pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.OBJECT_PROPERTY() ) ); 976 } 977 if (prof.DATATYPE_PROPERTY() != null) { 978 pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.DATATYPE_PROPERTY() ) ); 979 } 980 if (prof.FUNCTIONAL_PROPERTY() != null) { 981 pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.FUNCTIONAL_PROPERTY() ) ); 982 } 983 if (prof.INVERSE_FUNCTIONAL_PROPERTY() != null) { 984 pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.INVERSE_FUNCTIONAL_PROPERTY() ) ); 985 } 986 if (prof.SYMMETRIC_PROPERTY() != null) { 987 pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.SYMMETRIC_PROPERTY() ) ); 988 } 989 if (prof.TRANSITIVE_PROPERTY() != null) { 990 pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.TRANSITIVE_PROPERTY() ) ); 991 } 992 if (prof.ANNOTATION_PROPERTY() != null) { 993 pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.ANNOTATION_PROPERTY() ) ); 994 } 995 996 return pi; 997 } 998 999 1007 protected boolean canProveSuperClass( Resource sup ) { 1008 OntModel om = (OntModel) getModel(); 1009 if (om.getReasoner() != null) { 1010 if (om.getReasoner() 1011 .getReasonerCapabilities().contains( null, ReasonerVocabulary.supportsP, RDFS.subClassOf )) 1012 { 1013 return hasSuperClass( sup ); 1015 } 1016 } 1017 1018 Set seen = new HashSet(); 1020 List queue = new ArrayList(); 1021 queue.add( this ); 1022 1023 while (!queue.isEmpty()) { 1024 OntClass c = (OntClass) queue.remove( 0 ); 1025 if (!seen.contains( c )) { 1026 seen.add( c ); 1027 1028 if (c.equals( sup )) { 1029 return true; 1031 } 1032 else { 1033 for (Iterator i = c.listSuperClasses(); i.hasNext(); ) { 1035 queue.add( i.next() ); 1036 } 1037 } 1038 } 1039 } 1040 1041 return false; 1043 } 1044 1045 1049} 1050 1051 1052 1081 1082 | Popular Tags |