Jena操作本体的持久化方法(二):持久化到数据库
Saturday, April 7th, 2007 | 1 views其实对于本体的应用而言,似乎持久化到数据库才是正道,关于本体到数据库的映射有许多的论文讨论,我以为好的映射关系首先应当是完备的,既不减少信息,其次应当能充分利用数据库的优势(效率,权限控制等),毕竟,本体应用现在极大的障碍就是效率。
下面介绍一些我自己总结的一些关于持久化到数据库一些代码。需要说明一下的,对于语义网,我是一个真正的初学者,一些简单的所得所知全是自己摸索的,这些代码也是一个一个试Jena API试出来的,许多有问题的地方拜托能帮忙指正一下。
public class OntoDBUtil {
private OntoDBUtil(){
}
public static OntModelSpec getModelSpec( ModelMaker maker ) {
/*create a spec for the new ont model that will use no inference over models
made by the given maker (which is where we get the persistent models from)*/
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM );
spec.setImportModelMaker( maker );
return spec;
}
/* 连接数据库*/
static IDBConnection connectDB(String DB_URL,String DB_USER,String DB_PASSWD,String DB_NAME){
return new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB_NAME);
} /*从文件读取本体并将其存入数据库*/
static OntModel createDBModelFromFile(IDBConnection con,String name,String filePath){
ModelMaker maker = ModelFactory.createModelRDBMaker(con);
Model base=maker.createModel(name);
OntModel newmodel=ModelFactory.createOntologyModel(getModelSpec(maker),base);
newmodel.read(filePath);
return newmodel;
}
/*从数据库中得到已存入本体*/
static OntModel getModelFromDB(IDBConnection con,String name){
ModelMaker maker = ModelFactory.createModelRDBMaker(con);
Model base=maker.getModel(name);
OntModel newmodel=ModelFactory.createOntologyModel(getModelSpec(maker),base);
return newmodel;
}
}