use of gate.persist.PersistenceException in project gate-core by GateNLP.
the class MainFrame method createSerialDataStore.
// setTitle(String title)
/**
* Method is used in NewDSAction
* @return the new datastore or null if an error occurs
*/
protected DataStore createSerialDataStore() {
DataStore ds = null;
// get the URL (a file in this case)
fileChooser.setDialogTitle("Please create a new empty 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.createDataStore("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 creation error!\n " + pe.toString(), "GATE", JOptionPane.ERROR_MESSAGE);
}
// catch
}
return ds;
}
use of gate.persist.PersistenceException in project gate-core by GateNLP.
the class MainFrame method openSearchableDataStore.
// createSearchableDataStore()
/**
* Method is used in OpenDSAction
* @return the opened datastore or null if an error occurs
*/
protected DataStore openSearchableDataStore() {
DataStore ds = null;
// get the URL (a file in this case)
fileChooser.setDialogTitle("Select the datastore directory");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setResource("gate.DataStore.data");
if (fileChooser.showOpenDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
URL dsURL = fileChooser.getSelectedFile().toURI().toURL();
ds = Factory.openDataStore("gate.persist.LuceneDataStoreImpl", 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;
}
use of gate.persist.PersistenceException in project gate-core by GateNLP.
the class SerialDatastoreViewer method initGuiComponents.
protected void initGuiComponents() {
treeRoot = new DefaultMutableTreeNode(datastore.getName(), true);
treeModel = new DefaultTreeModel(treeRoot, true);
mainTree = new JTree();
mainTree.setModel(treeModel);
mainTree.setExpandsSelectedPaths(true);
mainTree.expandPath(new TreePath(treeRoot));
mainTree.addTreeWillExpandListener(new TreeWillExpandListener() {
@Override
public void treeWillCollapse(TreeExpansionEvent e) {
// ignore these events as we don't care about collapsing trees, timmmmmmmmmber!
}
@Override
public void treeWillExpand(TreeExpansionEvent e) {
TreePath path = e.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (node.getChildCount() == 0 && node.getUserObject() instanceof DSType) {
DSType dsType = (DSType) node.getUserObject();
if (dsType.expanded)
return;
node.removeAllChildren();
try {
Iterator<String> lrIDsIter = datastore.getLrIds(dsType.type).iterator();
while (lrIDsIter.hasNext()) {
String id = lrIDsIter.next();
DSEntry entry = new DSEntry(datastore.getLrName(id), id, dsType.type);
DefaultMutableTreeNode lrNode = new DefaultMutableTreeNode(entry, false);
treeModel.insertNodeInto(lrNode, node, node.getChildCount());
node.add(lrNode);
}
dsType.expanded = true;
} catch (PersistenceException pe) {
throw new GateRuntimeException(pe.toString());
}
}
}
});
try {
Iterator<String> lrTypesIter = datastore.getLrTypes().iterator();
CreoleRegister cReg = Gate.getCreoleRegister();
while (lrTypesIter.hasNext()) {
String type = lrTypesIter.next();
ResourceData rData = cReg.get(type);
DSType dsType = new DSType(rData.getName(), type);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(dsType);
treeModel.insertNodeInto(node, treeRoot, treeRoot.getChildCount());
}
} catch (PersistenceException pe) {
throw new GateRuntimeException(pe.toString());
}
DefaultTreeSelectionModel selectionModel = new DefaultTreeSelectionModel();
selectionModel.setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
mainTree.setSelectionModel(selectionModel);
getViewport().setView(mainTree);
popup = new JPopupMenu();
deleteAction = new DeleteAction();
loadAction = new LoadAction();
popup.add(deleteAction);
popup.add(loadAction);
}
use of gate.persist.PersistenceException in project gate-core by GateNLP.
the class SerialDatastoreViewer method resourceWritten.
@Override
public void resourceWritten(DatastoreEvent e) {
Resource res = e.getResource();
String resID = (String) e.getResourceID();
String resType = Gate.getCreoleRegister().get(res.getClass().getName()).getName();
DefaultMutableTreeNode parent = treeRoot;
DefaultMutableTreeNode node = null;
// first look for the type node
Enumeration<?> childrenEnum = parent.children();
boolean found = false;
while (childrenEnum.hasMoreElements() && !found) {
node = (DefaultMutableTreeNode) childrenEnum.nextElement();
if (node.getUserObject() instanceof DSType) {
found = ((DSType) node.getUserObject()).name.equals(resType);
}
}
if (!found) {
// exhausted the children without finding the node -> new type
node = new DefaultMutableTreeNode(new DSType(resType, res.getClass().getName()));
treeModel.insertNodeInto(node, parent, parent.getChildCount());
}
if (node.getUserObject() instanceof DSType) {
if (!((DSType) node.getUserObject()).expanded)
return;
}
// now look for the resource node
parent = node;
childrenEnum = parent.children();
found = false;
while (childrenEnum.hasMoreElements() && !found) {
node = (DefaultMutableTreeNode) childrenEnum.nextElement();
found = ((DSEntry) node.getUserObject()).id.equals(resID);
}
if (!found) {
// exhausted the children without finding the node -> new resource
try {
DSEntry entry = new DSEntry(datastore.getLrName(resID), resID, res.getClass().getName());
node = new DefaultMutableTreeNode(entry, false);
treeModel.insertNodeInto(node, parent, parent.getChildCount());
} catch (PersistenceException pe) {
pe.printStackTrace(Err.getPrintWriter());
}
}
}
use of gate.persist.PersistenceException 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);
}
}
Aggregations