Introduction to RDF and Jena RDF APIs (5)

Source: Internet
Author: User
Tags vcard

Reproduced http://www.cnblogs.com/yuyd902/archive/2008/10/08/1306652.html

Container

RDF defines a special type of resource to represent a collection of things. These resources are called containers. A container can be a resource or text member. There are three types of containers:

A bag is an unordered set.
An ALT is an unordered set used to indicate the standby options.
A seq is an ordered set.

A container is represented by a resource. The resource has an RDF: Type attribute, whose attribute value is "RDF: Bag", "RDF: Alt", "RDF: seq", or "sub-types", depending on the container type. The first member of the container is the attribute value corresponding to the container's RDF: _ 1 attribute; the second member is the value of the container's RDF: _ 2 attribute, and so on. These RDF: _ NNN attributes are called ordinal attributes.

For example, a simple bag container model with Smith's vCard may look like this:


Although the members of the bag container are represented by attributes such as RDF: _ 1 and rdf_2, the order of these attributes is not important. Even if we exchange the values of RDF: _ 1 and RDF: _ 2, the exchanged model still indicates the same information.

ALT is designed to indicate that it is selected. For example, assume that there is a resource that represents a software product. It may have a property indicating where the next software product can be obtained. This attribute value may be a set of ALT types. ALT is unordered. Except for the RDF: _ 1 attribute, ALT indicates the default option.

Although we can use basic resource and attribute mechanisms to process containers, Jena has designed explicit interfaces and execution classes for processing containers. Avoid using lower-Layer Methods to change the container while using an object to operate the container.

Let's modify tutorial8 to create a bag:

// Java code
// Create a bag
Bag Smiths = model. createbag ();

// Select all the resources with a vCard. FN Property
// Whose value ends with "Smith"
Specified titerator iter = model. liststatements (
New simpleselector (null, vCard. FN, (rdfnode) null ){
Public Boolean selects (statement S ){
Return S. getstring (). endswith ("Smith ");
}
});
// Add the Smith's to the bag
While (ITER. hasnext ()){
Smiths. Add (ITER. Next (). getsubject ());
}

 

# Jruby code
Smiths = model. createbag

Iter = model. liststatements (
Class. New (Java: simpleselector ){
Def selects s
Java. Lang. String. New (S. getstring). endswith 'Smith'
End
}. New (nil, vCard: FN, nil ))

While ITER. hasnext
Smiths. Add ITER. nextstatement. getsubject
End


If we output the model, we can see that it contains components similar to the following:

<RDF: RDF
Xmlns: RDF = 'HTTP: // www.w3.org/5o/02/22-rdf-syntax-ns #'
Xmlns: vCard = 'HTTP: // www.w3.org/2001/vcard-rdf/3.0hangzhou #'
>
...
<RDF: Description RDF: nodeid = "A3">
<RDF: Type RDF: Resource = 'HTTP: // www.w3.org/5o/02/22-rdf-syntax-ns#bag'/>
<RDF: _ 1 RDF: Resource = 'HTTP: // somewhere/johnsmith/'/>
<RDF: _ 2 RDF: Resource = 'HTTP: // somewhere/rebeccasmith/'/>
</RDF: Description>
</RDF: RDF>


This indicates the bag resource.

The container interface provides an iterator to list container content:

// Java code
// Print out the members of the bag
Nodeiterator iter2 = Smiths. iterator ();
If (iter2.hasnext ()){
System. Out. println ("the bag contains :");
While (iter2.hasnext ()){
System. Out. println ("" +
(Resource) iter2.next ())
. Getproperty (vCard. FN)
. Getstring ());
}
} Else {
System. Out. println ("the bag is empty ");
}

 

# Jruby code
Iter2 = Smiths. iterator

If iter2.hasnext
Puts 'The bag contains :'

While iter2.hasnext
Puts model \
. Getresource (iter2.next. getstring )\
. Getrequiredproperty (vCard: FN). getstring
End
Else
Puts 'The bag is empty'
End
End


The following output is generated:
The bag contains:
John Smith
Becky Smith

Some minor problems have occurred here. Normally, jruby does not require type conversion. However, if the following code is used directly:

# Jruby code
Puts iter2.next. getrequiredproperty (vCard: FN). getstring


But cannot be correctly executed. The specific reason is not clear. I only know that the object returned by iter2.next is a com. HP. hpl. Jena. RDF. model. impl. literalimpl object, and does not include the corresponding getrequiredproperty function. I also found something for this question, but I didn't get a clear answer, so I used a compromise to implement the function.

The executable code in this example can be found in tutorial10.

The methods provided by the Jena class to manipulate containers include adding new members, inserting new members in the container, and deleting existing members. The Jena container class currently ensures that the list of ordinal attributes used starts with RDF: _ 1 and is adjacent. The core team of RDF relaxed this restriction to allow partial container representation. So this is one of the places that Jena may modify in the future.

More about text (literals) and Data Types

The RDF text (literals) is not just a simple string. The text may have a language label to indicate the language used by the text. The text "chat" with English language labels is considered different from the text "chat" with French language labels. This strange feature is the artefact produced by the original RDF/XML syntax ).

In fact, there are two types of text. In a string, the component is a simple string. On the other hand, the string component is expected to be a well-formatted XML segment. When an RDF model is written in the form of RDF/XML, a special attribute Construction Using parsetype = 'literal' will be used to represent it.

In Jena, these attributes are set when a text is created. For example, in tutorial11:

// Java code
// Create the resource
Resource r = model. createresource ();

// Add the property
R. addproperty (RDFS. Label, model. createliteral ("chat", "en "))
. Addproperty (RDFS. Label, model. createliteral ("chat", "FR "))
. Addproperty (RDFS. Label, model. createliteral ("<em> chat </em>", true ));

// Write out the model
Model. Write (system. Out );

 

# Jruby code
Require 'java'

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

Model = Java: modelfactory. createdefaultmodel

R = model. createresource

R. addproperty (RDFS: Label, model. createliteral ('chat', 'en '))\
. Addproperty (RDFS: Label, model. createliteral ('chat', 'Fr '))\
. Addproperty (RDFS: Label, model. createliteral ('<em> chat </em>', true ))

Model. Write Java: printwriter. New (Java. Lang. system. out)
Puts

Model = modelfactory. createdefaultmodel

R = model. createresource

R. addproperty (RDFS: Label, '11 ')\
. Addproperty (RDFS: Label, 11)

Model. Write Java. Lang. system. Out, 'N'-triple'
End


Will generate:

<RDF: RDF
Xmlns: RDF = 'HTTP: // www.w3.org/5o/02/22-rdf-syntax-ns #'
Xmlns: RDFS = 'HTTP: // www.w3.org/2000/01/rdf-schema #'
>
<RDF: Description RDF: nodeid = "A0">
<RDFS: Label XML: lang = 'en'> chat </RDFS: Label>
<RDFS: Label XML: lang = 'Fr '> chat </RDFS: Label>
<RDFS: Label XML: lang = 'en' RDF: parsetype = 'literal'> <em> chat </em> </RDFS: Label>
</RDF: Description>
</RDF: RDF>


If the two texts are considered the same, they must be both XML texts or simple texts. In addition, neither of them has a language tag or the same language tag. For simple text, the strings of the two must be the same. There are two requirements for the equality of XML text. First, the preceding requirements must be met and the strings must be the same. Second, if the cannonicalization of their strings is the same, they are the same. (Note: The Chinese explanation of cannonicalization is not found .).
 
The Jena interface also supports type text. The old-fashioned method of treating text types is to treat them as short strings: typed values are converted to strings through common Java methods, and these strings are stored in the model. For example, you can try it (NOTE: For simple text, we can omit model. createliteral (...) ):

// Java code
// Create the resource
Resource r = model. createresource ();

// Add the property
R. addproperty (RDFS. label, "11 ")
. Addproperty (RDFS. Label, 11 );

// Write out the model
Model. Write (system. Out, "N-TRIPLE ");


The output is as follows:
_: A...

Because both texts are strings "11", only one statement is added.

The core team of RDF defined a mechanism that supports the RDF data type. Jena supports those mechanisms that use typed text, but this is not discussed in this tutorial.

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.