Monthly Archives: April 2015

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

 

Getting Started with Apache Jena and Eclipse

Apache Jena is an Open Source Framework for building Semantic Web and Linked Data applications. Jena provides an API to help write Java code that handles RDF, RDFS, RDFa, OWL and SPARQL. It supports serialisation of RDF graph for:

  • relational databases
  • RDF/XML
  • Turtle
  • Notation 3

A similar framework  RDF4J (formerly known as Sesame) queries, processes and analyses RDF constructs (it supports SPARQL and SeEQL), however it does not currently support OWL. This tutorial will be a step-by-step guide on how to attach Jena to an Eclipse project in order to write and print out a sample RDF statement. Step 1 – Create a new Java Project in Eclipse First of all, you will need to create a new Java project in Eclipse. This can be done by selecting File -> New-> Java Project. You will be prompted with the below window where you will specify the name of your project and the chosen execution environment JRE (in our example, we used 1.7): Step1-JenaTutorial   Step 2 – Create your sample Class  Create a sample Class called MyFirstRDFStatement from Jena-Tutorial-Project -> New -> Class. Initially, this class will only include the Class declaration and main method, but we will later be enriched with the necessary code to create a RDF statement and output it as Turtle format. screenshot2   Step 3 Adding the Jena Libraries to your Eclipse Project In order to start writing and reading RDF statements, we need to import the Jena Libraries into our project. Go to Window -> Preferences -> BuildPath -> UserLibraries:  screenshot3 You would have already downloaded the Jena Libraries from here. Create a New User Libraries by clicking on New and specifying your custom name: screenshot4 From the left click on ADD External JARs and navigate to the location where your libraries are located (in my case, they were placed in my Download folder): screenshot13_001   Step 4: Create an empty Model with ModelFactory The first element you will need to include in your MyFirstRDFStatement class is a Model. In Jena terms, a Model is a set, or better, a container for RDF statements. The Model interface is contained in the package com.hp.hpl.jena.rdf.model and it provides methods for creating resources, properties, literals and statements. In our example, the class ModelFactory provides methods for creating a standard kind of Model such as createDefaultModel, which simply creates a fresh Model with some default specification: screenshot10 It should be noted that our Model is currently ’empty’, therefore we would need to create the resource and add its property. Step 5: Create the Resource and its Property In order to create the Resource and Property, you will need to use the Model methods createResource and createProperty, as follows: screenshot11   Step 6: Write a serialised representation of the Model The last step of our tutorial consists in outputting the Model in a chosen format – in our case Turtle. We will employ the Model method to write with the following signature :

 Model write(OutputStream out, String lang)

screenshot12

Although our statement has been correctly printed out (<http://www.johnwebpage.com&gt; <John Smith> “this is a property”^^<http://www.w3.org/2001/XMLSchema#string&gt; .), 3 red lines come up in the console (which have been deliberately left in the screenshot) and they refer to an issue concerning Log4J. Jena uses Log4J as a logging system and this error is caused by the fact that the configuration file log4j.properties  cannot be found. It is very likely you will encounter the same issue, so have a look at these resources:

http://stackoverflow.com/questions/20365204/configure-eclipse-for-log4j

http://www.mkyong.com/logging/log4j-log4j-properties-examples/

http://logging.apache.org/log4j/1.2/faq.html#noconfig

Probably, the easiest way to fix an issue with Log4J is to follow these simple steps:

  • Find a properties file named jena-log4j.properties and copy it;
  • Navigate to your workspace folder where you have your source project;
  • Rename the file to just log4j.properties;
  • Go to bin folder and paste the file there
  • Clean the project and run it again.

The errors should have gone and you should be returned with the following:

screenshot15