Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Meet Jena, a Semantic Web Platform for Java : Page 2

Tools for developing semantically aware applications are rapidly growing more Java friendly. Take a closer look at Jena, an open source toolkit for processing and working with semantic web data.

Putting Resources in Context
In RDF a named resource has a URI as an identifier. The semantic web operates in open, web-scale systems by design. It's clearly important to avoid accidentally naming resources using the same identifier that someone else has used for a different concept. However, it should be possible to be able to make statements independently about the same object in different source contexts. While a music publisher may use a resource to list an artist's recording catalog, another user might use the same URI to review recordings, or publicize an upcoming concert. This open linkability is a central value for semantic web applications.

For similar reasons, RDF also uses URIs to denote predicate names. Since URIs are usually quite long strings, RDF borrows a technique from XML to abbreviate them as namespace:name pairs (for example, foaf:Person), which makes it easier to write down examples compactly.

A literal can only be the object of a subject-predicate-object triple, but a resource can be either a subject or an object. In fact, the same resource can be a subject or an object (or even both) for any number of triples. Drawing out this scenario visually produces a directed, labeled graph, and collections of RDF statements are typically called graphs. Figure 1 demonstrates a simple example of a graph that uses data from the Friend-of-a-Friend (FOAF) vocabulary.

Figure 1. FOAF Graph: A labeled graph is a collection of RDF statements.

Each arc or edge in the graph represents an RDF statement. The graph shown in Figure 1 contains seven triples in total, and of the seven nodes in the graph four are literals and three are resources. Only one of the resources, the one denoting the Person type (or, in RDF parlance, class), is labeled with a URI. The other two resources are anonymous nodes, which are sometimes called bNodes.

Tradition dictates that the first tutorial program is some variant of HelloWorld. Here, HelloWorld.java, available for download, performs three simple tasks: reading in a FOAF RDF document similar to the one shown in Figure 1, counting the triples, and writing it out again in a different format.

The input file is encoded in an XML dialect used to represent RDF. Despite being part of the RDF standard, RDF/XML is widely held to be a human-unfriendly syntax compared to other XML dialects because the serialization needs to encode the subject-predicate-object structure of RDF and is intended to be consumed by a computer, not a human. N3, on the other hand, is a textual format that is rather more compact and human friendly. The HelloWorld program will consume RDF/XML and produce N3:

public class HelloWorld { public static final String FOAF_FILE = "http://www.hpl.hp.com/people/Ian_Dickinson/foaf.rdf"; private static Logger log = Logger.getLogger( HelloWorld.class ); public static void main( String[] args ) { // create the simplest model there is Model m = ModelFactory.createDefaultModel(); // use the file manager to read an RDF document into the model FileManager.get().readModel( m, FOAF_FILE ); log.debug( "We have loaded a model with no. statements = " + m.size() ); // write the model out in a different format m.write( System.out, "N3" ); } }

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.