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:

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:admin="http://webns.net/mvcb/">
<foaf:PersonalProfileDocument rdf:about="">
<foaf:maker rdf:resource="#me"/>
<foaf:primaryTopic rdf:resource="#me"/>
<admin:generatorAgent rdf:resource="http://www.ldodds.com/foaf/foaf-a-matic"/>
</foaf:PersonalProfileDocument>
<foaf:Person rdf:ID="me">
<foaf:name>Ilaria C</foaf:name>
<foaf:title>Dr</foaf:title>
<foaf:givenname>Ilaria</foaf:givenname>
<foaf:family_name>C</foaf:family_name>
<foaf:nick>ila</foaf:nick>
<foaf:homepage rdf:resource="https://semanticreatures.com"/>
</rdf:RDF>

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 = "http://www.ilariahome.com";
 String firstName = "Ilaria";
 String familyName = "C";
 String title = "Dr";
 String nickName = "ila";
 String homePage = "http://www.semanticreatures.com";
 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(FOAF.name, 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:

screenshot17

 

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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: