Search in sources :

Example 1 with OntologyNetworkConfiguration

use of org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration in project stanbol by apache.

the class ScopeManagerImpl method rebuildScopes.

private void rebuildScopes() {
    OntologyNetworkConfiguration struct = ontologyProvider.getOntologyNetworkConfiguration();
    for (String scopeId : struct.getScopeIDs()) {
        long before = System.currentTimeMillis();
        log.debug("Rebuilding scope with ID \"{}\".", scopeId);
        Collection<OWLOntologyID> coreOnts = struct.getCoreOntologyKeysForScope(scopeId);
        OntologyInputSource<?>[] srcs = new OntologyInputSource<?>[coreOnts.size()];
        int i = 0;
        for (OWLOntologyID coreOnt : coreOnts) {
            log.debug("Core ontology key : {}", coreOnts);
            srcs[i++] = new StoredOntologySource(coreOnt);
        }
        Scope scope;
        try {
            scope = createOntologyScope(scopeId, srcs);
        } catch (DuplicateIDException e) {
            String dupe = e.getDuplicateID();
            log.warn("Scope \"{}\" already exists and will be reused.", dupe);
            scope = getScope(dupe);
        }
        OntologySpace custom = scope.getCustomSpace();
        // Register even if some ontologies were to fail to be restored afterwards.
        scopeMap.put(scopeId, scope);
        for (OWLOntologyID key : struct.getCustomOntologyKeysForScope(scopeId)) try {
            log.debug("Custom ontology key : {}", key);
            custom.addOntology(new StoredOntologySource(key));
        } catch (MissingOntologyException ex) {
            log.error("Could not find an ontology with public key {} to be managed by scope \"{}\". Proceeding to next ontology.", key, scopeId);
            continue;
        } catch (Exception ex) {
            log.error("Exception caught while trying to add ontology with public key " + key + " to rebuilt scope \"" + scopeId + "\". proceeding to next ontology", ex);
            continue;
        }
        log.info("Scope \"{}\" rebuilt in {} ms.", scopeId, System.currentTimeMillis() - before);
    }
}
Also used : MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) StoredOntologySource(org.apache.stanbol.ontologymanager.servicesapi.io.StoredOntologySource) 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) OntologyNetworkConfiguration(org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource)

Example 2 with OntologyNetworkConfiguration

use of org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration in project stanbol by apache.

the class ClerezzaOntologyProvider method getOntologyNetworkConfiguration.

public OntologyNetworkConfiguration getOntologyNetworkConfiguration() {
    Map<String, Collection<OWLOntologyID>> coreOntologies = new HashMap<String, Collection<OWLOntologyID>>(), customOntologies = new HashMap<String, Collection<OWLOntologyID>>();
    Map<String, Collection<String>> attachedScopes = new HashMap<String, Collection<String>>();
    final Graph meta = store.getGraph(new IRI(metaGraphId));
    // Scopes first
    for (Iterator<Triple> it = meta.filter(null, RDF.type, SCOPE_URIREF); it.hasNext(); ) {
        // for each
        // scope
        Triple ta = it.next();
        BlankNodeOrIRI sub = ta.getSubject();
        if (sub instanceof IRI) {
            String s = ((IRI) sub).getUnicodeString(), prefix = _NS_STANBOL_INTERNAL + Scope.shortName + "/";
            if (s.startsWith(prefix)) {
                String scopeId = s.substring(prefix.length());
                log.info("Rebuilding scope \"{}\".", scopeId);
                coreOntologies.put(scopeId, new TreeSet<OWLOntologyID>());
                customOntologies.put(scopeId, new TreeSet<OWLOntologyID>());
                IRI core_ur = null, custom_ur = null;
                RDFTerm r;
                // Check core space
                Iterator<Triple> it2 = meta.filter(sub, HAS_SPACE_CORE_URIREF, null);
                if (it2.hasNext()) {
                    r = it2.next().getObject();
                    if (r instanceof IRI)
                        core_ur = (IRI) r;
                } else {
                    it2 = meta.filter(null, IS_SPACE_CORE_OF_URIREF, sub);
                    if (it2.hasNext()) {
                        r = it2.next().getSubject();
                        if (r instanceof IRI)
                            core_ur = (IRI) r;
                    }
                }
                // Check custom space
                it2 = meta.filter(sub, HAS_SPACE_CUSTOM_URIREF, null);
                if (it2.hasNext()) {
                    r = it2.next().getObject();
                    if (r instanceof IRI)
                        custom_ur = (IRI) r;
                } else {
                    it2 = meta.filter(null, IS_SPACE_CUSTOM_OF_URIREF, sub);
                    if (it2.hasNext()) {
                        r = it2.next().getSubject();
                        if (r instanceof IRI)
                            custom_ur = (IRI) r;
                    }
                }
                // retrieve the ontologies
                if (core_ur != null) {
                    for (it2 = meta.filter(core_ur, null, null); it2.hasNext(); ) {
                        Triple t = it2.next();
                        IRI predicate = t.getPredicate();
                        if (predicate.equals(MANAGES_URIREF)) {
                            if (t.getObject() instanceof IRI)
                                coreOntologies.get(scopeId).add(// FIXME must be very
                                keymap.buildPublicKey((IRI) t.getObject()));
                        }
                    }
                    for (it2 = meta.filter(null, null, core_ur); it2.hasNext(); ) {
                        Triple t = it2.next();
                        IRI predicate = t.getPredicate();
                        if (predicate.equals(IS_MANAGED_BY_URIREF)) {
                            if (t.getSubject() instanceof IRI)
                                coreOntologies.get(scopeId).add(// FIXME must be very
                                keymap.buildPublicKey((IRI) t.getSubject()));
                        }
                    }
                }
                if (custom_ur != null) {
                    for (it2 = meta.filter(custom_ur, null, null); it2.hasNext(); ) {
                        Triple t = it2.next();
                        IRI predicate = t.getPredicate();
                        if (predicate.equals(MANAGES_URIREF)) {
                            if (t.getObject() instanceof IRI)
                                customOntologies.get(scopeId).add(// FIXME must be very
                                keymap.buildPublicKey((IRI) t.getObject()));
                        }
                    }
                    for (it2 = meta.filter(null, null, custom_ur); it2.hasNext(); ) {
                        Triple t = it2.next();
                        IRI predicate = t.getPredicate();
                        if (predicate.equals(IS_MANAGED_BY_URIREF)) {
                            if (t.getSubject() instanceof IRI)
                                customOntologies.get(scopeId).add(// FIXME must be very
                                keymap.buildPublicKey((IRI) t.getSubject()));
                        }
                    }
                }
            }
        }
    }
    // Sessions next
    Map<String, Collection<OWLOntologyID>> sessionOntologies = new HashMap<String, Collection<OWLOntologyID>>();
    for (Iterator<Triple> it = meta.filter(null, RDF.type, SESSION_URIREF); it.hasNext(); ) {
        // for each
        // scope
        Triple ta = it.next();
        BlankNodeOrIRI sub = ta.getSubject();
        if (sub instanceof IRI) {
            IRI ses_ur = (IRI) sub;
            String s = ((IRI) sub).getUnicodeString();
            String prefix = _NS_STANBOL_INTERNAL + Session.shortName + "/";
            if (s.startsWith(prefix)) {
                String sessionId = s.substring(prefix.length());
                log.info("Rebuilding session \"{}\".", sessionId);
                sessionOntologies.put(sessionId, new TreeSet<OWLOntologyID>());
                attachedScopes.put(sessionId, new TreeSet<String>());
                // retrieve the ontologies
                if (ses_ur != null) {
                    for (Iterator<Triple> it2 = meta.filter(ses_ur, MANAGES_URIREF, null); it2.hasNext(); ) {
                        RDFTerm obj = it2.next().getObject();
                        if (obj instanceof IRI)
                            sessionOntologies.get(sessionId).add(// FIXME must be very temporary!
                            keymap.buildPublicKey((IRI) obj));
                    }
                    for (Iterator<Triple> it2 = meta.filter(null, IS_MANAGED_BY_URIREF, ses_ur); it2.hasNext(); ) {
                        RDFTerm subj = it2.next().getSubject();
                        if (subj instanceof IRI)
                            sessionOntologies.get(sessionId).add(// FIXME must be very temporary!
                            keymap.buildPublicKey((IRI) subj));
                    }
                    for (Iterator<Triple> it2 = meta.filter(null, APPENDED_TO_URIREF, ses_ur); it2.hasNext(); ) {
                        RDFTerm subj = it2.next().getSubject();
                        if (subj instanceof IRI) {
                            String s1 = ((IRI) subj).getUnicodeString();
                            String prefix1 = _NS_STANBOL_INTERNAL + Scope.shortName + "/";
                            if (s1.startsWith(prefix1)) {
                                String scopeId = s1.substring(prefix1.length());
                                attachedScopes.get(sessionId).add(scopeId);
                            }
                        }
                    }
                    for (Iterator<Triple> it2 = meta.filter(ses_ur, HAS_APPENDED_URIREF, null); it2.hasNext(); ) {
                        RDFTerm obj = it2.next().getObject();
                        if (obj instanceof IRI) {
                            String s1 = ((IRI) obj).getUnicodeString();
                            String prefix1 = _NS_STANBOL_INTERNAL + Scope.shortName + "/";
                            if (s1.startsWith(prefix1)) {
                                String scopeId = s1.substring(prefix1.length());
                                attachedScopes.get(sessionId).add(scopeId);
                            }
                        }
                    }
                }
            }
        }
    }
    return new OntologyNetworkConfiguration(coreOntologies, customOntologies, sessionOntologies, attachedScopes);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) OntologyNetworkConfiguration(org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) Collection(java.util.Collection)

Example 3 with OntologyNetworkConfiguration

use of org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration in project stanbol by apache.

the class SessionManagerImpl method rebuildSessions.

private void rebuildSessions() {
    if (ontologyProvider == null) {
        log.warn("No ontology provider supplied. Cannot rebuild sessions");
        return;
    }
    OntologyNetworkConfiguration struct = ontologyProvider.getOntologyNetworkConfiguration();
    for (String sessionId : struct.getSessionIDs()) {
        long before = System.currentTimeMillis();
        log.debug("Rebuilding session with ID \"{}\"", sessionId);
        Session session;
        try {
            session = createSession(sessionId);
        } catch (DuplicateSessionIDException e) {
            log.warn("Session \"{}\" already exists and will be reused.", sessionId);
            session = getSession(sessionId);
        } catch (SessionLimitException e) {
            log.error("Cannot create session {}. Session limit of {} reached.", sessionId, getActiveSessionLimit());
            break;
        }
        // Register even if some ontologies were to fail to be restored afterwards.
        sessionsByID.put(sessionId, session);
        // Restored sessions are inactive at first.
        session.setActive(false);
        for (OWLOntologyID key : struct.getOntologyKeysForSession(sessionId)) try {
            session.addOntology(new StoredOntologySource(key));
        } catch (MissingOntologyException ex) {
            log.error("Could not find an ontology with public key {} to be managed by session \"{}\". Proceeding to next ontology.", key, sessionId);
            continue;
        } catch (Exception ex) {
            log.error("Exception caught while trying to add ontology with public key " + key + " to rebuilt session \"" + sessionId + "\". Proceeding to next ontology.", ex);
            continue;
        }
        for (String scopeId : struct.getAttachedScopes(sessionId)) {
            /*
                 * The scope is attached by reference, so we won't have to bother checking if the scope has
                 * been rebuilt by then (which could not happen if the SessionManager is being activated
                 * first).
                 */
            session.attachScope(scopeId);
        }
        log.info("Session \"{}\" rebuilt in {} ms.", sessionId, System.currentTimeMillis() - before);
    }
}
Also used : MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) SessionLimitException(org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException) StoredOntologySource(org.apache.stanbol.ontologymanager.servicesapi.io.StoredOntologySource) NonReferenceableSessionException(org.apache.stanbol.ontologymanager.servicesapi.session.NonReferenceableSessionException) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) SessionLimitException(org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) IOException(java.io.IOException) DuplicateSessionIDException(org.apache.stanbol.ontologymanager.servicesapi.session.DuplicateSessionIDException) OntologyNetworkConfiguration(org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session) DuplicateSessionIDException(org.apache.stanbol.ontologymanager.servicesapi.session.DuplicateSessionIDException)

Aggregations

OntologyNetworkConfiguration (org.apache.stanbol.ontologymanager.ontonet.api.OntologyNetworkConfiguration)3 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)3 IOException (java.io.IOException)2 MissingOntologyException (org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException)2 StoredOntologySource (org.apache.stanbol.ontologymanager.servicesapi.io.StoredOntologySource)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)1 Triple (org.apache.clerezza.commons.rdf.Triple)1 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)1 DuplicateIDException (org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException)1 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)1 OntologyInputSource (org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource)1 NoSuchScopeException (org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException)1 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)1 Scope (org.apache.stanbol.ontologymanager.servicesapi.scope.Scope)1 DuplicateSessionIDException (org.apache.stanbol.ontologymanager.servicesapi.session.DuplicateSessionIDException)1