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