Search in sources :

Example 6 with Registry

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());
}
Also used : RegistryManagerImpl(org.apache.stanbol.ontologymanager.registry.impl.RegistryManagerImpl) 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) Test(org.junit.Test)

Example 7 with Registry

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;
}
Also used : OWLObject(org.semanticweb.owlapi.model.OWLObject) IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAxiomVisitor(org.semanticweb.owlapi.model.OWLAxiomVisitor) HashMap(java.util.HashMap) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLAxiomVisitorAdapter(org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLObjectPropertyExpression(org.semanticweb.owlapi.model.OWLObjectPropertyExpression) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 8 with Registry

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);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) Hashtable(java.util.Hashtable) RegistryManagerImpl(org.apache.stanbol.ontologymanager.registry.impl.RegistryManagerImpl) 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) ClerezzaOntologyProvider(org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider) Test(org.junit.Test)

Example 9 with Registry

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

Example 10 with Registry

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

Aggregations

Registry (org.apache.stanbol.ontologymanager.registry.api.model.Registry)13 RegistryItem (org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)12 Library (org.apache.stanbol.ontologymanager.registry.api.model.Library)8 Test (org.junit.Test)7 IRI (org.semanticweb.owlapi.model.IRI)6 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)6 RegistryManagerImpl (org.apache.stanbol.ontologymanager.registry.impl.RegistryManagerImpl)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)3 RegistryOntology (org.apache.stanbol.ontologymanager.registry.api.model.RegistryOntology)3 ClerezzaOntologyProvider (org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider)2 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)2 OWLObject (org.semanticweb.owlapi.model.OWLObject)2 OWLOntologyAlreadyExistsException (org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)2 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)2 Hashtable (java.util.Hashtable)1 Type (org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem.Type)1 IRIDocumentSource (org.semanticweb.owlapi.io.IRIDocumentSource)1 OWLOntologyDocumentSource (org.semanticweb.owlapi.io.OWLOntologyDocumentSource)1