Search in sources :

Example 1 with OWLOntologyDocumentSource

use of org.semanticweb.owlapi.io.OWLOntologyDocumentSource in project stanbol by apache.

the class ScopeManagerImpl method activate.

/**
     * Called within both OSGi and non-OSGi environments.
     * 
     * @param configuration
     * @throws IOException
     */
protected void activate(Dictionary<String, Object> configuration) throws IOException {
    long before = System.currentTimeMillis();
    // Assign singleton instance
    me = this;
    // Parse configuration
    if (offline != null)
        ontonetNS = offline.getDefaultOntologyNetworkNamespace();
    scopeRegistryId = (String) configuration.get(ScopeManager.ID_SCOPE_REGISTRY);
    if (scopeRegistryId == null)
        scopeRegistryId = _ID_SCOPE_REGISTRY_DEFAULT;
    configPath = (String) configuration.get(ScopeManager.CONFIG_ONTOLOGY_PATH);
    if (configPath == null)
        configPath = _CONFIG_ONTOLOGY_PATH_DEFAULT;
    // Bind components, starting with the local directories.
    List<String> dirs = new ArrayList<String>();
    try {
        for (IRI iri : offline.getOntologySourceLocations()) dirs.add(iri.toString());
    } catch (NullPointerException ex) {
    // Ok, go empty
    }
    bindResources();
    // String tfile = (String) configuration.get(CONFIG_FILE_PATH);
    // if (tfile != null) this.configPath = tfile;
    // String tns = (String) configuration.get(KRES_NAMESPACE);
    // if (tns != null) this.kresNs = tns;
    // configPath = (String) configuration.get(CONFIG_FILE_PATH);
    // If there is no configuration file, just start with an empty scope set
    Object connectivityPolicy = configuration.get(ScopeManager.CONNECTIVITY_POLICY);
    if (connectivityPolicy == null) {
        this.connectivityPolicyString = _CONNECTIVITY_POLICY_DEFAULT;
    } else {
        this.connectivityPolicyString = connectivityPolicy.toString();
    }
    String configPath = getOntologyNetworkConfigurationPath();
    if (configPath != null && !configPath.trim().isEmpty()) {
        OWLOntology oConf = null;
        OWLOntologyManager tempMgr = OWLOntologyManagerFactory.createOWLOntologyManager(offline.getOntologySourceLocations().toArray(new IRI[0]));
        OWLOntologyDocumentSource oConfSrc = null;
        try {
            log.debug("Try to load the configuration ontology from a local bundle relative path");
            InputStream is = this.getClass().getResourceAsStream(configPath);
            oConfSrc = new StreamDocumentSource(is);
        } catch (Exception e1) {
            try {
                log.debug("Cannot load from a local bundle relative path", e1);
                log.debug("Try to load the configuration ontology resolving the given IRI");
                IRI iri = IRI.create(configPath);
                if (!iri.isAbsolute())
                    throw new Exception("IRI seems to be not absolute! value was: " + iri.toQuotedString());
                oConfSrc = new IRIDocumentSource(iri);
            } catch (Exception e) {
                try {
                    log.debug("Cannot load from the web", e1);
                    log.debug("Try to load the configuration ontology as full local file path");
                    oConfSrc = new FileDocumentSource(new File(configPath));
                } catch (Exception e2) {
                    log.error("Cannot load the configuration ontology from parameter value: " + configPath, e2);
                }
            }
        }
        if (oConfSrc == null) {
            log.warn("No ONM configuration file found at path " + configPath + ". Starting with blank scope set.");
        } else {
            try {
                oConf = tempMgr.loadOntologyFromOntologyDocument(oConfSrc);
            } catch (OWLOntologyCreationException e) {
                log.error("Cannot create the configuration ontology", e);
            }
        }
        // Create and populate the scopes from the config ontology.
        bootstrapOntologyNetwork(oConf);
    } else {
        // No ontology supplied. Access the local graph
        rebuildScopes();
    }
    log.debug(ScopeManager.class + " activated. Time : {} ms.", System.currentTimeMillis() - before);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IRIDocumentSource(org.semanticweb.owlapi.io.IRIDocumentSource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) NoSuchScopeException(org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) IOException(java.io.IOException) StreamDocumentSource(org.semanticweb.owlapi.io.StreamDocumentSource) ScopeManager(org.apache.stanbol.ontologymanager.servicesapi.scope.ScopeManager) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) FileDocumentSource(org.semanticweb.owlapi.io.FileDocumentSource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLOntologyDocumentSource(org.semanticweb.owlapi.io.OWLOntologyDocumentSource) File(java.io.File)

Example 2 with OWLOntologyDocumentSource

use of org.semanticweb.owlapi.io.OWLOntologyDocumentSource 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)

Aggregations

IRIDocumentSource (org.semanticweb.owlapi.io.IRIDocumentSource)2 OWLOntologyDocumentSource (org.semanticweb.owlapi.io.OWLOntologyDocumentSource)2 StreamDocumentSource (org.semanticweb.owlapi.io.StreamDocumentSource)2 IRI (org.semanticweb.owlapi.model.IRI)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)2 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)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 RegistryItem (org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)1 DuplicateIDException (org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)1 MissingOntologyException (org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException)1