Search in sources :

Example 1 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class OntologyUtilities method getOntology.

/**
 * Checks the availability of an existing instance of the Ontology
 * with the given URL in the GATE's CreoleRegister. If found, returns
 * the first available one (doesn't guranttee in which order). If not
 * found, attempts to create one using OWLIM implementation with
 * RDF/XML as ontology type and if successful returns the newly
 * created instance of the ontology.
 * @throws ResourceInstantiationException
 * @deprecated - this method should be avoided
 */
@Deprecated
public static Ontology getOntology(URL url) throws ResourceInstantiationException {
    java.util.List<Resource> loadedOntologies = null;
    Ontology ontology = null;
    try {
        loadedOntologies = Gate.getCreoleRegister().getAllInstances(Ontology.class.getName());
    } catch (GateException ge) {
        throw new ResourceInstantiationException("Cannot list loaded ontologies", ge);
    }
    Iterator<Resource> ontIter = loadedOntologies.iterator();
    while (ontology == null && ontIter.hasNext()) {
        Ontology anOntology = (Ontology) ontIter.next();
        if (anOntology.getURL().equals(url)) {
            ontology = anOntology;
            break;
        }
    }
    try {
        // if not found, load it
        if (ontology == null) {
            // hardcoded to use OWL as the ontology type
            FeatureMap params = Factory.newFeatureMap();
            params.put("persistLocation", File.createTempFile("abc", "abc").getParentFile().toURI().toURL());
            params.put("rdfXmlURL", url);
            ontology = (Ontology) Factory.createResource("gate.creole.ontology.owlim.OWLIMOntologyLR", params);
        }
    } catch (Exception e) {
        throw new ResourceInstantiationException("Cannot create a new instance of ontology", e);
    }
    return ontology;
}
Also used : FeatureMap(gate.FeatureMap) GateException(gate.util.GateException) Resource(gate.Resource) ResourceInstantiationException(gate.creole.ResourceInstantiationException) GateException(gate.util.GateException) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Example 2 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class FeatureMapEditorDialog method initGuiComponents.

protected void initGuiComponents() {
    this.setMinimumSize(new Dimension(150, 300));
    getContentPane().setLayout(new BorderLayout());
    // create the FeaturesSchemaEditor for the main body of the dialog
    fmView = new FeaturesSchemaEditor();
    try {
        fmView.init();
    } catch (ResourceInstantiationException rie) {
        // can't happen, but needs to be caught to satisfy the compiler
        throw new GateRuntimeException("FeaturesSchemaEditor.init() threw " + "ResourceInstantiationException!", rie);
    }
    fmView.setTarget(tempFMHolder);
    // make sure the window is a sensible size
    Dimension preferredSize = fmView.getPreferredSize();
    if (preferredSize.height < 150) {
        preferredSize.height = 150;
    } else if (preferredSize.height > 300) {
        preferredSize.height = 300;
    }
    fmView.setPreferredSize(preferredSize);
    JPanel fmViewPanel = new JPanel(new BorderLayout());
    fmViewPanel.add(fmView, BorderLayout.CENTER);
    fmViewPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    getContentPane().add(fmViewPanel, BorderLayout.CENTER);
    // the bottom buttons
    Box buttonsBox = Box.createHorizontalBox();
    buttonsBox.add(Box.createHorizontalGlue());
    okButton = new JButton("OK");
    buttonsBox.add(okButton);
    buttonsBox.add(Box.createHorizontalStrut(5));
    cancelButton = new JButton("Cancel");
    buttonsBox.add(cancelButton);
    buttonsBox.add(Box.createHorizontalGlue());
    getContentPane().add(buttonsBox, BorderLayout.SOUTH);
}
Also used : BorderLayout(java.awt.BorderLayout) Dimension(java.awt.Dimension) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Example 3 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class AnnotationSetsView method initGUI.

@Override
protected void initGUI() {
    // get a pointer to the textual view used for highlights
    Iterator<DocumentView> centralViewsIter = owner.getCentralViews().iterator();
    while (textView == null && centralViewsIter.hasNext()) {
        DocumentView aView = centralViewsIter.next();
        if (aView instanceof TextualDocumentView)
            textView = (TextualDocumentView) aView;
    }
    textPane = (JTextArea) ((JScrollPane) textView.getGUI()).getViewport().getView();
    // get a pointer to the list view
    Iterator<DocumentView> horizontalViewsIter = owner.getHorizontalViews().iterator();
    while (listView == null && horizontalViewsIter.hasNext()) {
        DocumentView aView = horizontalViewsIter.next();
        if (aView instanceof AnnotationListView)
            listView = (AnnotationListView) aView;
    }
    // get a pointer to the stack view
    horizontalViewsIter = owner.getHorizontalViews().iterator();
    while (stackView == null && horizontalViewsIter.hasNext()) {
        DocumentView aView = horizontalViewsIter.next();
        if (aView instanceof AnnotationStackView)
            stackView = (AnnotationStackView) aView;
    }
    mainTable = new XJTable();
    tableModel = new SetsTableModel();
    mainTable.setSortable(false);
    mainTable.setModel(tableModel);
    mainTable.setRowMargin(0);
    mainTable.getColumnModel().setColumnMargin(0);
    SetsTableCellRenderer cellRenderer = new SetsTableCellRenderer();
    mainTable.getColumnModel().getColumn(NAME_COL).setCellRenderer(cellRenderer);
    mainTable.getColumnModel().getColumn(SELECTED_COL).setCellRenderer(cellRenderer);
    SetsTableCellEditor cellEditor = new SetsTableCellEditor();
    mainTable.getColumnModel().getColumn(SELECTED_COL).setCellEditor(cellEditor);
    mainTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    mainTable.setColumnSelectionAllowed(false);
    mainTable.setRowSelectionAllowed(true);
    mainTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    // block autocreation of new columns from now on
    mainTable.setAutoCreateColumnsFromModel(false);
    mainTable.setTableHeader(null);
    mainTable.setShowGrid(false);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    // the background colour seems to change somewhere when using the GTK+
    // look and feel on Linux, so we copy the value now and set it
    Color tableBG = mainTable.getBackground();
    // make a copy of the value (as the reference gets changed somewhere)
    tableBG = new Color(tableBG.getRGB());
    mainTable.setBackground(tableBG);
    scroller = new JScrollPane(mainTable);
    scroller.getViewport().setOpaque(true);
    scroller.getViewport().setBackground(tableBG);
    try {
        annotationEditor = createAnnotationEditor(textView, this);
    } catch (ResourceInstantiationException e) {
        // this should not really happen
        throw new GateRuntimeException("Could not initialise the annotation editor!", e);
    }
    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridy = 0;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridwidth = 2;
    constraints.weighty = 1;
    constraints.weightx = 1;
    constraints.fill = GridBagConstraints.BOTH;
    mainPanel.add(scroller, constraints);
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.weighty = 0;
    newSetNameTextField = new JTextField();
    mainPanel.add(newSetNameTextField, constraints);
    constraints.weightx = 0;
    newSetAction = new NewAnnotationSetAction();
    mainPanel.add(new JButton(newSetAction), constraints);
    populateUI();
    tableModel.fireTableDataChanged();
    eventMinder.start();
    initListeners();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) XJTable(gate.swing.XJTable) GridBagLayout(java.awt.GridBagLayout) Color(java.awt.Color) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) ResourceInstantiationException(gate.creole.ResourceInstantiationException) GateRuntimeException(gate.util.GateRuntimeException)

Example 4 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class ControllerMetadataViewer method init.

@SuppressWarnings("rawtypes")
@Override
public Resource init() throws ResourceInstantiationException {
    setLayout(new BorderLayout());
    add(new JScrollPane(display), BorderLayout.CENTER);
    try {
        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new ResourceInstantiationException("Unable to construct an XML parser", e);
    }
    if (Configuration.isTrue("xr.use.listeners", true)) {
        List l = display.getMouseTrackingListeners();
        for (Iterator i = l.iterator(); i.hasNext(); ) {
            FSMouseListener listener = (FSMouseListener) i.next();
            if (listener instanceof LinkListener) {
                display.removeMouseTrackingListener(listener);
            }
        }
        display.addMouseTrackingListener(new LinkListener() {

            @Override
            public void linkClicked(BasicPanel panel, String uri) {
                // open any links in an actual web browser
                MainFrame.getInstance().showHelpFrame(uri, null);
            }
        });
    }
    return this;
}
Also used : JScrollPane(javax.swing.JScrollPane) BasicPanel(org.xhtmlrenderer.swing.BasicPanel) BorderLayout(java.awt.BorderLayout) LinkListener(org.xhtmlrenderer.swing.LinkListener) Iterator(java.util.Iterator) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FSMouseListener(org.xhtmlrenderer.swing.FSMouseListener) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Example 5 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class CorpusBenchmarkTool method evaluateCorpus.

// generateCorpus
protected void evaluateCorpus(File fileDir, File processedDir, File markedDir, File errorDir) {
    // 1. check if we have input files and the processed Dir
    if (fileDir == null || !fileDir.exists())
        return;
    if (processedDir == null || !processedDir.exists())
        // if the user wants evaluation of marked and stored that's not possible
        if (isMarkedStored) {
            Out.prln("Cannot evaluate because no processed documents exist.");
            return;
        } else
            isMarkedClean = true;
    // create the error directory or clean it up if needed
    File errDir = null;
    if (isMoreInfoMode) {
        errDir = errorDir;
        if (errDir == null) {
            errDir = new File(currDir, ERROR_DIR_NAME);
        } else {
            // get rid of the directory, coz we wants it clean
            if (!Files.rmdir(errDir))
                Out.prln("cannot delete old error directory: " + errDir);
        }
        Out.prln("Create error directory: " + errDir + "<BR><BR>");
        errDir.mkdir();
    }
    // looked for marked texts only if the directory exists
    boolean processMarked = markedDir != null && markedDir.exists();
    if (!processMarked && (isMarkedStored || isMarkedClean)) {
        Out.prln("Cannot evaluate because no human-annotated documents exist.");
        return;
    }
    if (isMarkedStored) {
        evaluateMarkedStored(markedDir, processedDir, errDir);
        return;
    } else if (isMarkedClean) {
        evaluateMarkedClean(markedDir, fileDir, errDir);
        return;
    }
    Document persDoc = null;
    Document cleanDoc = null;
    Document markedDoc = null;
    // open the datastore and process each document
    try {
        // open the data store
        DataStore sds = Factory.openDataStore("gate.persist.SerialDataStore", processedDir.toURI().toURL().toExternalForm());
        List<String> lrIDs = sds.getLrIds("gate.corpora.DocumentImpl");
        for (int i = 0; i < lrIDs.size(); i++) {
            String docID = lrIDs.get(i);
            // read the stored document
            FeatureMap features = Factory.newFeatureMap();
            features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
            features.put(DataStore.LR_ID_FEATURE_NAME, docID);
            FeatureMap hparams = Factory.newFeatureMap();
            // Gate.setHiddenAttribute(hparams, true);
            persDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", features, hparams);
            if (isMoreInfoMode) {
                StringBuffer errName = new StringBuffer(persDoc.getName());
                errName.replace(persDoc.getName().lastIndexOf("."), persDoc.getName().length(), ".err");
                Out.prln("<H2>" + "<a href=\"err/" + errName.toString() + "\">" + persDoc.getName() + "</a>" + "</H2>");
            } else
                Out.prln("<H2>" + persDoc.getName() + "</H2>");
            File cleanDocFile = new File(fileDir, persDoc.getName());
            // try reading the original document from clean
            if (!cleanDocFile.exists()) {
                Out.prln("Warning: Cannot find original document " + persDoc.getName() + " in " + fileDir);
            } else {
                FeatureMap params = Factory.newFeatureMap();
                params.put(Document.DOCUMENT_URL_PARAMETER_NAME, cleanDocFile.toURI().toURL());
                params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, documentEncoding);
                // create the document
                cleanDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params, hparams);
                cleanDoc.setName(persDoc.getName());
            }
            // try finding the marked document
            StringBuffer docName = new StringBuffer(persDoc.getName());
            if (!isMarkedDS) {
                docName.replace(persDoc.getName().lastIndexOf("."), docName.length(), ".xml");
                File markedDocFile = new File(markedDir, docName.toString());
                if (!processMarked || !markedDocFile.exists()) {
                    Out.prln("Warning: Cannot find human-annotated document " + markedDocFile + " in " + markedDir);
                } else {
                    FeatureMap params = Factory.newFeatureMap();
                    params.put(Document.DOCUMENT_URL_PARAMETER_NAME, markedDocFile.toURI().toURL());
                    params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, documentEncoding);
                    // create the document
                    markedDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params, hparams);
                    markedDoc.setName(persDoc.getName());
                }
            } else {
                // open marked from a DS
                // open the data store
                DataStore sds1 = Factory.openDataStore("gate.persist.SerialDataStore", markedDir.toURI().toURL().toExternalForm());
                List<String> lrIDs1 = sds1.getLrIds("gate.corpora.DocumentImpl");
                boolean found = false;
                int k = 0;
                // search for the marked doc with the same name
                while (k < lrIDs1.size() && !found) {
                    String docID1 = lrIDs1.get(k);
                    // read the stored document
                    FeatureMap features1 = Factory.newFeatureMap();
                    features1.put(DataStore.DATASTORE_FEATURE_NAME, sds1);
                    features1.put(DataStore.LR_ID_FEATURE_NAME, docID1);
                    Document tempDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", features1, hparams);
                    // check whether this is our doc
                    if (((String) tempDoc.getFeatures().get("gate.SourceURL")).endsWith(persDoc.getName())) {
                        found = true;
                        markedDoc = tempDoc;
                    } else
                        k++;
                }
            }
            evaluateDocuments(persDoc, cleanDoc, markedDoc, errDir);
            if (persDoc != null) {
                final gate.Document pd = persDoc;
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Factory.deleteResource(pd);
                    }
                });
            }
            if (cleanDoc != null) {
                final gate.Document cd = cleanDoc;
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Factory.deleteResource(cd);
                    }
                });
            }
            if (markedDoc != null) {
                final gate.Document md = markedDoc;
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Factory.deleteResource(md);
                    }
                });
            }
        }
        // for loop through saved docs
        sds.close();
    } catch (java.net.MalformedURLException ex) {
        throw (GateRuntimeException) new GateRuntimeException("CorpusBenchmark: " + ex.getMessage()).initCause(ex);
    } catch (PersistenceException ex1) {
        throw (GateRuntimeException) new GateRuntimeException("CorpusBenchmark: " + ex1.getMessage()).initCause(ex1);
    } catch (ResourceInstantiationException ex2) {
        throw (GateRuntimeException) new GateRuntimeException("CorpusBenchmark: " + ex2.getMessage()).initCause(ex2);
    }
}
Also used : Document(gate.Document) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) SerialDataStore(gate.persist.SerialDataStore) DataStore(gate.DataStore) PersistenceException(gate.persist.PersistenceException) Document(gate.Document) File(java.io.File)

Aggregations

ResourceInstantiationException (gate.creole.ResourceInstantiationException)29 FeatureMap (gate.FeatureMap)10 PersistenceException (gate.persist.PersistenceException)10 ResourceData (gate.creole.ResourceData)9 GateRuntimeException (gate.util.GateRuntimeException)6 Document (gate.Document)5 File (java.io.File)5 ParameterException (gate.creole.ParameterException)4 SerialDataStore (gate.persist.SerialDataStore)4 IOException (java.io.IOException)4 List (java.util.List)4 ProcessingResource (gate.ProcessingResource)3 Resource (gate.Resource)3 ExecutionException (gate.creole.ExecutionException)3 Parameter (gate.creole.Parameter)3 ParameterList (gate.creole.ParameterList)3 Plugin (gate.creole.Plugin)3 Dimension (java.awt.Dimension)3 BeanInfo (java.beans.BeanInfo)3 URL (java.net.URL)3