Introduction to RDF and Jena RDF APIs (1)

Source: Internet
Author: User
Tags vcard

Reprinted from http://www.cnblogs.com/yuyd902/archive/2008/10/08/1306647.html

This article is a translation of "An Introduction to RDF and the Jena rdf api", which is annotated in appropriate places. Some Code provides jruby implementation.

Preface
 
This article is a tutorial on W3C Resource Description Framework (RDF) and Jena (a Java rdf api. This article is intended for programmers who are not familiar with RDF and who can achieve the best learning performance by establishing a prototype, or who want to quickly operate Jena for other reasons. We assume that the reader has some XML and Java knowledge before reading this article.

If the reader quickly enters the operation stage without understanding the RDF data model, it will often lead to failure and disappointment. However, learning data models by light is boring and often leads to tortuous metaphysical difficulties. A better way to learn is to practice the data model while understanding it. You can first learn a little data model and then try it again, so that you can achieve the effect of combining theoretical practices. The data model itself is very simple, so the learning process is not too long.

RDF has XML syntax, so many people who are familiar with XML will think of using XML syntax to think about RDF. However, this is not true. RDF should be understood in the form of its data model. RDF data is expressed in XML, but the importance of understanding the data model is more important than understanding the syntax importance.

A running example of Jena API, including the working source code of all examples in this tutorial, can be downloaded at http://www.hz.hp.com/semweb.

Introduction
 
The Resource Description Framework (RDF) is a standard for describing resources (technically W3C recommendation standards ). What is a resource? This is really a difficult question to answer, and its precise definition is still under debate. For our purposes, we can think of resources as anything we can identify. In this tutorial, you are a resource, and your home page is also a resource. Numbers 1 and the huge whale in the story are resources.

In this tutorial, our example is centered around people. Assume that people will use vcards (vCard is called an e-commerce card, which is mainly used to record the contact information of the Communication thin, and exchange data between different devices-from Baidu Zhidao ), vCard is described by the RDF representation mechanism. We 'd better consider RDF as a graph composed of nodes and arrows. A simple vCard may look like this in RDF:


Resource John Smith is represented by an ellipse in the figure and identified by a uniform resource identifier (URI). In this example, it is "http: //.../johnsmith ". If you want to access this resource through your browser, you may fail. The Fool joke of April cannot stand the test. On the contrary, if your browser passes John Smith to your desktop, you should be surprised. If you are not familiar with Uri's, you can think of them as simple and unfamiliar names.

Resource property ). In these examples, we are very interested in the attributes on John Smith's business card. Figure 1 shows only one attribute, John Smith's full name. An attribute is represented by an arrow with a property name. The attribute name is also a URI, but the URI is very lengthy and bulky, so it is displayed as xml qname in the figure. The Section before ":" is called the namespace prefix and represents a namespace. The part after ":" is called a local name and represents a name in the namespace. When writing in the form of rdf xml, attributes are often expressed in the form of QNAME, which is a simple abbreviated method in graphics and text. However, strictly speaking, attributes should be identified by Uris. Namespace Prefix: A local name is abbreviated as a URI for connecting a namespace to a local name. When accessing a browser, a URI with no mandatory attributes must point to specific things.

Each attribute has a value. In this example, the value is a text (literal). Now we can regard it as a string. The text is displayed as a rectangle in the graph.

Jena is a Java API that we can use to create and manipulate the RDF diagram of the preceding example graph. Jena has an object class that represents graphs, resources, attributes, and texts. Resource. The property and text interfaces are called resource, property, and literal. In Jena, a graph is called a model and expressed by the model interface.

The code for creating the above example graph or the above model is simple:

// Java code
// Some definitions
Static string personuri = "http: // somewhere/johnsmith ";
Static string fullname = "John Smith ";
 
// Create an empty Model
Model = modelfactory. createdefaultmodel ();
 
// Create the resource
Resource johnsmith = model. createresource (personuri );
 
// Add the property
Johnsmith. addproperty (vCard. FN, fullname );

 

# Jruby code
Require 'java'

Module Java
Include_package 'com. HP. hpl. Jena. RDF. model'
Include_package 'com. HP. hpl. Jena. Vocabulary'

Personuri = "http: // somewhere/johnsmith"
Fullname = "John Smith"

Model = Java: modelfactory. createdefaultmodel

John_smith = model. createresource personuri

John_smith.addproperty vCard: FN, fullname
End


These codes first define some constants, and then use the createdefaultmode () method in the modelfactory class to create an empty memory-based model (model or model ). Jena also includes other implementation methods for the model interface. For example, for relational databases, these model interfaces can also be created from modelfactory.

John Smith is created and an attribute is added to the resource. This attribute is provided by a "constant" class vCard, which stores all the defined representation objects in the vCard schema. Jena also provides common class Representation Methods for some other famous models, such as the RDF and RDF modes, Dublin Core Standards, and DAML.

Code for creating resources and adding properties can be written in a more compact cascade mode:

// Java code
Resource johnsmith =
Model. createresource (personuri)
. Addproperty (vCard. FN, fullname );

 

# Jruby code
John_smith = \
Model. createresource (personuri )\
. Addproperty (vCard: FN, fullname)


The working code of this example can be found in tutorial1 In the tutorial package of the materials released by Jena. As an exercise, you can obtain this code and modify it to create a simple vCard.

Now let's add more details to vCard to explore more features of RDF and Jena.

In the first example, the attribute value is a text. However, other resources can also be used as the attribute values for the RDF attributes. The following example uses the commonly used RDF technology to show how to express different parts of John Smith's name:


Here we add a new attribute, vCard: N, to represent the structure of John Smith's name. This model has some interesting points. Note attribute vCard: N uses a resource as the starting property value. Note that the elliptic that represents the compound name does not have a URI identifier. It is considered as a blank node ).

The Jena code for creating this example is also very simple. First, there are some declarations and the creation of empty models.

// Java code
// Some definitions
String personuri = "http: // somewhere/johnsmith ";
String givenname = "John ";
String familyname = "Smith ";
String fullname = givenname + "" + familyname;
 
// Create an empty 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 ));

 

# Jruby code
Require 'java'

Module Java
Include_package 'com. HP. hpl. Jena. RDF. model'
Include_package 'com. HP. hpl. Jena. Vocabulary'

Personuri = "http: // somewhere/johnsmith"
Givenname = "John"
Familyname = "Smith"
Fullname = givenname + "" + familyname

Model = Java: modelfactory. createdefaultmodel

John_smith = \
Model. createresource (personuri )\
. Addproperty (vCard: FN, fullname )\
. Addproperty (vCard: N ,\
Model. createresource ()\
. Addproperty (vCard: Given, givenname )\
. Addproperty (vCard: family, familyname ))
End


The working code of this example can be obtained in tutorial of the tutorial package of Jena release material.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.