Jena:如何检查某个实例是否属于某个类?

时间:2013-05-24 11:26:19

标签: jena ontology

嗯,抱歉这个天真的问题,但到目前为止我还没有找到任何相关信息......我使用Jena schemagenpottery.java生成pottery.rdf。我的本体中的属性和类被翻译成类似的东西:

public static final DatatypeProperty colors = m_model.createDatatypeProperty( URL_0 );    
public static final OntClass Class_1 = m_model.createClass( URL_1 );
public static final OntClass Class_2 = m_model.createClass( URL_2 );
pottery.java中的

说我想列出Class_1所有实例的名称和颜色。我该怎么做?我现在所知道的是如何使用以下代码列出所有实例,无论它们属于哪个类:

ResIterator iter = model.listResourcesWithProperty(pottery.colors);
while (iter.hasNext()) {
  Resource r = iter.nextResource();
  System.out.println("  " + r.getLocalName() + " " + 
                     r.getRequiredProperty(pottery.colors).getString() );
}

简而言之,我的问题是如何在上面的代码中添加类限制。

3 个答案:

答案 0 :(得分:1)

您可以通过检查资源resource是否具有值type来检查资源RDF.type是否具有特定的RDF类型type。如果您的代码在上面,您可以添加以下形式的条件:

if ( r.hasProperty( RDF.type, importantType ) ) {
  System.out.println("  " + r.getLocalName() + " " + 
                     r.getRequiredProperty(pottery.colors).getString() );
}

答案 1 :(得分:0)

你大部分时间都在那里:

ResIterator iter = model.listResourcesWithProperty(RDF.type, Class_1);

将列出所有类型为RDF.type属性)值Class_1的内容。

试试jena tutorial。你可能会发现jena本体api对于这类任务来说更方便,但基本的api很好。

答案 2 :(得分:0)

假设:

  • Pottery.java是您使用schemagen生成的Java类,包含公共常量,例如Class_1Class_2等。

  • model是包含您要检查的RDF数据的OntModel

然后:

// list the resources that are instance of Class_1 in model:
for (ExtendedIterator<Individual> i = model.listIndividuals(Pottery.Class_1); 
       i.hasNext(); ) {
  Individual instance = i.next();
  System.out.println( instance.toString() + " is an instance of Class_1" );
}

编辑:我看到你也想要实例的颜色:

for (ExtendedIterator<Individual> i = model.listIndividuals(Pottery.Class_1); 
       i.hasNext(); ) {
  Individual instance = i.next();
  RDFNode cs = intance.getPropertyValue( Potter.colors );
  System.out.println( instance.toString() + " is an instance of Class_1" + 
                      " with colors " + cs );
}

有关详细信息,请参阅耶拿Ontology API documentation

相关问题