Skip to content
This repository has been archived by the owner on Sep 3, 2018. It is now read-only.

Commit

Permalink
Support re-initializing after removing the cache file, slightly change
Browse files Browse the repository at this point in the history
how gazstores are re-used when re-initializing.
  • Loading branch information
johann-petrak committed May 18, 2015
1 parent e507354 commit a3df0dc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion creole.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<CREOLE-DIRECTORY
ID="com.jpetrak.StringAnnotation"
VERSION="3.1"
VERSION="3.2"
DESCRIPTION="Extended Gazetteer, Java Regular Expression Annotator"
HELPURL="http://github.com/johann-petrak/gateplugin-stringannotation/wiki"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,21 @@
import org.apache.log4j.Logger;

import com.jpetrak.gate.stringannotation.extendedgazetteer.trie.GazStoreTrie3;
import gate.CreoleRegister;
import gate.Gate;
import gate.GateConstants;
import gate.creole.ResourceData;
import gate.gui.ActionsPublisher;
import gate.gui.MainFrame;
import gate.gui.NewResourceDialog;
import java.awt.event.ActionEvent;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.zip.GZIPInputStream;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.SHORT_DESCRIPTION;
import javax.swing.JOptionPane;

import org.yaml.snakeyaml.Yaml;

Expand All @@ -50,7 +63,7 @@
*
* @author Johann Petrak
*/
public abstract class GazetteerBase extends AbstractLanguageAnalyser {
public abstract class GazetteerBase extends AbstractLanguageAnalyser implements ActionsPublisher {

/**
*
Expand Down Expand Up @@ -174,6 +187,32 @@ private synchronized void incrementGazStore() throws ResourceInstantiationExcept
logger.info(gazStore.statsString());
}

private synchronized void replaceGazStore() throws ResourceInstantiationException {
String uniqueGazStoreKey = genUniqueGazStoreKey();
logger.info("Replacing gazetteer for " + getConfigFileURL());
System.gc();
long startTime = System.currentTimeMillis();
long before = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
GazStore gs = loadedGazStores.get(uniqueGazStoreKey);
try {
loadData();
gazStore.compact();
} catch (Exception ex) {
throw new ResourceInstantiationException("Could not load gazetteer", ex);
}
loadedGazStores.put(uniqueGazStoreKey, gazStore);
logger.info("GazStore replaced for " + uniqueGazStoreKey);

long endTime = System.currentTimeMillis();
System.gc();
long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
logger.info("Gazetteer created in (secs): " + ((endTime - startTime) / 1000.0));
logger.info("Heap memory increase (estimate,MB): "
+ String.format("%01.3f", ((after - before) / (1024.0 * 1024.0))));
logger.info(gazStore.statsString());
}


private synchronized void decrementGazStore() {
String key = genUniqueGazStoreKey();
GazStore gs = loadedGazStores.get(key);
Expand Down Expand Up @@ -206,11 +245,10 @@ public void save(File whereTo) throws IOException {
@Override
/**
*/
// TODO: we may want to delete the cache and re-init from the list files when
// reInit() is called?
public void reInit() throws ResourceInstantiationException {
removeGazStore();
init();
//removeGazStore();
//init();
replaceGazStore();
}

protected void loadData() throws UnsupportedEncodingException, IOException, ResourceInstantiationException {
Expand Down Expand Up @@ -518,5 +556,46 @@ public List<FeatureMap> lookups2FeatureMaps(Iterator<Lookup> lookups) {
}
return fms;
}

private List<Action> actions;

@Override
public List<Action> getActions() {
if (actions == null) {
actions = new ArrayList<Action>();

// Action 1: remove the gazbin file and re-initialize the gazetteer
actions.add(
new AbstractAction("Remove cache and re-initialize") {
{
putValue(SHORT_DESCRIPTION,
"Remove cache and re-initialize");
putValue(GateConstants.MENU_PATH_KEY,
new String[]{"WTFISTHIS??????"});
}
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt) {
File configFile = gate.util.Files.fileFromURL(getConfigFileURL());
String configFileName = configFile.getAbsolutePath();
String gazbinFileName = configFileName.replaceAll("(?:\\.def$|\\.defyaml)", ".gazbin");
if (configFileName.equals(gazbinFileName)) {
throw new GateRuntimeException("Config file must have def or defyaml extension!");
}
File gazbinFile = new File(gazbinFileName);
gazbinFile.delete();
try {
reInit();
} catch (ResourceInstantiationException ex) {
throw new GateRuntimeException("Re-initialization failed",ex);
}
}
});


}
return actions;
}
} // ExtendedGazetteer

0 comments on commit a3df0dc

Please sign in to comment.