Reputation: 81
I have created a few methods in StoryModel. If there is any mistake in the code I really appreciate corrections.
public class StoryModel {
String dbName = "DB3";
String sns = "http://somebook/SemBook#";
String ns;
Dataset ds;
OntModel om;
// create model and connect to database
public StoryModel(String storyName){
ns = sns + storyName;
ds = TDBFactory.createDataset(dbName);
om = ModelFactory.createOntologyModel();
}
// create classes in model
public void initModel() {
om.createClass(ns + "Person");
om.createClass(ns + "Event");
om.createClass(ns + "Place");
om.createClass(ns + "Time");
om.createClass(ns + "Object");
saveModel();
}
//Read & write in database
//Display it
public void saveModel() {
ds.begin(ReadWrite.WRITE);
om.write(System.out, "RDF/XML-ABBREV");
}
// Create resource to class in model
public Resource createResource(String resourceName, String clsName) {
OntResource resource = om.createOntResource(ns + resourceName);
String ruri = ns + resource;
OntClass clsuri = om.getOntClass(ns + clsName);
Individual i = om.createIndividual(ruri, clsuri);
return i;
}
/*
* Return an RDF resource object given the URI of the resource as a string. The
* URI can be represented in full, or as a "prefix:localName".
* @param resourceName The full URI of the resource
* @return An RDF resource object
*/
public Resource stringToResource( String resourceName ) {
String resourceURI = om.expandPrefix( resourceName );
return om.getResource( resourceURI );
}
// Delete the resource
public void deleteResource(String resourceName) {
try {
OntResource resource = om.getOntResource(ns + resourceName);
if (resource != null)
{
resource.remove();
}
else {
System.out.println("Resource not present");
}
}
catch (Exception e) { }
}
}
In SemBookMain I created a model and initialized classes to resource using methods which are created in StoryModel and display it
public class SemBookMain {
public static void main(String[] args) {
// create and initialize a model
StoryModel sm = new StoryModel("alice");
//sm.saveModel();
sm.initModel();
// add resources
String clsName = "Person";
String[] ar = {"Alice", "Peter", "Ben", "Robin"};
for (String r : ar) {
Resource res = sm.createResource(r, clsName);
}
sm.saveModel();
}
}
I am really sorry I added output as comment below so please see to my comment.
I don't understand about ERROR 1 & 2 and why the resource are not displayed. There is something I am missing but couldn't figure it out.
Upvotes: 0
Views: 687
Reputation: 16630
ds.begin(ReadWrite.WRITE);
but no ds.commit
initModel
calls saveModel
and then you later call saveModel
again.
Follow the code patterns at:
http://jena.apache.org/documentation/tdb/tdb_transactions.html#write-transactions
Upvotes: 2