我在StoryModel中創建了幾個方法。如果代碼中有任何錯誤,我真的很感謝修正。在java中使用jena API顯示RDF
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) { }
}
}
在SemBookMain我創建了一個模型,並初始化類都使用與StoryModel創建的方法與資源,並顯示它
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();
}
}
我真的很抱歉,我加輸出如下,請看到的評論我的評論。
我不明白關於錯誤1 & 2以及爲什麼資源不顯示。有一些我失蹤,但無法弄清楚。
對不起失蹤輸出 – user2057437 2013-03-15 04:03:44
錯誤1:log4j的:警告沒有附加目的地可以爲記錄器(org.apache.jena.info).log4j發現:警告請正確初始化log4j系統。 – user2057437 2013-03-15 04:07:11
輸出: –
user2057437
2013-03-15 04:08:59