2014-05-09 28 views
0

我有一個包含在Bag中的類型爲com.hp.hpl.jena.rdf.model.Resource的對象的引用。我想列出包含此資源的所有包。是否有類似listResourcesWithProperty的功能可用於搜索容器。 '使用Jena獲取包含特定RDF資源的包(容器)

該包沒有添加任何屬性。它只有一個資源集合Bag.add(RDFNode o)

+0

「袋子沒有屬性添加,只具有使用Bag.add(RDFNode O)加入資源的集合」'Bag.add'是加入語句到模型。畢竟,Bag界面只是模型中三元組的一個方便層。 –

回答

1

這實際上有點棘手,如果您沒有可用的推理,因爲一些屬性實際上用於指示RDF容器成員資格。例如,這裏的三個包,第一和第三,其中都包含resouces 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。似乎沒有(如下例所示)耶拿的RDFS reasoners這樣做。因此,您可能只需遍歷包含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 
+0

感謝您的詳細回覆。很有幫助。 – user592748