List object properties from a instance in Jena

Leandro picture Leandro · Jun 11, 2012 · Viewed 8.3k times · Source

How can I list all Object Properties associated to a instance in Jena?

For example: A Person has an Object Property called "hasVehicle" which is associated with a class Vehicle

Answer

Ian Dickinson picture Ian Dickinson · Jun 12, 2012

The appropriate Jena method is OntClass.listDeclaredProperties. There are some nuances to be aware of; the Jena RDF frames how-to explains in detail.

Update

OK, I've looked at your code sample, and read your description, and I'm afraid I don't understand what you want to do. What I've done is re-write your code sample so that it does something that I guess you might want, based on your description in the comment:

package test;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class LeandroTest
{
    public static String NS = "http://www.owl-ontologies.com/TestProject.owl#";

    public static void main( String[] args ) {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );
        FileManager.get().readModel( m, "./src/main/resources/project-test.owl" );

        OntClass equipe = m.getOntClass( NS + "Equipe" );
        OntProperty nome = m.getOntProperty( NS + "nome" );

        for (ExtendedIterator<? extends OntResource> instances = equipe.listInstances(); instances.hasNext(); ) {
            OntResource equipeInstance = instances.next();
            System.out.println( "Equipe instance: " + equipeInstance.getProperty( nome ).getString() );

            // find out the resources that link to the instance
            for (StmtIterator stmts = m.listStatements( null, null, equipeInstance ); stmts.hasNext(); ) {
                Individual ind = stmts.next().getSubject().as( Individual.class );

                // show the properties of this individual
                System.out.println( "  " + ind.getURI() );
                for (StmtIterator j = ind.listProperties(); j.hasNext(); ) {
                    Statement s = j.next();
                    System.out.print( "    " + s.getPredicate().getLocalName() + " -> " );

                    if (s.getObject().isLiteral()) {
                        System.out.println( s.getLiteral().getLexicalForm() );
                    }
                    else {
                        System.out.println( s.getObject() );
                    }
                }
            }
        }
    }
}

This gives the following output, by first listing all resources of rdf:type #Equipe, then for each of those it lists the resources in the model that link to that Equipe, then for of those linked resources it lists all of the RDF the properties. I don't think that's a particularly useful thing to do, but hopefully it will show you some patterns for traversing RDF graphs in Jena.

Equipe instance: Erica
Equipe instance: Etiene
  http://www.owl-ontologies.com/TestProject.owl#EtapaExecucao_01
    EtapaExecucao_DataModificao -> 2010-03-29T10:54:05
    caso_de_teste -> http://www.owl-ontologies.com/TestProject.owl#CasoDeTeste_01
    EtapaExecucao_StatusTeste -> Passou
    EtapaExecucao_Reprodutibilidade -> Sempre
    type -> http://www.owl-ontologies.com/TestProject.owl#EtapaExecucao
    EtapaExecucao_VersaoDefeitoSurgiu -> Release ICAMMH_01.00
    EtapaExecucao_Severidade -> Minimo
    EtapaExecucao_VersaoDefeitoCorrigiu -> Release ICAMMH_02.00
    DataExecucao -> 2009-07-10T09:42:02
    EtapaExecucao_StatusDoDefeito -> Nao sera corrigido
    EtapaExecucao_DataSubmissao -> 2009-06-30T09:43:01
    Tipos_Fases -> http://www.owl-ontologies.com/TestProject.owl#FaseTesteExecucao
    EtapaExecucao_Resolucao -> Fechado
    executor_do_teste -> http://www.owl-ontologies.com/TestProject.owl#Etiene
    EtapaExecucao_PrioridadeCorrecao -> Normal
Equipe instance: Fabio
Equipe instance: Melis

Some general suggestions, particularly if you have any follow-up questions:

  • ask specific questions, it's very hard to answer a vague unclear question;
  • provide runnable code if possible: you can take my code below, drop it into a code environment like Eclipse and try it out
  • provide the code and data in the question, not linked off on pastebin
  • take some time to reduce the code and data to the minimum form necessary to show the problem: your Protégé file was over 600 lines long