简体   繁体   中英

Conversion from OWLOntology to Jena Model in Java

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.

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.

为了避免通过 i/o 流进行这种可怕的转换,您可以使用ONT-API :它实现了从图中直接读取 owl 公理,无需任何转换

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM