Search code examples
javaowlontologyowl-apiiri

Specifying ontology IRI in java


I am trying to merge two ontologies to create a new single ontology. For this, how should I specify the IRI of the new ontology? Should it be the path to the new file or some url?

Here is my code :

void createRepOntology(OWLOntology O1, OWLOntology O2) throws IOException, OWLOntologyCreationException, OWLOntologyStorageException
{
    OWLOntologyManager man = OWLManager.createOWLOntologyManager();
    OWLDataFactory fac =  man.getOWLDataFactory();
    OWLOntology ont;
    FileWriter writer = new FileWriter(new File(System.getProperty("user.dir") + "/repOnt.owl"));
    String url = "file://" + System.getProperty("user.dir") + "/repOnt.owl";

    IRI iri = IRI.create(url);

    ont = man.createOntology(iri);

            . . .

    OWLClass c = fac.getOWLClass(IRI.create(url1));
    OWLAxiom ax = fac.getOWLDeclarationAxiom(c);
    man.addAxiom(ont, ax);
    man.saveOntology(ont);

}

The ontology created begins as follows :

<?xml version="1.0"?>
<rdf:RDF xmlns="file:///home/repOnt.owl#"
 xml:base="file:///home/repOnt.owl"
 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 xmlns:owl="http://www.w3.org/2002/07/owl#"
 xmlns:productCatalog="http://www.semanticweb.org/ontologies/2015/11/productCatalog#"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:ElectronicDevices="http://www.semanticweb.org/ontologies/2015/9/ElectronicDevices#"
 xmlns:Acer_Backpack_15="http://www.semanticweb.org/ontologies/2015/11/productCatalog#Acer_Backpack_15.6&#39;&#39;">
<owl:Ontology rdf:about="file:///home/repOnt.owl"/>

The value of xmlns is the path to the file. But when I specify a url as the IRI of the new ontology like: http://www.semanticweb.org/ontologies/2015/11/productCatalog#, it gives the following exception :

Exception in thread "main" org.semanticweb.owlapi.model.OWLOntologyStorageException: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
at org.semanticweb.owlapi.util.AbstractOWLOntologyStorer.storeOntology(AbstractOWLOntologyStorer.java:147)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:841)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:827)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:821)
at ontomap.RepOnt.createRepOntology(RepOnt.java:112)
at ontomap.RepOnt.run(RepOnt.java:85)
at ontomap.RepOnt.main(RepOnt.java:49)
Caused by: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1074)
at org.semanticweb.owlapi.util.AbstractOWLOntologyStorer.storeOntology(AbstractOWLOntologyStorer.java:105)
... 6 more
Java Result: 1

So what is the correct way to specify the IRI of the new ontology?


Solution

  • Should it be the path to the new file or some url?

    The ontology IRI should be an IRI. While there are "file IRIs" (e.g., file://something-here), you'd typically want the ontology IRI to be something useful, and if you intend to publish the ontology online, it can be helpful (but it is not mandatory) to make the ontology IRI an IRI from which you can retrieve the actual ontology.

    The issue of letting the OWL API know where to save an ontology that you've created is really a different problem. The examples in the OWL API documentation include a test case called shouldSaveOntology that shows that there's a version of OWLOntologyManager#saveOntology() that takes two arguments, the ontology and the path at which to save it, and provides an example:

    File file = folder.newFile("owlapiexamples_saving.owl");
    manager.saveOntology(ontology, IRI.create(file.toURI()));