Introduction to RDF and Jena RDF APIs (4)

Source: Internet
Author: User
Tags vcard

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

Let's see how to better control the selected statement. Simpleselector can be inherited, and its select method can be modified to implement better filtering:

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

 

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


This example uses a concise Java technology, that is, to reload an inline method when creating an instance of this type. (Similarly, this method is also available in jruby .) Here selects (...) The method checks to ensure that the full name ends with "Smith. It is important to note that the filtering of predicates and objects is performed when selects (…) is called (...) Method, so the additional test will only be applied to the matching statement. The complete code can be found in tutorial8 and the following output is generated:

The database contains vcards:
John Smith
Becky Smith

You may think of the following code:

// Do all filtering in the selects Method
Specified titerator iter = model. liststatements (
New
Simpleselector (null, null, (rdfnode) null ){
Public Boolean selects (statement S ){
Return (subject = NULL | S. getsubject (). Equals (subject ))
& (Predicate = NULL | S. getpredicate (). Equals (predicate ))
& (Object = NULL | S. GetObject (). Equals (object ))
}
}
});


Equivalent:

Specified titerator iter =
Model. liststatements (New simpleselector (subject, predicate, object)


Although they may be equivalent in terms of functionality, the first form lists all the statements in the model and then tests them one by one. In the second form, indexes can be created during execution to provide performance. You can try it out on your own in a big model, but please pour yourself a cup of coffee for a break.

Model Operations

Jena provides three methods to manipulate a model as a set. That is, three common set operations: Sum, intersection, and difference.

The merge operation of the two models is to represent and operate the statement set of the two models. This is one of the key operations supported by the RDF design. This operation allows you to merge separated data sources. Consider the following two models:


And


When they are merged, the two http://...JohnSmith will be merged into one, the duplicate vCard: FN arrow will be discarded, and then the following will be generated:


Let's take a look at the functions of this Code (the complete code is in tutorial9) and see what will happen.

// Java code
// Read the RDF/XML files
Model1.read (New inputstreamreader (in1 ),"");
Model2.read (New inputstreamreader (in2 ),"");

// Merge the models
Model = model1.union (model2 );

// Print the model as RDF/XML
Model. Write (system. Out, "RDF/XML-ABBREV ");

 

# 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'

Inputfilename1 = 'vc-db-3.rdf'
Inputfilename2 = 'vc-db-4.rdf'

Model1 = Java: modelfactory. createdefamodel Model
Model2 = Java: modelfactory. createdefamodel Model

In1 = Java: filemanager. Get. Open inputfilename1

If in1 = Nil
Raise argumenterror, 'file: '+ inputfilename1 + 'not Found'
End

In2 = Java: filemanager. Get. Open inputfilename2

If in2 = Nil
Raise argumenterror, 'file: '+ inputfilename2 + 'not Found'
End

Model1.read in1 ,''
Model2.read in2 ,''

Model = model1.union model2

Model. Write Java. Lang. system. Out, 'rdf/XML-ABBREV'
Puts
End


Petty writer generates the following output:

<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: // somewhere/johnsmith/">
<VCard: email>
<VCard: Internet>
<RDF: value> John@somewhere.com </RDF: value>
</VCard: Internet>
</VCard: email>
<VCard: n rdf: parsetype = "resource">
<VCard: Given> JOHN </vCard: Given>
<VCard: Family> Smith </vCard: Family>
</VCard: n>
<VCard: FN> john smith </vCard: FN>
</RDF: Description>
</RDF: RDF>


Even if you are not familiar with the syntax details of RDF/XML, you can still clearly see that the model is merged as expected. In a similar way, we can calculate the intersection and difference operations of the model.

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.