Conversion from OWLOntology to Jena Model in Java

2020-04-30 18:00发布

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.

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

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?

I read solution of this : Sparql query doesn't upadate when insert some data through java code

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.

2条回答
smile是对你的礼貌
2楼-- · 2020-04-30 18:15

To avoid such kind of dreadful transformation through i/o streams you can use ONT-API: it implements direct reading of the owl-axioms from the graph without any conversion

查看更多
可以哭但决不认输i
3楼-- · 2020-04-30 18:26

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

Alternatively, you could simply use ByteArrayOutputStream and ByteArrayInputStream instead of piped streams.

查看更多
登录 后发表回答