从OWLOntology到Java的Jena模型的转换 [英] Conversion from OWLOntology to Jena Model in Java
问题描述
我需要将数据从OWLOntology对象(OWL api的一部分)转换为Model对象(Jena Api的一部分).我的Java程序应该能够加载owl文件并将其内容发送到fuseki服务器.根据我的读物,只有Jena Api才可以通过Java程序使用fuseki服务器,这就是我使用它的原因.
I need to convert data from OWLOntology object(part of OWL api) to Model object(part of Jena Api). My Java program should be able to load owl file and send its content to fuseki server. According to what I read, working with fuseki server via Java program is possible only with Jena Api, that's why I use it.
因此,我找到了一些使用Jena api将本体发送到fuseki服务器的示例,并将其修改为该功能:
So I found some example of sending ontologies to fuseki server using Jena api, and modified it to this function :
private static void sendOntologyToFuseki(DatasetAccessor accessor, OWLOntology owlModel){
Model model;
/*
..
conversion from OWLOntology to Model
..
*/
if(accessor != null){
accessor.add(model);
}
}
此功能应将新的本体添加到fuseki服务器.任何想法如何填补缺失的转化?或任何其他想法,如何使用OWL API将本体发送到Fusionki服务器?
This function should add new ontologies to fuseki server. Any ideas how to fill missing conversion? Or any other ideas, how to send ontologies to fuseki server using OWL api?
我阅读了以下解决方案: Sparql查询不会更新通过Java代码插入一些数据时
I read solution of this : Sparql query doesn't upadate when insert some data through java code
但是我的Java程序的目的是递增地发送这些本体,因为它是非常大的数据,如果我将它们加载到本地内存中,则我的计算机将无法对其进行管理.
but purpose of my java program is to send these ontologies incrementally, because it's quite big data and if I load them into local memory, my computer does not manage it.
推荐答案
该想法是编写Java OutputStream
并将其通过管道传送到InputStream
.一个可能的实现可能看起来像这样:
The idea is to write to a Java OutputStream
and pipe this into an InputStream
. A possible implementation could look like this:
/**
* Converts an OWL API ontology into a JENA API model.
* @param ontology the OWL API ontology
* @return the JENA API model
*/
public static Model getModel(final OWLOntology ontology) {
Model model = ModelFactory.createDefaultModel();
try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is)) {
new Thread(new Runnable() {
@Override
public void run() {
try {
ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), os);
os.close();
} catch (OWLOntologyStorageException | IOException e) {
e.printStackTrace();
}
}
}).start();
model.read(is, null, "TURTLE");
return model;
} catch (Exception e) {
throw new RuntimeException("Could not convert OWL API ontology to JENA API model.", e);
}
}
或者,您可以简单地使用ByteArrayOutputStream
和ByteArrayInputStream
代替管道流.
Alternatively, you could simply use ByteArrayOutputStream
and ByteArrayInputStream
instead of piped streams.
这篇关于从OWLOntology到Java的Jena模型的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!