Search in sources :

Example 1 with BlankOntologySource

use of org.apache.stanbol.ontologymanager.sources.owlapi.BlankOntologySource in project stanbol by apache.

the class TestOntologySpaces method testIdentifiers.

/**
 * Checks whether attempting to create ontology spaces with invalid identifiers or namespaces results in
 * the appropriate exceptions being thrown.
 *
 * @throws Exception
 *             if an unexpected error occurs.
 */
@Test
public void testIdentifiers() throws Exception {
    OntologySpace shouldBeNull = null, shouldBeNotNull = null;
    // Null identifier (invalid).
    try {
        shouldBeNull = factory.createOntologySpace(null, SpaceType.CORE, new BlankOntologySource());
        fail("Expected IllegalArgumentException not thrown despite null scope identifier.");
    } catch (IllegalArgumentException ex) {
    }
    assertNull(shouldBeNull);
    // More than one slash in identifier (invalid).
    try {
        shouldBeNull = factory.createOntologySpace("Sc0/p3", SpaceType.CORE, new BlankOntologySource());
        fail("Expected IllegalArgumentException not thrown despite null scope identifier.");
    } catch (IllegalArgumentException ex) {
    }
    assertNull(shouldBeNull);
    /* Now test namespaces. */
    // Null namespace (invalid).
    factory.setDefaultNamespace(null);
    try {
        shouldBeNull = factory.createOntologySpace("Sc0p3", SpaceType.CORE, new BlankOntologySource());
        fail("Expected IllegalArgumentException not thrown despite null OntoNet namespace.");
    } catch (IllegalArgumentException ex) {
    }
    assertNull(shouldBeNull);
    // Namespace with query (invalid).
    factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology/?query=true"));
    try {
        shouldBeNull = factory.createOntologySpace("Sc0p3", SpaceType.CORE, new BlankOntologySource());
        fail("Expected IllegalArgumentException not thrown despite query in OntoNet namespace.");
    } catch (IllegalArgumentException ex) {
    }
    assertNull(shouldBeNull);
    // Namespace with fragment (invalid).
    factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology#fragment"));
    try {
        shouldBeNull = factory.createOntologySpace("Sc0p3", SpaceType.CORE, new BlankOntologySource());
        fail("Expected IllegalArgumentException not thrown despite fragment in OntoNet namespace.");
    } catch (IllegalArgumentException ex) {
    }
    assertNull(shouldBeNull);
    // Namespace ending with hash (invalid).
    factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology#"));
    try {
        shouldBeNull = factory.createOntologySpace("Sc0p3", SpaceType.CORE);
        fail("Expected IllegalArgumentException not thrown despite fragment in OntoNet namespace.");
    } catch (IllegalArgumentException ex) {
    }
    assertNull(shouldBeNull);
    // Namespace ending with neither (valid, should automatically add slash).
    factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology"));
    shouldBeNotNull = factory.createOntologySpace("Sc0p3", SpaceType.CORE);
    assertNotNull(shouldBeNotNull);
    assertTrue(shouldBeNotNull.getDefaultNamespace().toString().endsWith("/"));
    shouldBeNotNull = null;
    // Namespace ending with slash (valid).
    factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology/"));
    shouldBeNotNull = factory.createOntologySpace("Sc0p3", SpaceType.CORE);
    assertNotNull(shouldBeNotNull);
}
Also used : OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) BlankOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.BlankOntologySource) Test(org.junit.Test)

Example 2 with BlankOntologySource

use of org.apache.stanbol.ontologymanager.sources.owlapi.BlankOntologySource in project stanbol by apache.

the class ScopeManagerImpl method bootstrapOntologyNetwork.

private void bootstrapOntologyNetwork(OWLOntology configOntology) {
    if (configOntology == null) {
        log.info("Ontology Network Manager starting with empty scope set.");
        return;
    }
    try {
        /**
         * We create and register the scopes before activating
         */
        for (String scopeId : OntologyNetworkConfigurationUtils.getScopes(configOntology)) {
            String[] cores = OntologyNetworkConfigurationUtils.getCoreOntologies(configOntology, scopeId);
            String[] customs = OntologyNetworkConfigurationUtils.getCustomOntologies(configOntology, scopeId);
            // "Be a man. Use printf"
            log.debug("Detected scope \"{}\"", scopeId);
            for (String s : cores) log.debug("\tDetected core ontology {}", s);
            for (String s : customs) log.debug("\tDetected custom ontology {}", s);
            // Create the scope
            log.debug("Rebuilding scope \"{}\"", scopeId);
            Scope sc = null;
            sc = /* factory. */
            createOntologyScope(scopeId, new BlankOntologySource());
            // Populate the core space
            if (cores.length > 0) {
                OntologySpace corespc = sc.getCoreSpace();
                corespc.tearDown();
                for (int i = 0; i < cores.length; i++) try {
                    corespc.addOntology(new RootOntologySource(IRI.create(cores[i])));
                } catch (Exception ex) {
                    log.warn("Failed to import ontology " + cores[i], ex);
                    continue;
                }
            }
            sc.setUp();
            registerScope(sc);
            sc.getCustomSpace().tearDown();
            for (String locationIri : customs) {
                try {
                    OntologyInputSource<?> src = new RootOntologySource(IRI.create(locationIri));
                    sc.getCustomSpace().addOntology(src);
                    log.debug("Added ontology from location {}", locationIri);
                } catch (UnmodifiableOntologyCollectorException e) {
                    log.error("An error occurred while trying to add the ontology from location: " + locationIri, e);
                    continue;
                }
            }
            sc.getCustomSpace().setUp();
        }
        /**
         * Try to get activation policies
         */
        toActivate = OntologyNetworkConfigurationUtils.getScopesToActivate(configOntology);
        for (String scopeID : toActivate) {
            try {
                scopeID = scopeID.trim();
                setScopeActive(scopeID, true);
                log.info("Ontology scope " + scopeID + " activated.");
            } catch (NoSuchScopeException ex) {
                log.warn("Tried to activate unavailable scope " + scopeID + ".");
            } catch (Exception ex) {
                log.error("Exception caught while activating scope " + scopeID + " . Skipping.", ex);
                continue;
            }
        }
    } catch (Throwable e) {
        log.warn("Invalid ONM configuration file found. " + "Starting with blank scope set.", e);
    }
}
Also used : Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) BlankOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.BlankOntologySource) NoSuchScopeException(org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) NoSuchScopeException(org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) IOException(java.io.IOException)

Aggregations

OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)2 BlankOntologySource (org.apache.stanbol.ontologymanager.sources.owlapi.BlankOntologySource)2 IOException (java.io.IOException)1 DuplicateIDException (org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)1 MissingOntologyException (org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException)1 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)1 NoSuchScopeException (org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException)1 Scope (org.apache.stanbol.ontologymanager.servicesapi.scope.Scope)1 RootOntologySource (org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource)1 Test (org.junit.Test)1 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)1