0

我有一个对com.hp.hpl.jena.rdf.model.Resource包含在Bag. 我想列出所有包含此资源的包。是否有类似的功能listResourcesWithProperty可用于搜索容器。'

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

4

1 回答 1

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
于 2014-05-09T16:49:47.300 回答