On 16/01/14 15:58, Ed Swing wrote:
First, a new ontology is created, populated and stored in TDB:
OntModel newModel = ModelFactory.createOntologyModel();
// various things to populate the ontology model omitted
System.out.println("Starting write:");
Dataset dataset =TDBFactory.createDataset(directory);
Dataset.addNamedModel("metadata", newModel);
dataset.begin(ReadWrite.WRITE);
try {
dataset.commit();
} finally {
dataset.end();
}
Then later, in another method, I want to read this ontology (and edit it):
Dataset dataset =TDBFactory.createDataset(directory);
System.out.println("Starting read:");
Model model = null;
dataset.begin(ReadWrite.READ);
// Get model inside the transaction
model = dataset.getNamedModel("metadata");
dataset.end();
OntModel ontModel =
ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF,
model);
When you do dataset.getNamedModel("metadata") what you get is a Java
object that provides the Model interface as a view of the dataset. The
real data is still in the dataset.
If you want to update ontMode, you'll need to be inside a write
transaction, which you commit.
dataset.begin(ReadWrite.WRITE);
model = dataset.getNamedModel("metadata");
OntModel ontModel =
ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF,
model);
String NS = "http://example/" ;
Resource s = ontModel.createResource(NS+"s") ;
Property p = ontModel.createProperty(NS+"p") ;
ontModel.add(s, p, "xyz") ;
dataset.commit() ;
dataset.end();
You don't need the Dataset.addNamedModel - you can always get a named
model from a TDB dataset. .addNamedModel is a copy-in operation for TDB.
Andy