Jena 操作 RDF 文件


1. RDF 入門

  • RDF(Resource Description Framework)是由W3C規定的,描述資源(resource)的數據模型(data model),;
  • RDF 使用Web標識符來標識事物,並通過屬性和屬性值來描述資源;
    • 資源:可擁有URI的任何事物,如:http://localhost:8080/JohnSmith
    • 屬性:擁有名稱的資源,如:人的全名(FullName),職位等;
    • 屬性值:某個屬性的值,如:JohnSmith;

/**
 * 使用Jena表示上圖
 */

// 定義
static String personURI = "http://localhost:8080/JohnSmith";
static String fullName = "John Smith";

// 創建空的Model, 即圖(Graph)
Model model = ModelFactory.createDefaultModel();

// 創建資源
Resource johnSmith = model.createResource(personURI);

// 添加屬性
johnSmith.addProperty(VCARD.FN, fullName);

/**
 * 或者
 */
 Resource johnSmith = model.createResource(personURI)
                           .addProperty(VCARD.FN, fullName);

/**
 * 更加復雜的圖形表示(資源表示中存在空節點)
 */

public class Tutorial2{
    public static void main(String[] args){
        String personURI = "http://localhost:8080/JohnSmith";
        String givenName = "John";
        String familyName = "Smith";
        String fullName = givenName + " " + familyName;

        // 創建Model
        Model model = ModelFactory.createDefaultModel();

        // 創建Resource,並添加屬性
        Resource johnSmith = model.createResource(personURI)
                                  .addProperty(VCARD.FN, fullName)
                                  .addProperty(VCARD.N, 
                                        model.createResource()
                                             .addProperty(VCARD.Given, givenName)
                                             .addProperty(VCARD.Family, familyName));
    }
}

2. RDF Statements(RDF 陳述)

  • 資源,屬性和屬性值的組合可形成一個陳述;
  • 陳述(Statement)包括:
    • subject(主體)
    • predicate(謂語)
    • object(客體)
/**
 * 使用Statement,讀取RDF內容(使用上面的代碼)
 */
 
 StmtIterator iter = model.listStatements();
 while(iter.hasNext()){
     // 打印 subject,predicate, object
     Statement stmt = iter.nextStatement();
     Resource subject = stmt.getSubject();
     Property predicate = stmt.getPredicate();
     RDFNode object = stmt.getObject();

     System.out.print(subject.toString());
     System.out.print(" " + predicate.toString() + " ");
     if(object instanceof Resource){
         // 如果為 資源
         System.out.print(object.toString());
     } else {
         // 如果為文本
         System.out.print(" \"" + object.toString() + "\"");
     }

     System.out.println(" .");
 }

 // 上述代碼,可以簡寫為: model.write(System.out, "N-TRIPLES");


 ### 輸出結果:
735a32cc-f7a3-4be5-b70f-9689fcd5a4b4 http://www.w3.org/2001/vcard-rdf/3.0#Family  "Smith" .
735a32cc-f7a3-4be5-b70f-9689fcd5a4b4 http://www.w3.org/2001/vcard-rdf/3.0#Given  "John" .
http://localhost:8080/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#N 735a32cc-f7a3-4be5-b70f-9689fcd5a4b4 .
http://localhost:8080/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#FN  "John Smith" .

3. RDF 的寫和讀

/**
 * RDF XML 格式輸出
 */
 public class Tutorial2{
    public static void main(String[] args){
        String personURI = "http://localhost:8080/JohnSmith";
        String givenName = "John";
        String familyName = "Smith";
        String fullName = givenName + " " + familyName;

        // 創建Model
        Model model = ModelFactory.createDefaultModel();

        // 創建Resource,並添加屬性
        Resource johnSmith = model.createResource(personURI)
                                  .addProperty(VCARD.FN, fullName)
                                  .addProperty(VCARD.N, 
                                        model.createResource()
                                             .addProperty(VCARD.Given, givenName)
                                             .addProperty(VCARD.Family, familyName));
        
        // RDF xml 格式輸出
        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:about="http://localhost:8080/JohnSmith">
    <vcard:N rdf:parseType="Resource">
      <vcard:Family>Smith</vcard:Family>
      <vcard:Given>John</vcard:Given>
    </vcard:N>
    <vcard:FN>John Smith</vcard:FN>
  </rdf:Description>
</rdf:RDF>


/**
 * 讀取 RDF/XML 格式的文件
 *    下載地址:http://jena.apache.org/tutorials/sparql_data/vc-db-1.rdf
 */
 // 創建空Model
 Model model = ModelFactory.createDefaultModel();

 // 使用FileManager,獲取輸入流
 String inputFileName = "";
 InputStream in = FileManager.get().open(inputFileName);
 if(in == null){
     throw new IllegalArgumentException(
         "File: " + inputFileName + " not found.");
     )
 }

 // 讀取 RDF/XML 文件
 model.read(in, null);

 // 將讀取的內容,輸出到控制台
 model.write(System.out);

4. 操作Model

/**
 * 獲取Model中存儲的信息(接上例)
 */

// 如果存在,直接返回;不存在,則新創建一個,再返回
String johnSmithURI = "http://somewhere/JohnSmith";
Resource vcard = model.getResource(johnSmithURI);

// 獲取資源的屬性(該屬性可能為資源,或者文本)
// 若為資源:
Resource name = vcard.getProperty(VCARD.N)
                     .getResource();

// 若為文本:
String fullName = vcard.getProperty(VCARD.FN)
                       .getString();

// 如果存在多個同名屬性:
StmtIterator iter = vcard.listProperties("屬性名");
while(iter.hasNext()){
    System.out.println(iter.nextStatement()
                           .getObject()
                           .toString());
}


/**
 * 根據屬性(Property)搜索Model(接上例)
 */

// 搜索屬性名為:VCARD.FN
ResIterator iter = model.listSubjectsWithProperty(VCARD.FN);
if(iter.hasNext()){
    System.out.println("The database contains vcards for:");
    while(iter.hasNext()){
        System.out.println("  " + iter.nextResource()
                                      .getProperty(VCARD.FN)
                                      .getString());
    }
} else {
    System.out.println("No vcards were found in the database.");
}


// 精細化搜索:屬性名為 VCARD.FN, 且屬性值以 “Smith” 結尾
// model.listStatements(Selector s)
// Selector selector = new SimpleSelector(subject, predicate, object);
StmeIterator iter = model.listStatements(
    new SimpleSelector(null, VCARD.FN, (RDFNode)null){
        public boolean selects(Statement s){
            return s.getString().endsWith("Smith");
        }
    }
);

5. RDF 集合(Container)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM