1. 程式人生 > >語義網技術(2):jena的使用——更多示例和程式碼分析(上,例子從例2-例5)

語義網技術(2):jena的使用——更多示例和程式碼分析(上,例子從例2-例5)

已經畢業了,論文也交了,總算輕鬆一點了,現在也準備把RDF相關知識和Jena的程式設計技術統一做個總結,寫個系列的部落格,一來是相當於給自己做個筆記,二來也是分享一些自己學到的東西,提供一些資源,以供大家共同學習。

這次準備把Jena示例中其他幾個程式的程式碼解釋一下,而後將簡述RDF的相關知識概念,再之後將進階OWL階段。

好了,廢話不多說了,上程式碼,上一篇博文為例1,這一篇博文從例2開始,另外為了簡化篇幅,例子之前的某些licenses就不粘帖了。

例2.

該例子實現了一個標準化的建立model的過程,首先例項化一個model,用ModelFactory的中createDefaultModel()的方法建立一個標準的空model,然後為該模型建立資源,待有了資源後對資源新增屬性以及屬性值,並且用了model.createlist的方法來輸出。

package jena.examples.rdf;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;

/**
 * Tutorial 2 resources as property values
 */
public class Tutorial02 extends Object {

	public static void main(String args[]) {
		// some definitions
		String personURI = "http://somewhere/JohnSmith";
		String givenName = "John";
		String familyName = "Smith";
		String fullName = givenName + " " + familyName;

		// create an empty model
		Model model = ModelFactory.createDefaultModel();

		// create the resource
		// and add the properties cascading style
		Resource johnSmith = model.createResource(personURI).addProperty(VCARD.FN, fullName)
				.addProperty(VCARD.N, model.createResource().addProperty(VCARD.Given, givenName).addProperty(VCARD.Family, familyName));
		System.out.println(model.createList());
	}
}

執行結果為:

http://www.w3.org/1999/02/22-rdf-syntax-ns#nil

例3.

package jena.examples.rdf ;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;


/** Tutorial 3 Statement attribute accessor methods
 */
public class Tutorial03 extends Object {
    public static void main (String args[]) {
    
        // some definitions
        String personURI    = "http://somewhere/JohnSmith";
        String givenName    = "John";
        String familyName   = "Smith";
        String fullName     = givenName + " " + familyName;
        // create an empty model
        Model model = ModelFactory.createDefaultModel();

        // create the resource
        //   and add the properties cascading style
        Resource johnSmith 
          = model.createResource(personURI)
                 .addProperty(VCARD.FN, fullName)
                 .addProperty(VCARD.N, 
                              model.createResource()
                                   .addProperty(VCARD.Given, givenName)
                                   .addProperty(VCARD.Family, familyName));
        
        // list the statements in the graph
        StmtIterator iter = model.listStatements();
        
        // print out the predicate, subject and object of each statement
        while (iter.hasNext()) {
            Statement stmt      = iter.nextStatement();         // get next statement
            Resource  subject   = stmt.getSubject();   // get the subject
            Property  predicate = stmt.getPredicate(); // get the predicate
            RDFNode   object    = stmt.getObject();    // get the object
            
            System.out.print(subject.toString());
            System.out.print(" " + predicate.toString() + " ");
            if (object instanceof Resource) {
                System.out.print(object.toString());
            } else {
                // object is a literal
                System.out.print(" \"" + object.toString() + "\"");
            }
            System.out.println(" .");
        }
    }
}
執行結果為:
http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#N -8efb898:14efd96f6a0:-7fff .
http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#FN  "John Smith" .
-8efb898:14efd96f6a0:-7fff http://www.w3.org/2001/vcard-rdf/3.0#Family  "Smith" .
-8efb898:14efd96f6a0:-7fff http://www.w3.org/2001/vcard-rdf/3.0#Given  "John" .

例4.

該例子實現了一個標準化的建立model的過程,首先例項化一個model,用ModelFactory的中createDefaultModel()的方法建立一個標準的空model,然後為該模型建立資源,待有了資源後對資源新增屬性以及屬性值,是自上向下的思路實現一個基本模型的過程。當然在OWL中有對RDF更深入的擴充套件,待這個RDF的系列完成之後我將會繼續寫一下OWL操作的實際過程和相關API的總結。

package jena.examples.rdf ;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;

/** Tutorial 4 - create a model and write it in XML form to standard out
 */
public class Tutorial04 extends Object {
    
    // some definitions
    static String tutorialURI  = "http://hostname/rdf/tutorial/";
    static String briansName   = "Brian McBride";
    static String briansEmail1 = "[email protected]";
    static String briansEmail2 = "[email protected]";
    static String title        = "An Introduction to RDF and the Jena API";
    static String date         = "23/01/2001";
    
    public static void main (String args[]) {
    
        // some definitions
        String personURI    = "http://somewhere/JohnSmith";
        String givenName    = "John";
        String familyName   = "Smith";
        String fullName     = givenName + " " + familyName;
        // create an empty model
        Model model = ModelFactory.createDefaultModel();

        // create the resource
        //   and add the properties cascading style
        Resource johnSmith 
          = model.createResource(personURI)
                 .addProperty(VCARD.FN, fullName)
                 .addProperty(VCARD.N, 
                              model.createResource()
                                   .addProperty(VCARD.Given, givenName)
                                   .addProperty(VCARD.Family, familyName));
        
        // now write the model in XML form to a file
        model.write(System.out);
    }
}

執行結果為:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > 
  <rdf:Description rdf:nodeID="A0">
    <vcard:Family>Smith</vcard:Family>
    <vcard:Given>John</vcard:Given>
  </rdf:Description>
  <rdf:Description rdf:about="http://somewhere/JohnSmith">
    <vcard:N rdf:nodeID="A0"/>
    <vcard:FN>John Smith</vcard:FN>
  </rdf:Description>
</rdf:RDF>

例5.

該例子實現RDF的讀寫操作,從路徑讀取一個RDF檔案並在控制檯實現標準輸出。

package jena.examples.rdf ;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.FileManager;

import java.io.*;

/** Tutorial 5 - read RDF XML from a file and write it to standard out
 */
public class Tutorial05 extends Object {

    /**
        NOTE that the file is loaded from the class-path and so requires that
        the data-directory, as well as the directory containing the compiled
        class, must be added to the class-path when running this and
        subsequent examples.
    */    
    static final String inputFileName  = "vc-db-1.rdf";
                              
    public static void main (String args[]) {
        // create an empty model
        Model model = ModelFactory.createDefaultModel();

        InputStream in = FileManager.get().open( inputFileName );
        if (in == null) {
            throw new IllegalArgumentException( "File: " + inputFileName + " not found");
        }
        
        // read the RDF/XML file
        model.read(in, "");
                    
        // write it to standard out
        model.write(System.out);            
    }
}
這裡的vc-db-1.rdf檔案並不存在,我隨便找了一個rdf檔案,把檔名改成了這個“vc-db-1.rdf”,inputFileName路徑修改了下。我放在了D盤根目錄下,所以路徑改為了“D://vc-db-1.rdf”。執行的結果如下為:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:mv="http://protege.stanford.edu/mv#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://protege.stanford.edu/mv#test3_INSTANCE_00003">
    <mv:registeredTo rdf:resource="http://protege.stanford.edu/mv#test3_INSTANCE_00004"/>
    <rdfs:label>test3_INSTANCE_00003</rdfs:label>
    <rdf:type rdf:resource="http://protege.stanford.edu/mv#Truck"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://protege.stanford.edu/mv#test3_INSTANCE_00004">
    <rdfs:label>test3_INSTANCE_00004</rdfs:label>
    <mv:name>Ora Lassila</mv:name>
    <rdf:type rdf:resource="http://protege.stanford.edu/mv#Person"/>
  </rdf:Description>
</rdf:RDF>

相關推薦

語義技術2jena的使用——示例程式碼分析例子2-5

已經畢業了,論文也交了,總算輕鬆一點了,現在也準備把RDF相關知識和Jena的程式設計技術統一做個總結,寫個系列的部落格,一來是相當於給自己做個筆記,二來也是分享一些自己學到的東西,提供一些資源,以供大家共同學習。這次準備把Jena示例中其他幾個程式的程式碼解釋一下,而後將簡

Flask10-項目部署02的朋友訪問你的裝逼利器

margin server tmm sax -s static solid -m 請求 項目部署 WEB工作原理 客戶端(chrom) <=> WEB服務器(nginx) <=> WSGI(uWSGI) <=> Python(Flas

Flask10-專案部署02的朋友訪問你的裝逼利器

專案部署 WEB工作原理 客戶端(chrom) <=> WEB伺服器(nginx) <=> WSGI(uWSGI) <=> Python(Flask) <=> 資料庫(MySQL) Flask框架自帶一個測試伺服器,開發時直接執行即可;但是在生成環境中,必須

18.C#VS2010之EF框架新增edmx自動跟蹤實體生成器對映到資料庫表程式碼實體類

在上一篇文章新增好EF資料庫模型的基礎上,為了生成表結構對應的標準類,使用自動跟蹤實體生成器,這裡主要記錄檔案命名注意事項 1.雙擊.edmx檔案,在介面的空白處滑鼠右擊,選中“新增程式碼生成項”,選擇“ADO.NET自跟蹤實體生成器”,會生成兩個.tt檔案 2.注意:其

語義技術1jena的使用——環境以及例項

大四畢設做關於語義網技術的研究,其中需要學習關於protege,jena等工具,以及owl和rdf的技術,網上的資料比較少,在這裡給自己做一個學習的記錄。關於jena和protege簡單說,寫個式子:(jena→java)    =    (protege→使用者)不難理解,

Chapter 2 User Authentication, Authorization, and Security9防止登錄名用戶查看元數據

eight ssms ini auto 情況 con title cas mar 原文出處:http://blog.csdn.net/dba_huangzj/article/details/39003679。專題文件夾:http://blog.csdn.net/dba_

《Linux學習並不難》歸檔壓縮2tar包的使用管理

linux tar 壓縮 22.2 《Linux學習並不難》歸檔和壓縮(2):tar包的使用和管理使用tar命令可以將許多文件一起保存到一個單獨的磁帶或磁盤歸檔,並能從歸檔中單獨還原所需文件。命令語法:tar [選項] [文件|目錄]命令中各選項的含義如表所示。選項 選項含義 -c 創建新的

Arm虛擬化效能構架分析2

微信公眾號 mindshare思享   如下圖所示,Xen和KVM採用不同的方式使用arm的硬體虛擬化支援。 Xen作為type1 hypervisor設計比較容易直接使用arm構架提供的功能,直接將hypervisor運行於EL2,將VM的users pace

spark2.2.0記錄一次資料傾斜的解決擴容join

前言: 資料傾斜,一個在大資料處理中很常見的名詞,經由前人總結,現已有不少資料傾斜的解決方案(而且會發現大資料的不同框架的資料傾斜解決思想是一致的,只是實現方法不同),本文重點記錄這次遇到spark處理資料中的傾斜問題。 老話: 菜雞一隻,本人會對文中的結論負責,如果有說錯的,還請各位批評指出

西部開源技術總結11月10-11月11日課程一

一、基礎部分 ①字型大小標籤:使用h1-h6可以對字型大小進行簡易更改。 西部開源 西部開源 西部開源 西部開源 西部開源 西部開源 ②格式化標籤:對字型進行加粗、下劃線、傾斜更改。 西部開源 西部開源 西部開源 西部開源 ③上下標標籤:更改標籤內文字標註形式。

學習MongoDB--2-1MongoDB入門(概念簡介啟動)

開始進入正式學習使用MongoDB的階段了,首先還是詳細介紹一下MongoDB的一些概念吧: 1》 文件:這個是MongoDB中資料的基本單元,非常類似於關係型資料庫的行,但比傳統行能表示的資訊複雜很多。 2》 集合:這個在MongoDB中代表一組文件,類似於關係型資料庫中

各種音視訊編解碼學習詳解之 編解碼學習筆記Mpeg系列——Mpeg 1Mpeg 2

    最近在研究音視訊編解碼這一塊兒,看到@bitbit大神寫的【各種音視訊編解碼學習詳解】這篇文章,非常感謝,佩服的五體投地。奈何大神這邊文章太長,在這裡我把它分解很多小的篇幅,方便閱讀。大神部落格傳送門:https://www.cnblogs.com/skyofbitbit

【H.264/AVC視訊編解碼技術詳解】十五、H.264的變換編碼H.264整數變換量化的實現

《H.264/AVC視訊編解碼技術詳解》視訊教程已經在“CSDN學院”上線,視訊中詳述了H.264的背景、標準協議和實現,並通過一個實戰工程的形式對H.264的標準進行解析和實現,歡迎觀看! “紙上得來終覺淺,絕知此事要躬行”,只有自己按照標準文件以程式碼

com4j學習2Visio自定義模具形狀並新增連線點

前言: 既然我們想繪製跟自己業務相關的圖形,並讀取Visio圖形中的結構資訊,那麼我們自然會想到要自定義圖形,本文詳細講解如何自定義圖形。 正文: 首先我們要明白什麼是模具,什麼是形狀,以及兩者之間的關係?模具就相當於一個容器,裡面有很多個形狀,我們可

OpenFastPath2原生態Linux Socket應用如何移植到OpenFastPath?

sed 調用 系統 解決 oid tcp客戶端 led color sizeof 版本信息: ODP(Open Data Plane): 1.19.0.2 OFP(Open Fast Path): 3.0.0 1、存在的問題 OpenFastPath作為一個開源的用戶態

Istio 運維實戰系列2讓人頭大的『無頭服務』-

本系列文章將介紹使用者從 Spring Cloud,Dubbo 等傳統微服務框架遷移到 Istio 服務網格時的一些經驗,以及在使用 Istio 過程中可能遇到的一些常見問題的解決方法。 ## 什麼是『無頭服務』? 『無頭服務』即 Kubernetes 中的 Headless Service。Servic

ActiveMQ19Consumer高級特性之獨有消費者Exclusive Consumer

consumer高級特性之獨有消費者(exclusive consumer)一、簡介Queue中的消息是按照順序被分發到consumers的。然而,當你有多個consumers同時從相同的queue中提取消息時,你將失去這個保證。因為這些消息是被多個線程並發的處理。有的時候,保證消息按照順序處理是很重要的。如

ActiveMQ20Consumer高級特性之重新投遞Redelivery Policy

jms activemq 重新投遞 一、簡介ActiveMQ在接收消息的Client有以下幾種操作的時候,需要重新傳遞消息: 1:Client用了transactions,且在session中調用了rollback() 2:Client用了transactions,且在調用commit()之前關閉

利用Metaweblog技術的API接口同步到個博客網站詳細

other pla 呵呵 博客 ice nload -1 china 簡單 很早就有這個想法:自己有時候會用到多個博客,有些博客在一個網站上寫完之後,要同步到其他博客網站,自己只能復制粘貼,感覺特別沒意思,復制粘貼的麻木了。一直在想有哪些技術能實現一次寫博,多站同步。最近網

python函數6內置函數匿名函數

a20 *args -s 執行 code str 思維導圖 inpu 其他 我們學了這麽多關於函數的知識基本都是自己定義自己使用,那麽我們之前用的一些函數並不是我們自己定義的比如說print(),len(),type()等等,它們是哪來的呢? 一、內置函數 由pytho