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”]
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#&quot;
xmlns:foaf="http://xmlns.com/foaf/0.1/&quot;
xmlns:admin="http://webns.net/mvcb/"&gt;
<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"/&gt;
</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"/&gt;
</rdf:RDF>
[/code]

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 = "http://www.ilariahome.com&quot;;
String firstName = "Ilaria";
String familyName = "C";
String title = "Dr";
String nickName = "ila";
String homePage = "http://www.semanticreatures.com&quot;;
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");
}
}
[/code]

Here is the expected result from our Eclipse Console:

screenshot17

 

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.