Search in sources :

Example 1 with OWLOntologyAlreadyExistsException

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

the class AbstractOntologyCollectorImpl method exportToOWLOntology.

/**
     * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
     * afterwards.
     * 
     * FIXME not merging yet FIXME not including imported ontologies unless they are merged *before* storage.
     * 
     * @param merge
     * @return
     */
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI prefix) {
    long before = System.currentTimeMillis();
    // Create a new ontology
    OWLOntology root;
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    org.semanticweb.owlapi.model.IRI iri = org.semanticweb.owlapi.model.IRI.create(prefix + _id);
    try {
        root = ontologyManager.createOntology(iri);
    } catch (OWLOntologyAlreadyExistsException e) {
        // It should be impossible, but just in case.
        ontologyManager.removeOntology(ontologyManager.getOntology(iri));
        try {
            root = ontologyManager.createOntology(iri);
        } catch (OWLOntologyAlreadyExistsException e1) {
            root = ontologyManager.getOntology(iri);
        } catch (OWLOntologyCreationException e1) {
            log.error("Failed to assemble root ontology for scope " + iri, e);
            root = null;
        }
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to assemble root ontology for scope " + _id, e);
        root = null;
    }
    // Add the import declarations for directly managed ontologies.
    if (root != null) {
        if (merge) {
            final Set<OWLOntology> set = new HashSet<OWLOntology>();
            log.debug("Merging {} with its imports.", root);
            set.add(root);
            for (OWLOntologyID ontologyId : managedOntologies) {
                log.debug("Merging {} with {}.", ontologyId, root);
                set.add(getOntology(ontologyId, OWLOntology.class, true));
            }
            OWLOntologySetProvider provider = new OWLOntologySetProvider() {

                @Override
                public Set<OWLOntology> getOntologies() {
                    return set;
                }
            };
            OWLOntologyMerger merger = new OWLOntologyMerger(provider);
            try {
                root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to merge imports for ontology " + iri, e);
                root = null;
            }
        } else {
            // Add the import declarations for directly managed ontologies.
            List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
            OWLDataFactory df = ontologyManager.getOWLDataFactory();
            String base = prefix + getID();
            for (int i = 0; i < backwardPathLength; i++) base = URIUtils.upOne(URI.create(base)).toString();
            base += "/";
            // The key set of managedOntologies contains the ontology IRIs, not their storage keys.
            for (OWLOntologyID ontologyId : managedOntologies) {
                // XXX some day the versionIRI will be the only physical reference for the ontology
                org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(base + OntologyUtils.encode(ontologyId));
                changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI)));
            }
            ontologyManager.applyChanges(changes);
        }
    }
    log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);
    return root;
}
Also used : OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) AddImport(org.semanticweb.owlapi.model.AddImport) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) LinkedList(java.util.LinkedList) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 2 with OWLOntologyAlreadyExistsException

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

the class ODPRegistryCacheManager method retrieveRemoteResource.

/**
     * Gets the remote ontology and saves it locally
     * 
     * @param uri
     * @return
     * @throws OWLOntologyCreationException
     * @throws UnknownOWLOntologyException
     * @throws OWLOntologyStorageException
     */
private static synchronized OWLOntology retrieveRemoteResource(URI uri) throws OWLOntologyCreationException, UnknownOWLOntologyException, OWLOntologyStorageException {
    manager.setSilentMissingImportsHandling(true);
    manager.addMissingImportListener(new MissingImportListener() {

        public void importMissing(MissingImportEvent arg0) {
            if (!getUnresolvedURIs().contains(arg0.getImportedOntologyURI()))
                getUnresolvedURIs().add(arg0.getImportedOntologyURI().toURI());
        }
    });
    manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {

        @Override
        public void startedLoadingOntology(LoadingStartedEvent event) {
        // Nothing to do
        }

        @Override
        public void finishedLoadingOntology(LoadingFinishedEvent event) {
            URI onturi = event.getDocumentIRI().toURI();
            if (event.getException() != null) {
                getUnresolvedURIs().add(onturi);
                LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to resolve ontology at " + onturi + " . Skipping.", event.getException());
                return;
            }
            try {
                if (!uris.containsKey(onturi)) {
                    cacheOntology(onturi, newFile(), manager.getOntology(event.getOntologyID()));
                }
            } catch (UnknownOWLOntologyException e) {
                LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to cache ontology at " + onturi + " . Skipping.", e);
                getUnresolvedURIs().add(onturi);
            } catch (OWLOntologyStorageException e) {
                LoggerFactory.getLogger(ODPRegistryCacheManager.class).warn("Failed to cache ontology at " + onturi + " . Skipping.", e);
                getUnresolvedURIs().add(onturi);
            }
        }
    });
    OWLOntology ont;
    try {
        ont = manager.loadOntologyFromOntologyDocument(IRI.create(uri));
    } catch (OWLOntologyAlreadyExistsException e) {
        ont = manager.getOntology(e.getOntologyID().getOntologyIRI());
    }
    File file = newFile();
    cacheOntology(uri, file, ont);
    return ont;
}
Also used : MissingImportEvent(org.semanticweb.owlapi.model.MissingImportEvent) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) URI(java.net.URI) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) MissingImportListener(org.semanticweb.owlapi.model.MissingImportListener) OWLOntologyLoaderListener(org.semanticweb.owlapi.model.OWLOntologyLoaderListener) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) File(java.io.File) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 3 with OWLOntologyAlreadyExistsException

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

the class OWLAPIToClerezzaConverter method clerezzaGraphToOWLOntology.

public static OWLOntology clerezzaGraphToOWLOntology(org.apache.clerezza.commons.rdf.Graph graph, OWLOntologyManager ontologyManager) {
    /*
         * The root graph can be serialized and de-serialized, but before that we should decide what to do
         * with imports. We can proceed as follows:
         * 
         * for each import statement, - check if the ontology manager has (1) an ontology or (2) a mapping. -
         * if (1), just get it and add it to the merge pool - if (2), do nothing. the ontology manager should
         * handle it when loading the root, - if neither, fetch the graph and call the procideure on it
         * 
         * Alternatively, construct the whole reverse imports stack, then traverse it again, get the
         * OWLOntology version for each (with the phony mapper set) and add it to the merge pool
         * 
         * If it works, just add all the triples to a Graph, but no, we don't want to store that
         * change.
         */
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SerializingProvider serializingProvider = new JenaSerializerProvider();
    serializingProvider.serialize(out, graph, SupportedFormat.RDF_XML);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    OWLOntology ontology = null;
    try {
        ontology = ontologyManager.loadOntologyFromOntologyDocument(in);
    } catch (OWLOntologyAlreadyExistsException e) {
        ontology = ontologyManager.getOntology(e.getOntologyID());
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to serialize OWL Ontology " + ontology + "for conversion", e);
    }
    return ontology;
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SerializingProvider(org.apache.clerezza.rdf.core.serializedform.SerializingProvider) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)

Example 4 with OWLOntologyAlreadyExistsException

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

the class RegistryManagerImpl method activate.

protected void activate(Dictionary<String, Object> configuration) {
    // Parse configuration.
    try {
        lazyLoading = (Boolean) (configuration.get(RegistryManager.LAZY_LOADING));
    } catch (Exception ex) {
        lazyLoading = _LAZY_LOADING_DEFAULT;
    }
    try {
        retainIncomplete = (Boolean) (configuration.get(RegistryManager.RETAIN_INCOMPLETE));
    } catch (Exception ex) {
        retainIncomplete = _RETAIN_INCOMPLETE_DEFAULT;
    }
    Object obj = configuration.get(RegistryManager.REGISTRY_LOCATIONS);
    if (obj instanceof String[])
        locations = (String[]) obj;
    else if (obj instanceof String)
        locations = new String[] { (String) obj };
    if (locations == null)
        locations = new String[] { "stanbol_network.owl" };
    Object cachingPolicy = configuration.get(RegistryManager.CACHING_POLICY);
    if (cachingPolicy == null) {
        this.cachingPolicyString = _CACHING_POLICY_DEFAULT.name();
    } else {
        this.cachingPolicyString = cachingPolicy.toString();
    }
    final IRI[] offlineResources;
    if (this.offline != null) {
        List<IRI> paths = offline.getOntologySourceLocations();
        if (paths != null)
            offlineResources = paths.toArray(new IRI[0]);
        else
            // There are no offline paths.
            offlineResources = new IRI[0];
    } else
        // There's no offline configuration at all.
        offlineResources = new IRI[0];
    // Used only for creating the registry model, do not use for caching.
    OWLOntologyManager mgr = OWLOntologyManagerFactory.createOWLOntologyManager(offlineResources);
    OWLOntologyLoaderConfiguration conf = new OWLOntologyLoaderConfiguration();
    // If we are retaining incomplete registries, do not throw exceptions if imports fail.
    conf.setSilentMissingImportsHandling(retainIncomplete);
    // Load registries
    Set<OWLOntology> regOnts = new HashSet<OWLOntology>();
    for (String loc : locations) {
        try {
            IRI iri = IRI.create(loc);
            OWLOntologyDocumentSource src = null;
            OWLOntology o = null;
            if (iri.isAbsolute())
                src = new IRIDocumentSource(iri);
            else {
                // Relative IRI : use data file provider
                log.debug("Found relative IRI {} . Will try to retrieve from data file providers.", iri);
                Map<String, String> info = new HashMap<String, String>();
                if (dataFileProvider != null && dataFileProvider.isAvailable(null, loc, info))
                    src = new StreamDocumentSource(dataFileProvider.getInputStream(null, loc, info));
            }
            if (src != null)
                o = mgr.loadOntologyFromOntologyDocument(src, conf);
            if (o != null)
                regOnts.add(o);
            else
                log.warn("Failed to obtain OWL ontology from resource {}", loc);
        } catch (OWLOntologyAlreadyExistsException e) {
            log.info("Skipping cached ontology {}.", e.getOntologyID());
            continue;
        } catch (OWLOntologyCreationException e) {
            log.warn("Failed to load ontology " + loc + " - Skipping...", e);
            continue;
        } catch (Exception e) {
            log.warn("Invalid registry configuration " + loc + " - Skipping...", e);
            continue;
        }
    }
    // Create and set the cache.
    if (cachingPolicyString.equals(CachingPolicy.CENTRALISED.name())) {
        // this.cache = OWLOntologyManagerFactory.createOWLOntologyManager(offlineResources);
        if (cache == null) {
            log.warn("Caching policy is set as Centralised, but no ontology provider is supplied. Will use new in-memory tcProvider.");
            cache = new ClerezzaOntologyProvider(TcManager.getInstance(), offline, Parser.getInstance());
        }
    // else sta bene cosi'
    } else if (cachingPolicyString.equals(CachingPolicy.DISTRIBUTED.name())) {
        this.cache = null;
    }
    riFactory = new RegistryItemFactoryImpl(cache);
    // Build the model.
    createModel(regOnts);
    // Set the cache on libraries.
    Set<RegistryItem> visited = new HashSet<RegistryItem>();
    for (Registry reg : getRegistries()) for (RegistryItem child : reg.getChildren()) if (!visited.contains(child)) {
        if (child instanceof Library) {
            if (this.cache != null)
                ((Library) child).setCache(this.cache);
            else
                ((Library) child).setCache(new ClerezzaOntologyProvider(TcManager.getInstance(), offline, Parser.getInstance()));
        }
        visited.add(child);
    }
    if (isLazyLoading()) {
    // Nothing to do about it at the moment.
    } else {
        loadEager();
    }
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) HashMap(java.util.HashMap) OWLOntologyLoaderConfiguration(org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) IRIDocumentSource(org.semanticweb.owlapi.io.IRIDocumentSource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) StreamDocumentSource(org.semanticweb.owlapi.io.StreamDocumentSource) ClerezzaOntologyProvider(org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedObject(org.semanticweb.owlapi.model.OWLNamedObject) OWLObject(org.semanticweb.owlapi.model.OWLObject) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) OWLOntologyDocumentSource(org.semanticweb.owlapi.io.OWLOntologyDocumentSource) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) HashSet(java.util.HashSet)

Example 5 with OWLOntologyAlreadyExistsException

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

the class ODPRegistryCacheManager method retrieveLocalResource.

private static synchronized OWLOntology retrieveLocalResource(URI uri) throws OWLOntologyCreationException, ODPRegistryCacheException, URIUnresolvableException {
    File file = uris.get(uri);
    if (!file.exists()) {
        uris.remove(uri);
        return getOntology(uri);
    }
    manager.setSilentMissingImportsHandling(true);
    manager.addMissingImportListener(new MissingImportListener() {

        public void importMissing(MissingImportEvent arg0) {
            if (!getUnresolvedURIs().contains(arg0.getImportedOntologyURI()))
                getUnresolvedURIs().add(arg0.getImportedOntologyURI().toURI());
        }
    });
    IRI oi = oiri.get(uri);
    OWLOntology ontology = null;
    ontology = manager.getOntology(oi);
    if (ontology == null)
        try {
            ontology = manager.loadOntologyFromOntologyDocument(IRI.create(file));
        } catch (OWLOntologyAlreadyExistsException e) {
            ontology = manager.getOntology(e.getOntologyID());
        }
    return ontology;
}
Also used : MissingImportListener(org.semanticweb.owlapi.model.MissingImportListener) IRI(org.semanticweb.owlapi.model.IRI) MissingImportEvent(org.semanticweb.owlapi.model.MissingImportEvent) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) File(java.io.File) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)

Aggregations

OWLOntology (org.semanticweb.owlapi.model.OWLOntology)5 OWLOntologyAlreadyExistsException (org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)5 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)3 File (java.io.File)2 HashSet (java.util.HashSet)2 IRI (org.semanticweb.owlapi.model.IRI)2 MissingImportEvent (org.semanticweb.owlapi.model.MissingImportEvent)2 MissingImportListener (org.semanticweb.owlapi.model.MissingImportListener)2 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 SerializingProvider (org.apache.clerezza.rdf.core.serializedform.SerializingProvider)1 JenaSerializerProvider (org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider)1 ClerezzaOntologyProvider (org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider)1 RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)1 Library (org.apache.stanbol.ontologymanager.registry.api.model.Library)1 Registry (org.apache.stanbol.ontologymanager.registry.api.model.Registry)1