Hi Guys! 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, it 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.

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

asked 12 Apr '11, 17:38

Deb's gravatar image

Deb
18125
accept rate: 0%

The examples bellow helped me a lot. Thank so much! ;)

Debora

(15 Apr '11, 08:38) Deb Deb's gravatar image

Hi Debbie,

You seem to calling hasSubClass() on a different class object to listSubClasses(). Depending on what data you have in your model, essaClasse might have no subclasses, whereas cla does.

Is this deliberate? ...hard to tell since I don't know what data you have in your model, but you seem to be changing the namespace of the class? In that case, you should do so before the hasSubClass() call:

           while (classes.hasNext()) {
                OntClass essaClasse = (OntClass) classes.next();
                String vClasse = essaClasse.getLocalName().toString();

//change namespaces here
                OntClass cla = inf.getOntClass(URI + vClasse);

//call hasSubClass on new class
                if (cla.hasSubClass()) {
                    System.out.println("Classe: " + cla);

for (Iterator i = cla.listSubClasses(); i.hasNext();) {
                        OntClass c = (OntClass) i.next();
                        System.out.print("   " + c.getLocalName() + " " + "\n");
                    }
                }
            }

...alternatively, instead of changing namespaces, you could just check each class to see if it corresponds with the URI namespace.

permanent link

answered 12 Apr '11, 22:54

Signified's gravatar image

Signified ♦
23.9k1623
accept rate: 37%

I didn't have your data file, so I created one and with just a few changes it seems to work. I think the big thing is that you did not have a null pointer check for variable cla.

Code is at: https://gist.github.com/916880

Output with the schema I used:

 Classe: Person
   Brother 
   Parent 
   Uncle 
Classe: Parent
Classe: Brother
Classe: Uncle

As @Signified says you can achieve the same thing without having to specify a new fully qualified URI for the class. This seems like test code, so I thought maybe you were doing with the fully qualified URI to show yourself how it works. But, if you just want to see classes with subclasses then a more succinct version would look like:

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 = "schema.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, "");

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

                System.out.println("Classe: " + essaClasse.getLocalName());
                for (Iterator i = essaClasse.listSubClasses(); i.hasNext();) {
                    OntClass c = (OntClass) i.next();
                    System.out.print("   " + c.getLocalName() + "\n");
                } // end for
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
permanent link

answered 12 Apr '11, 22:59

harschware's gravatar image

harschware ♦
7.7k1616
accept rate: 20%

edited 12 Apr '11, 23:43

How do I print only the class that is subclass? I use

if (essaClasse.hasSubClass() == true)

but nothing happens, continue with the same result as before. Can anyone help me again??

Thanks!

(25 Aug '11, 08:03) Deb Deb's gravatar image

try executing the code. The key is that the for loop will only iterate over sub classes. If there are none, it will iterate 0 times. There is no need to test with essaClasse.hasSubClass().

(25 Aug '11, 19:28) harschware ♦ harschware's gravatar image

To list only the subclass immediate use listSubClasses(true)

for (Iterator i = essaClasse.listSubClasses(true); i.hasNext();)

permanent link

answered 10 Aug '11, 20:53

Deb's gravatar image

Deb
18125
accept rate: 0%

edited 10 Aug '11, 20:54

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×597
×175

question asked: 12 Apr '11, 17:38

question was seen: 3,242 times

last updated: 25 Aug '11, 19:28