如何列出与 Jena 中的实例关联的所有对象属性?
例如:一个Person有一个名为“ hasVehicle ”的对象属性,它与一个类Vehicle相关联
如何列出与 Jena 中的实例关联的所有对象属性?
例如:一个Person有一个名为“ hasVehicle ”的对象属性,它与一个类Vehicle相关联
适当的耶拿方法是OntClass.listDeclaredProperties
。有一些细微差别需要注意;Jena RDF 框架操作方法详细说明。
更新
好的,我查看了您的代码示例,并阅读了您的描述,恐怕我不明白您想要做什么。我所做的是根据您在评论中的描述重新编写您的代码示例,以便它执行我猜您可能想要的操作:
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
,然后对于每个资源,它列出模型中链接到该 Equipe 的资源,然后对于那些链接的资源,它列出所有 RDF 属性。我认为这不是一件特别有用的事情,但希望它会向您展示一些在 Jena 中遍历 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
一些一般性建议,特别是如果您有任何后续问题: