简体   繁体   中英

Jena API - How can I handle OWL files?

I have the below code to list individuals of an ontology:

public static void main(String[] args) {
    OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    String fileName = "C:/Users/Ikno/Desktop/workspace/Jena/ontrdf.rdf";

    try {
        InputStream inputStream = new FileInputStream(fileName);
        model.read(inputStream, "RDF/XML");
        //model.read(inputStream, "OWL/XML");
        inputStream.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    ExtendedIterator<Individual> itI = model.listIndividuals();

    while (itI.hasNext()) {
        Individual i = itI.next();
        System.out.println(i.getLocalName());
    }
}

The code is OK, and it returns all the individual local names. The problem is that I created the OWL ontology in Protégé and it is a wasteful step to convert it to RDF, just to be operated by Jena. If I uncomment model.read(inputStream, "OWL/XML"); , it gives me the following error:

class org.apache.jena.riot.RiotException
[line: 266, col: 31] {E201} Multiple children of property element

Can't Jena support this kind of operation with OWL format?

As an option you can use ONT-API , which is a kind of bridge between OWL-API and Apache Jena. Thus, it supports all jena and owl-api formats together (OWL/XML is the original format of OWL-API&protege only), but (note) without its own implementation.

According to Jena Documentation here If you look at the read method, it is clear that Jena does not support OWL/XML. Predefined values for lang (Second Arg to read method) are "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3". null represents the default language, "RDF/XML". "RDF/XML-ABBREV" is a synonym for "RDF/XML". So in your case I would save the ontology as RDF/XML and then read it with Jena.

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