use of org.apache.stanbol.ontologymanager.registry.api.model.Registry 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.Registry in project stanbol by apache.
the class RegistryManagerImpl method createModel.
@Override
public Set<Registry> createModel(Set<OWLOntology> registryOntologies) {
Set<Registry> results = new HashSet<Registry>();
// Reset population
population.clear();
// Build the transitive imports closure of the union.
Set<OWLOntology> closure = new HashSet<OWLOntology>();
for (OWLOntology rego : registryOntologies) closure.addAll(rego.getOWLOntologyManager().getImportsClosure(rego));
/*
* For each value in this map, index 0 is the score of the library class, while 1 is the score of the
* ontology class.
*/
final Map<IRI, int[]> candidateTypes = new HashMap<IRI, int[]>();
/*
* Scans class assertions and object property values and tries to determine the type of each
* individual it finds.
*/
OWLAxiomVisitor scanner = new OWLAxiomVisitorAdapter() {
/*
* For a given identifier, returns the array of integers whose value determine the likelihood if
* the corresponding entity being a library or an ontology. If no such array exists, it is
* created.
*/
private int[] checkScores(IRI key) {
int[] scores;
if (candidateTypes.containsKey(key))
scores = candidateTypes.get(key);
else {
scores = new int[] { 0, 0 };
candidateTypes.put(key, scores);
}
return scores;
}
@Override
public void visit(OWLAnnotationAssertionAxiom axiom) {
/*
* Works like object property assertions, in case hasOntology and isOntologyOf are not
* detected to be object properties (e.g. due to a failure to load codolight or the registry
* metamodel).
*/
OWLAnnotationProperty prop = axiom.getProperty();
if (hasOntologyAnn.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its library score.
OWLObject ind = axiom.getSubject();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[0]++;
}
// The axiom object gets a +1 in its ontology score.
ind = axiom.getValue();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[1]++;
}
} else if (isOntologyOfAnn.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its ontology score.
OWLObject ind = axiom.getSubject();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[1]++;
}
// The axiom object gets a +1 in its library score.
ind = axiom.getValue();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[0]++;
}
}
}
@Override
public void visit(OWLClassAssertionAxiom axiom) {
OWLIndividual ind = axiom.getIndividual();
// Do not accept anonymous registry items.
if (ind.isAnonymous())
return;
IRI iri = ind.asOWLNamedIndividual().getIRI();
int[] scores = checkScores(iri);
OWLClassExpression type = axiom.getClassExpression();
// If the type is stated to be a library, increase its library score.
if (cRegistryLibrary.equals(type)) {
scores[0]++;
} else // If the type is stated to be an ontology, increase its ontology score.
if (cOntology.equals(type)) {
scores[1]++;
}
}
@Override
public void visit(OWLObjectPropertyAssertionAxiom axiom) {
OWLObjectPropertyExpression prop = axiom.getProperty();
if (hasOntology.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its library score.
OWLIndividual ind = axiom.getSubject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[0]++;
}
// The axiom object gets a +1 in its ontology score.
ind = axiom.getObject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[1]++;
}
} else if (isOntologyOf.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its ontology score.
OWLIndividual ind = axiom.getSubject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[1]++;
}
// The axiom object gets a +1 in its library score.
ind = axiom.getObject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[0]++;
}
}
}
};
// First pass to determine the types.
for (OWLOntology o : closure) for (OWLAxiom ax : o.getAxioms()) ax.accept(scanner);
// Then populate on the registry
OWLDataFactory df = OWLManager.getOWLDataFactory();
for (IRI iri : candidateTypes.keySet()) {
int[] scores = candidateTypes.get(iri);
if (scores != null && (scores[0] > 0 || scores[1] > 0)) {
if (scores[0] > 0 && scores[1] == 0)
population.put(iri, riFactory.createLibrary(df.getOWLNamedIndividual(iri)));
else if (scores[0] == 0 && scores[1] > 0)
population.put(iri, riFactory.createRegistryOntology(df.getOWLNamedIndividual(iri)));
}
// else log.warn("Unable to determine type for registry item {}", iri);
}
for (OWLOntology oReg : registryOntologies) {
try {
results.add(populateRegistry(oReg));
} catch (RegistryContentException e) {
log.error("An error occurred while populating an ontology registry.", e);
}
}
return results;
}
use of org.apache.stanbol.ontologymanager.registry.api.model.Registry in project stanbol by apache.
the class TestOntologyLibrary method testLibraryLoad.
/**
* Uses a plain {@link RegistryManager} to load a single ontology library and checks for its expected hits
* and misses.
*
* @throws Exception
* if any error occurs;
*/
@Test
public void testLibraryLoad() throws Exception {
IRI localTestRegistry = IRI.create(getClass().getResource(registryResourcePath));
Dictionary<String, Object> regmanConf = new Hashtable<String, Object>();
regmanConf.put(RegistryManager.REGISTRY_LOCATIONS, new String[] { localTestRegistry.toString() });
// Instantiating the registry manager will also load the registry data.
regMgr = new RegistryManagerImpl(offline, new ClerezzaOntologyProvider(tcManager, offline, parser), regmanConf);
// The resulting manager must exist and have exactly one registry.
assertNotNull(regMgr);
Set<Registry> registries = regMgr.getRegistries();
assertFalse(registries.isEmpty());
assertEquals(1, registries.size());
Registry reg = registries.iterator().next();
assertTrue(reg.hasChildren());
Library lib = null;
// Look for test #Library2
for (RegistryItem item : reg.getChildren()) {
if (Locations.LIBRARY_TEST2.equals(item.getIRI())) {
lib = (Library) item;
break;
}
}
assertNotNull(lib);
// Should be in the library.
boolean hasShould = RegistryUtils.containsOntologyRecursive(lib, Locations.CHAR_DROPPED);
// Should NOT be in the library (belongs to another library in the same registry).
boolean hasShouldNot = RegistryUtils.containsOntologyRecursive(lib, Locations.CHAR_ACTIVE);
assertTrue(hasShould);
assertFalse(hasShouldNot);
}
use of org.apache.stanbol.ontologymanager.registry.api.model.Registry 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;
}
use of org.apache.stanbol.ontologymanager.registry.api.model.Registry 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;
}
Aggregations