2015 / 14 March

Sesame Framework for Semantic Web Programming


Your first thoughts might be “What the hell is Sesame? Actually wait.. what’s the Semantic Web?” Then again, many of you may already be familiar with these concepts. A quick google search will tell you all about the Semantic Web and it’s promises for a WEB 3.0

But for those of you that are lazy, here’s a TL;DR  : The Semantic Web is essentially just an extension of the current web that we have, but promotes better exchange and reuse of data in a structured way. I’ll talk more about the Semantic Web and its technology stack in a later post. The format of the data exchanged allows for embedded “semantics”. The most fundamental model used for this is RDF (Resource Description Framework).

This is where the Sesame framework comes in. Sesame is an open-source framework that allows for quick’N’easy handling and processing of RDF data. Jena is another open-source framework, spearheaded by Apache, and is pretty popular as well. I’ve used both and Sesame has been a lot less of a pain in the ass (for me).

Alright, let’s get started. I’m gonna assume you have your JDK, Eclipse/any other IDE etc etc installed and running. For the purpose of this post and perhaps others in the future you will also need Maven. If you haven’t set up Maven yet, check out these two short’N’quick posts:

Setting Up Maven For Windows/Mac

and

Escaping Dependency Nightmares by using Maven

If you insist on not using Maven and making your life complicated, then you can download the required JARs here: Download Sesame

To simplify things, let’s create a new Java project like we normally would.

Name it “SesameTest”

 

SesameTest

 

 

Now right click on your Project –> Configure –> Convert to Maven Project.

 

ConvertToMaven

 

 

You’re gonna get a new Maven pop-up asking you to create a new POM.xml. Click “Create New POM”. A pom.xml file should now appear in your project. You’ve now enabled the Maven nature for your project. Awesome.

Your pom.xml should be opened up, if not, open it and on the tabs below switch from “Overview” to “pom.xml”

 

pomxlml

 

You’re going to see  XML containing the basic pom of the project. We want to specify the Sesame dependencies somewhere in here. This will tell Maven to automatically pull in the dependencies from the repository and build the project. Awhh yea!!

On line number 5 you’re going to see  <version>0.0.1-SNAPSHOT</version>. Place your cursor at the end of the line and hit enter. You’re gonna place your dependencies here, between the </version> and <build>

 

pomunedited

 

In the screen shot above, I highlighted in yellow the spot where you need to copy paste the following dependencies:

[gist https://gist.github.com/2314628 /]

 
  
  		org.openrdf.sesame
  		sesame-runtime
  		2.7.9
  	
  	
  		ch.qos.logback
  		logback-classic
  		1.0.13
  	
  	
  		org.openrdf.sesame
  		sesame-repository-sail
  		2.7.9
  	
  	
  	
  org.openrdf.sesame
  sesame-rio-rdfxml
  2.7.9


  org.openrdf.sesame
  sesame-rio-turtle
  2.7.9

  	 
  org.openrdf.sesame 
  sesame-repository-sparql 
  2.7.9 

  
  


Bam. You’re done. You should see a new subfolder named “Maven Dependencies” in your Build Path library.You’re now ready to start developing your first Semantic based App.

Create a new Java class to test your semantic environment. Right click on your project –> New Class –> Name it FirstSemanticApp

Copy paste the following code into it and run.

import info.aduna.iteration.Iterations;

import org.openrdf.model.Model;
import org.openrdf.model.Namespace;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.LinkedHashModel;
import org.openrdf.model.vocabulary.FOAF;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.RDFS;
import org.openrdf.model.vocabulary.XMLSchema;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.Rio;
import org.openrdf.sail.memory.MemoryStore;

public class FirstSemanticApp {

public static void main(String[] args) throws RepositoryException, RDFHandlerException {

//Create your Repository that will hold your RDF
Repository rep = new SailRepository(new MemoryStore());
//Initialize it..
rep.initialize();
//Start adding values into your RDF for test purposes
ValueFactory nV = rep.getValueFactory();
String namespace = "http://myfirst.app.org/";
URI juggernaut = nV.createURI(namespace, "Juggernaut");

RepositoryConnection conn = rep.getConnection();
try {
conn.add(juggernaut, RDF.TYPE, FOAF.PERSON);
conn.add(juggernaut, RDFS.LABEL, nV.createLiteral("Juggernaut", XMLSchema.STRING));

RepositoryResult<Statement> statements =
conn.getStatements(null, null, null, true);

Model model = Iterations.addAll(statements, new LinkedHashModel());
model.setNamespace("rdf", RDF.NAMESPACE);
model.setNamespace("rdfs", RDFS.NAMESPACE);
Rio.write(model, System.out, RDFFormat.TURTLE);
}
finally {
conn.close();
}
}
}

 

You should get the following output in your eclipse console if everything is set up:
 

@prefix rdf:  .
@prefix rdfs:  .

 a  ;
	rdfs:label "Juggernaut"^^ .

Congratulations, you’ve just set up your environment to start developing semantic based applications!

To understand more about the code above, and/or to learn how to use the Sesame API you can checkout the Programming with Sesame documentation on their official website! It’s extremely clear and thorough.

No comments so far.

LEAVE YOUR COMMENT