Search in sources :

Example 11 with Scope

use of org.apache.stanbol.ontologymanager.servicesapi.scope.Scope in project stanbol by apache.

the class GraphMultiplexer method onOntologyRemoved.

@Override
public void onOntologyRemoved(OntologyCollector collector, OWLOntologyID removedOntology) {
    log.info("Heard removal of ontology {} from collector {}", removedOntology, collector.getID());
    String colltype = "";
    if (// Cannot be
    collector instanceof Scope)
        // Cannot be
        colltype = Scope.shortName + "/";
    else if (collector instanceof OntologySpace)
        colltype = OntologySpace.shortName + "/";
    else if (collector instanceof Session)
        colltype = Session.shortName + "/";
    IRI c = new IRI(_NS_STANBOL_INTERNAL + colltype + collector.getID());
    Set<OWLOntologyID> aliases = listAliases(removedOntology);
    aliases.add(removedOntology);
    boolean badState = true;
    for (OWLOntologyID alias : aliases) {
        IRI u = buildResource(alias);
        // XXX condense the following code
        log.debug("Checking ({},{}) pattern", c, u);
        for (Iterator<Triple> it = meta.filter(c, null, u); it.hasNext(); ) {
            IRI property = it.next().getPredicate();
            if (collector instanceof OntologySpace || collector instanceof Session) {
                if (property.equals(MANAGES_URIREF))
                    badState = false;
            }
        }
        log.debug("Checking ({},{}) pattern", u, c);
        for (Iterator<Triple> it = meta.filter(u, null, c); it.hasNext(); ) {
            IRI property = it.next().getPredicate();
            if (collector instanceof OntologySpace || collector instanceof Session) {
                if (property.equals(IS_MANAGED_BY_URIREF))
                    badState = false;
            }
        }
        synchronized (meta) {
            if (collector instanceof OntologySpace || collector instanceof Session) {
                meta.remove(new TripleImpl(c, MANAGES_URIREF, u));
                meta.remove(new TripleImpl(u, IS_MANAGED_BY_URIREF, c));
            }
        }
    }
    if (badState)
        throw new InvalidMetaGraphStateException("No relationship found between ontology collector " + c + " and stored ontology " + removedOntology + " (or its aliases).");
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session)

Example 12 with Scope

use of org.apache.stanbol.ontologymanager.servicesapi.scope.Scope in project stanbol by apache.

the class GraphMultiplexer method onOntologyAdded.

@Override
public void onOntologyAdded(OntologyCollector collector, OWLOntologyID addedOntology) {
    // When the ontology provider hears an ontology has been added to a collector, it has to register this
    // into the metadata graph.
    // log.info("Heard addition of ontology {} to collector {}", addedOntology, collector.getID());
    // log.info("This ontology is stored as {}", getKey(addedOntology));
    String colltype = "";
    if (// Cannot be
    collector instanceof Scope)
        // Cannot be
        colltype = Scope.shortName + "/";
    else if (collector instanceof OntologySpace)
        colltype = OntologySpace.shortName + "/";
    else if (collector instanceof Session)
        colltype = Session.shortName + "/";
    IRI c = new IRI(_NS_STANBOL_INTERNAL + colltype + collector.getID());
    IRI u = // keymap.getMapping(addedOntology);
    buildResource(addedOntology);
    // TODO OntologyProvider should not be aware of scopes, spaces or sessions. Move elsewhere.
    boolean hasValues = false;
    log.debug("Ontology {}", addedOntology);
    log.debug("-- is already managed by the following collectors :");
    for (Iterator<Triple> it = meta.filter(u, IS_MANAGED_BY_URIREF, null); it.hasNext(); ) {
        hasValues = true;
        log.debug("-- {}", it.next().getObject());
    }
    for (Iterator<Triple> it = meta.filter(null, MANAGES_URIREF, u); it.hasNext(); ) {
        hasValues = true;
        log.debug("-- {} (inverse)", it.next().getSubject());
    }
    if (!hasValues)
        log.debug("-- <none>");
    // Add both inverse triples. This graph has to be traversed efficiently, no need for reasoners.
    IRI predicate1 = null, predicate2 = null;
    if (collector instanceof OntologySpace) {
        predicate1 = MANAGES_URIREF;
        predicate2 = IS_MANAGED_BY_URIREF;
    } else if (collector instanceof Session) {
        // TODO implement model for sessions.
        predicate1 = MANAGES_URIREF;
        predicate2 = IS_MANAGED_BY_URIREF;
    } else {
        log.error("Unrecognized ontology collector type {} for \"{}\". Aborting.", collector.getClass(), collector.getID());
        return;
    }
    if (u != null)
        synchronized (meta) {
            Triple t;
            if (predicate1 != null) {
                t = new TripleImpl(c, predicate1, u);
                boolean b = meta.add(t);
                log.debug((b ? "Successful" : "Redundant") + " addition of meta triple");
                log.debug("-- {} ", t);
            }
            if (predicate2 != null) {
                t = new TripleImpl(u, predicate2, c);
                boolean b = meta.add(t);
                log.debug((b ? "Successful" : "Redundant") + " addition of meta triple");
                log.debug("-- {} ", t);
            }
        }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session)

Example 13 with Scope

use of org.apache.stanbol.ontologymanager.servicesapi.scope.Scope in project stanbol by apache.

the class SessionImpl method exportToOWLOntology.

/**
 * TODO support merging for attached scopes as well?
 */
@Override
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    OWLOntology o = super.exportToOWLOntology(merge, universalPrefix);
    org.semanticweb.owlapi.model.IRI iri = o.getOntologyID().getOntologyIRI();
    if (merge) {
        // Re-merge
        // FIXME try to avoid this.
        ScopeManager onm = ScopeManagerImpl.get();
        final Set<OWLOntology> set = new HashSet<OWLOntology>();
        set.add(o);
        for (String scopeID : attachedScopes) {
            log.debug(" ... Merging with attached scope {}.", scopeID);
            Scope sc = onm.getScope(scopeID);
            if (sc != null)
                set.add(sc.export(OWLOntology.class, merge));
            for (OWLOntologyID ontologyId : managedOntologies) {
                set.add(getOntology(ontologyId, OWLOntology.class, true));
            }
            OWLOntologySetProvider provider = new OWLOntologySetProvider() {

                @Override
                public Set<OWLOntology> getOntologies() {
                    return set;
                }
            };
            OWLOntologyMerger merger = new OWLOntologyMerger(provider);
            try {
                o = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to merge imports for ontology " + iri, e);
                o = null;
            }
        }
    } else
        attachScopeImportsOwlApi(o, universalPrefix);
    return o;
}
Also used : OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) ScopeManager(org.apache.stanbol.ontologymanager.servicesapi.scope.ScopeManager) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) HashSet(java.util.HashSet)

Example 14 with Scope

use of org.apache.stanbol.ontologymanager.servicesapi.scope.Scope in project stanbol by apache.

the class TestStorage method storedOntologyOutlivesScope.

/**
 * If an ontology is removed from a scope, or the scope itself is torn down, this should not result in the
 * deletion of that ontology in general.
 */
@Test
public void storedOntologyOutlivesScope() throws Exception {
    String ephemeralScopeId = "CaducousScope";
    OntologyInputSource<OWLOntology> ois = new RootOntologySource(org.semanticweb.owlapi.model.IRI.create(getClass().getResource("/ontologies/nonexistentcharacters.owl")));
    OWLOntologyID ontologyId = ois.getRootOntology().getOntologyID();
    Scope scope = onManager.createOntologyScope(ephemeralScopeId);
    // Initially, the ontology is not there
    assertFalse(ontologyProvider.hasOntology(ontologyId));
    // Once added, the ontology is there
    scope.getCustomSpace().addOntology(ois);
    assertTrue(ontologyProvider.hasOntology(ontologyId));
    // Once removed from the scope, the ontology is still there
    scope.getCustomSpace().removeOntology(ontologyId);
    assertTrue(ontologyProvider.hasOntology(ontologyId));
    // Once the scope is killed, the ontology is still there
    // TODO find a more appropriate method to kill scopes?
    scope.tearDown();
    assertTrue(ontologyProvider.hasOntology(ontologyId));
}
Also used : Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) Test(org.junit.Test)

Example 15 with Scope

use of org.apache.stanbol.ontologymanager.servicesapi.scope.Scope in project stanbol by apache.

the class TestOntologyNetworkPersistence method scopesAndSessionsOutliveOntoNet.

@Test
public void scopesAndSessionsOutliveOntoNet() throws Exception {
    /*
         * Both scopes will be created, but scope1 will be unregistered and we expect not to be able to
         * rebuild it.
         */
    String id1 = "scope1", id2 = "scope2", sid2 = "auto-" + System.currentTimeMillis();
    // Setup a network
    Scope scope1 = onManager.createOntologyScope(id1);
    assertNotNull(scope1);
    Scope scope2 = onManager.createOntologyScope(id2);
    assertNotNull(scope2);
    onManager.deregisterScope(scope1);
    // A session with a system ID
    Session ses1 = sessionManager.createSession();
    String sid1 = ses1.getID();
    assertNotNull(ses1);
    assertNotNull(sid1);
    assertFalse(sid1.isEmpty());
    // A session with an ID chosen manually
    Session ses2 = sessionManager.createSession(sid2);
    assertNotNull(ses2);
    assertNotNull(ses2.getID());
    assertEquals(sid2, ses2.getID());
    log.info("Stanbol going down...");
    // but keep the TcProvider
    resetOntologyProvider();
    resetManagers();
    // The unregistered scope should be missing.
    assertNull(onManager.getScope(id1));
    // The other collectors should have been rebuilt.
    assertNotNull(onManager.getScope(id2));
    assertNotNull(sessionManager.getSession(sid1));
    assertNotNull(sessionManager.getSession(sid2));
}
Also used : Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session) Test(org.junit.Test)

Aggregations

Scope (org.apache.stanbol.ontologymanager.servicesapi.scope.Scope)22 Test (org.junit.Test)7 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)7 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)6 HashSet (java.util.HashSet)5 IRI (org.apache.clerezza.commons.rdf.IRI)5 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)5 IOException (java.io.IOException)4 Triple (org.apache.clerezza.commons.rdf.Triple)4 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)4 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)4 GraphContentInputSource (org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource)4 Produces (javax.ws.rs.Produces)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)3 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)3 NoSuchScopeException (org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException)3 Session (org.apache.stanbol.ontologymanager.servicesapi.session.Session)3 InputStream (java.io.InputStream)2 POST (javax.ws.rs.POST)2