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