Permalink
Browse files

Added support for opening ontologies directly from a URL

  • Loading branch information...
stuzart committed Apr 19, 2012
1 parent 197d5ea commit b23a996f5040d88001797bcd7845a90698d1b02d
View
@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.sysmo-db</groupId>
<artifactId>rightfield</artifactId>
- <version>0.16</version>
+ <version>0.17</version>
<packaging>jar</packaging>
<name>RightField</name>
<description>
@@ -11,6 +11,13 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import org.apache.log4j.Logger;
import org.semanticweb.owlapi.apibinding.OWLManager;
@@ -84,7 +91,7 @@
public WorkbookManager() {
this.owlManager = OWLManager.createOWLOntologyManager();
- ontologyLoaderConfiguration.setSilentMissingImportsHandling(true);
+ ontologyLoaderConfiguration.setSilentMissingImportsHandling(true);
shortFormProvider = new BidirectionalShortFormProviderAdapter(new SimpleShortFormProvider());
entitySelectionModel = new EntitySelectionModel(owlManager.getOWLDataFactory().getOWLThing());
ontologyTermValidationManager = new OntologyTermValidationManager(this);
@@ -397,6 +404,54 @@ public void loadEmbeddedTermOntologies() {
setLabelRendering(true);
}
+ /**
+ * Uses a Future to allow the opening of the ontology source to timeout (since the owlAPI doesn't always seem to timeout for invalid URI's such
+ * @param source - the OWLOntologyDocumentSource containing the IRI of the ontology to be opened
+ * @param timeout - time in SECONDS
+ * @return the OWLOntology
+ * @throws OWLOntologyCreationException
+ */
+ private OWLOntology processOntologyDocumentSourceWithTimeout(final OWLOntologyDocumentSource source,int timeout) throws OWLOntologyCreationException {
+
+ Callable<OWLOntology> callable = new Callable<OWLOntology>() {
+ @Override
+ public OWLOntology call() throws Exception {
+ return owlManager.loadOntologyFromOntologyDocument(source,
+ ontologyLoaderConfiguration);
+ }
+ };
+
+ OWLOntology ontology;
+
+ ExecutorService executor = Executors.newCachedThreadPool();
+ Future<OWLOntology> future = executor.submit(callable);
+ try {
+ ontology = future.get(timeout, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ logger.error("There was an error trying to open the ontology "
+ + source.getDocumentIRI().toString(), e);
+ throw new OWLOntologyCreationException(
+ "There was an error trying to open the ontology from "
+ + source.getDocumentIRI().toString() + " - "
+ + e.getMessage());
+ } catch (ExecutionException e) {
+ if (e.getCause() instanceof OWLOntologyCreationException) {
+ throw (OWLOntologyCreationException) e.getCause();
+ } else {
+ throw new OWLOntologyCreationException(
+ "There was an error trying to open the ontology from "
+ + source.getDocumentIRI().toString() + " - "
+ + e.getMessage());
+ }
+ } catch (TimeoutException e) {
+ throw new OWLOntologyCreationException(
+ "There was a timeout whilst fetching the ontology from "
+ + source.getDocumentIRI().toString());
+ }
+
+ return ontology;
+ }
+
public OWLOntology loadOntology(IRI physicalIRI) throws OWLOntologyCreationException {
OWLOntologyID newID = null;
@@ -407,8 +462,8 @@ public OWLOntology loadOntology(IRI physicalIRI) throws OWLOntologyCreationExcep
unloadOntology(physicalIRI);
OWLOntologyDocumentSource source = new IRIDocumentSource(BioPortalRepository.handleBioPortalAPIKey(physicalIRI));
-
- OWLOntology ontology = owlManager.loadOntologyFromOntologyDocument(source,ontologyLoaderConfiguration);
+
+ OWLOntology ontology = processOntologyDocumentSourceWithTimeout(source, 30);
logIRI = ontology.getOntologyID().getOntologyIRI();
//Create a new ID and use the physical IRI as a version ID
@@ -98,8 +98,12 @@ public int tabIndexForOntology(OWLOntology ontology) {
return indexOfTab(tabTitle(ontology));
}
- public String tabTitle(OWLOntology ontology) {
- return ontology.getOntologyID().getOntologyIRI().getFragment();
+ public String tabTitle(OWLOntology ontology) {
+ String title = ontology.getOntologyID().getOntologyIRI().getFragment();
+ if (title.trim().isEmpty()) {
+ title=ontology.getOntologyID().getVersionIRI().getFragment();
+ }
+ return title;
}
private synchronized void updateTabs() {
@@ -1,6 +1,8 @@
package uk.ac.manchester.cs.owl.semspreadsheets.ui;
import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
@@ -32,11 +34,11 @@ public static ErrorHandler getErrorHandler() {
}
public void handleError(Throwable throwable) {
- if(throwable instanceof OWLOntologyCreationException) {
- throwable.printStackTrace();
+ logger.debug("Error being handled",throwable);
+ if(throwable instanceof OWLOntologyCreationException) {
if(throwable instanceof OWLOntologyCreationIOException) {
OWLOntologyCreationIOException e = (OWLOntologyCreationIOException) throwable;
- JOptionPane.showMessageDialog(null, e.getCause().getMessage(), "Could not load ontology", JOptionPane.ERROR_MESSAGE);
+ JOptionPane.showMessageDialog(null, "An error occurred trying to open the onology:"+e.getCause().getMessage(), "Could not open ontology", JOptionPane.ERROR_MESSAGE);
}
else if(throwable instanceof UnparsableOntologyException) {
JOptionPane.showMessageDialog(null, "The ontology document appears to be in an unsupported format or contains syntax errors", "Could not load ontology", JOptionPane.ERROR_MESSAGE);
@@ -47,13 +49,13 @@ else if(throwable instanceof UnparsableOntologyException) {
}
else if(throwable instanceof UnknownHostException) {
JOptionPane.showMessageDialog(null, "You are not connected to the internet. Please check your network connection.", "Not connected to network", JOptionPane.ERROR_MESSAGE);
- }
- else if (throwable instanceof IOException) {
- JOptionPane.showMessageDialog(null, throwable.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
- }
+ }
else if (throwable instanceof BioPortalAccessDeniedException) {
JOptionPane.showMessageDialog(null, "Access to the BioPortal API was forbidden. This could be due to an invalid API key.", "Error", JOptionPane.ERROR_MESSAGE);
}
+ else if (throwable instanceof MalformedURLException || throwable instanceof URISyntaxException) {
+ JOptionPane.showMessageDialog(null, "The URL provided was invalid", "Error", JOptionPane.ERROR_MESSAGE);
+ }
else {
JOptionPane.showMessageDialog(null, "An Unexpected "+throwable.getClass().getSimpleName() +" error occurred: " + throwable.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
logger.error("Unexpected error reported",throwable);
@@ -9,7 +9,10 @@
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
+import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -47,6 +50,7 @@
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.OnlineHelpAction;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.OpenFromBioPortalAction;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.OpenOntologyAction;
+import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.OpenOntologyFromURLAction;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.OpenWorkbookAction;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.CloseSelectedOntologyAction;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.RemoveSheetAction;
@@ -58,6 +62,7 @@
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.SheetCellCutAction;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.action.SheetCellPasteAction;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.task.FetchBioportalOntologyListTask;
+import uk.ac.manchester.cs.owl.semspreadsheets.ui.task.LoadOntologyFromURITask;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.task.LoadOntologyTask;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.task.LoadRepositoryItemTask;
import uk.ac.manchester.cs.owl.semspreadsheets.ui.task.TaskManager;
@@ -151,6 +156,7 @@ private void setupMenuItems() {
fileMenu.add(new CloseWorkbookAction(this));
fileMenu.add(new JSeparator());
fileMenu.add(new OpenOntologyAction(this));
+ fileMenu.add(new OpenOntologyFromURLAction(this));
fileMenu.add(new OpenFromBioPortalAction(this));
removeOntologyMenuItem = new CloseSelectedOntologyMenuItem(new CloseSelectedOntologyAction(this),getWorkbookManager());
removeOntologyMenuItem.setSelectedOntology(getSelectedOntology());
@@ -296,6 +302,21 @@ public void loadOntology() throws OWLOntologyCreationException {
}
taskManager.runTask(new LoadOntologyTask(file));
}
+
+ public void loadOntologyFromURL() throws OWLOntologyCreationException {
+ String urlStr = JOptionPane.showInputDialog(this,"Enter the URL for the ontology","URL for ontology",JOptionPane.PLAIN_MESSAGE);
+ if (urlStr!=null) {
+ URI uri;
+ try {
+ uri = new URL(urlStr).toURI();
+ taskManager.runTask(new LoadOntologyFromURITask(uri));
+ } catch (MalformedURLException e) {
+ ErrorHandler.getErrorHandler().handleError(e);
+ } catch (URISyntaxException e) {
+ ErrorHandler.getErrorHandler().handleError(e);
+ }
+ }
+ }
public void loadBioportalOntology() throws Exception {
Collection<RepositoryItem> ontologies = taskManager.runTask(new FetchBioportalOntologyListTask());
@@ -505,4 +526,6 @@ public void removeOntology(OWLOntology ontology) {
+
+
}
@@ -6,10 +6,7 @@
import uk.ac.manchester.cs.owl.semspreadsheets.ui.WorkbookFrame;
/**
- * Author: Matthew Horridge<br>
- * The University of Manchester<br>
- * Information Management Group<br>
- * Date: 08-Nov-2009
+ * @author Matthew Horridge
*/
@SuppressWarnings("serial")
public class OpenOntologyAction extends WorkbookFrameAction {
@@ -0,0 +1,26 @@
+package uk.ac.manchester.cs.owl.semspreadsheets.ui.action;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+
+import uk.ac.manchester.cs.owl.semspreadsheets.ui.WorkbookFrame;
+
+@SuppressWarnings("serial")
+public class OpenOntologyFromURLAction extends WorkbookFrameAction {
+
+ public OpenOntologyFromURLAction(WorkbookFrame workbookFrame) {
+ super("Open ontology from a URL...", workbookFrame);
+ setAcceleratorKey(KeyEvent.VK_L, true, false);
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ try {
+ getWorkbookFrame().loadOntologyFromURL();
+ }
+ catch (Exception e1) {
+ //will already have been handled inside LoadOntologyFromURITask
+ }
+ }
+
+}
@@ -0,0 +1,39 @@
+package uk.ac.manchester.cs.owl.semspreadsheets.ui.task;
+
+import java.net.URI;
+
+import org.semanticweb.owlapi.model.IRI;
+import org.semanticweb.owlapi.model.OWLOntology;
+import org.semanticweb.owlapi.model.OWLOntologyCreationException;
+
+import uk.ac.manchester.cs.owl.semspreadsheets.ui.ErrorHandler;
+
+/**
+ * Task to handle opening an ontology from a URL
+ *
+ * @author Stuart Owen
+ *
+ */
+public class LoadOntologyFromURITask extends AbstractTask<OWLOntology, OWLOntologyCreationException> {
+
+ private final URI uri;
+
+ public LoadOntologyFromURITask(URI uri) {
+ this.uri = uri;
+ }
+
+ public OWLOntology runTask() throws OWLOntologyCreationException {
+ try {
+ return getWorkbookFrame().getWorkbookManager().loadOntology(IRI.create(uri));
+ }
+ catch(OWLOntologyCreationException e) {
+ ErrorHandler.getErrorHandler().handleError(e);
+ throw e;
+ }
+ }
+
+ public String getTitle() {
+ return "Loading ontology";
+ }
+
+}
@@ -21,7 +21,7 @@ public LoadOntologyTask(File file) {
}
public OWLOntology runTask() throws OWLOntologyCreationException {
- try {
+ try {
return getWorkbookFrame().getWorkbookManager().loadOntology(IRI.create(file));
}
catch(OWLOntologyCreationException e) {

0 comments on commit b23a996

Please sign in to comment.