Generate a FOAF Document with Apache Jena

In our first tutorial, we saw how to get started with Apache Jena using Eclipse IDE and we also became familiar with the interface Model and the class ModelFactory. We learned how to write a simple RDF graph with a Resource and a Property. This post will look into generating a sample FOAF document (Friend of Friend, a machine-readable ontology describing people and their activities and relations) by employing Apache Jena.

First of all, I have created a simple FOAF description  by using the Javascript-based application FOAF-a-matic. It is a very straightforward task as you will only need to include the info you would like to be in your FOAF file and click the button ‘FOAF me’ at the bottom of the page. This is the resulting FOAF with the info provided:

<foaf:PersonalProfileDocument rdf:about="">
<foaf:maker rdf:resource="#me"/>
<foaf:primaryTopic rdf:resource="#me"/>
<admin:generatorAgent rdf:resource=""/>
<foaf:Person rdf:ID="me">
<foaf:name>Ilaria C</foaf:name>
<foaf:homepage rdf:resource=""/>

Our goal is to generate your own FOAF document with the following attribute: foaf : name; foaf : title; foaf : givenname; foaf : family_name; foaf : nick and foaf : homepage. Here is our Jena implementation:

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;

public class FOAFGeneration extends Object {

 public static void main (String args[]) {

 // some definitions
 String personURI = "";
 String firstName = "Ilaria";
 String familyName = "C";
 String title = "Dr";
 String nickName = "ila";
 String homePage = "";
 String fullName = firstName + " " + familyName;

 // create an empty model
 Model model = ModelFactory.createDefaultModel();

 // create the resource
 // and add the properties cascading styleResource
 model.createResource(personURI, FOAF.Person)
 .addProperty(, fullName)
 .addProperty(FOAF.title, title)
 .addProperty(FOAF.givenname, firstName)
 .addProperty(FOAF.family_name, familyName)
 .addProperty(FOAF.nick, nickName)
 .addProperty(FOAF.homepage, homePage);

 // write the model in Turtle
 model.write(System.out, "Turtle");

Here is the expected result from our Eclipse Console:




Posted on April 5, 2015, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: