语义网技术:jena的使用(2)——更多示例和代码分析(上,例子从例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>

你可能感兴趣的:(jena,语义网)