Wednesday, September 16, 2009

Uploading RDF vocabularies (GRAILS, Virtuoso and Sesame)

If you need to upload an RDF vocabulary in Virtuoso, the easiest way to go is doing it through the Virtuoso Conductor. It is pretty straightforward, you define the graph name and the file to upload and that is it. When developing an application though, you may want to be able to upload the files programmatically (for instance for managing metadata associated to the vocabulary and creating a catalog).

The following snipped of code is a modified version of the one I found in the openrdf forum:

Repository myRepository = new VirtuosoRepository("jdbc:virtuoso://localhost:1111","dba","dba");
myRepository.initialize();
File file = new File("/Users/paolociccarese/Desktop/contact.rdf");
RepositoryConnection con = myRepository.getConnection();
URI context = new URIImpl("http://paolociccarese.info/contact");
con.add(file, null, RDFFormat.RDFXML, context);
con.close();
For running this code it is necessary to perform a bunch of imports:

import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.rio.RDFFormat
import org.openrdf.repository.Repository
import org.openrdf.repository.RepositoryConnection
To make it work, if you use grails and ivy (ivy.xml), you should add at least the following dependencies:

<dependency org="org.openrdf" name="openrdf-repository-api" rev="2.0.1" conf="compile"/>
<dependency org="org.openrdf" name="openrdf-rio-rdfxml" rev="2.0.1" conf="compile"/>
<dependency org="org.openrdf" name="openrdf-model" rev="2.0.1" conf="compile"/>

and you also need to link to the maven repository for these in the ivysettings.xml file:

<ibiblio name="aduna" root="http://repository.aduna-software.org/maven2" m2compatible="true"/>

and finally run the command >grails get-dependencies again.

No comments: