2014-04-01 58 views
1

我正在編寫一個代碼生成器,用於從此處定義的架構http://schema.rdfs.org/all.ttl生成實體(Java語言中的POJO)。我正在使用Jena來解析ttl文件並檢索我需要生成的元數據。使用Jena解析schema.org ttl/owl文件

Jena成功解析文件,但由於某種原因,它沒有列出給定實體的所有屬性,例如Person。我不知道我是否做錯了什麼,使用了錯誤的API等。下面是重現場景中的代碼示例:

public class PersonParser { 

    public static void main(String[] args) { 
     OntModel model = ModelFactory.createOntologyModel(); 
     URL url = Thread.currentThread().getContextClassLoader().getResource("schema_org.ttl"); 
     model.read(url.toString(), "TURTLE"); 
     OntClass ontclass = model.getOntClass("http://schema.org/Person"); 
     Iterator<OntProperty> props = ontclass.listDeclaredProperties(); 
     while (props.hasNext()) { 
      OntProperty p = props.next(); 
      System.out.println("p:" + p.getLocalName()); 
     } 
    } 
} 

基本上,我在尋找才叫人,並試圖一類列出所有的屬性和我所得到的是:

p:alternateName 
p:deathDate 
p:alumniOf 
p:sameAs 
p:url 
p:additionalName 
p:homeLocation 
p:description 
p:nationality 
p:sibling 
p:follows 
p:siblings 
p:colleagues 
p:memberOf 
p:knows 
p:name 
p:gender 
p:birthDate 
p:children 
p:familyName 
p:jobTitle 
p:workLocation 
p:parents 
p:affiliation 
p:givenName 
p:honorificPrefix 
p:parent 
p:colleague 
p:additionalType 
p:honorificSuffix 
p:image 
p:worksFor 
p:relatedTo 
p:spouse 
p:performerIn 

但是,如果你看一下http://schema.org/Person,它有一堆,它沒有列表(例如address)屬性。的schema:addresshttp://schema.rdfs.org/all.ttl的聲明是:

schema:address a rdf:Property; 
    rdfs:label "Address"@en; 
    rdfs:comment "Physical address of the item."@en; 
    rdfs:domain [ a owl:Class; owl:unionOf (schema:Person schema:Place schema:Organization) ]; 
    rdfs:range schema:PostalAddress; 
    rdfs:isDefinedBy <http://schema.org/Person>; 
    rdfs:isDefinedBy <http://schema.org/Place>; 
    rdfs:isDefinedBy <http://schema.org/Organization>; 
    . 

有沒有人碰到這個?我應該使用不同的耶拿接口來解析模式嗎?

回答

3

注意,上listDeclaredProperties的文檔是(強調):

listDeclaredProperties

com.hp.hpl.jena.util.iterator.ExtendedIterator<OntProperty> listDeclaredProperties(boolean direct) 

返回經過與框狀 視圖這個類的相關聯的屬性的迭代器。這捕獲了類的一個直觀的概念 屬性。這對於在用戶界面中呈現本類 類是有用的,例如通過自動構造一個 表單來實例化類的實例。通過將該類的OntModel中的 屬性的域與類本身進行比較,確定該類的類似框架視圖中的屬性。請參閱: Presenting RDF as frames瞭解更多詳情。

請注意,許多確定屬性是否與 關聯的情況取決於RDFS或OWL推理。因此,該方法可能僅在具有附加 推理程序的模型中才返回完整結果 。

參數:

  • 直接 - 如果爲true,限制屬性返回那些直接與此類關聯。如果爲false,則此類的超類 的屬性將不會在此 類的聲明屬性中列出。

返回:

與此類通過他們的域名相關聯的屬性的迭代。

因此,即使在查看特定模式之前,請注意,除非您使用推理器,否則可能無法獲得您期望的所有結果。然後,注意地址屬性如何聲明:

schema:address a rdf:Property; 
    rdfs:label "Address"@en; 
    rdfs:comment "Physical address of the item."@en; 
    rdfs:domain [ a owl:Class; owl:unionOf (schema:Person schema:Place schema:Organization) ]; 
    rdfs:range schema:PostalAddress; 
    rdfs:isDefinedBy <http://schema.org/Person>; 
    rdfs:isDefinedBy <http://schema.org/Place>; 
    rdfs:isDefinedBy <http://schema.org/Organization>; 

地址的域是工會等級:組織。這是一個超類Person,但它是一個複雜的類表達式,不僅僅是一個簡單的命名類,所以如文檔中提到的那樣,您可能需要一個推理器讓Jena認識到它是一個超類Person

與OWL語義

比較

我覺得用一個推理將使耶拿認識到地址的域名是的父類,從而將其包含在listDeclaredProperties的結果。值得注意的是,這與OWL語義有何不同。

在OWL中,對於類D來說,屬性P的意義是什麼,意味着只要有屬性P的三元組,我們就可以推斷出該對象是一個D.這可以用治

P rdfs:domain D  X P Y 
------------------------- 
    X rdf:type D 

所以,即使可能有一個地址,只是因爲東西有一個地址是不夠的,告訴我們那東西;它仍然可能是地方組織

+0

然後我明白的是,某些類的listDeclaredProperties(也獲得了超類的屬性),它不同於OWL中熟悉的語義(一個類是子類屬性的一個域)。 但是,除了迭代屬性以外,是否有某種方式可以以OWL語義方式獲取屬性,並且在運行推理器時詢問它是否具有域? –