Skip to content

Instantly share code, notes, and snippets.

@ijdickinson
Created October 3, 2012 22:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ijdickinson/3830267 to your computer and use it in GitHub Desktop.
Save ijdickinson/3830267 to your computer and use it in GitHub Desktop.
Example of loading ontology imports from named graphs in a local TDB store
package example;
import java.io.File;
import java.io.StringReader;
import org.openjena.atlas.lib.NotImplemented;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.tdb.TDB;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.vocabulary.*;
/**
* <p>This Jena example re-uses named graphs stored in a TDB model
* as the imports in an ontology.</p>
*
* @author Ian Dickinson, Epimorphics (mailto:ian@epimorphics.com)
*/
public class ImportTDBModelsExample
{
/***********************************/
/* Constants */
/***********************************/
public static String ONT1 = "http://example.org/test#ont1";
public static String ONT2 = "http://example.org/test#ont2";
// the model we're going to load, which imports ont1 and ont2
public static String SOURCE =
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n" +
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n" +
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n" +
"<http://example.org/test#upper> a owl:Ontology ;\n" +
" owl:imports <" + ONT1 + "> ;\n" +
" owl:imports <" + ONT2 + "> .\n";
/***********************************/
/* Static variables */
/***********************************/
/***********************************/
/* Instance variables */
/***********************************/
/***********************************/
/* Constructors */
/***********************************/
/***********************************/
/* External signature methods */
/***********************************/
public static void main( String[] args ) {
new ImportTDBModelsExample().run();
}
public void run() {
Dataset ds = initialiseTDB();
loadTDBContent( ds );
OntModel m = loadImportingOntology( ds.getDefaultModel(), ds );
m.writeAll( System.out, "Turtle", null );
}
/***********************************/
/* Internal implementation methods */
/***********************************/
/**
* Initialise the local TDB image if necessary.
*/
protected Dataset initialiseTDB() {
String tdbPath = "./target/data/tdb";
new File( tdbPath ).mkdirs();
return TDBFactory.createDataset( tdbPath );
}
/**
* Load some test content into TDB, unless it has already been initialized.
* @param ds
*/
protected void loadTDBContent( Dataset ds ) {
if (!ds.containsNamedModel( ONT1 )) {
loadExampleGraph( ONT1, ds, "The Dread Pirate Roberts" );
loadExampleGraph( ONT2, ds, "Chewbacca" );
}
}
/**
* Create a graph with the given name in the dataset, and initialise it with
* some fake content (namely an owl:Ontology resource). This is a proxy for
* loading a real ontology into the model.
*
* @param graphName
* @param ds
* @param creator
*/
protected void loadExampleGraph( String graphName, Dataset ds, String creator ) {
Model m = ModelFactory.createDefaultModel();
m.createResource( graphName )
.addProperty( RDF.type, OWL.Ontology)
.addProperty( DCTerms.creator, creator );
ds.addNamedModel( graphName, m );
TDB.sync( m );
}
/**
* Now we create an ontology model that imports ont1 and ont2, but arrange
* that these are obtained from the TDB image.
*
* @param base
*/
protected OntModel loadImportingOntology( Model base, Dataset ds ) {
// this is a test, so empty the base first just to be sure
base.removeAll();
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM );
spec.setImportModelGetter( new LocalTDBModelGetter( ds ) );
OntModel om = ModelFactory.createOntologyModel( spec, base );
// now read the source model
StringReader in = new StringReader( SOURCE );
om.read( in, null, "Turtle" );
return om;
}
/***********************************/
/* Inner class definitions */
/***********************************/
/**
* <p>A type of model getter that loads models from a local TDB instance,
* if they exist as named graphs using the model URI as the graph name.</p>
*/
static class LocalTDBModelGetter implements ModelGetter {
private Dataset ds;
public LocalTDBModelGetter( Dataset dataset ) {
ds = dataset;
}
@Override
public Model getModel( String uri ) {
throw new NotImplemented( "getModel( String ) is not implemented" );
}
@Override
public Model getModel( String uri, ModelReader loadIfAbsent ) {
Model m = ds.getNamedModel( uri );
// create the model if necessary. In actual fact, this example code
// will not exercise this code path, since we pre-define the models
// we want to see in TDB
if (m == null) {
m = ModelFactory.createDefaultModel();
loadIfAbsent.readModel( m, uri );
ds.addNamedModel( uri, m );
}
return m;
}
} // LocalTDBModelGetter
}
@crapo
Copy link

crapo commented Oct 16, 2013

I tried out this code with Jena 2.11.0, TDB 1.0.0. The code works the first time through but then if I run it again (repo already exists, models loaded) I get the error below. I guess prefix mappings are required?

Exception in thread "main" java.lang.NullPointerException
at com.hp.hpl.jena.tdb.store.DatasetPrefixesTDB.readPrefixMap(DatasetPrefixesTDB.java:174)
at com.hp.hpl.jena.sparql.graph.GraphPrefixesProjection.getNsPrefixMap(GraphPrefixesProjection.java:62)
at com.hp.hpl.jena.tdb.store.DatasetPrefixesTDB.getPrefixMapping(DatasetPrefixesTDB.java:223)
at com.hp.hpl.jena.tdb.store.DatasetPrefixesTDB.getPrefixMapping(DatasetPrefixesTDB.java:214)
at com.hp.hpl.jena.tdb.store.GraphTDB.createPrefixMapping(GraphTDB.java:75)
at com.hp.hpl.jena.graph.impl.GraphBase.getPrefixMapping(GraphBase.java:186)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.getPrefixMapping(ModelCom.java:959)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.withDefaultMappings(ModelCom.java:1003)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.(ModelCom.java:74)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.(ModelCom.java:70)
at com.hp.hpl.jena.rdf.model.ModelFactory.createModelForGraph(ModelFactory.java:176)
at com.hp.hpl.jena.sparql.core.DatasetImpl.graph2model(DatasetImpl.java:271)
at com.hp.hpl.jena.sparql.core.DatasetImpl.getDefaultModel(DatasetImpl.java:103)
at com.ge.research.sadl.jena.reasoner.ImportTDBModelsExample.run(ImportTDBModelsExample.java:64)
at com.ge.research.sadl.jena.reasoner.ImportTDBModelsExample.main(ImportTDBModelsExample.java:58)

@crapo
Copy link

crapo commented Oct 16, 2013

calling ds.close() at the end of run() seems to solve the problem.

@symulation
Copy link

org.openjena.atlas.lib.NotImplemented seems to have been deprecated. Import org.apache.jena.atlas.lib.NotImplemented instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment