use of edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException in project cogtool by cogtool.
the class ProjectController method createExportForSanlab.
protected IListenerAction createExportForSanlab() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return ProjectSelectionState.class;
}
public boolean performAction(Object actionParms) {
ProjectSelectionState sel = (ProjectSelectionState) actionParms;
// Must have selected tasks and design
Design design = sel.getSelectedDesign();
AUndertaking[] tasks = sel.getSelectedTasks(TaskSelectionState.ORDER_SELECTION);
if ((design == null) || (tasks == null) || (tasks.length == 0)) {
return false;
}
// Fetch traces for the default algorithm (TODO:)
List<String> traces = getTraces(sel, MODELGEN_ALG, project.getDefaultAlgo());
// Ask user for location of saved file.
File exportFile = interaction.selectExportLocation("cogtool-sanlab", CogToolFileTypes.TEXT_FILE_EXT);
// User canceled
if (exportFile == null) {
return false;
}
boolean okToProceed = false;
for (AUndertaking task : tasks) {
// If no script set exists for this cell, create
TaskApplication ta = project.getTaskApplication(task, design);
if (ta != null) {
if (ta.getDesign() != design) {
throw new RcvrIllegalStateException("Unexpected Design mis-match for SANLab (" + ta.getDesign() + ", " + design + ")");
}
// If no script exists for this cell, create one
Script script = DemoStateManager.ensureScript(ta, MODELGEN_ALG);
try {
IPredictionAlgo taAlg = ta.determineActiveAlgorithm(project);
if (!(taAlg instanceof ACTRPredictionAlgo)) {
throw new RcvrIllegalStateException("Can't export model for SANLab from a non ACT-R task.");
}
if (script.getAssociatedPath() != null) {
File f = new File(script.getAssociatedPath());
// The following will throw an IOException if
// the input file doesn't exist; this is exactly
// the same behaviour as if we're trying to do
// a recompute, and is better than silently
// substituting a generated model file
FileUtil.copyTextFileToFile(f, exportFile);
return true;
}
ACTRPredictionAlgo algo = (ACTRPredictionAlgo) taAlg;
algo.outputModel(design, task, ta.getDemonstration().getStartFrame(), script, exportFile, null);
} catch (UnsupportedOperationException ex) {
throw new RcvrUnimplementedFnException(ex);
} catch (IOException ex) {
throw new RcvrIOException(("IOException exporting SANLab model file for task " + task.getName() + " in design " + design.getName()), ex);
}
}
}
// TODO: should we move this file write somewhere else?
PrintWriter writer = null;
try {
// Attempt to open the output file
FileOutputStream out = new FileOutputStream(exportFile, true);
writer = new PrintWriter(out);
writer.println("\n\n;;; TRACE STARTS HERE");
Matcher mt = TRACE_PAT.matcher("");
// Put each trace line on its own output line
Iterator<String> iter = traces.iterator();
while (iter.hasNext()) {
String s = iter.next();
if (mt.reset(s).matches()) {
writer.println(s);
}
}
writer.flush();
okToProceed = true;
} catch (IOException e) {
throw new RcvrIOSaveException("Writing the trace logs for " + "SANLab failed. \n\n" + "Please try again.", e);
} finally {
if (writer != null) {
writer.close();
}
}
return okToProceed;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException in project cogtool by cogtool.
the class ProjectController method createExportTraces.
// TODO there's way too much cloning of code in the next three methods; refactor them
// Action for ExportTraces
protected IListenerAction createExportTraces() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return ProjectSelectionState.class;
}
public boolean performAction(Object actionParms) {
ProjectSelectionState sel = (ProjectSelectionState) actionParms;
// Fetch traces for the default algorithm (TODO:)
List<String> traces = getTraces(sel, MODELGEN_ALG, project.getDefaultAlgo());
// Ask user for location of saved file.
File exportFile = interaction.selectExportLocation("Exported ACT-R trace", CogToolFileTypes.TEXT_FILE_EXT);
// User canceled
if (exportFile == null) {
return false;
}
boolean okToProceed = false;
// TODO: should we move this file write somewhere else?
PrintWriter writer = null;
try {
// Attempt to open the output file
FileOutputStream out = new FileOutputStream(exportFile);
writer = new PrintWriter(out);
// Put each trace line on its own output line
Iterator<String> iter = traces.iterator();
while (iter.hasNext()) {
String s = iter.next();
writer.println(s);
}
writer.flush();
okToProceed = true;
} catch (IOException e) {
throw new RcvrIOSaveException("Writing the trace logs" + "failed. \n\n" + "Please try again.", e);
} finally {
if (writer != null) {
writer.close();
}
}
return okToProceed;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException in project cogtool by cogtool.
the class ProjectController method exportDesignToXML.
private boolean exportDesignToXML(Design design) {
String defaultFilename = ((design != null) ? design.getName() : project.getName()) + ".xml";
File exportFile = null;
if (interaction != null && CogTool.exportCSVKludgeDir == null) {
exportFile = interaction.selectXMLFile(false, defaultFilename);
} else {
exportFile = new File(CogTool.exportCSVKludgeDir, defaultFilename);
}
if (exportFile == null) {
return false;
}
OutputStream oStream = null;
String completionMsg;
try {
try {
oStream = new FileOutputStream(exportFile);
Writer xmlWriter = new BufferedWriter(new OutputStreamWriter(oStream, "UTF-8"));
if (design == null) {
ExportCogToolXML.exportXML(project, xmlWriter, "UTF-8");
completionMsg = allDesignsExportedToXml;
} else {
Map<ITaskDesign, TaskApplication> designTAs = project.taskApplicationsForDesign(design);
ExportCogToolXML.exportXML(design, designTAs, xmlWriter, "UTF-8");
completionMsg = designExportedToXml;
}
xmlWriter.flush();
} finally {
if (oStream != null) {
oStream.close();
}
}
} catch (IllegalArgumentException e) {
throw new RcvrIllegalStateException("Invalid argument exporting to XML", e);
} catch (IllegalStateException e) {
throw new RcvrIllegalStateException("Exporting to XML", e);
} catch (IOException e) {
throw new RcvrIOSaveException("Exporting to XML", e);
} catch (Exception e) {
throw new RecoverableException("Exporting to XML", e);
}
if (interaction != null) {
interaction.setStatusMessage(completionMsg + " " + exportFile.getAbsolutePath());
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException in project cogtool by cogtool.
the class DesignEditorController method createExportDesignToXMLAction.
protected IListenerAction createExportDesignToXMLAction() {
return new AListenerAction() {
public boolean performAction(Object actionParms) {
String defaultFilename = design.getName() + ".xml";
File exportFile = interaction.selectXMLFile(false, defaultFilename);
if (exportFile == null) {
return false;
}
OutputStream oStream = null;
try {
try {
oStream = new FileOutputStream(exportFile);
Writer xmlWriter = new BufferedWriter(new OutputStreamWriter(oStream, "UTF-8"));
Map<ITaskDesign, TaskApplication> designTAs = project.taskApplicationsForDesign(design);
ExportCogToolXML.exportXML(design, designTAs, xmlWriter, "UTF-8");
xmlWriter.flush();
} finally {
if (oStream != null) {
oStream.close();
}
}
} catch (IllegalArgumentException e) {
throw new RcvrIllegalStateException("Invalid argument exporting to XML", e);
} catch (IllegalStateException e) {
throw new RcvrIllegalStateException("Exporting to XML", e);
} catch (IOException e) {
throw new RcvrIOSaveException("Exporting to XML", e);
} catch (Exception e) {
throw new RecoverableException("Exporting to XML", e);
}
interaction.setStatusMessage(L10N.get("PC.DesignExportedToXML", "Design exported to XML:") + " " + exportFile.getName());
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException in project cogtool by cogtool.
the class DefaultController method save.
/**
* Perform a Save operation, using a stored save location.
*
* @return true if file was saved, false otherwise
* @throws RcvrIOException if the save operation fails
*/
protected boolean save() {
try {
// Save to this file's original location.
project.setBuildVersion(CogTool.getVersion());
persist.save(project, null);
// Tell undo manager(s) that a save has just occurred
try {
UndoManager.markSavePoint(project);
} catch (IllegalStateException ex) {
throw new RcvrCannotUndoRedoException("Marking save point", ex);
}
return true;
} catch (IOException e) {
throw new RcvrIOSaveException("Error persisting project: " + e.getMessage(), e);
}
}
Aggregations