(this post deals with features provided jenabean 1.0.1, available from the project site. You’ll also need Jena, HP’s semenatic web framework.)
In my last post we looked at reading SIOC directly off the web. The other side of the coin is producing syntactically valid SIOC statements from java. You may want to create RDF for another consumer or perhaps you want to persist the SIOC statements directly into a Jena model. Either way, if you use the direct approach of coding to Jena’s RDF api, you’ll be writing quite a few lines of code. This task can be made simpler using Jenabean’s “Thing” utility along with a specialized interface to the Sioc vocabulary. We’ll be looking at this simple example, which duplicates the primary RDF example givin in the SIOC specification document.
<sioc:Post > <dcterms:title>Creating connections <dcterms:created>2006-09-07T09:3 <sioc:has_container rdf:resource="h <sioc:has_creator> <sioc:User rdf:about="http://john <rdfs:seeAlso rdf:resource="ht </sioc:User> </sioc:has_creator> <sioc:content>SIOC provides a unifi <sioc:topic rdfs:label="Semantic We <sioc:topic rdfs:label="Blogs" rdf:res <sioc:has_reply> <sioc:Post rdf:about="http://john <rdfs:seeAlso rdf:resource="htamp;sioc_id=123928"/> </sioc:Post> </sioc:has_reply> </sioc:Post>
I like this example as it combines several vocabularis, namely SIOC, Dublin Core, and RDFS for the labels. Any code generator RDF binding tool would most likely only generate the properties directly listed within the SIOC specification, so it might be difficult to combine the dcterms and rdfs vocabularies. Jenabean’s model connected programming model makes this easy, using interfaces that declare each of the vocabularies as a set of methods. Using Jenabean, the same RDF (same triples to be precise, the actual RDF may differ in appearance) in the example above can be produced with the following code:
Thing thing = new Thing(m);
thing.at(uri1).
as(DCTerms.class).
title("Creating connections between discussion clouds with SIOC").
created("2006-09-07T09:33:30Z").
isa(Sioc.Post.class).
has_container(thing.at(uri2)).
has_creator(
thing.at(uri3).isa(Sioc.User.class).
seeAlso( thing.at(uri4))).
content(literal1).
topic(thing.at(uri6).as(Rdfs.class).label("blogs")).
topic(thing.at(uri9).as(Rdfs.class).label("SemanticWeb")).
has_reply(
thing.at(uri7).
isa(Sioc.Post.class).
seeAlso(thing.at(uri8)));
Notice that the java code sans URL text is quite possibly more terse than the original RDF. The java code reads almost like N3, for example, thing.at(uri) declares a new resource in the model. You might read it as “there is a thing at this URI”. as polymorphs the resource reference to a particular vocabulary, so we first declare the two verbs from dcterms, title and created. In a similar way, we can also polymorph to a particular ontology class, so next the code states that the resource isa instance of an SIOC Post. This does two things, first it asserts the classification in the model, and 2nd it returns an interface for the same vocabulary so that we can continue to chain our SIOC conversation about the resource. My favorite part of this style of RDF programming using Jena is that the interface allows our IDE to help, notice how it can code complete only the verbs were able to assert in the SIOC vocabulary, that is, untill we “as” or “isa” our way into a new vocabulary…

Code completion based on vocabulary interface.
Notice also that the SIOC interface includes aspects of RDFS, as in
// rdfs:seeAlso
seeAlso( thing.at(uri4))).
thing.at(uri9).as(Rdfs.class).label("SemanticWeb")
// rdfs:label
This is a simple mechanism where in the SIOC interface extends the RDFS interface. Finally, these interfaces are not necessarily the official interfaces to their respective vocabularies. They are provided as examples and conveniences, however, you can use them as models to create your own interfaces that work along with the Thing utility to make asserting RDF in Jena fun and simple.
[...] The Web Semantic blog has described how to process SIOC feeds with Jenabean. This was followed up by a nice description of how to write out SIOC triples using Jenabean. [...]