OK - this makes some sense. But what if you have no intention of writing the
model back into TDB? For instance, you want to do the following steps:
1. read an ontology T-Box definition from one TDB dataset
2. create a new TDB directory to contain an extended form of the
ontology
3. create a new OntModel from the original ontology and add more
class/property definitions to it
4. write the new ontology into the new store.
Right now, in order to read the ontology, you need to be inside a WRITE
transaction. But once you try to create the OntModel from the base model in
step 3, you run into this transaction problem.
Can you read the model within a WRITE transaction, close the dataset without
actually committing, and then still use the model?
-----Original Message-----
From: Andy Seaborne [mailto:[email protected]]
Sent: Thursday, January 16, 2014 11:26 AM
To: [email protected]
Subject: Re: Allocation attempt on NodeTableReadOnly
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