【问题标题】:Get bag (container) that contains a particular RDF resource using Jena使用 Jena 获取包含特定 RDF 资源的包(容器)
【发布时间】:2014-06-27 10:12:42
【问题描述】:

我引用了 com.hp.hpl.jena.rdf.model.Resource 类型的对象,该对象包含在 Bag 中。我想列出所有包含此资源的包。有没有类似listResourcesWithProperty的功能可以用来搜索容器。 '

袋子没有添加任何属性。它只有一个使用Bag.add(RDFNode o)添加的资源集合

【问题讨论】:

  • “Bag 没有添加任何属性。它只有使用 Bag.add(RDFNode o) 添加的资源集合”Bag.add 正在向模型添加语句。毕竟,Bag 接口只是模型中三元组的一个便利层。

标签: rdf jena semantic-web rdfs bag


【解决方案1】:

如果您没有可用的推理,这实际上有点棘手,因为许多属性实际上用于指示 RDF 容器成员资格。例如,这是一个带有三个包的模型,其中第一个和第三个都包含资源 x 和 y:

@prefix :      <http://stackoverflow.com/q/23568540/1281433/> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Bag1   a       rdf:Bag ;
        rdf:_1  :x ;
        rdf:_2  :y .

:Bag2   a       rdf:Bag ;
        rdf:_1  :y .

:Bag3   a       rdf:Bag ;
        rdf:_1  :y ;
        rdf:_2  :x .

当然,问题在于第一个和第三个包的属性,例如资源 x 是不一样的。但是,每个rdf:_nnn 属性都是rdfs:ContainerMembershipProperty 的一个实例。反过来,每个 rdfs:ContainerMembershipProperty 都是rdfs:member 的超属性。这意味着,如果您有一个可以推断的推理器,您可以询问哪些资源具有 x(或 y)作为 rdfs:member。 Jena 的 RDFS 推理器似乎没有(如以下示例所示)这样做。因此,您可能只需要遍历将 x 作为对象的语句,并检查该属性是否是容器成员属性。

import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Bag;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;


public class BagExample {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        String NS = "http://stackoverflow.com/q/23568540/1281433/";
        model.setNsPrefix( "", NS );
        model.setNsPrefix( "rdf", RDF.getURI() );

        Bag bag1 = model.createBag( NS+"Bag1" );
        Bag bag2 = model.createBag( NS+"Bag2" );
        Bag bag3 = model.createBag( NS+"Bag3" );

        Resource x = model.createResource( NS+"x" );
        Resource y = model.createResource( NS+"y" );

        bag1.add( x ).add( y );
        bag2.add( y );
        bag3.add( y ).add( x );

        RDFDataMgr.write( System.out, model, Lang.TURTLE );

        System.out.println( "=== Bags with X (no inference) ===" );
        ResIterator bagsWithX = model.listSubjectsWithProperty( RDFS.member, x );
        while ( bagsWithX.hasNext() ) {
            System.out.println( bagsWithX.next() );
        }

        System.out.println( "=== Bags with X (RDFS inference) ===" );
        OntModel rdfsModel = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_TRANS_INF, model );
        bagsWithX = rdfsModel.listSubjectsWithProperty( RDFS.member, x );
        while ( bagsWithX.hasNext() ) {
            System.out.println( bagsWithX.next() );
        }


        System.out.println( "=== Bags with X (manual checking) ===" );
        StmtIterator xStmts = model.listStatements( null, null, x );
        while ( xStmts.hasNext() ) {
            Statement s = xStmts.next();
            // This checks whether the URI begins with rdf:_.  A proper
            // solution would make sure that the suffix is actually numeric.
                    // You might also want to check whether the subject actually is 
                    // a Bag.  It could be a member of other kinds of containers, too.
            if ( s.getPredicate().getURI().startsWith( RDF.getURI()+"_" ) ) {
                System.out.println( s.getObject() );
            }
        }
    }
}
=== Bags with X (no inference) ===
=== Bags with X (RDFS inference) ===
=== Bags with X (manual checking) ===
http://stackoverflow.com/q/23568540/1281433/x
http://stackoverflow.com/q/23568540/1281433/x

【讨论】:

  • 感谢您如此详细的回复。很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多