Deb
Deb

Reputation: 431

Issues to list classes and subclasses with Jena

I'm not getting to list all subclasses of a class. I list the classes, my algorithm checks if each class has a subclass. If true, was to list all subclasses. But this doesn´t happen, it seems to ignore the condition "if (essaClasse.hasSubClass). Can anyone help me? Bellow the code part.

Thanks!

Debora (Rio de Janeiro - Brasil)

The full code:

package testejena;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import java.io.InputStream;
import java.util.Iterator;


public class testeProp {

    static final String inputFileName = "OBRecortada3.owl";

    public static void main(String args[]) {

        try {

            //create the reasoning model using the base
            OntModel inf = ModelFactory.createOntologyModel();

            // use the FileManager to find the input file
            InputStream in = FileManager.get().open(inputFileName);
            if (in == null) {
                throw new IllegalArgumentException("File: " + inputFileName + " not found");
            }

            inf.read(in, "");

            String URI = "http://www.owl-ontologies.com/OntologyBase.owl#";

            ExtendedIterator classes = inf.listClasses();
            while (classes.hasNext()) {
                OntClass essaClasse = (OntClass) classes.next();

                String vClasse = essaClasse.getLocalName().toString();

                if (essaClasse.hasSubClass()) {
                    System.out.println("Classe: " + vClasse);
                    OntClass cla = inf.getOntClass(URI + vClasse);
                    for (Iterator i = cla.listSubClasses(); i.hasNext();) {
                        OntClass c = (OntClass) i.next();
                        System.out.print("   " + c.getLocalName() + " " + "\n");
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}}

Upvotes: 2

Views: 4898

Answers (2)

Khairul Islam
Khairul Islam

Reputation: 1215

your algorithm wasn't working because you didn't specify an OntModelSpec in your OntModel. Specifying an OntModelSpec this code works perfectly!

OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);

Upvotes: 1

Ian Dickinson
Ian Dickinson

Reputation: 13305

You don't show your data or complete code (including the bit where you set up the OntModel object), so it's hard to give definitive advice. The hasSubClass method is tested in the Jena unit tests, so it's unlikely (though not impossible) that it contains a bug. I would suggest checking that:

  • you are correctly loading the data into the Model before you run the above code, using a debugger or log statements to show, for example, the number of triples loaded

  • that ontology you are loading does in fact contain sub-class statements, including checking the prefix declaration used to define rdfs in any rdfs:subClassOf triples (it must be exactly http://www.w3.org/2000/01/rdf-schema#)

Upvotes: 0

Related Questions