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();
}
}
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;
}
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;
}
Aggregations