Java tutorial for Strap

Pierre-Antoine Champin

This very short tutorial demonstrates how to use the Java binding of the Strap protocol.

Basic concepts

Strap has been designed to enable independant software component to share information represented as RDF graphs without having to exchange a complete serialized representation of that graph. It is a client-server protocol where the server acts as a graph provider, and the client acts as a graph user. The protocol provides 4 operations:

The first three operations are the basic operations on RDF graphs provided by most RDF APIs, including Jena and Sesame, so Strap can be embeded seamlessly in those APIs, as the examples below will demonstrate. Note that to run those examples, you will need to one of the aforementioned API installed and in your classpath.

The communication between the server and the client requires a bi-directionnal communication stream. This will most often be a TCP socket, but could also be a pair of pipes for components on the same system.

A quick start

Launch a Strap server, for example

java fr.cnrs.liris.strap.sesame.SesameStrapTcpServer some_file.rdf

and then write one of the following programs.

Example with Sesame

import fr.cnrs.liris.strap.sesame.SesameStrapTcpClient;
import org.openrdf.sesame.Sesame;
import org.openrdf.sesame.config.RepositoryConfig;

public class Test {
public static void main(String[] args) throws Exception {
RepositoryConfig repConfig =
new RepositoryConfig("myCustomRep");

String uri = "strap://localhost:1234/";

repConfig.addSail(SesameStrapTcpClient.makeConfig(uri));
repConfig.setWorldReadable(true);
repConfig.setWorldWriteable(true);

org.openrdf.sesame.repository.local.LocalRepository rep =
Sesame.getService().createRepository(repConfig);

// serlialize the content of the repository
java.io.InputStream is = rep.extractRDF(
org.openrdf.sesame.constants.RDFFormat.RDFXML,
true, true, true, true
);
for(int i=is.read(); i!=-1; i=is.read())
System.out.print((char)i);
}

You can see that Strap is hidden in the Sail layer, so that the repository rep does not have to care about the fact that the RDF graph is managed by another application, possibly developed in a different programming language (e.g. python) and possibly running an another machine. We can now use the poser of the Sesame API (HTTP protocol, SERQL queries...) on top of that other component.

Example with Jena

/* Jena example */
import fr.cnrs.liris.strap.jena.JenaStrapTcpClient;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class JenaStrapTest {
public static void main(String[] args) throws Exception {
JenaStrapTcpClient jsc = new JenaStrapTcpClient (
"strap://localhost:1234/");
Model m = ModelFactory.createModelForGraph(jsc);
m.write (System.out);
}
}

You can see that, besides its creation, the model m is not different from any other Jena model. Every application expecting a Jena model can be used with this one, with the added value that the graph is actually provided by an independant software component, possibly developed in a different programming language (e.g. python) and possibly running an another machine.