Search in sources :

Example 1 with RcvrIOException

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

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

the class ProjectController method createGenerateACTRModelAction.

// createImportHumanCSVFileAction
protected IListenerAction createGenerateACTRModelAction() {
    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);
            for (AUndertaking task : tasks) {
                TaskApplication ta = project.getTaskApplication(task, design);
                Script s = DemoStateManager.ensureScript(ta, KLMCognitiveGenerator.ONLY);
                // TODO There's too much algorithm specific code
                //      in here; but for now it seems the expedient
                //      thing to do -- all this needs to be thought
                //      through for all back ends, and restructured
                String path = s.getAssociatedPath();
                String filename = null;
                if (path == null) {
                    filename = design.getName() + "-" + task.getName();
                } else {
                    filename = (new File(path)).getName();
                    if ((filename != null) && filename.endsWith(CogToolFileTypes.LISP_FILE_EXT)) {
                        filename = filename.substring(0, filename.length() - CogToolFileTypes.LISP_FILE_EXT.length());
                    }
                }
                File file = interaction.selectExportLocation(filename, CogToolFileTypes.LISP_FILE_EXT);
                if (file == null) {
                    return false;
                }
                s.setAssociatedPath(file.getAbsolutePath());
                // so we have to delete it.
                if (file.length() == 0) {
                    file.delete();
                }
                try {
                    IPredictionAlgo taAlg = ta.determineActiveAlgorithm(project);
                    if (!(taAlg instanceof ACTRPredictionAlgo)) {
                        throw new RcvrIllegalStateException("Can't generate ACT-R Model from a non ACT-R task.");
                    }
                    ACTRPredictionAlgo algo = (ACTRPredictionAlgo) taAlg;
                    algo.outputModel(design, task, s.getDemonstration().getStartFrame(), s, file, null);
                } catch (UnsupportedOperationException ex) {
                    throw new RcvrUnimplementedFnException(ex);
                } catch (IOException ex) {
                    throw new RcvrIOException(("IOException generating model file for task " + task.getName() + " in design " + design.getName()), ex);
                }
            }
            return false;
        }
    };
}
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) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) 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) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) File(java.io.File) ACTRPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo)

Example 3 with RcvrIOException

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

Example 4 with RcvrIOException

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

the class DefaultController method saveAs.

/**
     * Perform a Save As... operation, prompting for a save location.
     *
     * @return true if file was saved, false otherwise
     * @throws RcvrIOException if the save operation fails
     */
protected boolean saveAs() {
    Interaction stdInteraction = getUI().getStandardInteraction();
    try {
        boolean nameIsInUse;
        File saveLoc;
        do {
            // Request a new file name, using the project's current name
            // as the default.
            // TODO: It is "evil" that the test for existing files is
            //       hidden within this call; separate at some point (mlh)
            saveLoc = stdInteraction.selectFileDest(project.getName());
            // If the save dialog was canceled, do nothing.
            if (saveLoc == null) {
                return false;
            }
            // If saveLoc is already open, refuse to save there.
            nameIsInUse = persist.isRegistered(saveLoc, project);
            if (nameIsInUse) {
                switch(stdInteraction.protestBeingEdited(saveLoc)) {
                    case Interaction.SAVE:
                        {
                            nameIsInUse = false;
                            break;
                        }
                    case Interaction.NO_SAVE:
                        {
                            // Simply loop to ask for a new name
                            break;
                        }
                    case Interaction.CANCEL:
                        {
                            return false;
                        }
                }
            }
        } while (nameIsInUse);
        CogToolPref.setRecent(saveLoc.getCanonicalPath());
        // Set the title BEFORE saving the project.
        String name = saveLoc.getName();
        // Remove the .cgt, if it is there.
        int periodIndex = name.lastIndexOf('.');
        if (periodIndex > 0) {
            name = name.substring(0, periodIndex);
        }
        project.setName(name);
        // Save to the selected location.
        project.setBuildVersion(CogTool.getVersion());
        persist.save(project, saveLoc);
        // 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) Interaction(edu.cmu.cs.hcii.cogtool.ui.Interaction) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) File(java.io.File) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)

Example 5 with RcvrIOException

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

the class ImageRenderer method getImage.

/**
     * Checks to see if this image has already been loaded; if so, returns it
     * from the cache of loaded images in imageRegistry. If not, loads it from file
     * and puts it in the cache.
     * @param path The path from which this image can be loaded.
     * @return Image that is located at path
     */
public static Image getImage(String path) {
    Image pic = imageRegistry.get(path);
    if (pic == null) {
        pic = GraphicsUtil.getImageFromResource(path);
        if (pic == null) {
            throw new RcvrIOException("No image found at " + path);
        }
        imageRegistry.put(path, pic);
    }
    return pic;
}
Also used : Image(org.eclipse.swt.graphics.Image) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException)

Aggregations

RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)7 IOException (java.io.IOException)6 File (java.io.File)5 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)4 Design (edu.cmu.cs.hcii.cogtool.model.Design)4 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)4 Script (edu.cmu.cs.hcii.cogtool.model.Script)4 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)4 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)4 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)4 RcvrUnimplementedFnException (edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException)4 ACTRPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo)3 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)3 RcvrIOSaveException (edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)3 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)3 RcvrCannotUndoRedoException (edu.cmu.cs.hcii.cogtool.util.RcvrCannotUndoRedoException)2 Interaction (edu.cmu.cs.hcii.cogtool.ui.Interaction)1 FileOutputStream (java.io.FileOutputStream)1 PrintWriter (java.io.PrintWriter)1 Matcher (java.util.regex.Matcher)1