Mapping between RDF and Java objects with Jena
Well, Jena-bundled Schemagen can be used to generate Java class representations for RDF ontologies (classes and properties will be represented by static constants). But this only helps at the schema level. When dealing with ontology instances in an RDF store one faces a similar problem than when working with relational databases: you would like to have a Java domain model representing your external data.
With the Jena Ontology API you can access/query the RDF graph in various ways and then you can instanciate and populate your Java objects from the RDF graph resulting in duplication of data: one version in the RDF store and one version as Java objects im memory.
To avoid this gap, just wrap the underlying graph (can be a RDBMS-backed graph) by a Java class representing your RDF-S/OWL class and do the actually graph-access on-the-fly when the object is accessed. For example create an abstract wrapper base like this:
public abstract class RdfWrapperBase {
protected Individual instance;
public RdfWrapperBase(Individual instance) {
this.instance = instance;
}
public String getUri() {
return instance.getURI();
}
...
}
For each concept to access via Java code, just extend this base class and add your custom access/modifier methods. Every time you want to use an ontology instance (which should have an URI) as a Java object, just create a new wrapped instance like this (example with a class "Person"):
Person p = new Person(myModel.getIndividual(uri)); Address a = p.getAddress();
Address is also a Java wrapper for an address ontology class. The method getAddress() hides the details how the data is fetched from the underlying Jena graph/model. It could look like this:
public Address getAddress() {
return new Address((Individual) instance
.getPropertyValue(PersonVocab.hasAddress)
.as(Individual.class));
The Mindswap project Jastor is a more powerful framework targeted to to OWL-Java mapping in a similar way. For instance, it also reflects the class hierarchy of ontologies in Java.
- Login to post comments