Getting Started With Elmo

James Leigh


Table of Contents
Introduction
Object Model
Elmo Framework
Reading and Writing Java Objects
Transactions
Concurrency Control
References

Introduction

Elmo is a persistence framework that allows Java applications to access RDF stores. Elmo maps objects to RDF resources in a non-intrusive manner and enables the developer to work at the object level. Elmo enables you to map a Java object model to the Resource Description Framework (RDF). RDF is a simple way to represent resources and their properties. In RDF every property and type is represented using a Uniform Resource Identifier (URI).


Object Model

Elmo maps RDF types and properties using its @rdf annotation. To map a JavaBean to an RDF resource, the @rdf annotation should be placed on the class and its fields, as show below. The full class name must also be listed in "META-INF/org.openrdf.elmo.concepts".

@rdf("http://www.example.com/rdf/2008/elmo#MyClass")
public class MyClass {
    @rdf("http://www.example.com/rdf/2008/elmo#myProperty")
    private String myProperty;
    public String getMyProperty() {
        return myProperty;
    }
    public void setMyProperty(String myProperty) {
        this.myProperty = myProperty;
    }
}

Elmo also supports using Java interfaces instead of Java classes, as shown below. Interfaces, like classes, should also be listed in the above concepts file. Using an interface provides a slightly less verbose option, while allowing RDF resources to have multiple types.

@rdf("http://www.example.com/rdf/2008/elmo#MyClass")
public interface MyClass {
    @rdf("http://www.example.com/rdf/2008/elmo#myProperty")
    public String getMyProperty();
    public void setMyProperty(String myProperty);
}

Elmo Framework

Elmo stores all resources and properties in an RDF repository. This could be an in-memory repository, an on-disk repository, a remote repository, or any repository supported by Sesame.

Elmo manages the object instances using a SesameManager. Each SesameManager is associated with a unique connection to the repository. SesameManager is a lightweight object created by the SesameManagerFactory. The SesameManager provides a gateway between Java Objects and the RDF repository. The methods "persist" and "create" allow the creation of new Objects in the repository, while the methods "merge" and "designate" allow addition roles or properties to be added to Objects that may already be in the repository.

ElmoModule module = new ElmoModule();
SesameManagerFactory factory = new SesameManagerFactory(module, repository);
SesameManager manager = factory.createElmoManager();

MyClass object = manager.persist(new MyClass()); // MyClass is a class

MyClass object = manager.create(MyClass.class); // MyClass is an interface

Reading and Writing Java Objects

The SesameManager provides access to the Sesame RDF API. Using this API the RDF repository can be exported or other RDF data can be imported. Access to the underlying Sesame RepositoryConnection is through the "getConnection()" method of the SesameManager. This connection should not be closed and is shared with the SesameManager. The follow commands show how to export and import RDF in the repository.

manager.getConnection().export(new OrganizedRDFXMLWriter("export.rdf"));

manager.getConnection().add(new File("export.rdf"), "", RDFFormat.RDFXML);

Transactions

Transactions in Elmo are done through the SesameManager#getTransaction() method as shown below. Elmo does not support nested transactions. A transaction can be detected using the isActive method.

manager.getTransaction().begin();
try {
    MyClass object = manager.create(MyClass.class);
    object.setMyProperty("value");
    manager.getTransaction().commit();
    return object;
} catch (Exception e) {
    manager.getTransaction().rollback();
    e.printStackTrace();
}

Concurrency Control

Elmo provides no locking or concurrency control, unless already provided by the underlying RDF repository. Most RDF repositories do not provide concurrency control. Therefore external concurrency control techniques may be required.


References

For more information about RDF and Sesame see Getting Started With Sesame . For more information about Elmo see the Elmo User Guide at http://www.openrdf.org/ .