You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by en...@apache.org on 2011/04/01 14:08:31 UTC
svn commit: r1087671 [4/13] - in /incubator/stanbol/trunk/ontologymanager:
./ ontonet/ ontonet/.settings/ ontonet/src/ ontonet/src/main/
ontonet/src/main/java/ ontonet/src/main/java/org/
ontonet/src/main/java/org/apache/ ontonet/src/main/java/org/apach...
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/ScopeRegistryImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/ScopeRegistryImpl.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/ScopeRegistryImpl.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/ScopeRegistryImpl.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,215 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.ontology;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.NoSuchScopeException;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyScope;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.ScopeEventListener;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.ScopeRegistry;
+import org.semanticweb.owlapi.model.IRI;
+
+/**
+ * Default implementation of an ontology scope registry.
+ *
+ * @author alessandro
+ *
+ */
+public class ScopeRegistryImpl implements ScopeRegistry {
+
+ private Set<IRI> activeScopeIRIs;
+
+ private Set<ScopeEventListener> scopeListeners;
+
+ private Map<IRI, OntologyScope> scopeMap;
+
+ public ScopeRegistryImpl() {
+ scopeMap = new HashMap<IRI, OntologyScope>();
+ activeScopeIRIs = new HashSet<IRI>();
+ scopeListeners = new HashSet<ScopeEventListener>();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#addScopeRegistrationListener(eu.iksproject.kres.api.manager.ontology.ScopeEventListener)
+ */
+ @Override
+ public void addScopeRegistrationListener(ScopeEventListener listener) {
+ scopeListeners.add(listener);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#clearScopeRegistrationListeners()
+ */
+ @Override
+ public void clearScopeRegistrationListeners() {
+ scopeListeners.clear();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#containsScope(org.semanticweb.owlapi.model.IRI)
+ */
+ @Override
+ public boolean containsScope(IRI scopeID) {
+ // containsKey() is not reliable enough
+ return scopeMap.get(scopeID) != null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#deregisterScope(eu.iksproject.kres.api.manager.ontology.OntologyScope)
+ */
+ @Override
+ public synchronized void deregisterScope(OntologyScope scope) {
+ IRI id = scope.getID();
+ if (!containsScope(id))
+ throw new NoSuchScopeException(id);
+ // For sure it is deactivated...
+ setScopeActive(id, false);
+// activeScopeIRIs.remove(id);
+ scopeMap.remove(id);
+ fireScopeDeregistered(scope);
+ }
+
+ protected void fireScopeActivationChange(IRI scopeID, boolean activated) {
+ OntologyScope scope = scopeMap.get(scopeID);
+ if (activated)
+ for (ScopeEventListener l : scopeListeners)
+ l.scopeActivated(scope);
+ else
+ for (ScopeEventListener l : scopeListeners)
+ l.scopeDeactivated(scope);
+ }
+
+ /**
+ * Notifies all registered scope listeners that an ontology scope has been
+ * removed.
+ *
+ * @param scope
+ * the scope that was removed.
+ */
+ protected void fireScopeDeregistered(OntologyScope scope) {
+ for (ScopeEventListener l : scopeListeners)
+ l.scopeDeregistered(scope);
+ }
+
+ /**
+ * Notifies all registered scope listeners that an ontology scope has been
+ * added.
+ *
+ * @param scope
+ * the scope that was added.
+ */
+ protected void fireScopeRegistered(OntologyScope scope) {
+ for (ScopeEventListener l : scopeListeners)
+ l.scopeRegistered(scope);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#getActiveScopes()
+ */
+ @Override
+ public Set<OntologyScope> getActiveScopes() {
+ Set<OntologyScope> scopes = new HashSet<OntologyScope>();
+ for (IRI id : activeScopeIRIs)
+ scopes.add(scopeMap.get(id));
+ return scopes;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#getRegisteredScopes()
+ */
+ @Override
+ public synchronized Set<OntologyScope> getRegisteredScopes() {
+ return new HashSet<OntologyScope>(scopeMap.values());
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#getScope(org.semanticweb.owlapi.model.IRI)
+ */
+ @Override
+ public OntologyScope getScope(IRI scopeID) {
+ return scopeMap.get(scopeID);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#getScopeRegistrationListeners()
+ */
+ @Override
+ public Set<ScopeEventListener> getScopeRegistrationListeners() {
+ return scopeListeners;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#isScopeActive(org.semanticweb.owlapi.model.IRI)
+ */
+ @Override
+ public boolean isScopeActive(IRI scopeID) {
+ if (!containsScope(scopeID))
+ throw new NoSuchScopeException(scopeID);
+ return activeScopeIRIs.contains(scopeID);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#registerScope(eu.iksproject.kres.api.manager.ontology.OntologyScope)
+ */
+ @Override
+ public synchronized void registerScope(OntologyScope scope) {
+ registerScope(scope, false);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#registerScope(eu.iksproject.kres.api.manager.ontology.OntologyScope, boolean)
+ */
+ @Override
+ public synchronized void registerScope(OntologyScope scope, boolean activate) {
+ scopeMap.put(scope.getID(), scope);
+ setScopeActive(scope.getID(), activate);
+ fireScopeRegistered(scope);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#removeScopeRegistrationListener(eu.iksproject.kres.api.manager.ontology.ScopeEventListener)
+ */
+ @Override
+ public void removeScopeRegistrationListener(ScopeEventListener listener) {
+ scopeListeners.remove(listener);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see eu.iksproject.kres.api.manager.ontology.ScopeRegistry#setScopeActive(org.semanticweb.owlapi.model.IRI, boolean)
+ */
+ @Override
+ public void setScopeActive(IRI scopeID, boolean active) {
+ if (!containsScope(scopeID))
+ throw new NoSuchScopeException(scopeID);
+ // Prevent no-changes from firing events.
+ boolean previousStatus = isScopeActive(scopeID);
+ OntologyScope scope = getScope(scopeID);
+ if (active == previousStatus)
+ return;
+ if (active) {
+ scope.setUp();
+ activeScopeIRIs.add(scopeID);
+ } else {
+ scope.tearDown();
+ activeScopeIRIs.remove(scopeID);
+ }
+ fireScopeActivationChange(scopeID, active);
+ }
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/SessionOntologySpaceImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/SessionOntologySpaceImpl.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/SessionOntologySpaceImpl.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/ontology/SessionOntologySpaceImpl.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,99 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.ontology;
+
+import java.util.Random;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.io.RootOntologySource;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.SessionOntologySpace;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.SpaceType;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.UnmodifiableOntologySpaceException;
+import org.apache.stanbol.ontologymanager.ontonet.impl.io.ClerezzaOntologyStorage;
+import org.apache.stanbol.ontologymanager.ontonet.impl.util.StringUtils;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class SessionOntologySpaceImpl extends AbstractOntologySpaceImpl
+ implements SessionOntologySpace {
+
+
+ public static final String SUFFIX = SpaceType.SESSION.getIRISuffix();
+// static {
+// SUFFIX = SpaceType.SESSION.getIRISuffix();
+// }
+
+ public SessionOntologySpaceImpl(IRI scopeID, ClerezzaOntologyStorage storage) {
+ // FIXME : sync session id with session space ID
+ super(IRI.create(StringUtils.stripIRITerminator(scopeID) + "/"
+ + SpaceType.SESSION.getIRISuffix() + "-"
+ + new Random().nextLong()), SpaceType.SESSION,storage/*, scopeID*/);
+
+ IRI iri = IRI.create(StringUtils.stripIRITerminator(getID())
+ + "/root.owl");
+ try {
+ setTopOntology(new RootOntologySource(ontologyManager
+ .createOntology(iri), null), false);
+ } catch (OWLOntologyCreationException e) {
+ log.error("KReS :: Could not create session space root ontology "
+ + iri, e);
+ } catch (UnmodifiableOntologySpaceException e) {
+ // Should not happen...
+ log
+ .error(
+ "KReS :: Session space ontology "
+ + iri
+ + " was denied modification by the space itself. This should not happen.",
+ e);
+ }
+ }
+
+ public SessionOntologySpaceImpl(IRI scopeID, ClerezzaOntologyStorage storage,
+ OWLOntologyManager ontologyManager) {
+ // FIXME : sync session id with session space ID
+ super(IRI.create(StringUtils.stripIRITerminator(scopeID) + "/"
+ + SpaceType.SESSION.getIRISuffix() + "-"
+ + new Random().nextLong()), SpaceType.SESSION,storage, /*scopeID,*/ ontologyManager);
+ Logger log = LoggerFactory.getLogger(getClass());
+ IRI iri = IRI.create(StringUtils.stripIRITerminator(getID())
+ + "/root.owl");
+ try {
+ setTopOntology(new RootOntologySource(ontologyManager
+ .createOntology(iri), null), false);
+ } catch (OWLOntologyCreationException e) {
+ log.error("KReS :: Could not create session space root ontology "
+ + iri, e);
+ } catch (UnmodifiableOntologySpaceException e) {
+ // Should not happen...
+ log
+ .error(
+ "KReS :: Session space ontology "
+ + iri
+ + " was denied modification by the space itself. This should not happen.",
+ e);
+ }
+ }
+
+ /**
+ * Session spaces expose their ontology managers.
+ */
+ @Override
+ public OWLOntologyManager getOntologyManager() {
+ return ontologyManager;
+ }
+
+ /**
+ * Once it is set up, a session space is write-enabled.
+ */
+ @Override
+ public synchronized void setUp() {
+ locked = false;
+ }
+
+ @Override
+ public synchronized void tearDown() {
+ // TODO Auto-generated method stub
+ }
+
+}
Propchange: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Apr 1 12:08:25 2011
@@ -0,0 +1 @@
+target
Propchange: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Apr 1 12:08:25 2011
@@ -0,0 +1 @@
+target
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheException.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheException.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheException.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheException.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,24 @@
+/**
+ *
+ */
+package org.apache.stanbol.ontologymanager.ontonet.impl.registry.cache;
+
+/**
+ * @author Enrico Daga
+ *
+ */
+public class ODPRegistryCacheException extends Exception {
+
+ public ODPRegistryCacheException(Throwable e) {
+ super(e);
+ }
+
+ public ODPRegistryCacheException() {
+ }
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 9018256167443781857L;
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheManager.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheManager.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheManager.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/ODPRegistryCacheManager.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,350 @@
+/**
+ *
+ */
+package org.apache.stanbol.ontologymanager.ontonet.impl.registry.cache;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.io.FileDocumentSource;
+import org.semanticweb.owlapi.io.OWLOntologyDocumentSource;
+import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.MissingImportEvent;
+import org.semanticweb.owlapi.model.MissingImportListener;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyLoaderListener;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.semanticweb.owlapi.model.OWLOntologyStorageException;
+import org.semanticweb.owlapi.model.UnknownOWLOntologyException;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Enrico Daga
+ *
+ */
+public class ODPRegistryCacheManager {
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ /*
+ * TODO bundle path or something
+ */
+ public static final String WORKSPACE_PATH = ""/*
+ * ResourcesPlugin.getWorkspace()
+ * .getRoot().getLocationURI().
+ * toString()
+ */;
+
+ public static final String TEMPORARY_DIR_NAME = ".xd";
+ public static final String TEMPORARY_FILE_PREFIX = "uri";
+ public static final String TEMPORARY_FILE_EXTENSION = ".res";
+ public static final String TEMPORARY_URI_REGISTRY = ".registry";
+ private static final String SEPARATOR = System
+ .getProperty("file.separator");
+ private static final String URI_SEPARATOR = "/";
+
+ private static Map<URI, File> uris = new HashMap<URI, File>();
+ private static Map<URI, IRI> oiri = new HashMap<URI, IRI>();
+
+ private static Set<URI> unresolvedURIs = new HashSet<URI>();
+
+ private static OWLOntologyManager manager = OWLManager
+ .createOWLOntologyManager();
+
+ public static OWLOntologyManager getManager() {
+ return manager;
+ }
+
+ public static void addResource(OWLOntology ontology, URI virtualPhysicalURI)
+ throws ODPRegistryCacheException {
+
+ File file = newFile();
+ try {
+ cacheOntology(virtualPhysicalURI, file, ontology);
+ // manager.saveOntology(ontology, new RDFXMLOntologyFormat(), file
+ // .toURI());
+ } catch (UnknownOWLOntologyException e) {
+ throw new ODPRegistryCacheException(e);
+ } catch (OWLOntologyStorageException e) {
+ throw new ODPRegistryCacheException(e);
+ }
+ // uris.put(virtualPhysicalURI, file);
+ }
+
+ /**
+ * uri is the physical uri
+ *
+ * @param uri
+ * @return
+ * @throws ODPRegistryCacheException
+ * @throws URIUnresolvableException
+ */
+ public synchronized static OWLOntology getOntology(URI uri)
+ throws ODPRegistryCacheException, URIUnresolvableException {
+ if (getUnresolvedURIs().contains(uri))
+ throw new URIUnresolvableException();
+ try {
+ if (uris.containsKey(uri))
+ return retrieveLocalResource(uri);
+ else
+ return retrieveRemoteResource(uri);
+ } catch (UnknownOWLOntologyException e) {
+ throw new ODPRegistryCacheException(e);
+ } catch (OWLOntologyCreationException e) {
+ throw new ODPRegistryCacheException(e);
+ } catch (OWLOntologyStorageException e) {
+ throw new ODPRegistryCacheException(e);
+ }
+ }
+
+ public synchronized static OWLOntologyDocumentSource getOntologyInputSource(
+ URI uri) throws ODPRegistryCacheException, URIUnresolvableException {
+ if (getUnresolvedURIs().contains(uri))
+ throw new URIUnresolvableException();
+ if (uris.containsKey(uri)) {
+ File f = uris.get(uri);
+ FileDocumentSource fds = new FileDocumentSource(f);
+ return fds;
+ } else {
+ try {
+ retrieveRemoteResource(uri);
+ return getOntologyInputSource(uri);
+ } catch (UnknownOWLOntologyException e) {
+ throw new ODPRegistryCacheException(e);
+ } catch (OWLOntologyCreationException e) {
+ throw new ODPRegistryCacheException(e);
+ } catch (OWLOntologyStorageException e) {
+ throw new ODPRegistryCacheException(e);
+ }
+
+ }
+
+ }
+
+ public static URI getRegistryURI() {
+ return URI.create(WORKSPACE_PATH + URI_SEPARATOR + TEMPORARY_DIR_NAME
+ + URI_SEPARATOR + TEMPORARY_URI_REGISTRY);
+ }
+
+ public static boolean registryContains(URI ontologyURI) {
+ return uris.containsKey(ontologyURI);
+ }
+
+ public static URI getTemporaryFolder() {
+ return URI.create(WORKSPACE_PATH + URI_SEPARATOR + TEMPORARY_DIR_NAME);
+ }
+
+ /**
+ * @return the unresolvedURIs
+ */
+ public static Set<URI> getUnresolvedURIs() {
+ return unresolvedURIs;
+ }
+
+ public static File newFile() {
+ File file = new File(URI.create(getTemporaryFolder().toString()
+ + URI_SEPARATOR + TEMPORARY_FILE_PREFIX
+ + System.currentTimeMillis() + TEMPORARY_FILE_EXTENSION));
+ return file;
+ }
+
+ private synchronized static OWLOntology retrieveLocalResource(URI uri)
+ throws OWLOntologyCreationException, ODPRegistryCacheException,
+ URIUnresolvableException {
+ File file = uris.get(uri);
+ if (!file.exists()) {
+ uris.remove(uri);
+ return getOntology(uri);
+ }
+ manager.setSilentMissingImportsHandling(true);
+ manager.addMissingImportListener(new MissingImportListener() {
+ public void importMissing(MissingImportEvent arg0) {
+ if (!getUnresolvedURIs()
+ .contains(arg0.getImportedOntologyURI()))
+ getUnresolvedURIs().add(arg0.getImportedOntologyURI());
+ }
+ });
+ IRI oi = oiri.get(uri);
+ OWLOntology ontology = null;
+ ontology = manager.getOntology(oi);
+ if (ontology == null)
+ try {
+ ontology = manager.loadOntologyFromOntologyDocument(IRI
+ .create(file));
+ } catch (OWLOntologyAlreadyExistsException e) {
+ ontology = manager.getOntology(e.getOntologyID());
+ }
+
+ return ontology;
+ }
+
+ /**
+ * Gets the remote ontology and saves it locally
+ *
+ * @param uri
+ * @return
+ * @throws OWLOntologyCreationException
+ * @throws UnknownOWLOntologyException
+ * @throws OWLOntologyStorageException
+ */
+ private synchronized static OWLOntology retrieveRemoteResource(URI uri)
+ throws OWLOntologyCreationException, UnknownOWLOntologyException,
+ OWLOntologyStorageException {
+
+ manager.setSilentMissingImportsHandling(true);
+ manager.addMissingImportListener(new MissingImportListener() {
+ public void importMissing(MissingImportEvent arg0) {
+ if (!getUnresolvedURIs()
+ .contains(arg0.getImportedOntologyURI()))
+ getUnresolvedURIs().add(arg0.getImportedOntologyURI());
+ }
+ });
+ manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {
+
+ @Override
+ public void startedLoadingOntology(LoadingStartedEvent event) {
+ // Nothing to do
+ }
+
+ @Override
+ public void finishedLoadingOntology(LoadingFinishedEvent event) {
+
+ URI onturi = event.getDocumentIRI().toURI();
+
+ if (event.getException() != null) {
+ getUnresolvedURIs().add(onturi);
+ LoggerFactory.getLogger(ODPRegistryCacheManager.class)
+ .warn(
+ "Failed to resolve ontology at " + onturi
+ + " . Skipping.",
+ event.getException());
+ return;
+ }
+ try {
+ if (!uris.containsKey(onturi)) {
+ cacheOntology(onturi, newFile(), manager
+ .getOntology(event.getOntologyID()));
+ }
+ } catch (UnknownOWLOntologyException e) {
+ LoggerFactory.getLogger(ODPRegistryCacheManager.class)
+ .warn(
+ "Failed to cache ontology at " + onturi
+ + " . Skipping.", e);
+ getUnresolvedURIs().add(onturi);
+ } catch (OWLOntologyStorageException e) {
+ LoggerFactory.getLogger(ODPRegistryCacheManager.class)
+ .warn(
+ "Failed to cache ontology at " + onturi
+ + " . Skipping.", e);
+ getUnresolvedURIs().add(onturi);
+ }
+ }
+ });
+
+ OWLOntology ont;
+ try {
+ ont = manager.loadOntologyFromOntologyDocument(IRI.create(uri));
+ } catch (OWLOntologyAlreadyExistsException e) {
+ ont = manager.getOntology(e.getOntologyID().getOntologyIRI());
+ }
+ File file = newFile();
+ cacheOntology(uri, file, ont);
+
+ return ont;
+ }
+
+ private static synchronized void cacheOntology(URI physicalRemoteUri,
+ File file, OWLOntology ont) throws UnknownOWLOntologyException,
+ OWLOntologyStorageException {
+ uris.put(physicalRemoteUri, file);
+ oiri.put(physicalRemoteUri, ont.getOntologyID().getOntologyIRI());
+ manager.setOntologyDocumentIRI(ont, IRI.create(file));
+ manager.saveOntology(ont, new RDFXMLOntologyFormat(), IRI.create(file));
+ }
+
+ public synchronized static boolean save() {
+ File registry = new File(ODPRegistryCacheManager.getRegistryURI());
+ if (registry.exists())
+ registry.delete();
+ try {
+ registry.createNewFile();
+ BufferedWriter writer = new BufferedWriter(new FileWriter(registry));
+
+ for (URI u : uris.keySet()) {
+ writer.write(u.toString() + "|" + uris.get(u).toString());
+ writer.newLine();
+ }
+ writer.close();
+ } catch (IOException e) {
+ return false;
+ }
+ return true;
+ }
+
+ public synchronized static boolean load() {
+ File registry = new File(ODPRegistryCacheManager.getRegistryURI());
+ if (!registry.exists())
+ return false;
+
+ Map<URI, File> newUris = new HashMap<URI, File>();
+
+ try {
+ BufferedReader reader = new BufferedReader(new FileReader(registry));
+
+ String line = null;
+ while ((line = reader.readLine()) != null) {
+ if (line.indexOf('|') < 0)
+ continue;
+ String[] splitted = line.split("\\|");
+ newUris.put(URI.create(splitted[0]), new File(splitted[1]));
+ }
+ reader.close();
+ } catch (FileNotFoundException e) {
+ LoggerFactory.getLogger(ODPRegistryCacheManager.class).error(
+ "Failed to load registry " + getRegistryURI()
+ + " File not found.", e);
+ } catch (IOException e) {
+ LoggerFactory.getLogger(ODPRegistryCacheManager.class).error(
+ "Failed to load registry " + getRegistryURI(), e);
+ }
+ uris.clear();
+ uris = newUris;
+ return true;
+ }
+
+ public synchronized static boolean clean() {
+ // FileUriHelper fu = new FileUriHelper();
+ try {
+ // fu
+ // .deleteDir(new File(ODPRegistryCacheManager
+ // .getTemporaryFolder()));
+
+ uris.clear();
+ manager = null;
+ manager = OWLManager.createOWLOntologyManager();
+ } catch (Exception e) {
+ LoggerFactory.getLogger(ODPRegistryCacheManager.class).error(
+ "OWL cache manager cleanup failed. ", e);
+ return false;
+ }
+
+ return true;
+ }
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/URIUnresolvableException.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/URIUnresolvableException.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/URIUnresolvableException.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/cache/URIUnresolvableException.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,18 @@
+/**
+ *
+ */
+package org.apache.stanbol.ontologymanager.ontonet.impl.registry.cache;
+
+/**
+ * @author Enrico Daga
+ * @see ODPRegistryCacheManager
+ *
+ */
+public class URIUnresolvableException extends ODPRegistryCacheException {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 2787947324775105883L;
+
+}
Propchange: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/model/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Apr 1 12:08:25 2011
@@ -0,0 +1 @@
+target
Propchange: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/model/impl/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Apr 1 12:08:25 2011
@@ -0,0 +1 @@
+target
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/model/impl/RegistryLoaderImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/model/impl/RegistryLoaderImpl.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/model/impl/RegistryLoaderImpl.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/registry/model/impl/RegistryLoaderImpl.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,564 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.registry.model.impl;
+
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.ONManager;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.RegistryLoader;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.io.XDRegistrySource;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.AbstractRegistryItem;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.Registry;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.RegistryContentException;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.RegistryItem;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.RegistryLibrary;
+import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.RegistryOntology;
+import org.apache.stanbol.ontologymanager.ontonet.impl.registry.cache.ODPRegistryCacheException;
+import org.apache.stanbol.ontologymanager.ontonet.impl.registry.cache.ODPRegistryCacheManager;
+import org.apache.stanbol.ontologymanager.ontonet.impl.registry.cache.URIUnresolvableException;
+import org.apache.stanbol.ontologymanager.ontonet.xd.utils.RDFSLabelGetter;
+import org.apache.stanbol.ontologymanager.ontonet.xd.vocabulary.CODOVocabulary;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.io.OWLOntologyCreationIOException;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLAxiom;
+import org.semanticweb.owlapi.model.OWLClass;
+import org.semanticweb.owlapi.model.OWLDataFactory;
+import org.semanticweb.owlapi.model.OWLIndividual;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+import org.semanticweb.owlapi.model.OWLObjectProperty;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException;
+import org.semanticweb.owlapi.model.OWLOntologyChangeException;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.semanticweb.owlapi.model.OWLOntologySetProvider;
+import org.semanticweb.owlapi.util.OWLAxiomFilter;
+import org.semanticweb.owlapi.util.OWLOntologyMerger;
+import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class RegistryLoaderImpl implements RegistryLoader {
+
+ private final OWLClass cRegistryLibrary;
+
+ private ONManager onm;
+
+ private Logger log = LoggerFactory.getLogger(getClass());
+
+ private final OWLObjectProperty isPartOf, isOntologyOf;
+
+ private final IRI mergedOntologyIRI = IRI
+ .create(CODOVocabulary.REPOSITORY_MERGED_ONTOLOGY);
+
+ private Map<URI, OWLOntology> registryOntologiesCache = new HashMap<URI, OWLOntology>();
+
+ /**
+ */
+ public RegistryLoaderImpl(ONManager onm) {
+ this.onm = onm;
+ OWLDataFactory factory = OWLManager.getOWLDataFactory();
+ cRegistryLibrary = factory.getOWLClass(IRI
+ .create(CODOVocabulary.CODD_OntologyLibrary));
+ isPartOf = factory.getOWLObjectProperty(IRI
+ .create(CODOVocabulary.PARTOF_IsPartOf));
+ isOntologyOf = factory.getOWLObjectProperty(IRI
+ .create(CODOVocabulary.ODPM_IsOntologyOf));
+ }
+
+ public Set<OWLOntology> gatherOntologies(RegistryItem registryItem,
+ OWLOntologyManager manager, boolean recurseRegistries)
+ throws OWLOntologyCreationException {
+
+ Set<OWLOntology> result = new HashSet<OWLOntology>();
+
+ if (registryItem instanceof Registry)
+ for (RegistryItem item : ((Registry) registryItem).getChildren())
+ try {
+ result.addAll(gatherOntologies(item, manager,
+ recurseRegistries));
+ } catch (OWLOntologyCreationException e) {
+ log
+ .warn(
+ "KReS :: [NONFATAL] Could not gather ontologies for registry "
+ + registryItem.getName()
+ + ". Skipping.", e);
+ continue;
+ }
+ else if (registryItem.isOntology())
+ try {
+ result.add(manager.loadOntologyFromOntologyDocument(IRI
+ .create(((RegistryOntology) registryItem).getURL())));
+ } catch (OWLOntologyAlreadyExistsException ex) {
+ // E chissenefrega, ce la aggiungiamo lo stesso.
+ result.add(manager.getOntology(ex.getOntologyID()));
+ } catch (OWLOntologyCreationIOException ex) {
+ // Che ce potemo fa'?
+ } catch (URISyntaxException e) {
+ log.warn("KReS :: [NONFATAL] Malformed URI for ontology "
+ + registryItem.getName() + ". Skipping.", e);
+ }
+ else if (registryItem.isLibrary())
+ for (RegistryItem item : ((RegistryLibrary) registryItem)
+ .getChildren()) {
+ result
+ .addAll(gatherOntologies(item, manager,
+ recurseRegistries));
+ }
+ return result;
+ }
+
+ public RegistryLibrary getLibrary(Registry reg, IRI libraryID) {
+ for (RegistryItem child : reg.getChildren()) {
+ try {
+ if (child.isLibrary()
+ && IRI.create(child.getURL()).equals(libraryID))
+ return (RegistryLibrary) child;
+ } catch (URISyntaxException e) {
+ // If some URL is not well-formed here and there, sticazzi
+ continue;
+ }
+ }
+ return null;
+ }
+
+ private OWLOntology getMergedOntology(IRI registryLocation)
+ throws RegistryContentException {
+ try {
+ return getMergedOntology(registryLocation.toURI().toURL());
+ } catch (MalformedURLException e) {
+ log.warn(
+ "KReS :: [NONFATAL] Malformed URI for merged ontology from registry "
+ + registryLocation, e);
+ return null;
+ }
+ }
+
+ private OWLOntology getMergedOntology(URL registryLocation)
+ throws RegistryContentException {
+ OWLOntology ontology = null;
+
+ try {
+ IRI mergedOntology = mergedOntologyIRI.resolve("#"
+ + URLEncoder.encode(registryLocation.toString(), "UTF-8"));
+ if (!ODPRegistryCacheManager.registryContains(mergedOntology
+ .toURI())) {
+
+ // final OWLOntology ont =
+ // getOntologyForRegistryLocation(registryLocation
+ // .toURI());
+
+ final OWLOntology ont = getOntologyForRegistryLocationNoCached(registryLocation
+ .toURI());
+ if (ont == null)
+ throw new RegistryContentException(
+ new NullPointerException("Registry unavailable: "
+ + registryLocation.toURI()));
+
+ OWLOntologySetProvider provider = new OWLOntologySetProvider() {
+ public Set<OWLOntology> getOntologies() {
+ return ODPRegistryCacheManager.getManager()
+ .getImportsClosure(ont);
+ }
+ };
+ final OWLDataFactory factory = ODPRegistryCacheManager
+ .getManager().getOWLDataFactory();
+
+ // We filter only interesting axioms
+ OWLAxiomFilter filter = new OWLAxiomFilter() {
+ public boolean passes(OWLAxiom arg0) {
+ if (arg0
+ .getSignature()
+ .contains(
+ factory
+ .getOWLClass(IRI
+ .create(CODOVocabulary.CODD_OntologyLibrary)))
+ || arg0
+ .getSignature()
+ .contains(
+ factory
+ .getOWLClass(IRI
+ .create(CODOVocabulary.CODK_Ontology)))
+ || arg0
+ .getSignature()
+ .contains(
+ factory
+ .getOWLObjectProperty(IRI
+ .create(CODOVocabulary.ODPM_IsOntologyOf)))
+ || arg0
+ .getSignature()
+ .contains(
+ factory
+ .getOWLObjectProperty(IRI
+ .create(CODOVocabulary.PARTOF_IsPartOf)))
+ || arg0
+ .getSignature()
+ .contains(
+ factory
+ .getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL
+ .getIRI()))) {
+ return true;
+ }
+ return false;
+ }
+ };
+
+ OWLOntologyMerger merger = new OWLOntologyMerger(provider,
+ filter);
+ OWLOntology merged = merger.createMergedOntology(
+ ODPRegistryCacheManager.getManager(), mergedOntology);
+ ODPRegistryCacheManager.addResource(merged, mergedOntology
+ .toURI());
+ ontology = merged;
+ } else {
+ ontology = ODPRegistryCacheManager.getOntology(mergedOntology
+ .toURI());
+ }
+ } catch (URIUnresolvableException e) {
+ throw new RegistryContentException(e);
+ } catch (OWLOntologyCreationException e) {
+ throw new RegistryContentException(e);
+ } catch (OWLOntologyChangeException e) {
+ throw new RegistryContentException(e);
+ } catch (ODPRegistryCacheException e) {
+ throw new RegistryContentException(e);
+ } catch (UnsupportedEncodingException e) {
+ throw new RegistryContentException(e);
+ } catch (URISyntaxException e) {
+ throw new RegistryContentException(e);
+ }
+ return ontology;
+ }
+
+ private OWLOntology getOntologyForRegistryLocation(URI location) {
+ return registryOntologiesCache.get(location);
+ }
+
+ private OWLOntology getOntologyForRegistryLocationNoCached(URI location) {
+ try {
+ return OWLManager.createOWLOntologyManager()
+ .loadOntologyFromOntologyDocument(IRI.create(location));
+ } catch (OWLOntologyCreationException e) {
+ log.error("KReS :: Registry loader failed to load ontology at "
+ + location, e);
+ return null;
+ }
+ }
+
+ public Object getParent(Object child) {
+ if (child instanceof AbstractRegistryItem) {
+ return ((RegistryItem) child).getParent();
+ }
+ return null;
+ }
+
+ private Set<OWLIndividual> getParentContainer(OWLNamedIndividual child,
+ OWLOntology ontology) {
+
+ if (child.getObjectPropertyValues(ontology).containsKey(isPartOf)
+ || child.getObjectPropertyValues(ontology).containsKey(
+ isOntologyOf)) {
+ Set<OWLIndividual> partOfSet = child.getObjectPropertyValues(
+ ontology).get(isPartOf);
+ Set<OWLIndividual> ontologyOfSet = child.getObjectPropertyValues(
+ ontology).get(isOntologyOf);
+
+ Set<OWLIndividual> mergedSet = new HashSet<OWLIndividual>();
+ if (partOfSet != null)
+ mergedSet.addAll(partOfSet);
+ if (ontologyOfSet != null)
+ mergedSet.addAll(ontologyOfSet);
+ return mergedSet;
+ } else
+ return new HashSet<OWLIndividual>();
+ }
+
+ private Set<OWLNamedIndividual> getParts(OWLIndividual parent,
+ OWLOntology ontology) {
+ Set<OWLNamedIndividual> indies = ontology.getIndividualsInSignature();
+ Iterator<OWLNamedIndividual> iter = indies.iterator();
+ Set<OWLNamedIndividual> tor = new HashSet<OWLNamedIndividual>();
+ // For each individual in this ontology
+ while (iter.hasNext()) {
+ OWLNamedIndividual n = iter.next();
+ // Get its parent wrt to isPartOf or isOntologyOf relationships
+ for (OWLIndividual i : this.getParentContainer(n, ontology)) {
+ if (i.equals(parent)) {
+ tor.add(n);
+ break;
+ }
+ }
+ }
+ return tor;
+ }
+
+ private List<Registry> getRegistries() {
+ List<Registry> registries = new ArrayList<Registry>();
+ // String storedStringValue = XDRegistryPlugin.getDefault()
+ // .getPreferenceStore().getString(
+ // PreferenceConstants.P_ODP_REGISTRIES);
+ String[] regs = new String[] {}/*
+ * URLListEditor
+ * .parsePreferenceStoreValue
+ * (storedStringValue)
+ */;
+
+ for (int i = 0; i < regs.length; i++) {
+ Registry registry1 = null;
+ try {
+ // TODO Find a way to obtain registry names
+ String registryName = ""/*
+ * URLListEditor
+ * .parseNameValueString(regs[i])[0]
+ */;
+ // TODO Find a way to obtain registry locations
+ String registryLocation = ""/*
+ * URLListEditor
+ * .parseNameValueString(regs[i])[1]
+ */;
+ registry1 = new Registry(registryName);
+ registry1.setURL(new URL(registryLocation));
+ } catch (Exception e) {
+ if (registry1 != null) {
+ registry1.setError(e.getLocalizedMessage());
+ log.error("KReS :: Error on ODP registry: "
+ + registry1.getName(), e);
+ }
+ }
+ if (registry1 != null)
+ registries.add(registry1);
+ else
+ log.error("KReS :: Cannot load ODP registry: " + regs[i]);
+ }
+ return registries;
+ }
+
+ private List<Registry> getRegistries(XDRegistrySource source) {
+
+ List<Registry> registries = new ArrayList<Registry>();
+
+ if (source.getPhysicalIRI() != null) {
+
+ } else if (source.isInputStreamAvailable()) {
+
+ } else if (source.isReaderAvailable()) {
+
+ }
+
+ return registries;
+ }
+
+ private RegistryLibrary getTree(OWLNamedIndividual i, OWLOntology ontology) {
+
+ RegistryLibrary to = new RegistryLibrary(new RDFSLabelGetter(ontology,
+ i.getIRI(), false).getPreferred());
+ try {
+ Set<OWLNamedIndividual> children = this.getParts(i, ontology);
+ if (children.size() == 0)
+ return to;
+ for (OWLNamedIndividual childIndividual : children) {
+ if (isLibrary(childIndividual, ontology)) {
+ RegistryLibrary t = this.getTree(childIndividual, ontology);
+ to.addChild(t);
+ } else if (isOntology(childIndividual, ontology)) {
+ RegistryOntology t = new RegistryOntology(
+ new RDFSLabelGetter(ontology, childIndividual
+ .getIRI(), false).getPreferred());
+ t.setURL(childIndividual.getIRI().toURI().toURL());
+ to.addChild(t);
+ }
+ }
+ } catch (MalformedURLException e) {
+ log.error(
+ "KReS :: MalformedURLException caught while getting tree for "
+ + i.getIRI(), e);
+
+ }
+ return to;
+ }
+
+ public boolean hasChildren(Object parent) {
+ if (parent instanceof RegistryLibrary)
+ return ((RegistryLibrary) parent).hasChildren();
+ return false;
+ }
+
+ public boolean hasLibrary(Registry reg, IRI libraryID) {
+ for (RegistryItem child : reg.getChildren()) {
+ try {
+ if (child.isLibrary()
+ && IRI.create(child.getURL()).equals(libraryID))
+ return true;
+ } catch (URISyntaxException e) {
+ // If some URL is not well-formed here and there, sticazzi
+ continue;
+ }
+ }
+ return false;
+ }
+
+ private boolean isLibrary(OWLIndividual indy, OWLOntology ontology) {
+ OWLClass folderClass = OWLManager.getOWLDataFactory().getOWLClass(
+ IRI.create(CODOVocabulary.CODD_OntologyLibrary));
+ return (folderClass.getIndividuals(ontology).contains(indy));
+ }
+
+ private boolean isOntology(OWLIndividual indy, OWLOntology ontology) {
+ OWLClass ontologyClass = OWLManager.getOWLDataFactory().getOWLClass(
+ IRI.create(CODOVocabulary.CODK_Ontology));
+ return (ontologyClass.getIndividuals(ontology).contains(indy));
+ }
+
+ public void loadLocations() throws RegistryContentException {
+
+ try {
+
+ registryOntologiesCache.clear();
+ List<Registry> registries = getRegistries();
+
+ int regsize = registries.size();
+ int c = 0;
+ for (Registry current : registries) {
+ c++;
+ log.debug("Loading " + current.toString() + " [" + c + "/"
+ + regsize + "]");
+ if (!ODPRegistryCacheManager.registryContains(current.getURL()
+ .toURI())) {
+ try {
+ log.debug("Fetching: " + current.getURL().toURI());
+ registryOntologiesCache.put(current.getURL().toURI(),
+ ODPRegistryCacheManager.getOntology(current
+ .getURL().toURI()));
+ } catch (URIUnresolvableException e) {
+ log.error("KReS :: could not resolve URI "
+ + current.getURL().toURI(), e);
+ registryOntologiesCache.put(current.getURL().toURI(),
+ null);
+ } catch (ODPRegistryCacheException e) {
+ log.error("KReS :: failed to cache ontology "
+ + current.getURL().toURI(), e);
+ registryOntologiesCache.put(current.getURL().toURI(),
+ null);
+ }
+ }
+ }
+ c = 0;
+ for (Registry registry : registries) {
+ c++;
+ try {
+ registry = setupRegistry(registry);
+ } catch (RegistryContentException e) {
+ registry.setError(" [Unable to load from location "
+ + registry.getURL().toString() + "]");
+ }
+ }
+ } catch (Throwable th) {
+ log
+ .error(
+ "KreS :: Exception occurred while trying to get registry locations.",
+ th);
+ }
+ }
+
+ /**
+ * The ontology at <code>physicalIRI</code> may in turn include more than
+ * one registry.
+ *
+ * @param physicalIRI
+ * @return
+ */
+ public Set<Registry> loadRegistriesEager(IRI physicalIRI) {
+
+ Set<Registry> results = new HashSet<Registry>();
+ OWLOntologyManager mgr = onm.getOwlCacheManager();// getManager();
+
+ try {
+ OWLOntology ontology = mgr.loadOntology(physicalIRI);
+ for (OWLIndividual ind : cRegistryLibrary.getIndividuals(ontology))
+ if (ind.isNamed()) {
+ OWLNamedIndividual nind = ind.asOWLNamedIndividual();
+ IRI regiri = nind.getIRI();
+ Registry registry = new Registry(regiri.getFragment());
+ try {
+ // TODO: avoid using toURL crap
+ registry.setURL(regiri.toURI().toURL());
+ } catch (MalformedURLException e) {
+ // Why should a well-formed IRI be a malformed URL
+ // anyway ?
+ log.warn("KReS :: ontology document IRI " + physicalIRI
+ + " matches a malformed URI pattern.", e);
+ }
+ // Find the ontologies in this registry
+ // If this is individual is not "ontology of" or "part of",
+ // then proceed.
+ if (!nind.getObjectPropertyValues(ontology).containsKey(
+ isPartOf)
+ && !nind.getObjectPropertyValues(ontology)
+ .containsKey(isOntologyOf)) {
+ registry.addChild(this.getTree(
+ (OWLNamedIndividual) nind, ontology));
+ }
+ results.add(registry);
+ }
+ } catch (OWLOntologyAlreadyExistsException e) {
+ log.warn("KReS :: ontology " + e.getOntologyID()
+ + " exists and will not be reloaded.", e);
+ // Do nothing. Existing ontologies are fine.
+ } catch (OWLOntologyCreationException e) {
+ log.error("KReS :: Could not load ontology " + physicalIRI + " .",
+ e);
+ } finally {
+ }
+ return results;
+ }
+
+ /**
+ * Requires that Registry objects are created earlier. Problem is, we might
+ * not know their names a priori.
+ *
+ * @param registry
+ * @return
+ * @throws RegistryContentException
+ */
+ private Registry setupRegistry(Registry registry)
+ throws RegistryContentException {
+
+ // For each registry:
+ registry.removeChildren();
+ OWLOntology ontology = getMergedOntology(registry.getURL());
+
+ // TODO: Restore ODP cache manager.
+ // setManager(ODPRegistryCacheManager.getManager());
+ Set<OWLIndividual> folderSet = cRegistryLibrary
+ .getIndividuals(ontology);
+
+ // Look for first level elements;
+ Iterator<OWLIndividual> iter = folderSet.iterator();
+ while (iter.hasNext()) {
+ OWLIndividual i = iter.next();
+ if (i instanceof OWLNamedIndividual) {
+ if (!i.getObjectPropertyValues(ontology).containsKey(isPartOf)
+ && !i.getObjectPropertyValues(ontology).containsKey(
+ isOntologyOf)) {
+ registry.addChild(this.getTree((OWLNamedIndividual) i,
+ ontology));
+ }
+ }
+ }
+
+ return registry;
+ }
+
+}
Propchange: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Apr 1 12:08:25 2011
@@ -0,0 +1 @@
+target
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/ScopeSetRenderer.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/ScopeSetRenderer.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/ScopeSetRenderer.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/ScopeSetRenderer.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,68 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.renderers;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyScope;
+import org.apache.stanbol.ontologymanager.ontonet.xd.vocabulary.Vocabulary;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.model.AddAxiom;
+import org.semanticweb.owlapi.model.AddImport;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLAxiom;
+import org.semanticweb.owlapi.model.OWLClass;
+import org.semanticweb.owlapi.model.OWLDataFactory;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyChange;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Just an attempt. If we like it, make an API out of it.
+ *
+ * @author alessandro
+ *
+ */
+public class ScopeSetRenderer {
+
+ private static OWLDataFactory __factory = OWLManager.getOWLDataFactory();
+
+ private static IRI _scopeIri = IRI
+ .create("http://kres.iks-project.eu/ontology/onm/meta.owl#Scope");
+
+ private static OWLClass cScope = __factory.getOWLClass(_scopeIri);
+
+ public static OWLOntology getScopes(Set<OntologyScope> scopes) {
+
+ OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
+ OWLOntology ont = null;
+ try {
+ ont = mgr.createOntology();
+ } catch (OWLOntologyCreationException e) {
+ LoggerFactory
+ .getLogger(ScopeSetRenderer.class)
+ .error(
+ "KReS :: could not create empty ontology for rendering scopes.",
+ e);
+ return null;
+ }
+ List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
+ // The ODP metadata vocabulary is always imported.
+ // TODO : also import the ONM meta when it goes online.
+ additions.add(new AddImport(ont, __factory
+ .getOWLImportsDeclaration(Vocabulary.ODPM.getIRI())));
+ for (OntologyScope scope : scopes) {
+ OWLNamedIndividual iScope = __factory.getOWLNamedIndividual(scope
+ .getID());
+ OWLAxiom ax = __factory.getOWLClassAssertionAxiom(cScope, iScope);
+ additions.add(new AddAxiom(ont, ax));
+ }
+ mgr.applyChanges(additions);
+
+ return ont;
+ }
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/SessionRenderer.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/SessionRenderer.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/SessionRenderer.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/renderers/SessionRenderer.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,83 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.renderers;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.session.Session;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
+import org.semanticweb.owlapi.io.StringDocumentTarget;
+import org.semanticweb.owlapi.model.AddAxiom;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLClass;
+import org.semanticweb.owlapi.model.OWLDataFactory;
+import org.semanticweb.owlapi.model.OWLDataProperty;
+import org.semanticweb.owlapi.model.OWLDatatype;
+import org.semanticweb.owlapi.model.OWLNamedIndividual;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyChange;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.semanticweb.owlapi.model.OWLOntologyStorageException;
+import org.semanticweb.owlapi.model.OWLTypedLiteral;
+import org.slf4j.LoggerFactory;
+
+public class SessionRenderer {
+
+ private static OWLDataFactory __factory = OWLManager.getOWLDataFactory();
+
+ private static final IRI _sessionIri = IRI
+ .create("http://kres.iks-project.eu/ontology/onm/meta.owl#Session");
+
+ private static final IRI _hasIdIri = IRI
+ .create("http://kres.iks-project.eu/ontology/onm/meta.owl#hasID");
+
+ private static OWLClass cSession = __factory.getOWLClass(_sessionIri);
+
+ private static OWLDataProperty hasId = __factory
+ .getOWLDataProperty(_hasIdIri);
+
+ static {
+
+ }
+
+ public static String getSessionMetadataRDF(Session session) {
+ OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
+ OWLOntology ont = null;
+ try {
+ ont = mgr.createOntology(IRI.create(session.getID() + "/meta.owl"));
+ } catch (OWLOntologyCreationException e) {
+ LoggerFactory
+ .getLogger(ScopeSetRenderer.class)
+ .error(
+ "KReS :: could not create empty ontology for rendering sesion metadata.",
+ e);
+ return null;
+ }
+
+ List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
+
+ OWLNamedIndividual iSes = __factory.getOWLNamedIndividual(session
+ .getID());
+ additions.add(new AddAxiom(ont, __factory.getOWLClassAssertionAxiom(
+ cSession, iSes)));
+ OWLDatatype anyURI = __factory.getOWLDatatype(IRI
+ .create("http://www.w3.org/2001/XMLSchema#anyURI"));
+ OWLTypedLiteral hasIdValue = __factory.getOWLTypedLiteral(session
+ .getID().toString(), anyURI);
+ additions.add(new AddAxiom(ont, __factory
+ .getOWLDataPropertyAssertionAxiom(hasId, iSes, hasIdValue)));
+ mgr.applyChanges(additions);
+
+ StringDocumentTarget tgt = new StringDocumentTarget();
+ try {
+ mgr.saveOntology(ont, new RDFXMLOntologyFormat(), tgt);
+ } catch (OWLOntologyStorageException e) {
+ LoggerFactory.getLogger(ScopeSetRenderer.class).error(
+ "KReS :: could not save session metadata ontology.", e);
+ }
+ return tgt.toString();
+
+ }
+
+}
Propchange: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Apr 1 12:08:25 2011
@@ -0,0 +1 @@
+target
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/ScopeSessionSynchronizer.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/ScopeSessionSynchronizer.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/ScopeSessionSynchronizer.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/ScopeSessionSynchronizer.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,51 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.session;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.ONManager;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyScope;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologySpaceFactory;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.Session;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionEvent;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionListener;
+import org.semanticweb.owlapi.model.IRI;
+
+public class ScopeSessionSynchronizer implements SessionListener {
+
+ private ONManager manager;
+
+ public ScopeSessionSynchronizer(ONManager manager) {
+ // WARN do not use ONManager here, as it will most probably be
+ // instantiated by it.
+ this.manager = manager;
+ }
+
+ private void addSessionSpaces(IRI sessionId) {
+ OntologySpaceFactory factory = manager
+ .getOntologySpaceFactory();
+ for (OntologyScope scope : manager.getScopeRegistry()
+ .getActiveScopes()) {
+ scope.addSessionSpace(factory.createSessionOntologySpace(scope
+ .getID()), sessionId);
+ }
+ }
+
+ @Override
+ public void sessionChanged(SessionEvent event) {
+ // System.err.println("Session " + event.getSession() + " has been "
+ // + event.getOperationType());
+ Session ses = event.getSession();
+ switch (event.getOperationType()) {
+ case CREATE:
+ ses.addSessionListener(this);
+ addSessionSpaces(ses.getID());
+ break;
+ case CLOSE:
+ break;
+ case KILL:
+ ses.removeSessionListener(this);
+ break;
+ default:
+ break;
+ }
+ }
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionImpl.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionImpl.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionImpl.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,186 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.session;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.session.Session;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.NonReferenceableSessionException;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionEvent;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionListener;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.Session.State;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionEvent.OperationType;
+import org.semanticweb.owlapi.model.IRI;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Standard implementation of the {@link Session} interface. A
+ * SessionImpl is initially inactive and creates its own identifier.
+ *
+ * @author alessandro
+ *
+ */
+public class SessionImpl implements Session {
+
+ /**
+ * A KReS session knows about its own ID.
+ */
+ protected IRI id = null;
+ protected Set<SessionListener> listeners;
+
+ /**
+ * A KReS session knows about its own state.
+ */
+ State state = State.HALTED;
+
+ /**
+ * Utility constructor for enforcing a given IRI as a session ID. It will
+ * not throw duplication exceptions, since a KReS session does not know
+ * about other sessions.
+ *
+ * @param sessionID
+ * the IRI to be set as unique identifier for this session
+ */
+ public SessionImpl(IRI sessionID) {
+ this.id = sessionID;
+ listeners = new HashSet<SessionListener>();
+ }
+
+ public SessionImpl(IRI sessionID, State initialState)
+ throws NonReferenceableSessionException {
+ this(sessionID);
+ if (initialState == State.ZOMBIE)
+ throw new NonReferenceableSessionException();
+ else
+ setActive(initialState == State.ACTIVE);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionListenable#addSessionListener
+ * (eu.iksproject.kres.api.manager.session.SessionListener)
+ */
+ @Override
+ public void addSessionListener(SessionListener listener) {
+ listeners.add(listener);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seeeu.iksproject.kres.api.manager.session.SessionListenable#
+ * clearSessionListeners()
+ */
+ @Override
+ public void clearSessionListeners() {
+ listeners.clear();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see eu.iksproject.kres.api.manager.session.Session#close()
+ */
+ @Override
+ public void close() throws NonReferenceableSessionException {
+ // if (getSessionState() == State.ZOMBIE)
+ // throw new NonReferenceableSessionException();
+ // state = State.ZOMBIE;
+ this.setActive(false);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see eu.iksproject.kres.api.manager.session.Session#getID()
+ */
+ @Override
+ public IRI getID() {
+ return id;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionListenable#getSessionListeners
+ * ()
+ */
+ @Override
+ public Collection<SessionListener> getSessionListeners() {
+ return listeners;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see eu.iksproject.kres.api.manager.session.Session#getSessionState()
+ */
+ @Override
+ public State getSessionState() {
+ return state;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see eu.iksproject.kres.api.manager.session.Session#isActive()
+ */
+ @Override
+ public boolean isActive() {
+ return state == State.ACTIVE;
+ }
+
+ @Override
+ public void removeSessionListener(SessionListener listener) {
+ listeners.remove(listener);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.Session#setActive(boolean)
+ */
+ @Override
+ public State setActive(boolean active)
+ throws NonReferenceableSessionException {
+ if (getSessionState() == State.ZOMBIE)
+ throw new NonReferenceableSessionException();
+ else
+ state = active ? State.ACTIVE : State.HALTED;
+ return getSessionState();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return getID().toString();
+ }
+
+ protected void fireClosed() {
+ SessionEvent e = null;
+ try {
+ e = new SessionEvent(this, OperationType.CLOSE);
+ } catch (Exception e1) {
+ LoggerFactory.getLogger(getClass()).error(
+ "KReS :: Could not close session " + getID(), e1);
+ return;
+ }
+ for (SessionListener l : listeners)
+ l.sessionChanged(e);
+ }
+
+ @Override
+ public void open() throws NonReferenceableSessionException {
+ setActive(true);
+ }
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionManagerImpl.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionManagerImpl.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/SessionManagerImpl.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,272 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.session;
+
+import java.io.OutputStream;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyScope;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.ScopeRegistry;
+import org.apache.stanbol.ontologymanager.ontonet.api.ontology.SessionOntologySpace;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.DuplicateSessionIDException;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.Session;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionIDGenerator;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionManager;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.NonReferenceableSessionException;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionEvent;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionListener;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.Session.State;
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionEvent.OperationType;
+import org.apache.stanbol.ontologymanager.ontonet.impl.io.ClerezzaOntologyStorage;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * Calls to <code>getSessionListeners()</code> return a {@link Set} of
+ * listeners.
+ *
+ * TODO: implement storage (using persistence layer).
+ *
+ * @author alessandro
+ *
+ */
+public class SessionManagerImpl implements SessionManager {
+
+ private Map<IRI, Session> sessionsByID;
+
+ protected Set<SessionListener> listeners;
+
+ protected Logger log = LoggerFactory.getLogger(getClass());
+
+ protected SessionIDGenerator idgen;
+
+ protected ScopeRegistry scopeRegistry;
+ protected ClerezzaOntologyStorage store;
+
+ public SessionManagerImpl(IRI baseIri, ScopeRegistry scopeRegistry, ClerezzaOntologyStorage store) {
+ idgen = new TimestampedSessionIDGenerator(baseIri);
+ listeners = new HashSet<SessionListener>();
+ sessionsByID = new HashMap<IRI, Session>();
+this.scopeRegistry = scopeRegistry;
+ this.store = store;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#addSessionListener
+ * (eu.iksproject.kres.api.manager.session.SessionListener)
+ */
+ @Override
+ public void addSessionListener(SessionListener listener) {
+ listeners.add(listener);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#clearSessionListeners
+ * ()
+ */
+ @Override
+ public void clearSessionListeners() {
+ listeners.clear();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#createSession()
+ */
+ @Override
+ public Session createSession() {
+ Set<IRI> exclude = getRegisteredSessionIDs();
+ Session session = null;
+ while (session == null)
+ try {
+ session = createSession(idgen.createSessionID(exclude));
+ } catch (DuplicateSessionIDException e) {
+ exclude.add(e.getDulicateID());
+ continue;
+ }
+ return session;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#createSession(org
+ * .semanticweb.owlapi.model.IRI)
+ */
+ @Override
+ public Session createSession(IRI sessionID)
+ throws DuplicateSessionIDException {
+ if (sessionsByID.containsKey(sessionID))
+ throw new DuplicateSessionIDException(sessionID);
+ Session session = new SessionImpl(sessionID);
+ addSession(session);
+ fireSessionCreated(session);
+ return session;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#destroySession(
+ * org.semanticweb.owlapi.model.IRI)
+ */
+ @Override
+ public void destroySession(IRI sessionID) {
+ try {
+ Session ses = sessionsByID.get(sessionID);
+ ses.close();
+ if (ses instanceof SessionImpl)
+ ((SessionImpl) ses).state = State.ZOMBIE;
+ // Make session no longer referenceable
+ removeSession(ses);
+ fireSessionDestroyed(ses);
+ } catch (NonReferenceableSessionException e) {
+ log.warn(
+ "KReS :: tried to kick a dead horse on session "
+ + sessionID
+ + " which was already in a zombie state.", e);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#getSession(org.
+ * semanticweb.owlapi.model.IRI)
+ */
+ @Override
+ public Session getSession(IRI sessionID) {
+ return sessionsByID.get(sessionID);
+ }
+
+ @Override
+ public Set<IRI> getRegisteredSessionIDs() {
+ return sessionsByID.keySet();
+ }
+
+ protected void fireSessionCreated(Session session) {
+ SessionEvent e;
+ try {
+ e = new SessionEvent(session, OperationType.CREATE);
+ for (SessionListener l : listeners)
+ l.sessionChanged(e);
+ } catch (Exception e1) {
+ LoggerFactory
+ .getLogger(getClass())
+ .error(
+ "KReS :: Exception occurred while attempting to fire session creation event for session "
+ + session.getID(), e1);
+ return;
+ }
+
+ }
+
+ protected void fireSessionDestroyed(Session session) {
+ SessionEvent e;
+ try {
+ e = new SessionEvent(session, OperationType.KILL);
+ for (SessionListener l : listeners)
+ l.sessionChanged(e);
+ } catch (Exception e1) {
+ LoggerFactory
+ .getLogger(getClass())
+ .error(
+ "KReS :: Exception occurred while attempting to fire session destruction event for session "
+ + session.getID(), e1);
+ return;
+ }
+
+ }
+
+ protected void addSession(Session session) {
+ sessionsByID.put(session.getID(), session);
+ }
+
+ protected void removeSession(Session session) {
+ IRI id = session.getID();
+ Session s2 = sessionsByID.get(id);
+ if (session == s2)
+ sessionsByID.remove(id);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#getSessionListeners
+ * ()
+ */
+ @Override
+ public Collection<SessionListener> getSessionListeners() {
+ return listeners;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * TODO : optimize with indexing.
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#getSessionSpaces
+ * (org.semanticweb.owlapi.model.IRI)
+ */
+ @Override
+ public Set<SessionOntologySpace> getSessionSpaces(IRI sessionID)
+ throws NonReferenceableSessionException {
+ Set<SessionOntologySpace> result = new HashSet<SessionOntologySpace>();
+ // Brute force search
+ for (OntologyScope scope : scopeRegistry
+ .getRegisteredScopes()) {
+ SessionOntologySpace space = scope.getSessionSpace(sessionID);
+ if (space != null)
+ result.add(space);
+ }
+ return result;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#removeSessionListener
+ * (eu.iksproject.kres.api.manager.session.SessionListener)
+ */
+ @Override
+ public void removeSessionListener(SessionListener listener) {
+ listeners.remove(listener);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * TODO : storage not implemented yet
+ *
+ * @see
+ * eu.iksproject.kres.api.manager.session.SessionManager#storeSession(org
+ * .semanticweb.owlapi.model.IRI, java.io.OutputStream)
+ */
+ @Override
+ public void storeSession(IRI sessionID, OutputStream out)
+ throws NonReferenceableSessionException {
+ for (SessionOntologySpace so : getSessionSpaces(sessionID))
+ for (OWLOntology o : so.getOntologies())
+ store.store(o);
+ }
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/TimestampedSessionIDGenerator.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/TimestampedSessionIDGenerator.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/TimestampedSessionIDGenerator.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/session/TimestampedSessionIDGenerator.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,44 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.session;
+
+import java.util.Date;
+import java.util.Set;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.session.SessionIDGenerator;
+import org.apache.stanbol.ontologymanager.ontonet.impl.util.StringUtils;
+import org.semanticweb.owlapi.model.IRI;
+
+
+public class TimestampedSessionIDGenerator implements SessionIDGenerator {
+
+ private IRI baseIRI;
+
+ public TimestampedSessionIDGenerator(IRI baseIRI) {
+ this.baseIRI = baseIRI;
+ }
+
+ @Override
+ public IRI createSessionID() {
+ return IRI.create(StringUtils.stripIRITerminator(baseIRI) + "/session/"
+ + new Date().getTime());
+ }
+
+ @Override
+ public IRI createSessionID(Set<IRI> exclude) {
+ IRI id = null;
+ do {
+ id = createSessionID();
+ } while (exclude.contains(id));
+ return id;
+ }
+
+ @Override
+ public IRI getBaseIRI() {
+ return baseIRI;
+ }
+
+ @Override
+ public void setBaseIRI(IRI baseIRI) {
+ this.baseIRI = baseIRI;
+ }
+
+}
Propchange: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Apr 1 12:08:25 2011
@@ -0,0 +1 @@
+target
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyConstants.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyConstants.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyConstants.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyConstants.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,10 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.util;
+
+public class OntologyConstants {
+
+ public static final String NS__KRES = "http://kres.iks-project.eu/";
+
+ public static final String NS__ODP = "http://www.ontologydesignpatterns.org/";
+
+ public static final String NS_ONM = NS__KRES + "ontology/meta/onm.owl#";
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyLoaderPrinter.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyLoaderPrinter.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyLoaderPrinter.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyLoaderPrinter.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,62 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.util;
+
+import java.io.PrintStream;
+
+import org.semanticweb.owlapi.io.OWLOntologyCreationIOException;
+import org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyLoaderListener;
+
+/**
+ * Prints ontology loading events to standard output.
+ *
+ * @author alessandro
+ *
+ */
+public class OntologyLoaderPrinter implements OWLOntologyLoaderListener {
+
+ private PrintStream printer = System.out;
+
+ public OntologyLoaderPrinter() {
+ this(System.out);
+ }
+
+ public OntologyLoaderPrinter(PrintStream printer) {
+ if (printer != null)
+ this.printer = printer;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.semanticweb.owlapi.model.OWLOntologyLoaderListener#finishedLoadingOntology(org.semanticweb.owlapi.model.OWLOntologyLoaderListener.LoadingFinishedEvent)
+ */
+ @Override
+ public void finishedLoadingOntology(LoadingFinishedEvent arg0) {
+ printer.print("KReS :: Loading of registry ontology "
+ + arg0.getDocumentIRI() + " ");
+ if (arg0.isSuccessful())
+ printer.println("OK");
+ else {
+ OWLOntologyCreationException ex = arg0.getException();
+ if (ex != null) {
+ if (ex instanceof OWLOntologyAlreadyExistsException)
+ printer.println("EXISTS");
+ else if (ex instanceof OWLOntologyCreationIOException)
+ printer.println("NOT FOUND");
+ else
+ printer.println("FAILED");
+ }
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.semanticweb.owlapi.model.OWLOntologyLoaderListener#startedLoadingOntology(org.semanticweb.owlapi.model.OWLOntologyLoaderListener.LoadingStartedEvent)
+ */
+ @Override
+ public void startedLoadingOntology(LoadingStartedEvent arg0) {
+ // System.out.print("KReS :: Loading registry ontology "+arg0.getDocumentIRI()+" ... ");
+
+ }
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyUtils.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyUtils.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyUtils.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/OntologyUtils.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,313 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.util;
+
+import java.io.PrintStream;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.stanbol.ontologymanager.ontonet.api.io.OntologyInputSource;
+import org.apache.stanbol.ontologymanager.ontonet.api.io.RootOntologySource;
+import org.semanticweb.owlapi.apibinding.OWLManager;
+import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
+import org.semanticweb.owlapi.io.StringDocumentTarget;
+import org.semanticweb.owlapi.model.AddAxiom;
+import org.semanticweb.owlapi.model.AddImport;
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLAxiom;
+import org.semanticweb.owlapi.model.OWLDataFactory;
+import org.semanticweb.owlapi.model.OWLImportsDeclaration;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyChange;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+import org.semanticweb.owlapi.model.OWLOntologyManager;
+import org.semanticweb.owlapi.model.OWLOntologyStorageException;
+import org.semanticweb.owlapi.model.UnknownOWLOntologyException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A set of static utility methods for managing ontologies in KReS.
+ *
+ * @author alessandro
+ *
+ */
+public class OntologyUtils {
+
+ private static final Logger logger = LoggerFactory.getLogger(OntologyUtils.class);
+
+ // /**
+ // * Creates an ontology with the specified IRI and only the import
+ // statements
+ // * for linking to all the ontologies in the subtrees set. Useful for
+ // running
+ // * reasoners on an ontology set, as reasoners are instantiated on a single
+ // * ontology.
+ // *
+ // * @param rootIri
+ // * @param subtrees
+ // * @return
+ // */
+ // public static OWLOntology buildImportTree(IRI rootIri,
+ // Set<OWLOntology> subtrees, OWLOntologyManager mgr) {
+ // OWLOntology root = null;
+ // try {
+ // root = rootIri != null ? mgr.createOntology(rootIri) : mgr
+ // .createOntology();
+ // } catch (OWLOntologyAlreadyExistsException e) {
+ // root = mgr.getOntology(rootIri);
+ // } catch (OWLOntologyCreationException e) {
+ // e.printStackTrace();
+ // return root;
+ // }
+ // return buildImportTree(root, subtrees, mgr);
+ // }
+
+ public static OWLOntology appendOntology(OntologyInputSource parentSrc,
+ OntologyInputSource childSrc, OWLOntologyManager ontologyManager) {
+ return appendOntology(parentSrc, childSrc, ontologyManager, null);
+ }
+
+ public static OWLOntology appendOntology(OntologyInputSource parentSrc,
+ OntologyInputSource childSrc) {
+ return appendOntology(parentSrc, childSrc, null, null);
+ }
+
+ public static OWLOntology appendOntology(OntologyInputSource parentSrc,
+ OntologyInputSource childSrc, IRI rewritePrefix) {
+ return appendOntology(parentSrc, childSrc, null, rewritePrefix);
+ }
+
+ /**
+ * This method appends one ontology (the child) to another (the parent) by
+ * proceeding as follows. If a physical URI can be obtained from the child
+ * source, an import statement using that physical URI will be added to the
+ * parent ontology, otherwise all the axioms from the child ontology will be
+ * copied to the parent. <br>
+ * Note: the ontology manager will not load additional ontologies.
+ *
+ * @param parentSrc
+ * must exist!
+ * @param childSrc
+ * @param ontologyManager
+ * can be null (e.g. when one does not want changes to be
+ * immediately reflected in their ontology manager), in which
+ * case a temporary ontology manager will be used.
+ * @param rewritePrefix
+ * . if not null, import statements will be generated in the form
+ * <tt>rewritePrefix/child_ontology_logical_IRI</tt>. It can be
+ * used for relocating the ontology document file elsewhere.
+ * @return the parent with the appended child
+ */
+ public static OWLOntology appendOntology(OntologyInputSource parentSrc,
+ OntologyInputSource childSrc, OWLOntologyManager ontologyManager,
+ IRI rewritePrefix) {
+
+ if (ontologyManager == null)
+ ontologyManager = OWLManager.createOWLOntologyManager();
+ OWLDataFactory factory = ontologyManager.getOWLDataFactory();
+ OWLOntology oParent = parentSrc.getRootOntology();
+ OWLOntology oChild = childSrc.getRootOntology();
+
+ // Named ontology with a provided absolute prefix. Use name and prefix
+ // for creating an new import statement.
+ if (!oChild.isAnonymous() && rewritePrefix != null
+ /* && rewritePrefix.isAbsolute() */) {
+ IRI impIri = IRI.create(rewritePrefix + "/"
+ + oChild.getOntologyID().getOntologyIRI());
+ OWLImportsDeclaration imp = factory
+ .getOWLImportsDeclaration(impIri);
+ ontologyManager.applyChange(new AddImport(oParent, imp));
+ }
+ // Anonymous, with physicalIRI. A plain import statement is added.
+ else if (childSrc.hasPhysicalIRI()) {
+ OWLImportsDeclaration imp = factory
+ .getOWLImportsDeclaration(childSrc.getPhysicalIRI());
+ ontologyManager.applyChange(new AddImport(oParent, imp));
+ }
+
+ // Anonymous and no physical IRI (e.g. in memory). Copy all axioms and
+ // import statements.
+ else {
+ ontologyManager.addAxioms(oParent, oChild.getAxioms());
+ for (OWLImportsDeclaration imp : oChild.getImportsDeclarations())
+ ontologyManager.applyChange(new AddImport(oParent, factory
+ .getOWLImportsDeclaration(imp.getIRI())));
+ }
+ return oParent;
+ }
+
+ public static OWLOntology buildImportTree(OntologyInputSource rootSrc,
+ Set<OWLOntology> subtrees) {
+
+ return buildImportTree(rootSrc.getRootOntology(), subtrees, OWLManager
+ .createOWLOntologyManager());
+
+ }
+
+ /**
+ *
+ * @param rootSrc
+ * @param subtrees
+ * @param mgr
+ * @return
+ */
+ public static OWLOntology buildImportTree(OntologyInputSource rootSrc,
+ Set<OWLOntology> subtrees, OWLOntologyManager mgr) {
+
+ return buildImportTree(rootSrc.getRootOntology(), subtrees, mgr);
+
+ }
+
+ /**
+ * Non-recursively adds import statements to the root ontology so that it is
+ * directly linked to all the ontologies in the subtrees set.
+ *
+ * @param root
+ * the ontology to which import subtrees should be appended. If
+ * null, a runtime exception will be thrown.
+ * @param subtrees
+ * the set of target ontologies for import statements. These can
+ * in turn be importing other ontologies, hence the
+ * "subtree" notation. A single statement will be added
+ * for each member of this set.
+ * @return the same input ontology as defined in <code>root</code>, but with
+ * the added import statements.
+ */
+ public static OWLOntology buildImportTree(OWLOntology root,
+ Set<OWLOntology> subtrees) {
+
+ return buildImportTree(root, subtrees, OWLManager
+ .createOWLOntologyManager());
+
+ }
+
+ /**
+ * Non-recursively adds import statements to the root ontology so that it is
+ * directly linked to all the ontologies in the subtrees set.
+ *
+ * @param parent
+ * the ontology to which import subtrees should be appended. If
+ * null, a runtime exception will be thrown.
+ * @param subtrees
+ * the set of target ontologies for import statements. These can
+ * in turn be importing other ontologies, hence the
+ * "subtree" notation. A single statement will be added
+ * for each member of this set.
+ * @param mgr
+ * the OWL ontology manager to use for constructing the import
+ * tree. If null, an internal one will be used instead, otherwise
+ * an existing ontology manager can be used e.g. for extracting
+ * import statements from its IRI mappers or known ontologies.
+ * Note that the supplied manager will <i>never</i> try to load
+ * any ontologies, even when they are unknown.
+ * @return the same input ontology as defined in <code>root</code>, but with
+ * the added import statements.
+ */
+ public static OWLOntology buildImportTree(OWLOntology parent,
+ Set<OWLOntology> subtrees, OWLOntologyManager mgr) {
+
+ if (parent == null)
+ throw new NullPointerException(
+ "Cannot append import trees to a nonexistent ontology.");
+
+ // If no manager was supplied, use a temporary one.
+ if (mgr == null)
+ mgr = OWLManager.createOWLOntologyManager();
+ OWLDataFactory owlFactory = mgr.getOWLDataFactory();
+ List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
+
+
+ for (OWLOntology o : subtrees) {
+
+ IRI importIri = null;
+ try {
+ /*
+ * First query the manager, as it could know the physical
+ * location of anonymous ontologies, if previously loaded or
+ * IRI-mapped.
+ */
+ importIri = mgr.getOntologyDocumentIRI(o);
+ } catch (UnknownOWLOntologyException ex) {
+ /*
+ * Otherwise, ask the ontology itself (the location of an
+ * anonymous ontology may have been known at creation/loading
+ * time, even if another manager built it.)
+ */
+ importIri = o.getOntologyID().getDefaultDocumentIRI();
+ } catch (Exception ex) {
+ logger.error(
+ "KReS :: Exception caught during tree building. Skipping import of ontology "
+ + o.getOntologyID(), ex);
+ } finally {
+ /*
+ * It is still possible that an imported ontology is anonymous
+ * but has no physical document IRI (for example, because it was
+ * only generated in-memory but not stored). In this case it is
+ * necessary (and generally safe) to copy all its axioms and
+ * import statements to the parent ontology, or else it is lost.
+ */
+ if (o.isAnonymous() && importIri == null) {
+ logger
+ .warn("KReS :: [NONFATAL] Anonymous import target "
+ + o.getOntologyID()
+ + " not mapped to physical IRI. Will add extracted axioms to parent ontology.");
+ for (OWLImportsDeclaration im : o.getImportsDeclarations())
+ changes.add(new AddImport(parent, im));
+ for (OWLAxiom im : o.getAxioms())
+ changes.add(new AddAxiom(parent, im));
+ } else if (importIri != null) {
+ // An anonymous ontology can still be imported if it has a
+ // valid document IRI.
+ changes.add(new AddImport(parent, owlFactory
+ .getOWLImportsDeclaration(importIri)));
+ }
+ }
+
+ } // End subtrees cycle.
+
+ // All possible error causes should have been dealt with by now, but we
+ // apply the changes one by one, just in case.
+ for (OWLOntologyChange im : changes)
+ try {
+ mgr.applyChange(im);
+ } catch (Exception ex) {
+ logger
+ .error(
+ "KReS :: Exception caught during tree building. Skipping import",
+ ex);
+ continue;
+ }
+ // mgr.applyChanges(changes);
+
+ return parent;
+ }
+
+ public static OWLOntology buildImportTree(Set<OWLOntology> subtrees)
+ throws OWLOntologyCreationException {
+
+ return buildImportTree(subtrees, OWLManager.createOWLOntologyManager());
+
+ }
+
+ public static OWLOntology buildImportTree(Set<OWLOntology> subtrees,
+ OWLOntologyManager mgr) throws OWLOntologyCreationException {
+
+ return buildImportTree(new RootOntologySource(mgr.createOntology()),
+ subtrees, mgr);
+
+ }
+
+ public static void printOntology(OWLOntology o, PrintStream printer) {
+
+ OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
+ StringDocumentTarget tgt = new StringDocumentTarget();
+ try {
+ mgr.saveOntology(o, new RDFXMLOntologyFormat(), tgt);
+ } catch (OWLOntologyStorageException e) {
+ e.printStackTrace(printer);
+ }
+ printer.println(tgt.toString());
+
+ }
+
+}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/StringUtils.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/StringUtils.java?rev=1087671&view=auto
==============================================================================
--- incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/StringUtils.java (added)
+++ incubator/stanbol/trunk/ontologymanager/ontonet/src/main/java/org/apache/stanbol/ontologymanager/ontonet/impl/util/StringUtils.java Fri Apr 1 12:08:25 2011
@@ -0,0 +1,23 @@
+package org.apache.stanbol.ontologymanager.ontonet.impl.util;
+
+import org.semanticweb.owlapi.model.IRI;
+
+public class StringUtils {
+
+ public static IRI stripIRITerminator(IRI iri) {
+ if (iri == null)
+ return null;
+ return IRI.create(stripIRITerminator(iri.toString()));
+ }
+
+ public static String stripIRITerminator(String iri) {
+ if (iri == null)
+ return null;
+ if (iri.endsWith("/") || iri.endsWith("#") || iri.endsWith(":"))
+ // Shorten the string by one
+ return stripIRITerminator(iri.substring(0, iri.length() - 1));
+ else
+ return iri;
+ }
+
+}