顯示具有 Semantic Web 標籤的文章。 顯示所有文章
顯示具有 Semantic Web 標籤的文章。 顯示所有文章

The Future of Web

由於我的研究領域牽涉到了本體論(Ontology)
也就是Tim ‎Berners-Lee(2004)提出的語意網Layer Cake中的一個部分:


因此我略懂RDF也是很正常的。
就像身為一個汽車維修員,隨身攜帶工具也是很正常的一樣XD
所以以下此篇將以語意網的角度淺談Web的未來。

一開始,讓我們先來了解一下Web 1.0 2.0與3.0的不同:

分類(Web)
描述
1.0

沒有互動的機制,所有的資訊都是單向的流通。
2.0

最主要的精神是可以進行協作與共享,模範生有Wikipedia,Facebook等等。
「Anyone can say Anything on Any topic」這個AAA Slogan[1]也將其精神表現得淋漓盡致。
3.0
根據維基百科的定義:
「該詞包含多層含義,用來概括網際網路發展過程中可能出現的各種不同的方向和特徵,包括:將網際網路本身轉化為一個泛型資料庫;跨瀏覽器、超瀏覽器的內容投遞和請求機制;人工智慧技術的運用;語意網。」
※泛型資料庫:我認為是與Linked Data相關概念,請參考Vcard。


語意網中有一層標準和現在的HTML標準很接近,但是又有點些許的不同。
這層便是RDF。(在Semantic Web Layer Cake中Ontology的下層)
和HTML同為XML檔案,且都是W3C定義的標準,其簡介如下:

RDF 語言是 W3C 的語意網活動的组成部分。W3C 的「語意網展望(Semantic Web Vision)」的目標是:
  • Web 資訊擁有精確的涵義 
  • Web 資訊可以被電腦理解並處理 
  • 電腦可以從 Web 上整合資訊

以下是RDF的範例代碼。在RDF中,所有的標籤皆可以是自訂的:
<?xml version="1.0"?>
<RDF>
  <Description about="http://www.w3school.com.cn/RDF">
    <author>David</author>
    <homepage>http://www.w3school.com.cn</homepage>
  </Description>
</RDF>
而從HTML4、HTML5甚至到HTML6來看
其實對於自定義標籤的限制是越來越寬了
從一開始的head、body,到後來還有header、footer等標籤
某種程度看來也是在幫助網頁本身的資訊能夠擁有更精確的涵義

就現在來說,我並不確定我們屬於Web的2.0、2.3、2.5的哪個階段
但可以確定的是,我們已經慢慢地朝著Web 3.0的方向前進
至於4.0是什麼樣的世界 真的很難以想像......

References,


[1] Semantic Web for the Working Ontologist. D.Allemang & J.Hendler, 2008.

Jena常用指令

資訊來自Jena javadoc,我將比較常用的翻譯一下
如果沒翻譯的可能就是比較不重要的,或是我現在用不到的。

Model

  • add

    Model add(Resource s, Property p, RDFNode o)
    對目前的Model新增一個statement(triple)。
    Parameters:
    s - the subject of the statement to add
    p - the predicate of the statement to add
    o - the object of the statement to add
    Returns:
    this model
  • createProperty

    Property createProperty(String nameSpace, String localName)
    Create a property with a given URI composed from a namespace part and a localname part by concatenating the strings.This method may return an existing property with the correct URI and model, or it may construct a fresh one, as it sees fit.
    Subsequent operations on the returned property may modify this model.
    Parameters:
    nameSpace - Property的命名空間。
    localName - 你幫該property取的名字。
    Returns:
    a property instance

Resouce

  • addProperty

    Resource addProperty(Property p, String o)
    對Resource(subject or object)建立property與object,成立一個三元關係(triple)。
    Parameters:
    p - 欲新增之property。
    o - property(predicate)所連接的object。
    Returns:
    This resource to allow cascading calls.
  • getProperty

    Statement getProperty(Property p)
    回傳和目前這個resource相關的property與object,若property p是property q的子類別,也會被return。
    Parameters:
    p - 欲查詢之property。
    Returns:
    回傳和目前這個resource相關的property與object,若property p是property q的子類別,也會被return。沒有statement的話會回傳null。

Jena SPARQL

SPARQL是用來查詢RDF graph的一個語法。
RDF graph就是一大堆triple的組成
在Jena中所提到的model,也就是RDF graph,statement就是triples
RDF dataset中則包含了更多的graph

SPARQL設定方面

command Line的部分 使用cd C:\位址 跳槽
若發生JENA_HOME not set
就是環境變數沒有設定
1.先echo %PATH%,看看目前有沒有紀錄到
2.沒有的話入PATH=C:\Program Files\Java\(JDK資料夾)\bin;%PATH%
3.完成!!!!!!!!!!!!!

no query string or query file
bat\sparql.bat --data=vc-db-1.rdf --query=q1.rq
把兩個檔案放到同目錄就好了

基本用法

SELECT ?欄位
WHERE
  {
      條件(必須是triple)
  }

String Matching

{FILTER regex(?g, "r", "i") }  "r"是你想查詢的字串

Testing Values

SELECT ?resource
WHERE
  {
    ?resource info:age ?age .
    FILTER (?age >= 24)          使用FILTER(條件)
  }

OPTIONAL(IF)

SELECT ?name ?age
WHERE
{
    ?person vcard:FN  ?name .
    OPTIONAL { ?person info:age ?age }
}
!bound(?age) 這是一個unbound的動作,意即不符合結果的資料也會列出

在prefix前面有小老鼠的可能是n3格式,把rdf改成n3就可以解碼,如
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

UNION

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?name
WHERE
{
   { [] foaf:name ?name } UNION { [] vCard:FN ?name }
}
其實sparql的語法很free,也可以修改成這樣
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?name
WHERE
{
  [] ?p ?name
  FILTER ( ?p = foaf:name || ?p = vCard:FN )
}
如果把第一個例子的?name改成?name1與?name2,那結果可以更明顯的顯示
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?name1 ?name2
WHERE
{
   { [] foaf:name ?name1 } UNION { [] vCard:FN ?name2 }
}

---------------------------------
| name1         | name2         |
=================================
| "Matt Jones"  |               |
| "Sarah Jones" |               |
|               | "Becky Smith" |
|               | "John Smith"  |
---------------------------------

Datasets

輸入sparql --help查看全部指令,可以看到 
Dataset
     --data=FILE            Data for the datset - triple or quad formats
     --graph=FILE           Graph for default graph of the datset(指定dataset中的預設graph)
     --namedGraph=FILE      Add a graph into the dataset as a named graph(對graph命名,並且加入dataset)
列出所有資料,語法sparql --graph ds-dft.ttl --namedgraph ds-ng-1.ttl --named graph ds-ng-2.ttl --query xxxxx.rq
PREFIX  xsd:    <http://www.w3.org/2001/XMLSchema#>
PREFIX  dc:     <http://purl.org/dc/elements/1.1/>
PREFIX  :       <.>

SELECT *
{
    { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } }
}
另外用法,注意GRAPH後面一定要先留空格
PREFIX  xsd:    <http://www.w3.org/2001/XMLSchema#>
PREFIX  dc:     <http://purl.org/dc/elements/1.1/>
PREFIX  :       <.>

SELECT *
{
    { ?s ?p ?o } UNION { GRAPH :ds-ng-1.ttl { ?s ?p ?o } } 列出和ds-ng-1.ttl聯集資料
}
也可以把指令改寫成--query xxxxx.rq 只是要把FROM與FROM NAMED加入取代--graph與--namedgraph
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc:  <http://purl.org/dc/elements/1.1/>
PREFIX :    <.>

SELECT *
FROM       <ds-dft.ttl>
FROM NAMED <ds-ng-1.ttl>
FROM NAMED <ds-ng-2.ttl>
{
   { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } }
}
也可以combo FILTER來使用
PREFIX  xsd:    <http://www.w3.org/2001/XMLSchema#>
PREFIX  dc:     <http://purl.org/dc/elements/1.1/>
PREFIX  :       <.>

SELECT ?date ?title
{
  ?g dc:date ?date . FILTER (?date > "2005-08-01T00:00:00Z"^^xsd:dateTime )
  GRAPH ?g
      { ?b dc:title ?title }
}

Manipulating SPARQL using ARQ

ARQ使用JAVA的方式來操作SPARQL(施工中)

References:


SPARQL Tutorial

Tutorial - Manipulating SPARQL using ARQ

Jena RDF Core API

Jena是使用Java編寫的API,可以用來做本體論的新增、查詢、修改與推論。

若還沒有建立Jena環境,可以參考如何在Eclipse中建立第一個Jena專案

以下在Jena中所提到的model、statement分別可以對應到RDF graph與triple。triple(statement)通常以名詞──動詞──名詞(主詞subject、謂詞predicate、受詞object)的方式表示,例如

Shakespear write Camelot

graph則是一大堆triple的集合。

簡單操作

1.建立Model
從ModelFactory中創造預設的Model
Model model = ModelFactory.createDefaultModel();

2.從Model中建立Resource
Resource johnSmith = model.createResource(personURI);

3.對resource增加property
//addProperty(Property p, RDFNode o)
johnSmith.addProperty(VCARD.FN, fullName);



把所有的triple列出來

1.使用Statement Iterator
StmtIterator iter = model.listStatements();

2.在while迴圈中建立Statement
Statement stmt      = iter.nextStatement(); 

3.抓出getter
Resource  subject   = stmt.getSubject();     // get the subject
Property  predicate = stmt.getPredicate();   // get the predicate
RDFNode   object    = stmt.getObject();      // get the object

4.也可以直接輸出XML格式
model.write(System.out);

Triple-annotation格式
model.write(System.out, "N-TRIPLES");

讀取rdf檔案

1.建立Model
Model model = ModelFactory.createDefaultModel();

2.使用FileManager打開檔案
 InputStream in = FileManager.get().open( inputFileName );

3.列印XML
model.read(in, null);
model.write(System.out);

P.S.:找不到vc-db-1.rdf? 放在和你放jena資料夾一樣的地方就好囉

設定前置命名空間

1.建立Model
Model model = ModelFactory.createDefaultModel();

2.設定前置
 String nsA = "http://somewhere/else#";
 String nsB = "http://nowhere/else#";

3.產生resources, properties,
 Resource root = m.createResource( nsA + "root" );//類似○,代表subject與object 
 Property P = m.createProperty( nsA + "P" );      //類似→,代表predicate
 Resource x = m.createResource( nsA + "x" );      //類似○,代表subject與object

4.將resource與property間的關係建立起來
 m.add( root, P, x );

5.查看結果
 m.write( System.out );

PS.:有沒有m.setNsPrefix的差異如下:
<j.1:P rdf:resource="http://somewhere/else#x"/> //no set
<nsA:P rdf:resource="http://somewhere/else#x"/> //set

m.read("location")
http://stackoverflow.com/questions/15782656/reading-local-owl-file-using-jena-response-filenotfoundexception

抽取URI

1.新增一個Resource, 存放等一下要retrive的resource
Resource vcard = model.getResource("http://somewhere/JohnSmith/");;

2.對Resource使用getProperty,把資料抽出來
String fullName = vcard.getProperty(VCARD.FN).getString();

PS.:這邊getString會直接輸出資料值,使用toString會輸出一整串XD

3.可以對取出的Resource增加資料
vcard.addProperty(VCARD.NICKNAME, "Smithy")

4.使用StmtIterator 來把所有的property列出來
StmtIterator iter = vcard.listProperties(VCARD.NICKNAME);

查詢Model

這邊是說如果什麼事情都用model.listStatement()來做也就太累了
1.所以使用ResIterator來代替Resource的查詢,找出有VCARD:FN屬性的資源
ResIterator iter = model.listSubjectsWithProperty(VCARD.FN);

2.進入if 迴圈 iter.hasNext print 
if (iter.hasNext()) {
    System.out.println("The database contains vcards for:");
    while (iter.hasNext()) {
        System.out.println("  " + iter.nextResource()
                                      .getProperty(VCARD.FN)
                                      .getString());
    }

有另外的方法,使用new SimpleSelector

1.再seletor中分別填入S, P, O
StmtIterator iter = model.listStatements(new SimpleSelector(null, VCARD.FN, (RDFNode) null)

操作Model

關於union, intersection與difference的操作
Model model = model1.union(model2);

容器(container)

關於容器,有三種,分別是BAG, ALT, SEQ
BAG是沒有規則隨便亂放的
ALT雖然也是沒有規則,但是基本上用來放替代品
SEQ是有規則的


Reference:

An Introduction to RDF and the Jena RDF API

Reflection of Jena Documentation

Jena官方網站
An Introduction to RDF and the Jena RDF API
SPARQL Tutorial

Jena是一個用來開發語意網(Semantic Web)與連結資料(Linked Data)的Java API,支援對RDF的CRUD、查詢(Query)與推論(Inference)。

官方的教學中牽涉到幾種編碼與檔案格式,如RDF(S)、N-Triple

RDF格式和HTML看起來差不多,只是它屬性的值前後都得以它的屬性名稱包裝起來:
<fruit>banana</fruit>

N-Triple直接顯示triple的關係,不像RDF呈現的方式那樣有階層的排序:
<http://.../monkey><http://.../eat><http://.../banana>

triple是以subject, predicate, object所組成,可以聯想成英文中的主詞、動詞與受詞。

Jena可以透過SPARQL來進行查詢,有兩種檔案格式:.rq & .n3
兩者最大的差別在於PREFIX前面有沒有加@,所以按照官方教學進行SPARQL指令出現錯誤時,可以試著修改檔案格式rq為n3試試看。

Getting Started with Protégé-Frames

以下則是看完所得到的重點:

Create Class

Class的Role分為Concrete與Abstract
移動Class時,先按滑鼠再按Ctrl便可以複製。

Create Slots

Slots的命名規則:全部使用小寫,並且使用底線來分隔它們。相較於用大寫開頭的Class,這種方式可以讓我們更容易分辨。
在Protege中,我們可以按下工作列上的Help/ Icons來查看所有Icon所代表的意思。

要在Class標籤中的Class修改Slot時,會出現Select Slot View的視窗,包含以下選項:
View top-level slot 修改最上層的slot,也就是對slot的修改會套用到所有的slots。
View slot at class   只修改目前的slot,不會對其他slots造成影響。

Create Instance

新增Instance的時候可能會看到Instance Browser的視窗顯示的都是預設名稱
這時候只要從倒三角的icon給他按下去選擇set Display Slot,選擇Name後就好啦

Customize a Form

想找出隱藏的欄位,要從最左邊的Form Browser視窗點選放大鏡
按叉叉會自動排序

References
Getting started with Protege Frames