Deb Deb - 1 year ago 98
Java Question

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.


(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.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");
}, "");

String URI = "";

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

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);
System.out.print(" " + c.getLocalName() + " " + "\n");
} catch (Exception e) {

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download