Search in sources :

Example 11 with Session

use of org.apache.stanbol.ontologymanager.servicesapi.session.Session 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 Session

use of org.apache.stanbol.ontologymanager.servicesapi.session.Session 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 Session

use of org.apache.stanbol.ontologymanager.servicesapi.session.Session in project stanbol by apache.

the class SessionImpl method equals.

@Override
public boolean equals(Object arg0) {
    if (arg0 == null)
        return false;
    if (!(arg0 instanceof Session))
        return false;
    if (this == arg0)
        return true;
    log.warn("{} only implements weak equality, i.e. managed ontologies are only checked by public key, not by content.", getClass());
    Session coll = (Session) arg0;
    return super.equals(arg0) && this.getAttachedScopes().equals(coll.getAttachedScopes()) && this.getAttachedScopes().equals(coll.getAttachedScopes()) && this.getSessionState().equals(coll.getSessionState());
}
Also used : Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session)

Example 14 with Session

use of org.apache.stanbol.ontologymanager.servicesapi.session.Session in project stanbol by apache.

the class TestSessions method testSessionCreationDestruction.

@Test
public void testSessionCreationDestruction() throws Exception {
    int size = 100;
    int initialSize = sessionManager.getRegisteredSessionIDs().size();
    Set<Session> sessions = new HashSet<Session>();
    // Create and open many sessions.
    synchronized (sessionManager) {
        for (int i = 0; i < size; i++) {
            Session ses = sessionManager.createSession();
            ses.open();
            sessions.add(ses);
        }
        // Check that 500 sessions have been created
        assertEquals(initialSize + size, sessionManager.getRegisteredSessionIDs().size());
    }
    boolean open = true;
    for (Session ses : sessions) open &= ses.getSessionState() == State.ACTIVE;
    // Check that all created sessions have been opened
    assertTrue(open);
    // Kill 'em all, to quote Metallica
    synchronized (sessionManager) {
        for (Session ses : sessions) sessionManager.destroySession(ses.getID());
        assertEquals(initialSize, sessionManager.getRegisteredSessionIDs().size());
    }
    // Check that they are all zombies
    boolean zombi = true;
    for (Session ses : sessions) zombi &= ses.getSessionState() == State.ZOMBIE;
    assertTrue(zombi);
    // Try to resurrect them (hopefully failing)
    boolean resurrect = false;
    for (Session ses : sessions) try {
        ses.open();
        resurrect |= true;
    } catch (NonReferenceableSessionException e) {
        resurrect |= false;
        continue;
    }
    assertFalse(resurrect);
}
Also used : NonReferenceableSessionException(org.apache.stanbol.ontologymanager.servicesapi.session.NonReferenceableSessionException) HashSet(java.util.HashSet) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session) Test(org.junit.Test)

Example 15 with Session

use of org.apache.stanbol.ontologymanager.servicesapi.session.Session 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

Session (org.apache.stanbol.ontologymanager.servicesapi.session.Session)16 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)5 Scope (org.apache.stanbol.ontologymanager.servicesapi.scope.Scope)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 IRI (org.apache.clerezza.commons.rdf.IRI)3 Triple (org.apache.clerezza.commons.rdf.Triple)3 DuplicateSessionIDException (org.apache.stanbol.ontologymanager.servicesapi.session.DuplicateSessionIDException)3 NonReferenceableSessionException (org.apache.stanbol.ontologymanager.servicesapi.session.NonReferenceableSessionException)3 SessionLimitException (org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException)3 HashSet (java.util.HashSet)2 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)2 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)2 SessionImpl (org.apache.stanbol.ontologymanager.multiplexer.clerezza.impl.SessionImpl)2 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)2 FileNotFoundException (java.io.FileNotFoundException)1 Set (java.util.Set)1 POST (javax.ws.rs.POST)1