Search in sources :

Example 1 with RcvrUnimplementedFnException

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

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

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

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

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

the class ProjectController method createEditACTRModelAction.

// TODO it is a crock that we're cloning this stuff; when we have time
//      we should design a shared mechanism to be used by all back ends
protected IListenerAction createEditACTRModelAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return ProjectSelectionState.class;
        }

        public boolean performAction(Object actionParms) {
            ProjectSelectionState selState = (ProjectSelectionState) actionParms;
            Design design = selState.getSelectedDesign();
            AUndertaking[] tasks = selState.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION);
            for (AUndertaking task : tasks) {
                TaskApplication ta = project.getTaskApplication(task, design);
                if (ta == null) {
                    return false;
                }
                // find the script
                Script script = ta.getScript(KLMCognitiveGenerator.ONLY);
                if (script == null) {
                    return false;
                }
                // get the resourcePath
                String resourcePath = script.getAssociatedPath();
                if (resourcePath == null) {
                    return false;
                }
                try {
                    FileUtil.editFile(new File(resourcePath));
                } catch (UnsupportedOperationException ex) {
                    throw new RcvrUnimplementedFnException("Editing a file is not implemented", ex);
                } catch (IOException ex) {
                    throw new RcvrIOException("Problem when trying to edit a file.", ex);
                }
            }
            return true;
        }
    };
}
Also used : Script(edu.cmu.cs.hcii.cogtool.model.Script) 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) 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)

Aggregations

RcvrUnimplementedFnException (edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException)6 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)5 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)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 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)4 File (java.io.File)4 IOException (java.io.IOException)4 ACTRPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo)3 ITraceWindow (edu.cmu.cs.hcii.cogtool.ui.Interaction.ITraceWindow)2 RcvrComputationException (edu.cmu.cs.hcii.cogtool.util.RcvrComputationException)2 RcvrIOSaveException (edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)1 RcvrParsingException (edu.cmu.cs.hcii.cogtool.util.RcvrParsingException)1 FileOutputStream (java.io.FileOutputStream)1 PrintWriter (java.io.PrintWriter)1