2017-07-28 81 views
2

由於unionOf和intersectionOf,我正在使用Apache Jena的API,其中圖形也包含一些匿名/空白節點。其中一個例子是:遍歷Jena中的匿名/空白節點

<owl:Class> 
    <owl:unionOf rdf:parseType="Collection"> 
     <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity1"/> 
     <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity2"/> 
    </owl:unionOf> 
</owl:Class> 

這是一個匿名節點/資源。當我試圖獲得其URI,它是這樣的:

「-50a5734d:15d839467d9:-1b8b」

我既不能夠使用這樣的URI(做SPARQL查詢由於異常的解析這些URI),也無法找到適當的Jena方法來處理它。

我正在尋找一種方法來爆炸這樣的節點並獲得它的所有嵌套資源。

例如在以下情況下,它應該返回<http:/.../Entity1><http:/.../Entity2><http:/.../Entity3>

<owl:Class> 
    <owl:unionOf rdf:parseType="Collection"> 
     <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity1"/> 
     <owl:unionOf rdf:parseType="Collection"> 
      <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity2"/> 
      <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity3"/> 
     </owl:unionOf> 
    </owl:unionOf> 
</owl:Class> 
  1. 有沒有處理呢耶拿任何內置的方法?

  2. 如果不是,我該如何有效地做到這一點?

+0

你應該總是看數據的龜序列化,而不是RDF/XML之一。然後你會看到你可以使用SPARQL屬性路徑。事實上,這對任意嵌套類不起作用,但因此,因爲它是OWL OWL推理器是要走的路 – AKSW

+0

首先看到整個查詢將是很好的,但原則上常見模式是'?subclass rdfs:subClassOf /(owl:unionOf/rdf:rest */rdf:first)+?superclass'如果您正在尋找由OWL中的類聯合定義的超類。 – AKSW

回答

3

我想這樣做,它很好地工作:

/** 
* Explodes <b>Anonymous resource</b> (Collection resource) in recursive way and provides 
* nested resources. Mainly considers <code>owl:unionOf</code>, <code>owl:intersactionOf</code>, <code>rdf:first</code> and <code>rdf:rest</code> 
* while traversing. 
* 
* @param resource 
* @return LinkedList<Resource> 
*/ 
private List<Resource> explodeAnonymousResource(Resource resource) 
{ 
    private static List<Property> collectionProperties = new LinkedList<Property>(Arrays.asList(OWL.unionOf,OWL.intersectionOf,RDF.first,RDF.rest)); 

    List<Resource> resources=new LinkedList<Resource>(); 
    Boolean needToTraverseNext=false; 

    if(resource.isAnon()) 
    { 
     for(Property cp:collectionProperties) 
     { 
      if(resource.hasProperty(cp) && !resource.getPropertyResourceValue(cp).equals(RDF.nil)) 
      { 
       Resource nextResource=resource.getPropertyResourceValue(cp); 
       resources.addAll(explodeAnonymousResource(nextResource)); 

       needToTraverseNext=true; 
      } 
     } 

     if(!needToTraverseNext) 
     { 
      resources.add(resource); 
     } 
    } 
    else 
    { 
     resources.add(resource); 
    } 

    return resources; 
} 
1

使用Jena的模型-API:

 String s = "<rdf:RDF\n" + 
      " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" + 
      " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" + 
      " xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n" + 
      " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n" + 
      " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\">\n" + 
      " <owl:Ontology/>\n" + 
      " <owl:Class>\n" + 
      " <owl:unionOf rdf:parseType=\"Collection\">\n" + 
      "  <owl:Class rdf:about=\"http://www.summyUrl.com/something#Entity1\"/>\n" + 
      "  <owl:Class>\n" + 
      "  <owl:unionOf rdf:parseType=\"Collection\">\n" + 
      "   <owl:Class rdf:about=\"http://www.summyUrl.com/something#Entity1\"/>\n" + 
      "   <owl:Class rdf:about=\"http://www.summyUrl.com/something#Entity2\"/>\n" + 
      "  </owl:unionOf>\n" + 
      "  </owl:Class>\n" + 
      " </owl:unionOf>\n" + 
      " </owl:Class>\n" + 
      "</rdf:RDF>"; 
    Model m = ModelFactory.createDefaultModel(); 
    try (InputStream in = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8))) { 
     m.read(in, Lang.RDFXML.getLabel()); 
    } 
    //m.write(System.out, "ttl"); 
    m.listStatements() 
      .mapWith(Statement::getObject) 
      .filterKeep(RDFNode::isURIResource) 
      .mapWith(RDFNode::asResource) 
      .filterDrop(OWL.Class::equals) 
      .filterDrop(OWL.Ontology::equals) 
      .filterDrop(RDF.nil::equals) 
      .mapWith(Resource::getURI) 
      .forEachRemaining(System.out::println); 

輸出:

http://www.summyUrl.com/something#Entity1 
http://www.summyUrl.com/something#Entity2 
http://www.summyUrl.com/something#Entity1 

這僅僅是一個例子。有很多的方式來處理事情