0

com.hp.hpl.jena.rdf.model.Resourceに含まれるタイプのオブジェクトへの参照がありますBag。このリソースを含むすべてのバッグを一覧表示したいと考えています。listResourcesWithPropertyコンテナを検索するために使用できる同様の機能はありますか。´

バッグにはプロパティが追加されていません。を使用して追加されたリソースのコレクションのみがありますBag.add(RDFNode o)

4

1 に答える 1

1

RDF コンテナーのメンバーシップを示すために多くのプロパティが実際に使用されるため、推論が利用できない場合、これは実際には少し注意が必要です。たとえば、ここに 3 つのバッグを持つモデルがあり、その 1 番目と 3 番目の両方にリソース 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 .

もちろん、問題は、1 番目と 3 番目のバッグがたとえばリソース 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 に答える