Search in sources :

Example 1 with RcvrIOSaveException

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;
        }
    };
}
Also used : Script(edu.cmu.cs.hcii.cogtool.model.Script) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) FileOutputStream(java.io.FileOutputStream) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) File(java.io.File) ACTRPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo) PrintWriter(java.io.PrintWriter)

Example 2 with RcvrIOSaveException

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;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) File(java.io.File) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) PrintWriter(java.io.PrintWriter)

Example 3 with RcvrIOSaveException

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;
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrXMLParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrXMLParsingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) RcvrCannotUndoRedoException(edu.cmu.cs.hcii.cogtool.util.RcvrCannotUndoRedoException) IOException(java.io.IOException) RcvrImportException(edu.cmu.cs.hcii.cogtool.util.RcvrImportException) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) RcvrOutOfMemoryException(edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException) FileNotFoundException(java.io.FileNotFoundException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) SAXException(org.xml.sax.SAXException) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrIOTempException(edu.cmu.cs.hcii.cogtool.util.RcvrIOTempException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedWriter(java.io.BufferedWriter) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Example 4 with RcvrIOSaveException

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;
        }
    };
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) IOException(java.io.IOException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) RcvrOutOfMemoryException(edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) SAXException(org.xml.sax.SAXException) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrUIException(edu.cmu.cs.hcii.cogtool.util.RcvrUIException) IOException(java.io.IOException) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedWriter(java.io.BufferedWriter) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Example 5 with RcvrIOSaveException

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);
    }
}
Also used : RcvrCannotUndoRedoException(edu.cmu.cs.hcii.cogtool.util.RcvrCannotUndoRedoException) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)

Aggregations

RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)7 RcvrIOSaveException (edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)7 IOException (java.io.IOException)7 File (java.io.File)6 FileOutputStream (java.io.FileOutputStream)4 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)3 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)3 RcvrCannotUndoRedoException (edu.cmu.cs.hcii.cogtool.util.RcvrCannotUndoRedoException)3 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)3 PrintWriter (java.io.PrintWriter)3 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)2 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)2 RcvrClipboardException (edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException)2 RcvrImageException (edu.cmu.cs.hcii.cogtool.util.RcvrImageException)2 RcvrOutOfMemoryException (edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException)2 RcvrUnimplementedFnException (edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException)2 RecoverableException (edu.cmu.cs.hcii.cogtool.util.RecoverableException)2 BufferedWriter (java.io.BufferedWriter)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2