Java developers are fortunate because they can use a good RDF framework in Jena. Jena provides an API for writing and reading RDF that can be saved and persisted in a variety of ways.
Jena's design goal is to handle the RDF data model well, just as JDBC is suitable for dealing with relational models. A large amount of code written in a database application is used to save Java objects, and some code is used to aggregate objects from the database. Semantic WEB applications written in Java code face a similar problem: they must implement the conversion between Java objects and RDF. As a result, developers must write a lot of code to eliminate the differences between their own models (typically JavaBeans) and Jena's RDF-centric APIs.
This article shows how Jenabean's JAVA-TO-RDF binding framework simplifies the process and reduces the amount of code needed. You will look at some Jena client code and compare it to the Jenabean based JavaBean programming model. First look at a simple example, and I'll show you how to do the following:
Save a bean as RDF
Bind its properties to a specific RDF property
Associating it with other objects
Re-read the Bean again
Jenabean programming Model
Consider the simple RDF example in Listing 1, which uses the N-triple (N3) format for easy reading:
Listing 1. RDF sample (N3 format)
a dc:Article ;
dc:creator "Philip McCarthy"^^xsd:string ;
dc:subject "jena, rdf, java, semantic web"^^xsd:string ;
dc:title "Introduction to Jena"^^xsd:string .
Listing 1 illustrates the "Jena profile" article written by Philip McCarthy and includes topics such as Jena, RDF, Java, and the Semantic Web. The glossary is part of the Dublin Core metadata classification. To replicate these RDF declarations using Jena's original Java API, you might want to perform a work similar to Listing 2:
Listing 2. Using the original Jena API to assert the RDF sample
String NS = "http://purl.org/dc/elements/1.1/";
OntModel m = createModel();
OntClass articleCls = m.createClass(NS +"Article");
Individual i = articleCls.createIndividual(
"http://www.ibm.com/developerworks/xml/library/j-jena/");
Property title = m.getProperty(NS + "title");
Literal l = m.createTypedLiteral("Introduction to Jena");
i.setPropertyValue(title,l);
Property creator = m.getProperty(NS + "creator");
l = m.createTypedLiteral("Philip McCarthy");
i.setPropertyValue(creator,l);
Property subject = m.getProperty(NS + "subject");
l = m.createTypedLiteral("jena, rdf, java, semantic web");
i.setPropertyValue(subject,l);
m.write(System.out, "N3");