这是我用来将OntModel
加载到Dataset
作为命名模型的代码。然后我尝试以两种不同的方式检索PrefixMapping
:
public static void loadDatasetwithNamedModels(){
OntModel namedModel = null;
Dataset dataset = null;
dataset = TDBFactory.createDataset("./path/to/TDB_DIR");
namedModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
dataset.begin(ReadWrite.WRITE);
try{
namedModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
FileManager.get().readModel( namedModel, "./Path/to/OWLFile.owl");
dataset.addNamedModel("MyNamedModel", namedModel);
//Method 1
ModelGenerator.setModel(ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, namedModel)); // 1. returns the baseModel Namespace prefix mapping.
//Method 2
//ModelGenerator.setModel(ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, dataset.getNamedModel("MyNamedModel"))); // 2. Does not returns the baseModel Namespace prefix mapping.
ModelGenerator.setDataset(dataset);
ModelGenerator.getPrefixMap();
dataset.commit();
} finally {
dataset.end();
System.out.println("Database Ready..");
}
}
方法1: - 使用读取OWL文件的OntModel
变量。在这种情况下,我得到所需的输出,其中包含由前缀:
和MyModel
方法1输出:
6
: http://www.semanticweb.org/ontologies/2013/8/MyModel#
rdfs : http://www.w3.org/2000/01/rdf-schema#
owl : http://www.w3.org/2002/07/owl#
xsd : http://www.w3.org/2001/XMLSchema#
MyModel : http://www.semanticweb.org/ontologies/2013/8/MyModel#
rdf : http://www.w3.org/1999/02/22-rdf-syntax-ns#
方法2: - 通过OntModel
使用获得的dataset.getNamedModel("MyNamedModel");
变量来获取数据集。在这种情况下,我找不到基本前缀。
方法2输出:
4
rdfs : http://www.w3.org/2000/01/rdf-schema#
owl : http://www.w3.org/2002/07/owl#
xsd : http://www.w3.org/2001/XMLSchema#
rdf : http://www.w3.org/1999/02/22-rdf-syntax-ns#
函数ModelGenerator.getPrefixMap()如下:
public static void getPrefixMap(){
Map<String, String> map = ModelGenerator.getModel().getNsPrefixMap();
System.out.println(map.size());
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String key = (String) it.next();
System.out.println("" + key + " : "+map.get(key) );
}
}