use of edu.cmu.cs.hcii.cogtool.util.RcvrParsingException in project cogtool by cogtool.
the class ProjectController method createImportHumanCSVFileAction.
// Action for ImportHumanCSVFile
protected IListenerAction createImportHumanCSVFileAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return ProjectSelectionState.class;
}
public boolean performAction(Object prms) {
ProjectSelectionState seln = (ProjectSelectionState) prms;
// Must have selected tasks and design
Design design = seln.getSelectedDesign();
AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION);
if ((design == null) || (tasks == null) || (tasks.length == 0)) {
return false;
}
List<String> outputLines = new LinkedList<String>();
List<String> errorLines = new LinkedList<String>();
// Get csv file
File dataFile = interaction.selectCSVFile();
if (dataFile == null) {
interaction.setStatusMessage(IMPORT_FAIL_NOFILE_MSG);
return false;
}
// Read lines out of file
FileReader reader = null;
try {
reader = new FileReader(dataFile);
FileUtil.readLines(reader, outputLines);
} catch (FileNotFoundException e) {
interaction.setStatusMessage(IMPORT_FAIL_NOFILE_MSG);
throw new RcvrIOTempException("Error in parsing csv", e);
} catch (IOException e) {
interaction.setStatusMessage(IMPORT_FAIL_NOREAD_MSG);
throw new RcvrParsingException("Error in parsing csv", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
reader = null;
}
}
// parse ResultSteps out of trace lines
TraceParser<ResultStep> parser = new HumanCSVParser();
List<ResultStep> steps;
try {
steps = parser.parseTrace(outputLines);
} catch (TraceParser.ParseException e) {
interaction.setStatusMessage(IMPORT_FAIL_PARSE_IMPL_MSG);
throw new RcvrParsingException("Error in parsing implementation", e);
}
System.out.println(steps);
if (steps.size() < 1) {
interaction.setStatusMessage(IMPORT_FAIL_PARSE_MSG);
return false;
}
double taskTime = -1.0;
Iterator<ResultStep> stepIt = steps.iterator();
while (stepIt.hasNext()) {
ResultStep step = stepIt.next();
taskTime = Math.max(taskTime, step.startTime + step.duration);
}
// Scale time to seconds.
taskTime /= 1000.0;
// -------------- Done parsing
DemoStateManager demoMgr = DemoStateManager.getStateManager(project, design);
final TaskApplication[] taskApps = new TaskApplication[tasks.length];
final APredictionResult[] oldResults = new APredictionResult[tasks.length];
final APredictionResult[] results = new APredictionResult[tasks.length];
final IPredictionAlgo[] oldActiveAlgos = new IPredictionAlgo[tasks.length];
for (int i = 0; i < tasks.length; i++) {
taskApps[i] = ensureTaskApplication(tasks[i], design, MODELGEN_ALG, demoMgr);
// If for some reason there are no result steps,
// create a TimePredictionResult that
// reflects COMPUTATION_FAILED.
// Otherwise, create a normal one.
Script script = taskApps[i].getScript(MODELGEN_ALG);
if (taskTime < 0.0) {
results[i] = new TimePredictionResult(dataFile.getName(), script, HumanDataAlgo.ONLY, outputLines, errorLines, steps);
} else {
results[i] = new TimePredictionResult(dataFile.getName(), script, HumanDataAlgo.ONLY, outputLines, errorLines, steps, taskTime);
}
oldResults[i] = taskApps[i].getResult(MODELGEN_ALG, HumanDataAlgo.ONLY);
taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, results[i]);
oldActiveAlgos[i] = taskApps[i].getActiveAlgorithm();
taskApps[i].setActiveAlgorithm(HumanDataAlgo.ONLY);
}
IUndoableEdit edit = new AUndoableEdit(ProjectLID.RecomputeScript) {
@Override
public String getPresentationName() {
return IMPORT_HUMAN_CSV;
}
@Override
public void redo() {
super.redo();
for (int i = 0; i < taskApps.length; i++) {
taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, results[i]);
taskApps[i].setActiveAlgorithm(HumanDataAlgo.ONLY);
}
}
@Override
public void undo() {
super.undo();
for (int i = 0; i < taskApps.length; i++) {
taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, oldResults[i]);
taskApps[i].setActiveAlgorithm(oldActiveAlgos[i]);
}
}
};
undoMgr.addEdit(edit);
interaction.setStatusMessage(IMPORT_SUCCESS_MSG);
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.RcvrParsingException in project cogtool by cogtool.
the class DictionaryEditorCmd method importDictionary.
/**
* Replace prevDict with dict after reading the entries from the file.
* @param csvFile TODO
*/
// I (dfm) believe the above is a lie. I think prevDict was already replaced
// by dict by the caller, and prevDict is simply a vestigal bit of data that
// is passed around and encapsulated in undo and redo forms for no reason
// at all. But I'm not sufficiently confident of that diagnosis to zap it
// just yet....
public static boolean importDictionary(final Design design, final ISimilarityDictionary dict, final ISimilarityDictionary prevDict, Interaction interaction, IUndoableEditSequence editSeq, String csvFile) {
File dataFile = (csvFile != null ? new File(csvFile) : interaction.selectCSVFile());
if (dataFile == null) {
return false;
}
FileReader reader = null;
List<String> outputLines = new ArrayList<String>();
try {
reader = new FileReader(dataFile);
FileUtil.readLines(reader, outputLines);
} catch (IOException e) {
interaction.reportProblem("File I/O Error", e.getMessage());
return false;
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
interaction.reportProblem("File I/O Error on Close", e.getMessage());
return false;
}
}
if (outputLines.size() == 0) {
interaction.reportProblem("Empty file", "Requested .csv file is empty");
return false;
}
if (outputLines.size() < 3) {
interaction.reportProblem("Invalid file", "Requested file is not a valid dictionary file");
return false;
}
String[] headerCol = CSVSupport.getCells(outputLines.get(0));
if ((headerCol.length != 2) || !(ISimilarityDictionary.COGTOOL_DICT_HEADER.equals(headerCol[0])) || !headerCol[1].equals(DICT_CSV_VERSION_0)) {
interaction.reportProblem("Invalid file", "Requested file is not a valid dictionary file");
return false;
}
final Map<DictEntry, DictValue> newEntries = new LinkedHashMap<DictEntry, DictValue>();
for (int i = 2; i < outputLines.size(); i++) {
String newLine = outputLines.get(i);
String[] cols = CSVSupport.getCells(newLine);
// [required] 0 is goal string
// [required] 1 is search string
// [required] 2 is algorithm name
// [optional] 3 is site (for Google algs) or url (for LSA)
// [optional] 4 is term space (for LSA)
String site = (cols.length > 3) ? cols[3] : "";
String space = (cols.length > 4) ? cols[4] : "";
DictEntry entry = new DictEntry(cols[0], cols[1], getNameAlgorithm(cols[2], site, space));
double similarity;
Date editedDate;
try {
similarity = parseDouble(cols[4]);
editedDate = new PersistentDate(PersistentDate.DATE_FORMAT.parse(cols[5]));
} catch (Exception e) {
throw new RcvrParsingException("Parsing error", e);
}
DictValue value = new DictValue(similarity, editedDate);
newEntries.put(entry, value);
}
dict.insertEntries(newEntries, false);
if (editSeq != null) {
editSeq.addEdit(new AUndoableEdit(ProjectLID.ImportDict) {
@Override
public String getPresentationName() {
return IMPORT_DICTIONARY;
}
@Override
public void redo() {
super.redo();
if (!NullSafe.equals(prevDict, WidgetAttributes.NO_DICTIONARY)) {
design.setAttribute(WidgetAttributes.DICTIONARY_ATTR, dict);
} else {
dict.insertEntries(newEntries, false);
}
}
@Override
public void undo() {
super.undo();
if (!NullSafe.equals(prevDict, WidgetAttributes.NO_DICTIONARY)) {
design.setAttribute(WidgetAttributes.DICTIONARY_ATTR, prevDict);
} else {
dict.removeEntries(newEntries);
}
}
});
}
if (interaction != null) {
interaction.setStatusMessage("Import successful");
}
return true;
}
Aggregations