use of org.apache.stanbol.ontologymanager.registry.api.model.Library in project stanbol by apache.
the class TestRegistryManager method testLoadingLazy.
/**
* Verifies that by setting the loading policy to lazy (LAZY_LOADING = true), any random library will
* respond false to a call to {@link Library#isLoaded()}, until its content is "touched" via a call to
* {@link Library#getOntologies()}, only after which will it return true.
*
* @throws Exception
*/
@Test
public void testLoadingLazy() throws Exception {
// Change the caching policy and setup a new registry manager.
configuration.put(RegistryManager.CACHING_POLICY, CachingPolicy.CENTRALISED);
configuration.put(RegistryManager.LAZY_LOADING, true);
regman = new RegistryManagerImpl(offline, provider, configuration);
// Check that the configuration was set.
assertNotNull(regman);
// Now pick a library.
Registry reg;
Iterator<Registry> it = regman.getRegistries().iterator();
do reg = it.next(); while (// We need a registry with at least 2 libraries to check that only one will be loaded.
it.hasNext() && !reg.hasChildren() || reg.getChildren().length < 2);
assertNotNull(reg);
// There has to be at least one library with 2 children or more from the test registries...
Library lib1 = null, lib2 = null;
RegistryItem[] children = reg.getChildren();
assertTrue(children.length >= 2);
for (int i = 0; i < children.length - 1 && lib1 == null && lib2 == null; i++) {
if (children[i] instanceof Library)
lib1 = (Library) (children[i]);
if (children[i + 1] instanceof Library)
lib2 = (Library) (children[i + 1]);
}
assertFalse(lib1 == lib2);
assertNotNull(lib1);
// ...but its ontologies must not be loaded yet.
assertFalse(lib1.isLoaded());
assertFalse(lib2.isLoaded());
// Touch the library. Also test that the listener system works.
assertFalse(lib1.getOntologies(OWLOntology.class).isEmpty());
assertTrue(lib1.isLoaded());
assertFalse(lib2.isLoaded());
}
use of org.apache.stanbol.ontologymanager.registry.api.model.Library in project stanbol by apache.
the class TestRegistryManager method testLoadingEager.
/**
* Verifies that by setting the loading policy to eager (LAZY_LOADING = false), any random library will
* respond true to a call to {@link Library#isLoaded()} without ever "touching" its content.
*
* @throws Exception
*/
@Test
public void testLoadingEager() throws Exception {
// Change the caching policy and setup a new registry manager.
configuration.put(RegistryManager.CACHING_POLICY, CachingPolicy.DISTRIBUTED);
configuration.put(RegistryManager.LAZY_LOADING, false);
regman = new RegistryManagerImpl(offline, provider, configuration);
// Check that the configuration was set.
assertNotNull(regman);
// Now pick a library.
Registry reg;
do reg = regman.getRegistries().iterator().next(); while (!reg.hasChildren());
assertNotNull(reg);
// There has to be at least one non-empty library from the test registries...
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);
// ...and its ontologies must already be loaded without having to request them.
assertTrue(lib.isLoaded());
}
use of org.apache.stanbol.ontologymanager.registry.api.model.Library 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;
}
use of org.apache.stanbol.ontologymanager.registry.api.model.Library 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();
}
}
use of org.apache.stanbol.ontologymanager.registry.api.model.Library 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;
}
Aggregations