Sesame - Jena Adapter

Hosted by: SourceForge.net Logo
how to use the adapter  
Jena
Sesame
Quick links:
Homepage
Install notes
Project page on SourceForge
Download latest release
Browse CVS repository
How to use the Sesame - Jena adapter

1. Setup and query a Sesame repository wrapping a Jena model from your code

To create a Sesame repository that wraps a Jena model you can follow these steps (for the sake of simplicity we omit exception handling):
  • Create a SailConfig

    SailConfig sc = new SailConfig("sesame_jena_adapter.sesame.sailimpl.jena_adapter.RdfSchemaRepository");

    If you prefer you can use an RdfSource or RdfSchemaSource instead of an RdfSchemaRepository.

  • Configure SAIL parameters

    This can be done in several ways. You can provide an existing Jena model, or you can set the filename or the url that will be used to load data into a newly created Jena model, and you can also specify the ontology language used for those data (defaults to RDF Schema).

    • Using an existing Jena model

      Create a Jena model in one of the many ways Jena let you do it, for example:

      String museumFile = "examples/museum.rdf";
      OntModel jenaModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF, null);
      jenaModel.read(new FileInputStream(museumFile), null);


      Note that you have to provide an OntModel in order to create an RdfSchemaSource or an RdfSchemaRepository.

      Now add the jena model to the SAIL parameters

      HashMap configParams = new HashMap();
      configParams.put("jenaModel", jenaModel);
      sc.setConfigParameters(configParams);


    • Fom a local file

      HashMap configParams = new HashMap();
      configParams.put("sourceFile", "<my filename here>");
      configParams.put("ontologyLanguage", "RDFS");
      // other values are: OWL, OWL_DL, OWL_LITE. Defaults to RDFS
      sc.setConfigParameters(configParams);


    • From a remote file

      HashMap configParams = new HashMap();
      configParams.put("sourceUrl", "<my url here>");
      configParams.put("ontologyLanguage", "RDFS"); // ditto
      sc.setConfigParameters(configParams);


  • Create the repository

    RepositoryConfig rc = new RepositoryConfig("<my repository id>");
    rc.addSail(sc);
    rc.setWorldReadable(true);


    You may want to have an org.openrdf.sesame.sailimpl.sync.SyncRdfSchemaRepository on top of the newly created repository.

  • Get your new repository

    LocalService ls = Sesame.getService();
    ls.addRepository(rc);
    SesameRepository rep = ls.getRepository("<my repository id>");


  • Execute RQL and SeRQL queries in the usual Sesame style

    String query = "<my query here>";
    // use your listener here
    TableQueryResultListener sql = new SimpleQueryListener();
    rep.performTableQuery(QueryLanguage.RQL, query, sql);


Dont't forget to set on the classpath all the required libraries!

2. Setup a Sesame repository wrapping a Jena model via Sesame system.conf

To use the adapter from a Sesame server, you have to copy the required libraries to your $SESAME_WEBAPP/WEB-INF/lib directory.

You can configure the adapter adding your SAIL configuration parameters to the system.conf file of your Sesame server. You can use the Configure Sesame! tool. Just create a new repository, go to the details window and add:

sesame_jena_adapter.sesame.sailimpl.jena_adapter.RdfSchemaRepository

(or sesame_jena_adapter.sesame.sailimpl.jena_adapter.RdfSchemaSource,
or sesame_jena_adapter.sesame.sailimpl.jena_adapter.RdfSource)

to the SAIL stack. You may want to add:

org.openrdf.sesame.sailimpl.sync.SyncRdfSchemaRepository

on top of it, too.

Now add to the sesame_jena_adapter.* SAIL the following parameters:

(optional) key: "ontologyLanguage" ; value: one of "RDFS" (default), "OWL", "OWL_DL", "OWL_LITE"

and one of:

key: "sourceFile" ; value: the path to the file containing the data to load into the Jena model (absolute path is easier to set)

or

key: "sourceUrl" ; value: an URL used to retrieve the data to laod into the Jena model (may refer to your locally running Sesame server!)

Don't forget to set access rights, as they default to no privilege for anyone.

Upload the configuration file to your Sesame server, and then you can access your Jena model as if it were a native Sesame repository, using the usual Web interface.

3. Execute RQL and SeRQL queries on a Jena model from your code (Sesame-transparent)

Almost identical to executing RDQL queries:

import sesame_jena_adapter.jena.rql.*;

String queryString = "<your RQL or SeRQL query here>";

Query query = new Query(queryString) ;
// or, if you are executing a SeRQL query:
// Query query = new Query(queryString, Query.Language.SERQL) ;

query.setSource(yourJenaModel);
// note: it requires an OntModel.

QueryEngine qe = new QueryEngine(query) ;
QueryResults results = qe.exec() ;

for ( Iterator iter = results ; iter.hasNext() ; )
{
ResultBinding rbind = (ResultBinding)iter.next() ;
Object obj = rbind.get("<variable name here>") ;
...
}
results.close() ;


Updated on 08 - 05 - 2004  
RDF Resource Description Framework Icon Valid XHTML 1.0!