Search in sources :

Example 11 with RegistryItem

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

the class TestRegistryManager method testCachingCentralised.

/**
 * Verifies that by instantiating a new {@link RegistryManager} with a centralised caching policy and
 * loading two registries, they share the same cache ontology manager.
 *
 * @throws Exception
 */
@Test
public void testCachingCentralised() throws Exception {
    // Change the caching policy and setup a new registry manager.
    configuration.put(RegistryManager.CACHING_POLICY, CachingPolicy.CENTRALISED);
    regman = new RegistryManagerImpl(offline, provider, configuration);
    // Check that the configuration was set.
    assertNotNull(regman);
    assertSame(CachingPolicy.CENTRALISED, regman.getCachingPolicy());
    // All registries must have the same cache.
    Iterator<Library> it = regman.getLibraries().iterator();
    OntologyProvider<?> cache = it.next().getCache();
    while (it.hasNext()) assertSame(cache, it.next().getCache());
    // Now "touch" a library.
    Registry reg;
    do reg = regman.getRegistries().iterator().next(); while (!reg.hasChildren());
    assertNotNull(reg);
    // There has to be at least one non-empty lib from the test ontologies.
    Library lib = null;
    RegistryItem[] children = reg.getChildren();
    for (int i = 0; i < children.length && lib == null; i++) if (children[i] instanceof Library)
        lib = (Library) (children[i]);
    assertNotNull(lib);
    // Touch the library. Also test that the listener system works.
    assertFalse(lib.getOntologies(OWLOntology.class).isEmpty());
}
Also used : RegistryManagerImpl(org.apache.stanbol.ontologymanager.registry.impl.RegistryManagerImpl) 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) Test(org.junit.Test)

Example 12 with RegistryItem

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

the class LibraryImpl method getOntologies.

@Override
public <O> Set<O> getOntologies(Class<O> returnType) throws RegistryContentException {
    /*
         * Note that this implementation is not synchronized. Listeners may indefinitely be notified before or
         * after the rest of this method is executed. If listeners call loadOntologies(), they could still get
         * a RegistryContentException, which however they can catch by calling loadOntologies() and
         * getOntologies() in sequence.
         */
    fireContentRequested(this);
    // If no listener has saved the day by loading the ontologies by now, an exception will be thrown.
    if (!loaded)
        throw new LibraryContentNotLoadedException(this);
    Set<O> ontologies = new HashSet<O>();
    for (RegistryItem child : getChildren()) {
        if (child instanceof RegistryOntology) {
            O o = getCache().getStoredOntology(child.getIRI(), returnType);
            // thrown when loading it), but just in case.
            if (o != null)
                ontologies.add(o);
            else
                throw new RegistryOntologyNotLoadedException((RegistryOntology) child);
        }
    }
    return ontologies;
}
Also used : RegistryOntology(org.apache.stanbol.ontologymanager.registry.api.model.RegistryOntology) RegistryOntologyNotLoadedException(org.apache.stanbol.ontologymanager.registry.api.RegistryOntologyNotLoadedException) LibraryContentNotLoadedException(org.apache.stanbol.ontologymanager.registry.api.LibraryContentNotLoadedException) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) HashSet(java.util.HashSet)

Example 13 with RegistryItem

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

the class RegistryManagerImpl method computeLoadFactors.

/**
 * @deprecated with each library having its own cache, load balancing is no longer necessary
 * @return
 */
protected Map<IRI, Float> computeLoadFactors() {
    Map<IRI, Float> loadFactors = new HashMap<IRI, Float>();
    for (Registry r : getRegistries()) {
        int tot = 0, num = 0;
        RegistryItem[] children = r.getChildren();
        for (int i = 0; i < children.length; i++) {
            if (children[i] instanceof Library) {
                if (((Library) children[i]).isLoaded())
                    num++;
                tot++;
            }
        }
        loadFactors.put(r.getIRI(), (float) num / (float) tot);
    }
    return loadFactors;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) HashMap(java.util.HashMap) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)

Example 14 with RegistryItem

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

the class RegistryManagerImpl method computeBestCandidate.

/**
 * @deprecated with each library having its own cache, load balancing is no longer necessary
 * @return
 */
protected Registry computeBestCandidate(Library lib) {
    Map<IRI, Float> loadFactors = computeLoadFactors();
    IRI current = null;
    float lowest = 1.0f;
    for (RegistryItem item : lib.getParents()) {
        IRI iri = item.getIRI();
        if (loadFactors.containsKey(iri)) {
            float f = loadFactors.get(iri);
            if (f < lowest) {
                lowest = f;
                current = iri;
            }
        }
    }
    return (Registry) (population.get(current));
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)

Example 15 with RegistryItem

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

the class RegistryManagerImpl method populateRegistry.

protected Registry populateRegistry(OWLOntology registry) throws RegistryContentException {
    log.debug("Populating registry content from ontology {}", registry);
    Registry reg = riFactory.createRegistry(registry);
    Set<OWLOntology> closure = registry.getOWLOntologyManager().getImportsClosure(registry);
    // Just scan all individuals. Recurse in case the registry imports more registries.
    for (OWLIndividual ind : registry.getIndividualsInSignature(true)) {
        // We do not allow anonymous registry items.
        if (ind.isAnonymous())
            continue;
        RegistryItem item = null;
        // IRI id = ind.asOWLNamedIndividual().getIRI();
        Type t = RegistryUtils.getType(ind, closure);
        if (t == null) {
            log.warn("Undetermined type for registry ontology individual {}", ind);
            continue;
        }
        switch(t) {
            case LIBRARY:
                log.debug("Found library for individual {}", ind);
                // Create the library and attach to parent and children
                item = populateLibrary(ind.asOWLNamedIndividual(), closure);
                reg.addChild(item);
                item.addRegistryContentListener(this);
                break;
            case ONTOLOGY:
                log.debug("Found ontology for individual {}", ind);
                // Create the ontology and attach to parent
                item = populateOntology(ind.asOWLNamedIndividual(), closure);
                item.addRegistryContentListener(this);
                // We don't know where to attach it within this method.
                break;
            default:
                break;
        }
    }
    try {
        reg.addRegistryContentListener(this);
        log.info("Registry {} added.", reg.getIRI());
        population.put(reg.getIRI(), reg);
    } catch (Exception e) {
        log.error("Invalid identifier for library item " + reg, e);
        return null;
    }
    return reg;
}
Also used : Type(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem.Type) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

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