Search in sources :

Example 1 with RcvrIllegalStateException

use of edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException 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 RcvrIllegalStateException

use of edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException 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 3 with RcvrIllegalStateException

use of edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException in project cogtool by cogtool.

the class SNIFACTCmd method computeInBackground.

public static SNIFACTExecContext computeInBackground(Project project, Design design, Interaction interaction, TaskGroup group, SNIFACTParameters parms) {
    isComputing = true;
    try {
        SNIFACTAnalysisWorkThread workThread = new SNIFACTAnalysisWorkThread(SNIFACTPredictionAlgo.ONLY, project, design, null, interaction, group, parms);
        if (SNIFACTPredictionAlgo.exportCTEModelFile != null) {
            return null;
        }
        ITraceWindow traceWin = interaction.createTraceWindow("Computation trace", workThread, "Trace output: stdout (top) and stderr (bottom)");
        workThread.setTraceWindow(traceWin);
        ThreadManager.startNewThread(workThread);
        return workThread.getExecContext();
    } catch (IPredictionAlgo.ComputationException ex) {
        throw new RcvrComputationException(ex);
    } catch (IllegalStateException ex) {
        throw new RcvrIllegalStateException(ex);
    } catch (UnsupportedOperationException ex) {
        throw new RcvrUnimplementedFnException(ex);
    } catch (Exception ex) {
        throw new RcvrComputationException(ex);
    }
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) ITraceWindow(edu.cmu.cs.hcii.cogtool.ui.Interaction.ITraceWindow) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) RcvrComputationException(edu.cmu.cs.hcii.cogtool.util.RcvrComputationException) RcvrComputationException(edu.cmu.cs.hcii.cogtool.util.RcvrComputationException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)

Example 4 with RcvrIllegalStateException

use of edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException in project cogtool by cogtool.

the class ComputePredictionCmd method computeInBackground.

/**
     * Perform the analysis in the background.  Set the result when done.
     */
public static APredictionResult computeInBackground(IPredictionAlgo computeAlg, Script s, Interaction interact) {
    try {
        DefaultAnalysisWorkThread workThread = new DefaultAnalysisWorkThread(computeAlg, s, null, interact);
        ITraceWindow traceWin = interact.createTraceWindow("Computation trace", workThread, "Trace output: stdout (top) and stderr (bottom)");
        workThread.setTraceWindow(traceWin);
        ThreadManager.startNewThread(workThread);
        return workThread.getResultProxy();
    } catch (IPredictionAlgo.ComputationException ex) {
        throw new RcvrComputationException(ex);
    } catch (IllegalStateException ex) {
        throw new RcvrIllegalStateException(ex);
    } catch (UnsupportedOperationException ex) {
        throw new RcvrUnimplementedFnException(ex);
    } catch (Exception ex) {
        throw new RcvrComputationException(ex);
    }
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) ITraceWindow(edu.cmu.cs.hcii.cogtool.ui.Interaction.ITraceWindow) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) RcvrComputationException(edu.cmu.cs.hcii.cogtool.util.RcvrComputationException) RcvrComputationException(edu.cmu.cs.hcii.cogtool.util.RcvrComputationException) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)

Example 5 with RcvrIllegalStateException

use of edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException 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)

Aggregations

RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)8 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)6 RcvrUnimplementedFnException (edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException)6 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)5 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)5 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)5 File (java.io.File)5 IOException (java.io.IOException)5 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)4 ACTRPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo)3 Design (edu.cmu.cs.hcii.cogtool.model.Design)3 Script (edu.cmu.cs.hcii.cogtool.model.Script)3 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)3 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)3 RcvrIOSaveException (edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)3 FileOutputStream (java.io.FileOutputStream)3 ITraceWindow (edu.cmu.cs.hcii.cogtool.ui.Interaction.ITraceWindow)2 RcvrClipboardException (edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException)2 RcvrComputationException (edu.cmu.cs.hcii.cogtool.util.RcvrComputationException)2 RcvrImageException (edu.cmu.cs.hcii.cogtool.util.RcvrImageException)2