Jena PrefixMapping: base namespace missing when mo

2019-05-20 22:08发布

This the code I am using to load OntModel to a Dataset as a Named Model. Then I try to retrieve the PrefixMapping for the same in two different ways:

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..");
    }
}

Method 1:- Using the OntModel variable in which the OWL file was read. In this case I get the desired output which contains the prefix mapping for the ontology URI denoted by Prefixes : and MyModel

Method 1 OUTPUT :

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#

Method 2:- Using the OntModel variable obtained frem the dataset via dataset.getNamedModel("MyNamedModel");. In this case i fail to find the base prefixes.

Method 2 OUTPUT :

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#

The function ModelGenerator.getPrefixMap() is as follows:

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) );
            }
}

0条回答
登录 后发表回答