📄 ontclassimpl.java
字号:
// conversion operations
/**
* <p>Answer a view of this class as an enumeration of the given individuals.</p>
* @param individuals A list of the individuals that will comprise the permitted values of this
* class converted to an enumeration
* @return This ontology class, converted to an enumeration of the given individuals
*/
public EnumeratedClass convertToEnumeratedClass( RDFList individuals ) {
setPropertyValue( getProfile().ONE_OF(), "ONE_OF", individuals );
return (EnumeratedClass) as( EnumeratedClass.class );
}
/**
* <p>Answer a view of this class as an intersection of the given classes.</p>
* @param classes A list of the classes that will comprise the operands of the intersection
* @return This ontology class, converted to an intersection of the given classes
*/
public IntersectionClass convertToIntersectionClass( RDFList classes ) {
setPropertyValue( getProfile().INTERSECTION_OF(), "INTERSECTION_OF", classes );
return (IntersectionClass) as( IntersectionClass.class );
}
/**
* <p>Answer a view of this class as a union of the given classes.</p>
* @param classes A list of the classes that will comprise the operands of the union
* @return This ontology class, converted to an union of the given classes
*/
public UnionClass convertToUnionClass( RDFList classes ) {
setPropertyValue( getProfile().UNION_OF(), "UNION_OF", classes );
return (UnionClass) as( UnionClass.class );
}
/**
* <p>Answer a view of this class as an complement of the given class.</p>
* @param cls An ontology classs that will be operand of the complement
* @return This ontology class, converted to an complement of the given class
*/
public ComplementClass convertToComplementClass( Resource cls ) {
setPropertyValue( getProfile().COMPLEMENT_OF(), "COMPLEMENT_OF", cls );
return (ComplementClass) as( ComplementClass.class );
}
/**
* <p>Answer a view of this class as an resriction on the given property.</p>
* @param prop A property this is the subject of a property restriction class expression
* @return This ontology class, converted to a restriction on the given property
*/
public Restriction convertToRestriction( Property prop ) {
if (!hasRDFType( getProfile().RESTRICTION(), "RESTRICTION", false )) {
setRDFType( getProfile().RESTRICTION() );
}
setPropertyValue( getProfile().ON_PROPERTY(), "ON_PROPERTY", prop );
return (Restriction) as( Restriction.class );
}
// Internal implementation methods
//////////////////////////////////
/**
* <p>Answer true if this class has the given class as a direct super-class, without using
* extra help from the reasoner.</p>
* @param cls The class to test
* @return True if the cls is a direct super-class of this class
*/
protected boolean hasSuperClassDirect(Resource cls) {
// we manually compute the maximal lower elements - this could be expensive in general
//return ResourceUtils.maximalLowerElements( listSuperClasses(), getProfile().SUB_CLASS_OF(), false ).contains( cls );
ExtendedIterator i = listDirectPropertyValues( getProfile().SUB_CLASS_OF(), "subClassOf", OntClass.class,
getProfile().SUB_CLASS_OF(), true, false );
try {
while (i.hasNext()) {
if (cls.equals( i.next() )) {
return true;
}
}
}
finally {
i.close();
}
return false;
}
/**
* <p>Answer true if this class lies with the domain of p<p>
* @param p
* @param direct
* @return
*/
protected boolean testDomain( Property p, boolean direct ) {
// we ignore any property in the DAML, OWL, etc namespace
String namespace = p.getNameSpace();
for (int i = 0; i < IGNORE_NAMESPACES.length; i++) {
if (namespace.equals( IGNORE_NAMESPACES[i] )) {
return false;
}
}
// check for global props, that have no specific domain constraint
boolean isGlobal = true;
// flag for detecting the direct case
boolean seenDirect = false;
for (StmtIterator i = getModel().listStatements( p, getProfile().DOMAIN(), (RDFNode) null ); i.hasNext(); ) {
Resource domain = i.nextStatement().getResource();
// there are some well-known values we ignore
if (!(domain.equals( getProfile().THING() ) || domain.equals( RDFS.Resource ))) {
// not a generic domain
isGlobal = false;
if (domain.equals( this )) {
// if this class is actually in the domain (as opposed to one of this class's
// super-classes), then we've detected the direct property case
seenDirect = true;
}
else if (!canProveSuperClass( domain )) {
// there is a class in the domain of p that is not a super-class of this class
return false;
}
}
}
if (direct) {
// if we're looking for direct props, we must either have seen the direct case
// or it's a global prop and this is a root class
return seenDirect || (isGlobal && isHierarchyRoot());
}
else {
// not direct, we must either found a global or a super-class prop
// otherwise the 'return false' above would have kicked in
return true;
}
}
/**
* <p>Answer an iterator over all of the properties in this model
* @return
*/
protected ExtendedIterator listAllProperties() {
OntModel mOnt = (OntModel) getModel();
Profile prof = mOnt.getProfile();
ExtendedIterator pi = mOnt.listStatements( null, RDF.type, getProfile().PROPERTY() );
// check reasoner capabilities - major performance improvement for inf models
if (mOnt.getReasoner() != null) {
Model caps = mOnt.getReasoner().getReasonerCapabilities();
if (caps.contains( null, ReasonerVocabulary.supportsP, OWL.ObjectProperty) ||
caps.contains( null, ReasonerVocabulary.supportsP, DAML_OIL.ObjectProperty))
{
// we conclude that the reasoner can do the necessary work to infer that
// all owl:ObjectProperty, owl:DatatypeProperty, etc, are rdf:Property resources
return pi;
}
}
// otherwise, we manually check the other property types
if (prof.OBJECT_PROPERTY() != null) {
pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.OBJECT_PROPERTY() ) );
}
if (prof.DATATYPE_PROPERTY() != null) {
pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.DATATYPE_PROPERTY() ) );
}
if (prof.FUNCTIONAL_PROPERTY() != null) {
pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.FUNCTIONAL_PROPERTY() ) );
}
if (prof.INVERSE_FUNCTIONAL_PROPERTY() != null) {
pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.INVERSE_FUNCTIONAL_PROPERTY() ) );
}
if (prof.SYMMETRIC_PROPERTY() != null) {
pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.SYMMETRIC_PROPERTY() ) );
}
if (prof.TRANSITIVE_PROPERTY() != null) {
pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.TRANSITIVE_PROPERTY() ) );
}
if (prof.ANNOTATION_PROPERTY() != null) {
pi = pi.andThen( mOnt.listStatements( null, RDF.type, prof.ANNOTATION_PROPERTY() ) );
}
return pi;
}
/**
* <p>Answer true if we can demonstrate that this class has the given super-class.
* If this model has a reasoner, this is equivalent to asking if the sub-class
* relation holds. Otherwise, we simulate basic reasoning by searching upwards
* through the class hierarchy.</p>
* @param sup A super-class to test for
* @return True if we can show that sup is a super-class of thsi class
*/
protected boolean canProveSuperClass( Resource sup ) {
OntModel om = (OntModel) getModel();
if (om.getReasoner() != null) {
if (om.getReasoner()
.getReasonerCapabilities().contains( null, ReasonerVocabulary.supportsP, RDFS.subClassOf ))
{
// this reasoner does transitive closure on sub-classes, so we just ask
return hasSuperClass( sup );
}
}
// otherwise, we have to search upwards through the class hierarchy
Set seen = new HashSet();
List queue = new ArrayList();
queue.add( this );
while (!queue.isEmpty()) {
OntClass c = (OntClass) queue.remove( 0 );
if (!seen.contains( c )) {
seen.add( c );
if (c.equals( sup )) {
// found the super class
return true;
}
else {
// queue the supers
for (Iterator i = c.listSuperClasses(); i.hasNext(); ) {
queue.add( i.next() );
}
}
}
}
// to get here, we didn't find the class we were looking for
return false;
}
//==============================================================================
// Inner class definitions
//==============================================================================
}
/*
(c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -