学习protege4.3-OWL API进行编程(一)


最近一直在学习protege4.3-OWL API相关内容,发现protege4.X版本在protege3.X版本的基础上做了很大的改动,而且网上有关protege3.X版本的API介绍较多,对protege4.X版本的API介绍较少,经过最近一段时间的学习,我来总结一下如何利用protege4.X版本的API进行编程,实现对本体的基本操作。

1.如何加载一个本体


加载已经存在的本体文件test.owl

OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
File file = new File("test.owl");
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);

2.如何保存本体


(1)将本体按原来的格式进行保存

manager.saveOntology(ontology, IRI.create(file.toURI()));


(2)将本体按owl/xml format格式进行保存

 //通过manager获得本体格式信息
OWLDocumentFormat format = manager.getOntologyFormat(ontology);
//将本体保存为 owl/xml 格式
OWLXMLDocumentFormat owlxmlFormat = new OWLXMLDocumentFormat();

if (format.isPrefixOWLOntologyFormat()) {            
    owlxmlFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());                                      
}
manager.saveOntology(ontology, owlxmlFormat, IRI.create(file.toURI()));


(3)将本体按Manchester OWL Syntax格式进行保存

//将本体保存为 Manchester OWL Syntax 格式
ManchesterSyntaxDocumentFormat manSyntaxFormat = new ManchesterSyntaxDocumentFormat();

if (format.isPrefixOWLOntologyFormat()) {   
   manSyntaxFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());                          
}

manager.saveOntology(ontology, manSyntaxFormat, new StreamDocumentTarget(new ByteArrayOutputStream()));

3.获取本体中的所有类


获取本体中所有的类,类的名称可通过getIRI.getFragment获得

for (OWLClass c : ontology1.getClassesInSignature()) {
    System.out.println(c.toString()+"----"+c.getIRI().getFragment()+"----"+c.getIRI().getStart());  
  }


结果格式如下:
类名结果

4.本体集成


将两个本体进行集成

//加载两个本体
File file1 = new File("test1.owl");
OWLOntology ontology1 = manager.loadOntologyFromOntologyDocument(file1);
File file2 = new File("test2.owl");
OWLOntology ontology2 = manager.loadOntologyFromOntologyDocument(file2);
//开始集成
OWLOntologyMerger merger = new OWLOntologyMerger(manager);
//集成后保存为merger.owl文件
File mergefile = new File("merger.owl");
IRI saveIRI=IRI.create(mergefile.toURI());
//集成后本体的命名空间
IRI mergedOntologyIRI = IRI.create("http://www.semanticweb.com/mymergedont");
OWLOntology merged = merger.createMergedOntology(manager, mergedOntologyIRI);
//保存集成后的本体
manager.saveOntology(merged,saveIRI);

你可能感兴趣的:(语义网)