Search in sources :

Example 16 with RegistryItem

use of org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem 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 17 with RegistryItem

use of org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem in project stanbol by apache.

the class RegistryManagerImpl method populateLibrary.

protected Library populateLibrary(OWLNamedObject ind, Set<OWLOntology> registries) throws RegistryContentException {
    IRI libId = ind.getIRI();
    RegistryItem lib = null;
    if (population.containsKey(libId)) {
        // We are not allowing multityping either.
        lib = population.get(libId);
        if (!(lib instanceof Library))
            throw new RegistryContentException("Inconsistent multityping: for item " + libId + " : {" + Library.class + ", " + lib.getClass() + "}");
    } else {
        lib = riFactory.createLibrary(ind);
        try {
            population.put(lib.getIRI(), lib);
        } catch (Exception e) {
            log.error("Invalid identifier for library item " + lib, e);
            return null;
        }
    }
    // EXIT nodes.
    Set<OWLNamedObject> ironts = new HashSet<OWLNamedObject>();
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (OWLOntology o : registries) {
        if (ind instanceof OWLIndividual) {
            // Get usages of hasOntology as an object property
            for (OWLIndividual value : ((OWLIndividual) ind).getObjectPropertyValues(hasOntology, o)) if (value.isNamed())
                ironts.add(value.asOWLNamedIndividual());
            // Get usages of hasOntology as an annotation property
            for (OWLAnnotationAssertionAxiom ann : o.getAnnotationAssertionAxioms(ind.getIRI())) if (hasOntologyAnn.equals(ann.getProperty())) {
                OWLAnnotationValue value = ann.getValue();
                if (value instanceof OWLNamedObject)
                    ironts.add((OWLNamedObject) value);
                else if (value instanceof IRI)
                    ironts.add(df.getOWLNamedIndividual((IRI) value));
            }
        }
    }
    for (OWLNamedObject iront : ironts) {
        IRI childId = iront.getIRI();
        // If some populate*() method has created it, it will be there.
        RegistryItem ront = population.get(childId);
        // Otherwise populating it will also put it in population.
        if (ront == null)
            ront = populateOntology(iront, registries);
        lib.addChild(ront);
        if (ontologyIndex.get(childId) == null)
            ontologyIndex.put(childId, new HashSet<IRI>());
        ontologyIndex.get(childId).add(libId);
    }
    return (Library) lib;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLNamedObject(org.semanticweb.owlapi.model.OWLNamedObject) OWLAnnotationValue(org.semanticweb.owlapi.model.OWLAnnotationValue) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 18 with RegistryItem

use of org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem in project stanbol by apache.

the class RegistryUtils method containsOntologyRecursive.

/**
 * Utility method to recurse into registry items.
 *
 * TODO: move this to main?
 *
 * @param item
 * @param ontologyId
 * @return
 */
public static boolean containsOntologyRecursive(RegistryItem item, IRI ontologyId) {
    boolean result = false;
    if (item instanceof RegistryOntology) {
        // An Ontology MUST have a non-null URI.
        try {
            IRI iri = item.getIRI();
            result |= iri.equals(ontologyId);
        } catch (Exception e) {
            return false;
        }
    } else if (item instanceof Library || item instanceof Registry)
        // Inspect children
        for (RegistryItem child : item.getChildren()) {
            result |= containsOntologyRecursive(child, ontologyId);
            if (result)
                break;
        }
    return result;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) RegistryOntology(org.apache.stanbol.ontologymanager.registry.api.model.RegistryOntology) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)

Aggregations

RegistryItem (org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)18 Registry (org.apache.stanbol.ontologymanager.registry.api.model.Registry)12 Library (org.apache.stanbol.ontologymanager.registry.api.model.Library)10 IRI (org.semanticweb.owlapi.model.IRI)8 RegistryOntology (org.apache.stanbol.ontologymanager.registry.api.model.RegistryOntology)7 Test (org.junit.Test)7 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)7 HashSet (java.util.HashSet)6 RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)4 RegistryManagerImpl (org.apache.stanbol.ontologymanager.registry.impl.RegistryManagerImpl)4 OWLOntologyAlreadyExistsException (org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)4 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)3 OWLNamedObject (org.semanticweb.owlapi.model.OWLNamedObject)3 HashMap (java.util.HashMap)2 ClerezzaOntologyProvider (org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider)2 LibraryContentNotLoadedException (org.apache.stanbol.ontologymanager.registry.api.LibraryContentNotLoadedException)2 OWLAnnotationAssertionAxiom (org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom)2 OWLAnnotationValue (org.semanticweb.owlapi.model.OWLAnnotationValue)2 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)2