列出Jena中实例的对象属性

时间:2012-06-11 19:40:16

标签: list jena object-properties object-property

如何列出与Jena中的实例关联的所有对象属性?

例如: 有一个名为“ hasVehicle ”的对象属性,该属性与类车辆

相关联

1 个答案:

答案 0 :(得分:5)

适当的Jena方法是OntClass.listDeclaredProperties。有一些细微差别需要注意;耶拿RDF frames how-to详细解释。

<强>更新

好的,我查看了你的代码示例,并阅读了你的描述,我担心我不明白你想做什么。我所做的就是重新编写你的代码示例,以便它根据你在评论中的描述做出你可能想要的猜测

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

这会提供以下输出,首先列出rdf:type #Equipe的所有资源,然后为每个资源列出模型中将链接到的资源然后,对于那些链接的资源,它列出了所有RDF属性。我认为这不是一件特别有用的事情,但希望它会向你展示一些在耶拿遍历RDF图的模式。

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

一些一般性建议,特别是如果您有任何后续问题:

  • 询问具体的问题,很难回答一个含糊不清的问题;
  • 尽可能提供 runnable 代码:您可以将我的代码放在下面,将其放入像Eclipse这样的代码环境中并试用它
  • 在问题中提供代码和数据,而不是在pastebin上链接
  • 需要一些时间将代码和数据减少到显示问题所需的最小形式:您的Protégé文件超过600行