jena工具的使用-jena自带代码的阅读-1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangfengling1023/article/details/81911594

最近在学习知识图谱相关的知识点,而有一个工具jena会有教程代码,阅读了一下,特地将其中的前5个简单的阅读整理一下,在下面会一 一进行展示

1、Tutorial01.java 代码如下:

package jena.examples.rdf ;

import org.apache.jena.rdf.model.*;

import org.apache.jena.vocabulary.*;

/** Tutorial 1 creating a simple model

 */

public class Tutorial01 extends Object {

    // some definitions  定义一些名字

    static String personURI    = "http://somewhere/JohnSmith";

    static String fullName     = "John Smith";

      public static void main (String args[]) {

        // create an empty model  建立一个空的模型

        Model model = ModelFactory.createDefaultModel();

       // create the resource 在模型中创建一个资源

       Resource johnSmith = model.createResource(personURI);

      // add the property  添加属性

      johnSmith.addProperty(VCARD.FN, fullName);

      }

}

 

先创建一个模型,在模型中创建资源,在资源中添加属性,添加属性的同时进行属性值的给定

这个例子中,资源 http://.../JohnSmith 表示一个人。这个人的全名是 John Smith,即 vcard:FN 属性的值是 John Smith。

在Jena中,资源用 Resource类来表示,其属性用Property类来表示。而整体模型用Model类来表示,即上图就是一个Model。一个Model对象可以包含多个资源。

ModelFactory类是一个Model工厂,用于创建model对象

使用Model的createResource方法在model中创建一个资源

使用资源的addProperty方法添加属性

 

2、Tutorial02.java 代码如下:

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));

        

      }

}

在资源中创建了一个资源

如资源http://.../JohnSmith 有一个vcard:FN 属性,其值是文本"John Smith ”。这个资源还有一个 vcard:N 属性,这个属性的值是另一个无名资源。该无名资源有两个属性,分别是 vcard:Given 和 vcard:Family。其值分别是文本的"John" 和 "Smith"。

3、Tutorial03.java 代码如下:

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();

//Jena 的 ModelFactory类是创建不同类型模型的首选方式

 

        // 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(" .");

        }

    }

}

Model 的每个箭头都是一个陈述(Statement)。Statement 由三部分组成,分别是主语、谓语和客体。

(1)主语:上图中箭头出发的位置,代表资源

(2)谓语:上图中的箭头,代表资源的属性

(3)客体:可以理解成宾语,代表属性的值,它可以是文本,也可以是一个资源

Model 类的listStatements 将返回一个Statement的Iterator。Statement有的主语、谓语、客体分别用getSubject、getPredicate、getObject来返回。其类型分别是Resource、Property和RDFNode。其中客体 object 类型可以是Resource 或者文本。

运行之后的结果如下所示:

4、Tutorial04.java 代码如下:

public class Tutorial04 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));

        // now write the model in XML form to a file

        model.write(System.out);

//        System.out.println();

    }

}

创建的model与第三个教程是一样的,可以通过model的write方法将模型中的内容写入一个输出流

运行的结果如下所示:

5、Tutorial05.java 代码如下:

public class Tutorial05 extends Object {

static final String inputFileName  = "lizi.rdf";              

    public static void main (String args[]) {

        // create an empty model

        Model model = ModelFactory.createDefaultModel();

        // 使用 FileManager 查找文件

        InputStream in = FileManager.get().open(Tutorial05.class.getResource("").getPath()+inputFileName );

        if (in == null) {

            throw new IllegalArgumentException( "File: " + inputFileName + " not found");

        }

        // read the RDF/XML file

        model.read(in, "");

 //Model 的read 方法可以读取RDF 输入到model 中。第二个参数可以指定格式。     

        // write it to standard out

        model.write(System.out);     

    }

}

Model 的read 方法可以读取RDF 输入到model 中。第二个参数可以指定格式。

运行的结果如下所示:

 

后续6-11的代码会持续更新,有同道者可以互相交流

 

 

 

猜你喜欢

转载自blog.csdn.net/yangfengling1023/article/details/81911594