Search in sources :

Example 11 with PersistenceException

use of gate.persist.PersistenceException in project gate-core by GateNLP.

the class LuceneDataStoreSearchGUI method setTarget.

/**
 * Called by the GUI when this viewer/editor has to initialise itself
 * for a specific object.
 *
 * @param target the object (be it a {@link gate.Resource},
 *          {@link gate.DataStore}or whatever) this viewer has to
 *          display
 */
@Override
public void setTarget(Object target) {
    if (!(target instanceof LuceneDataStoreImpl) && !(target instanceof Searcher)) {
        throw new IllegalArgumentException("The GATE LuceneDataStoreSearchGUI can only be used with a GATE LuceneDataStores!\n" + target.getClass().toString() + " is not a GATE LuceneDataStore or an object of Searcher!");
    }
    this.target = target;
    // standalone Java application
    if (target instanceof LuceneDataStoreImpl) {
        ((LuceneDataStoreImpl) target).addDatastoreListener(this);
        corpusToSearchIn.setEnabled(true);
        searcher = ((LuceneDataStoreImpl) target).getSearcher();
        updateSetsTypesAndFeatures();
        try {
            // get the corpus names from the datastore
            java.util.List<String> corpusPIds = ((LuceneDataStoreImpl) target).getLrIds(SerialCorpusImpl.class.getName());
            if (corpusIds != null) {
                for (Object corpusPId : corpusPIds) {
                    String name = ((LuceneDataStoreImpl) target).getLrName(corpusPId);
                    this.corpusIds.add(corpusPId);
                    // add the corpus name to combobox
                    ((DefaultComboBoxModel<String>) corpusToSearchIn.getModel()).addElement(name);
                }
            }
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    corpusToSearchIn.updateUI();
                    corpusToSearchIn.setSelectedItem(Constants.ENTIRE_DATASTORE);
                }
            });
        } catch (PersistenceException e) {
            System.out.println("Couldn't find any available corpusIds.");
            throw new GateRuntimeException(e);
        }
    } else // Java Web Start application
    {
        searcher = (Searcher) target;
        corpusToSearchIn.setEnabled(false);
        // find out all annotation sets that are indexed
        try {
            annotationSetIDsFromDataStore = searcher.getIndexedAnnotationSetNames();
            allAnnotTypesAndFeaturesFromDatastore = searcher.getAnnotationTypesMap();
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    updateAnnotationSetsList();
                }
            });
        } catch (SearchException e) {
            throw new GateRuntimeException(e);
        }
    }
}
Also used : Searcher(gate.creole.annic.Searcher) SearchException(gate.creole.annic.SearchException) LuceneDataStoreImpl(gate.persist.LuceneDataStoreImpl) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) SerialCorpusImpl(gate.corpora.SerialCorpusImpl) GateRuntimeException(gate.util.GateRuntimeException) PersistenceException(gate.persist.PersistenceException) EventObject(java.util.EventObject)

Example 12 with PersistenceException

use of gate.persist.PersistenceException in project gate-core by GateNLP.

the class MainFrame method openSerialDataStore.

// createSerialDataStore()
/**
 * Method is used in OpenDSAction
 * @return the opened datastore or null if an error occurs
 */
protected DataStore openSerialDataStore() {
    DataStore ds = null;
    // get the URL (a file in this case)
    fileChooser.setDialogTitle("Select the datastore directory");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    fileChooser.setResource("gate.persist.SerialDataStore");
    if (fileChooser.showOpenDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
        try {
            URL dsURL = fileChooser.getSelectedFile().toURI().toURL();
            ds = Factory.openDataStore("gate.persist.SerialDataStore", dsURL.toExternalForm());
        } catch (MalformedURLException mue) {
            JOptionPane.showMessageDialog(MainFrame.this, "Invalid location for the datastore\n " + mue.toString(), "GATE", JOptionPane.ERROR_MESSAGE);
        } catch (PersistenceException pe) {
            JOptionPane.showMessageDialog(MainFrame.this, "Datastore opening error!\n " + pe.toString(), "GATE", JOptionPane.ERROR_MESSAGE);
        }
    // catch
    }
    return ds;
}
Also used : MalformedURLException(java.net.MalformedURLException) DataStore(gate.DataStore) PersistenceException(gate.persist.PersistenceException) URL(java.net.URL)

Example 13 with PersistenceException

use of gate.persist.PersistenceException in project gate-core by GateNLP.

the class PRPersistence method extractDataFromSource.

/**
 * Populates this Persistence with the data that needs to be stored from the
 * original source object.
 */
@Override
public void extractDataFromSource(Object source) throws PersistenceException {
    if (!(source instanceof ProcessingResource)) {
        throw new UnsupportedOperationException(getClass().getName() + " can only be used for " + ProcessingResource.class.getName() + " objects!\n" + source.getClass().getName() + " is not a " + ProcessingResource.class.getName());
    }
    super.extractDataFromSource(source);
    Resource res = (Resource) source;
    ResourceData rData = Gate.getCreoleRegister().get(res.getClass().getName());
    if (rData == null)
        throw new PersistenceException("Could not find CREOLE data for " + res.getClass().getName());
    // now get the runtime params
    ParameterList params = rData.getParameterList();
    try {
        // get the values for the init time parameters
        runtimeParams = Factory.newFeatureMap();
        // this is a list of lists
        Iterator<List<Parameter>> parDisjIter = params.getRuntimeParameters().iterator();
        while (parDisjIter.hasNext()) {
            Iterator<Parameter> parIter = parDisjIter.next().iterator();
            while (parIter.hasNext()) {
                Parameter parameter = parIter.next();
                String parName = parameter.getName();
                Object parValue = res.getParameterValue(parName);
                if (storeParameterValue(parValue, parameter.getDefaultValue())) {
                    ((FeatureMap) runtimeParams).put(parName, parValue);
                }
            }
        }
        runtimeParams = PersistenceManager.getPersistentRepresentation(runtimeParams);
    } catch (ResourceInstantiationException | ParameterException rie) {
        throw new PersistenceException(rie);
    }
}
Also used : ResourceData(gate.creole.ResourceData) ProcessingResource(gate.ProcessingResource) Resource(gate.Resource) ProcessingResource(gate.ProcessingResource) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) PersistenceException(gate.persist.PersistenceException) ParameterList(gate.creole.ParameterList) Parameter(gate.creole.Parameter) ParameterList(gate.creole.ParameterList) List(java.util.List) ParameterException(gate.creole.ParameterException)

Example 14 with PersistenceException

use of gate.persist.PersistenceException in project gate-core by GateNLP.

the class PersistenceManager method loadObjectFromUrl.

public static Object loadObjectFromUrl(URL url) throws PersistenceException, IOException, ResourceInstantiationException {
    if (!Gate.isInitialised())
        throw new ResourceInstantiationException("You must call Gate.init() before you can restore resources");
    ProgressListener pListener = (ProgressListener) Gate.getListeners().get("gate.event.ProgressListener");
    StatusListener sListener = (gate.event.StatusListener) Gate.getListeners().get("gate.event.StatusListener");
    if (pListener != null)
        pListener.progressChanged(0);
    startLoadingFrom(url);
    // the actual stream obtained from the URL. We keep a reference to this
    // so we can ensure it gets closed.
    InputStream rawStream = null;
    try {
        long startTime = System.currentTimeMillis();
        // Determine whether the file contains an application serialized in
        // xml
        // format. Otherwise we will assume that it contains native
        // serializations.
        boolean xmlStream = isXmlApplicationFile(url);
        ObjectInputStream ois = null;
        HierarchicalStreamReader reader = null;
        XStream xstream = null;
        // whether serialization is native or xml.
        if (xmlStream) {
            // we don't want to strip the BOM on XML.
            Reader inputReader = new InputStreamReader(rawStream = url.openStream());
            try {
                XMLInputFactory inputFactory = XMLInputFactory.newInstance();
                inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
                XMLStreamReader xsr = inputFactory.createXMLStreamReader(url.toExternalForm(), inputReader);
                reader = new StaxReader(new QNameMap(), xsr);
            } catch (XMLStreamException xse) {
                // make sure the stream is closed, on error
                inputReader.close();
                throw new PersistenceException("Error creating reader", xse);
            }
            xstream = new XStream(new StaxDriver(new XStream11NameCoder())) {

                @Override
                protected boolean useXStream11XmlFriendlyMapper() {
                    return true;
                }
            };
            // make XStream load classes through the GATE ClassLoader
            xstream.setClassLoader(Gate.getClassLoader());
            // make the XML stream appear as a normal ObjectInputStream
            ois = xstream.createObjectInputStream(reader);
        } else {
            // use GateAwareObjectInputStream to load classes through the
            // GATE ClassLoader if they can't be loaded through the one
            // ObjectInputStream would normally use
            ois = new GateAwareObjectInputStream(url.openStream());
        }
        Object res = null;
        try {
            // first read the list of creole URLs.
            @SuppressWarnings("unchecked") Iterator<?> urlIter = ((Collection<?>) getTransientRepresentation(ois.readObject())).iterator();
            // and re-register them
            while (urlIter.hasNext()) {
                Object anUrl = urlIter.next();
                try {
                    if (anUrl instanceof URL)
                        Gate.getCreoleRegister().registerPlugin(new Plugin.Directory((URL) anUrl), false);
                    else if (anUrl instanceof Plugin)
                        Gate.getCreoleRegister().registerPlugin((Plugin) anUrl, false);
                } catch (GateException ge) {
                    System.out.println("We've hit an error!");
                    ge.printStackTrace();
                    ge.printStackTrace(Err.getPrintWriter());
                    Err.prln("Could not reload creole directory " + anUrl);
                }
            }
            // now we can read the saved object in the presence of all
            // the right plugins
            res = ois.readObject();
            // ensure a fresh start
            clearCurrentTransients();
            res = getTransientRepresentation(res);
            long endTime = System.currentTimeMillis();
            if (sListener != null)
                sListener.statusChanged("Loading completed in " + NumberFormat.getInstance().format((double) (endTime - startTime) / 1000) + " seconds");
            return res;
        } catch (ResourceInstantiationException rie) {
            if (sListener != null)
                sListener.statusChanged("Failure during instantiation of resources.");
            throw rie;
        } catch (PersistenceException pe) {
            if (sListener != null)
                sListener.statusChanged("Failure during persistence operations.");
            throw pe;
        } catch (Exception ex) {
            if (sListener != null)
                sListener.statusChanged("Loading failed!");
            throw new PersistenceException(ex);
        } finally {
            // make sure the stream gets closed
            if (ois != null)
                ois.close();
            if (reader != null)
                reader.close();
        }
    } finally {
        if (rawStream != null)
            rawStream.close();
        finishedLoading();
        if (pListener != null)
            pListener.processFinished();
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) BomStrippingInputStreamReader(gate.util.BomStrippingInputStreamReader) StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URL(java.net.URL) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) BomStrippingInputStreamReader(gate.util.BomStrippingInputStreamReader) InputStreamReader(java.io.InputStreamReader) GateAwareObjectInputStream(gate.persist.GateAwareObjectInputStream) ObjectInputStream(java.io.ObjectInputStream) GateAwareObjectInputStream(gate.persist.GateAwareObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) GateException(gate.util.GateException) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) PersistenceException(gate.persist.PersistenceException) GateRuntimeException(gate.util.GateRuntimeException) ResourceInstantiationException(gate.creole.ResourceInstantiationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GateException(gate.util.GateException) ResourceInstantiationException(gate.creole.ResourceInstantiationException) ProgressListener(gate.event.ProgressListener) XMLStreamException(javax.xml.stream.XMLStreamException) XStream11NameCoder(com.thoughtworks.xstream.io.xml.XStream11NameCoder) PersistenceException(gate.persist.PersistenceException) Collection(java.util.Collection) StatusListener(gate.event.StatusListener) XMLInputFactory(javax.xml.stream.XMLInputFactory) ObjectInputStream(java.io.ObjectInputStream) GateAwareObjectInputStream(gate.persist.GateAwareObjectInputStream) Plugin(gate.creole.Plugin)

Example 15 with PersistenceException

use of gate.persist.PersistenceException in project gate-core by GateNLP.

the class CorpusBenchmarkTool method generateCorpus.

// setStartDirectory
protected void generateCorpus(File fileDir, File outputDir) {
    // 1. check if we have input files
    if (fileDir == null)
        return;
    // 2. create the output directory or clean it up if needed
    File outDir = outputDir;
    if (outputDir == null) {
        outDir = new File(currDir, PROCESSED_DIR_NAME);
    } else {
        // get rid of the directory, coz datastore wants it clean
        if (!Files.rmdir(outDir))
            Out.prln("cannot delete old output directory: " + outDir);
    }
    outDir.mkdir();
    // create the datastore and process each document
    try {
        SerialDataStore sds = new SerialDataStore(outDir.toURI().toURL().toString());
        sds.create();
        sds.open();
        File[] files = fileDir.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isFile())
                continue;
            // create a document
            Out.prln("Processing and storing document: " + files[i].toURI().toURL() + "<P>");
            FeatureMap params = Factory.newFeatureMap();
            params.put(Document.DOCUMENT_URL_PARAMETER_NAME, files[i].toURI().toURL());
            params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, documentEncoding);
            FeatureMap features = Factory.newFeatureMap();
            // Gate.setHiddenAttribute(features, true);
            // create the document
            final Document doc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params, features);
            doc.setName(files[i].getName());
            processDocument(doc);
            final LanguageResource lr = sds.adopt(doc);
            sds.sync(lr);
            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    Factory.deleteResource(doc);
                    Factory.deleteResource(lr);
                }
            });
        }
        // for
        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 : LanguageResource(gate.LanguageResource) Document(gate.Document) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) PersistenceException(gate.persist.PersistenceException) File(java.io.File) SerialDataStore(gate.persist.SerialDataStore)

Aggregations

PersistenceException (gate.persist.PersistenceException)17 ResourceInstantiationException (gate.creole.ResourceInstantiationException)9 DataStore (gate.DataStore)6 GateRuntimeException (gate.util.GateRuntimeException)6 FeatureMap (gate.FeatureMap)5 SerialDataStore (gate.persist.SerialDataStore)5 MalformedURLException (java.net.MalformedURLException)5 URL (java.net.URL)5 Document (gate.Document)4 ParameterException (gate.creole.ParameterException)4 ResourceData (gate.creole.ResourceData)4 Resource (gate.Resource)3 File (java.io.File)3 AbstractResource (gate.creole.AbstractResource)2 Parameter (gate.creole.Parameter)2 ParameterList (gate.creole.ParameterList)2 Plugin (gate.creole.Plugin)2 StatusListener (gate.event.StatusListener)2 GateException (gate.util.GateException)2 IOException (java.io.IOException)2