Search in sources :

Example 1 with OWLOntologyIRIMapper

use of org.semanticweb.owlapi.model.OWLOntologyIRIMapper in project stanbol by apache.

the class ClerezzaOntologyProvider method loadInStore.

@Override
public OWLOntologyID loadInStore(final org.semanticweb.owlapi.model.IRI ontologyIri, String formatIdentifier, boolean force, Origin<?>... origins) throws IOException {
    log.debug("Loading {}", ontologyIri);
    if (ontologyIri == null)
        throw new IllegalArgumentException("Ontology IRI cannot be null.");
    org.semanticweb.owlapi.model.IRI location = null;
    if (force)
        location = null;
    else
        for (OWLOntologyIRIMapper mapper : mappers) {
            location = mapper.getDocumentIRI(ontologyIri);
            if (location != null)
                break;
        }
    if (location == null) {
        if (isOfflineMode())
            throw new IllegalStateException("Cannot retrieve " + ontologyIri + " while Stanbol is in offline mode. " + "No resource with that identifier was found locally.");
        else
            location = ontologyIri;
    }
    log.info("found {} in {}", ontologyIri, location);
    // Add the physical IRI to the origins.
    origins = Arrays.copyOf(origins, origins.length + 1);
    origins[origins.length - 1] = Origin.create(ontologyIri);
    checkReplaceability(origins);
    // Get ordered list of preferred/supported formats, or use the specified one.
    List<String> supported = OntologyUtils.getPreferredSupportedFormats(parser.getSupportedFormats());
    List<String> formats;
    if (formatIdentifier == null || "".equals(formatIdentifier.trim()))
        formats = supported;
    else {
        formats = new LinkedList<String>();
        // Pre-check supported format
        if (supported.contains(formatIdentifier))
            formats.add(formatIdentifier);
        for (String sup : supported) if (sup != null && !formats.contains(sup))
            formats.add(sup);
    }
    for (String currentFormat : formats) {
        try {
            final URLConnection con = location.toURI().toURL().openConnection();
            con.setRequestProperty("Accept", currentFormat);
            final InputStream is = con.getInputStream();
            if (is != null) {
                /*
                     * We provide the current format, so the recursive call won't be trying to sort preferred
                     * formats again. Also, we provide the ontologyIRI as the preferred key, since we already
                     * know it.
                     */
                OWLOntologyID key = loadInStore(is, currentFormat, force, origins);
                // if (key != null && !key.isEmpty()) setLocatorMapping(ontologyIri, key);
                return key;
            }
        } catch (UnsupportedFormatException e) {
            log.debug("FAILURE format {} (unsupported). Trying next one.", currentFormat);
            continue;
        } catch (Exception e) {
            log.debug("FAILURE format {} (parse error). Will try next one.", currentFormat);
            continue;
        }
    }
    // No parser worked, return null.
    log.error("All parsers failed, giving up.");
    return null;
}
Also used : InputStream(java.io.InputStream) URLConnection(java.net.URLConnection) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) EntityAlreadyExistsException(org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException) OntologyHandleException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException) IOException(java.io.IOException) OrphanOntologyKeyException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) OWLOntologyIRIMapper(org.semanticweb.owlapi.model.OWLOntologyIRIMapper) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID)

Example 2 with OWLOntologyIRIMapper

use of org.semanticweb.owlapi.model.OWLOntologyIRIMapper in project stanbol by apache.

the class TestOWLAPIInputSources method testAutoIRIMapper.

@Test
public void testAutoIRIMapper() throws Exception {
    URL url = getClass().getResource("/ontologies");
    assertNotNull(url);
    File file = new File(url.toURI());
    assertTrue(file.exists());
    assertTrue(file.isDirectory());
    OWLOntologyIRIMapper mapper = new AutoIRIMapper(file, true);
    IRI dummyiri = IRI.create("http://stanbol.apache.org/ontologies/peanuts/dummycharacters.owl");
    // Cleanup may be required if previous tests have failed.
    if (mapper.getDocumentIRI(dummyiri) != null) {
        new File(mapper.getDocumentIRI(dummyiri).toURI()).delete();
        ((AutoIRIMapper) mapper).update();
    }
    assertFalse(dummyiri.equals(mapper.getDocumentIRI(dummyiri)));
    // Create a new ontology in the test resources.
    OWLOntologyManager mgr = OWLOntologyManagerFactory.createOWLOntologyManager(null);
    OWLOntology o = mgr.createOntology(dummyiri);
    File f = new File(URI.create(url.toString() + "/dummycharacters.owl"));
    mgr.saveOntology(o, new WriterDocumentTarget(new FileWriter(f)));
    assertTrue(f.exists());
    ((AutoIRIMapper) mapper).update();
    // The old mapper should be able to locate the new ontology.
    assertFalse(dummyiri.equals(mapper.getDocumentIRI(dummyiri)));
    // A new mapper too
    OWLOntologyIRIMapper mapper2 = new AutoIRIMapper(new File(url.toURI()), true);
    assertFalse(dummyiri.equals(mapper2.getDocumentIRI(dummyiri)));
    // cleanup
    f.delete();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) WriterDocumentTarget(org.semanticweb.owlapi.io.WriterDocumentTarget) OWLOntologyIRIMapper(org.semanticweb.owlapi.model.OWLOntologyIRIMapper) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) FileWriter(java.io.FileWriter) AutoIRIMapper(org.semanticweb.owlapi.util.AutoIRIMapper) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Aggregations

OWLOntologyIRIMapper (org.semanticweb.owlapi.model.OWLOntologyIRIMapper)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 EntityAlreadyExistsException (org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException)1 UnsupportedFormatException (org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException)1 OntologyHandleException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException)1 OrphanOntologyKeyException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException)1 Test (org.junit.Test)1 WriterDocumentTarget (org.semanticweb.owlapi.io.WriterDocumentTarget)1 IRI (org.semanticweb.owlapi.model.IRI)1 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)1 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)1 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)1 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)1 AutoIRIMapper (org.semanticweb.owlapi.util.AutoIRIMapper)1