上一篇我們談到,像 RDF 這類屬於 XML 陣營的知識管理系統,其實是用另一條路徑去面對資料設計與建模這些老問題。當時我著眼於問題追蹤套件(issue tracker)的整體樣貌,示範 RDF 該如何與既有的 XML 應用並肩運作。這一回,我要把那份模式真正寫出來——一口氣給出 RDFS 與 DAML+OIL 兩個版本。
閱讀本文前,最好對 RDF、RDFS 與 DAML+OIL 已有基礎認識。我在完成上一篇之後,與同事 Roxane Ouellet 合寫了一份 DAML+OIL 入門(詳見參考資料),所以你不必埋頭啃厚重的規格書也能跟上腳步。
先看清單 1,這是問題追蹤程式目前使用的完整 RDFS 模式。
清單 1. 問題追蹤程式的 RDFS 模式
<?XML version="1.0" encoding="UTF-8"?>
<!DOCTYPE rdf:RDF [
<!ENTITY rdf "https://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!ENTITY rdfs "https://www.w3.org/2000/01/rdf-schema#">
<!ENTITY it "https://rdfinference.org/schemata/issue-tracker/">
<!ENTITY dc "https://purl.org/dc/elements/1.1/">
]>
<rdf:RDF
XMLns:rdf="&rdf;"
XMLns:rdfs="&rdfs;"
XMLns:it="⁢"
>
<rdfs:Class rdf:about="⁢Catalog">
<rdfs:label>Issue catalog</rdfs:label>
<rdfs:comment>
An optional collection of resources for which issues have or can
be defined. Use dc:relation to associate the catalog with its
resources.
</rdfs:comment>
</rdfs:Class>
<rdfs:Class rdf:about="⁢Issue">
<rdfs:label>Issue</rdfs:label>
<rdfs:comment>
A problem, suggestion or other matter for action or discussion
relevant to a resource. Use Dublin Core properties for base
description.
</rdfs:comment>
</rdfs:Class>
<rdfs:Property rdf:about="⁢issue">
<rdfs:label>issue</rdfs:label>
<rdfs:comment>Associate an issue to its resources</rdfs:comment>
<rdfs:range rdf:resource="⁢Issue"/>
</rdfs:Property>
<rdfs:Property rdf:about="⁢action">
<rdfs:label>action</rdfs:label>
<rdfs:comment>Associate an action with an issue</rdfs:comment>
<rdfs:domain rdf:resource="⁢Issue"/>
<rdfs:range rdf:resource="⁢Action"/>
</rdfs:Property>
<rdfs:Class rdf:about="⁢Action">
<rdfs:label>Action</rdfs:label>
<rdfs:comment>
An action to be taken with regard to an issue
</rdfs:comment>
</rdfs:Class>
<rdfs:Class rdf:about="⁢it:assignee">
<rdfs:label>Assign to</rdfs:label>
<rdfs:comment>
Specify the party to whom the action is assigned
</rdfs:comment>
<rdfs:domain rdf:resource="⁢Action"/>
</rdfs:Class>
<rdfs:Class rdf:about="⁢status">
<rdfs:label>status</rdfs:label>
<rdfs:comment>For instance, "not done" or "done"</rdfs:comment>
<rdfs:domain rdf:resource="⁢Action"/>
</rdfs:Class>
<rdfs:Class rdf:about="⁢comment">
<rdfs:label>comment</rdfs:label>
<rdfs:comment>Associate a comment with an issue</rdfs:comment>
<rdfs:domain rdf:resource="⁢Issue"/>
<rdfs:range rdf:resource="⁢Comment"/>
</rdfs:Class>
<rdfs:Class rdf:about="⁢Comment">
<rdfs:label>Comment</rdfs:label>
<rdfs:comment>A comment made with regard to an issue</rdfs:comment>
</rdfs:Class>
</rdf:RDF>
把它跟前几篇的 RDF 範例擺在一起看,你會發現不少地方變了,連用的名稱空間都換了。遺憾的是,我沒辦法像當初解釋「為什麼範例裡一個類都沒定義」那樣,鉅細靡遺地交代每一處改動。這份模式反映的是 RDFInference.org 上問題追蹤程式實際的運作狀態,包含基於種種理由所做的調整;稍後我會補上對應更新過的實例 RDF。
我也採用了幾項寫作約定。第一,所有名稱空間 URI 都定義在 DTD 內部子集的實體裡(這招是跟 Ouellet 學的),這麼做既少出錯,可讀性也更好。第二,我只用 rdf:about,不再用 rdf:ID——在吃過無數次「解析相對於包含文件之假設 URI 的 ID」的苦頭之後,我終於改了。請注意,只有在能確定存在明確的 xml:base 宣告、且所有需要互通的 RDF 處理器都支援 XML Base 時,我才會回頭使用 rdf:ID。
Catalog 類提供一種聚合手段,把所有「已有問題」或「允許使用者為它開問題」的資源收攏在一起。這主要是替應用端行方便:想像追蹤程式有個網頁表單,上面一個「有興趣的資源」下拉選單,要填滿這個清單,你可以掃過該目錄底下所有 dc:relation 陳述的受詞。而我等一下要給的 DAML+OIL 模式,示範的是另一條路。
其餘多是小修,例如為了詞類一致,把「assigned-to」改名為「assignee」。除此之外這份模式沒什麼古怪之處。接下來,看 DAML+OIL 版本。
DAML+OIL 是一套模式系統,對 RDFS 做了幾項關鍵補強:內建資料型別判定機制、支援列舉、允許屬性專門化,還能透過推論完成分類與型別判定。它也不滿足於「只是畫示意圖」,而是讓我們定義本體(ontology)——也就是我們對某個領域概念的理解——不過目前我主要仍使用它基礎的示意功能。清單 2 是與清單 1 對應的 DAML+OIL 版本。
清單 2. 問題追蹤程式的 DAML+OIL 模式
<?XML version="1.0" encoding="UTF-8"?>
<!DOCTYPE rdf:RDF [
<!ENTITY rdf "https://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!ENTITY xsd "https://www.w3.org/2000/10/XMLSchema#">
<!ENTITY rdfs "https://www.w3.org/2000/01/rdf-schema#">
<!ENTITY daml "https://www.daml.org/2001/03/daml+oil#">
<!ENTITY dc "https://purl.org/dc/elements/1.1/">
<!ENTITY it "https://rdfinference.org/schemata/issue-tracker/">
]>
<rdf:RDF
XMLns:rdf="&rdf;"
XMLns:rdfs="&rdfs;"
XMLns:daml="&daml;"
XMLns:xsd="&xsd;"
XMLns:it="⁢"
>
<daml:Ontology rdf:about="">
<daml:versionInfo>
<!-- Note: requires expansion by RCS, CVS or the like
$Revision$
</daml:versionInfo>
<rdfs:comment>
Ontology for an issue tracking system for arbitrary resources
</rdfs:comment>
<daml:imports rdf:resource="https://www.w3.org/2001/10/daml+oil"/>
</daml:Ontology>
<daml:Class rdf:about="⁢RelevantResource">
<rdfs:label>Relevant resource</rdfs:label>
<rdfs:comment>
An implied classification of resources that have related issues
</rdfs:comment>
<rdfs:subClassOf>
<daml:onProperty rdf:resource="⁢issue"/>
<daml:toClass rdf:resource="⁢Issue"/>
</rdfs:subClassOf>
</daml:Class>
<daml:Class rdf:about="⁢Issue">
<rdfs:label>Issue</rdfs:label>
<rdfs:comment>
A problem, suggestion or other matter for action or discussion
relevant to a resource. Use Dublin Core properties for base
description.
</rdfs:comment>
</daml:Class>
<daml:ObjectProperty rdf:about="⁢issue">
<rdfs:label>issue</rdfs:label>
<rdfs:comment>Associate an issue to its resources</rdfs:comment>
<rdfs:range rdf:resource="⁢Issue"/>
</daml:ObjectProperty>
<daml:ObjectProperty rdf:about="⁢action">
<rdfs:label>action</rdfs:label>
<rdfs:comment>Associate an action with an issue</rdfs:comment>
<rdfs:domain rdf:resource="⁢Issue"/>
<rdfs:range rdf:resource="⁢Action"/>
</daml:ObjectProperty>
<daml:Class rdf:about="⁢Action">
<rdfs:label>Action</rdfs:label>
<rdfs:comment>
An action to be taken with regard to an
issue</rdfs:comment>
</daml:Class>
<daml:ObjectProperty rdf:about="⁢it:assignee">
<rdfs:label>Assign to</rdfs:label>
<rdfs:comment>
Specify the party to whom the action is assigned
</rdfs:comment>
<rdfs:domain rdf:resource="⁢Action"/>
</daml:ObjectProperty>
<daml:ObjectProperty rdf:about="⁢status">
<rdfs:label>status</rdfs:label>
<rdfs:comment>For instance, "not done" or "done"</rdfs:comment>
<rdfs:domain rdf:resource="⁢Action"/>
</daml:ObjectProperty>
<daml:ObjectProperty rdf:about="⁢comment">
<rdfs:label>comment</rdfs:label>
<rdfs:comment>Associate a comment with an issue</rdfs:comment>
<rdfs:domain rdf:resource="⁢Issue"/>
<rdfs:range rdf:resource="⁢Comment"/>
</daml:ObjectProperty>
<daml:Class rdf:about="⁢Comment">
<rdfs:label>Comment</rdfs:label>
<rdfs:comment>A comment made with regard to an issue</rdfs:comment>
</daml:Class>
</rdf:RDF>
最上面那段本體宣告出現在任何定義之前,這是 DAML 的慣例:它描述這份文件本身、標明其模式身分(所以 rdf:about 是空的,把文件自己設為主題)。其中一個特徵是修訂資訊陳述,我用版本控制系統會展開的關鍵字來填;另一個特徵是 daml:imports,這是 DAML+OIL 新增的明確機制,用來把其他檔案裡的定義併進當前文件。在 DAML 出現以前,你要不就是手動把多份來源定義揉成一個模型,要不就退回 XInclude 那類層次更低的手段。按照慣例,我匯入核心 DAML+OIL 模式,為 DAML+OIL 專屬資源補上定義。
再來是那個特別的 RelevantResource 類。它從不顯式列舉自己的實例,而是靠對實例特性的推論來決定成員資格。細看定義就明白了:它被宣告為一個匿名內聯資源的子類,而該匿名資源又是 daml:Restriction 型別。這是 DAML+OIL 的一項特殊機制,讓你能依「實例有哪些屬性、屬性值落在哪個範圍」來撰寫規則。這裡的約束挑出所有具備 issue 屬性、且其值屬於 Issue 類的資源。藉由把子類劃到這個約束底下,RelevantResource 就成了一個虛擬類,容納所有滿足條件的資源集合。任何時刻,只要某個資源取得了具備適當型別值的適當屬性,它就自動成為這個虛擬類的成員,不需要任何顯式宣告。
當你手上並未掌握全部資訊空間時,這個能力格外可貴——這也正是 DAML+OIL 被推出來的理由,而它在鞏固語意網路這類技術上確實跨出了一大步。放回我們的例子:這讓問題追蹤程式不必像 RDFS 版本那樣(靠 Catalog 類)逐一登記所有資源。
類別我都用 daml:Class 定義,它是 rdfs:Class 的子類,因此享有 DAML 帶來的全部附加能力;屬性則用 daml:ObjectProperty。問題追蹤模式沒有任何屬性的值使用具體資料型別(字串、整數等),但值得補一句:這類屬性在 DAML+OIL 中會定義成 daml:DatatypeProperty 的子類。
這份 DAML+OIL 模式就是 RDFInference.org 上實際跑的那一份,後續專欄也將以它為基礎。
由於前面提到的那些調整,我把專欄到目前為止一直在看的問題範例也回頭修過一輪,見清單 3。
清單 3. 更新後的實例資料
<?XML version='1.0'?>
<!DOCTYPE rdf:RDF [
<!ENTITY rdf "https://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!ENTITY rdfs "https://www.w3.org/2000/01/rdf-schema#">
<!ENTITY daml "https://www.daml.org/2001/03/daml+oil#">
<!ENTITY dc "https://purl.org/dc/elements/1.1/">
<!ENTITY foaf "https://XMLns.com/foaf/0.1/">
<!ENTITY it "https://rdfinference.org/schemata/issue-tracker/">
<!ENTITY rit "https://rdfinference.org/ril/issue-tracker/">
]>
<rdf:RDF
XMLns:rdf="&rdf;"
XMLns:rdfs="&rdfs;"
XMLns:daml="&daml;"
XMLns:rit="&rit;"
XMLns:it="⁢"
XMLns:dc="&dc;"
XMLns:foaf="&foaf;"
XMLns="⁢"
>
<rdf:Description rdf:about='https://rdfinference.org/ril/ril-20010502'>
<issue rdf:resource='&rit;i2001030423'/>
<issue rdf:resource='&rit;i2001042003'/>
</rdf:Description>
<Issue rdf:about='&rit;i2001030423'>
<dc:title>Unnecessary abbreviation</dc:title>
<dc:creator rdf:resource='mailto:Alexandre.Fayolle@logilab.fr'/>
<dc:description>
Is the abbreviation of rdf:type predicates in queries necessary?
</dc:description>
<dc:date>2001-03-04</dc:date>
<comment rdf:parseType="Resource">
<dc:creator rdf:resource='mailto:Alexandre.Fayolle@logilab.fr'/>
<dc:description>
The abbreviation in listing 8 doesn't seem necessary to Nico
Chauvat or me.
</dc:description>
</comment>
<action rdf:parseType="Resource">
<dc:description>Organize a vote on this topic</dc:description>
<it:assignee rdf:resource='mailto:uche.ogbuji@fourthought.com'/>
</action>
</Issue>
<Issue rdf:about='&rit;i2001042003'>
<dc:title>Inconsistent versioning</dc:title>
<dc:creator rdf:resource='mailto:Nicolas.Chauvat@logilab.fr'/>
<dc:description>
The RIL versioning is not clear (there's a mix of 0.1, 0/1, 0.2
and 0/2)
</dc:description>
<dc:date>2001-04-20</dc:date>
<action rdf:parseType="Resource">
<dc:description>
Correct all to use the "0/1" form in the next draft.
</dc:description>
<it:assignee rdf:resource='mailto:uche.ogbuji@fourthought.com'/>
</action>
</Issue>
<rdf:Description rdf:about='mailto:Alexandre.Fayolle@logilab.fr'>
<foaf:name>Alexandre Fayolle</foaf:name>
</rdf:Description>
<rdf:Description rdf:about='mailto:uche.ogbuji@fourthought.com'>
<foaf:name>Uche Ogbuji</foaf:name>
</rdf:Description>
<rdf:Description rdf:about='mailto:Nicolas.Chauvat@logilab.fr'>
<foaf:name>Nicolas Chauvat</foaf:name>
</rdf:Description>
</rdf:RDF>
這裡我們定義了提出範例問題的那份資源。依 DAML+OIL 模式,https://rdfinference.org/ril/ril-20010502 會自動取得 RelevantResource 類的成員身分。另一處關鍵改動是:我們改用 mailto URL 來指向一個人,再由該 URL 透過 FOAF(Friend of a Friend)連結到對方的正式姓名。FOAF 是一套頗有名氣的 DAML+OIL 模式,專門描述「哪個信箱屬於誰」。順帶一提,RDF 世界裡還有另一條同樣知名的路可走,也就是把通訊錄資訊當作電子郵件附件嵌入的 vCard 格式所轉寫出的 vCard/RDF 模式。vCard RDF 的涵蓋面比 FOAF 更廣,但我們用不到它多出的那些特性。若真有需要,還有第三個選項 FOAFCorp,它在 FOAF 的核心個人檔案之上,加了與公司組織結構有關的元素。
產生畫面所需的 XSLT 變動很小,主要就是文字結果元素名稱與名稱空間 URI 的替換。
一般來說,即使你只想以寬鬆的方式套用上一篇談過的那些約束,手上也該有一份模式供文件遵循——哪怕別的用處都沒有。RDFS 依舊是最簡單、最普及的選擇;但 DAML+OIL 有許多可取之處,值得一試:除了多出的功能,它的核心語義也更清晰。既然問題追蹤程式已經有了模式,下一步我們來改進查詢的構造方式——我會介紹 Versa,一種適用於 RDF 的開放查詢語言,它能把目前所有的查詢程式碼寫得更短、跑得更快。