On Tue, Mar 19, 2013 at 10:07 AM, Ed Swing <[email protected]> wrote:
> Unfortunately, it looks like there's still a problem with the super/subclass
> relationship even after I fixed the inconsistent property. Specifically,
> here's the test code:
>
> OntModel model =
>
> ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
> FileInputStream inStream =
> new FileInputStream("ontology/terrorism.xml");
> model.read(inStream, null);
> inStream.close();
> String uriBase = model.getNsPrefixURI("");
> OntClass cls = model.getOntClass(uriBase + "TerrorAttack");
> System.out
> .println("TerAtt: " + cls + " Super=" + cls.getSuperClass());
> for (Iterator<OntClass> iter = cls.listSuperClasses(false); iter
> .hasNext();) {
> System.out.println("Super: " + iter.next());
> }
> OntClass newsEvt = model.getOntClass("NewsArticle");
> System.out.println("NewsEvt is a superclass of TerrorAttack: "
> + cls.hasSuperClass(newsEvt));
>
> Newest version of the ontology is attached. What is printed:
>
> TerAtt: http://blsdev1.vsticorp.com/terrorism#TerrorAttack
> Super=http://www.w3.org/2002/07/owl#Thing
> Super: http://www.w3.org/2002/07/owl#Thing
> Super: http://www.w3.org/2000/01/rdf-schema#Resource
> Super: 1d20fb81:13d82ecf576:-7fec
> Super: http://blsdev1.vsticorp.com/terrorism#Killing
> Super: http://blsdev1.vsticorp.com/terrorism#Event
> Super: 1d20fb81:13d82ecf576:-7fef
> Super: 1d20fb81:13d82ecf576:-7ff8
> Super: 1d20fb81:13d82ecf576:-7ff7
> NewsEvt is a superclass of TerrorAttack: true
>
> The superclass test is obviously incorrect. Not only are the NewsArticle and
> TerrorAttack classes disjoint, the NewsArticle class doesn't even appear when
> all the superclasses are listed. Why is the hasSuperClass() method returning
> true?
You're initializing newsEvt to model.getOntClass( "NewsArticle" ),
which returns null, instead of model.getOntClass( uriBase +
"NewsArticle" ), which returns an OntClass, and cls.hasSuperClass(
null ) return true. You can see the difference in this minimal
working example (*please* provide minimal working examples, i.e.,
*complete* java source files that people on the list can compile and
run):
import java.io.InputStream;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
public class Terrorism {
public static void main(String[] args) {
OntModel model = ModelFactory.createOntologyModel(
OntModelSpec.OWL_DL_MEM_RULE_INF );
InputStream in = Terrorism.class.getResourceAsStream(
"terrorism.xml" );
model.read( in, null );
String ns = model.getNsPrefixURI("");
OntClass terrorAttack = model.getOntClass( ns+"TerrorAttack" );
OntClass newsArticle = model.getOntClass( ns+"NewsArticle" );
System.out.println( terrorAttack +" URI superclasses:" );
ExtendedIterator<OntClass> classes =
terrorAttack.listSuperClasses();
while ( classes.hasNext() ) {
OntClass klass = classes.next();
if ( klass.isURIResource() ) {
System.out.println( "\t* "+klass );
}
}
System.out.println( newsArticle +" URI subclasses:" );
classes = newsArticle.listSubClasses();
while ( classes.hasNext() ) {
OntClass klass = classes.next();
if ( klass.isURIResource() ) {
System.out.println( "\t* "+klass );
}
}
for ( OntClass klass : new OntClass[] { newsArticle, null } ) {
System.out.println( klass+" is
"+(terrorAttack.hasSuperClass( klass
) ? "" : "not ")+"a superclass of "+terrorAttack );
}
}
}
which prints
http://blsdev1.vsticorp.com/terrorism#TerrorAttack URI superclasses:
* http://www.w3.org/2002/07/owl#Thing
* http://www.w3.org/2000/01/rdf-schema#Resource
* http://blsdev1.vsticorp.com/terrorism#Killing
* http://blsdev1.vsticorp.com/terrorism#Event
http://blsdev1.vsticorp.com/terrorism#NewsArticle URI subclasses:
http://blsdev1.vsticorp.com/terrorism#NewsArticle is not a superclass
of http://blsdev1.vsticorp.com/terrorism#TerrorAttack
null is a superclass of http://blsdev1.vsticorp.com/terrorism#TerrorAttack
Note that last line. I haven't looked yet, but I'm guessing that
cls.hasSuperClass( resource ) just queries the model for statements of
the form [ cls, rdfs:subClassOf, resource ], which, when resource is
null, is just [cls, rdfs:subClassOf, null ], which is just a query
whether cls has *any* superclass, i.e., cls.hasSuperClass( null ) is
equivalent to cls.hasSuperClass(). Since TerrorAttack has some
superclass, you get true.
//JT
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/