Search code examples
javasubclassrdfowlowl-api

How can I retrieve subclasses of a dedicated class in OWLapi?


I'm trying to write a method which loads sub classes of input class. This code has been working fine with RDF file written by OWL API 3.1.x(Pizza.owl) but now it does not work with a file generated by OWL API 3.4.x version.

Here below is code I wrote:

public ArrayList<String> getSubClassOf(String parentClass)
{
    String seq = "";
    tempList.clear();
    tempClass = factory.getOWLClass(":" + parentClass, pm);
    s = reasoner.getSubClasses(tempClass, false);
    i = s.iterator();   
    while(i.hasNext()){
        seq = i.next().toString();
        if(!seq.contains("Nothing"))
          tempList.add(seq.substring(seq.indexOf("#")+1,seq.indexOf(">")));
    }       
    return tempList;            
}

This is owl file generated by OWL API 3.4.2:

<!DOCTYPE Ontology [
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY xml "http://www.w3.org/XML/1998/namespace" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>


<Ontology xmlns="http://www.w3.org/2002/07/owl#"
 xml:base="http://localhost/CA/SmartHome/SmartHome_1113"
 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:xml="http://www.w3.org/XML/1998/namespace"
 ontologyIRI="http://localhost/CA/SmartHome/SmartHome_1113">
<Prefix name="" IRI="http://localhost/CA/SmartHome/SmartHome_1113.owl#"/>
<Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
<Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
<Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
<Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>
<Declaration>
    <Class IRI="#Adult"/>
</Declaration>

<Declaration>
    <Class IRI="#Person"/>
</Declaration>

<SubClassOf>
    <Class IRI="#Adult"/>
    <Class IRI="#Person"/>
</SubClassOf>

</Ontology>

It's pretty simple ontology consisting of 2 classes, and Adult is a subclass of Person


Solution

  • I tried replicating your issue but could not use your code as some variables are undefined in the snippet. I thinl the issue is with the prefix manager you're using, or the class name. The following snippet provides Adult as a result:

    public static void main(String[] args) throws Exception {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        // load the importing ontology
        OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(test_owl));
        OWLReasoner r = new FaCTPlusPlusReasonerFactory().createReasoner(ontology);
        OWLClass person = ontology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(IRI.create("http://localhost/CA/SmartHome/SmartHome_1113#Person"));
        Set<OWLClass> classes = r.getSubClasses(person, false).getFlattened();
        System.out.println(classes);
    }
    

    The output is:

    [<http://localhost/CA/SmartHome/SmartHome_1113#Adult>, owl:Nothing]

    Notice that you do not need to parse the strings to get to the result you want. OWLEntity has a method to determine if it matches nothing, and you can get the IRI fragment with OWLEntity.getIRI().getFragment()

    The ontology should be valid both with 3.1 and 3.4.x (I used 3.4.8)