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:

[code language=”xml”]
<foaf:PersonalProfileDocument rdf:about="">
<foaf:maker rdf:resource="#me"/>
<foaf:primaryTopic rdf:resource="#me"/>
<admin:generatorAgent rdf:resource=""/&gt;
<foaf:Person rdf:ID="me">
<foaf:name>Ilaria C</foaf:name>
<foaf:homepage rdf:resource=""/&gt;

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:

[code language=”java”]
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

This site uses Akismet to reduce spam. Learn how your comment data is processed.