Search in sources :

Example 1 with ACTRPredictionAlgo

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

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

use of edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo in project cogtool by cogtool.

the class ProjectView method getContextMenuForIntersection.

public Menu getContextMenuForIntersection(Project project, AUndertaking undertaking, Design design) {
    // Get the task application being clicked on
    TaskApplication ta = project.getTaskApplication(undertaking, design);
    boolean hasExternalFile = false;
    boolean isVisualizable = false;
    Set<IPredictionAlgo> algsWithResults = new HashSet<IPredictionAlgo>();
    IPredictionAlgo algo = null;
    if (ta != null) {
        // TODO: mlh make access to model gen algo generic
        Script script = ta.getScript(KLMCognitiveGenerator.ONLY);
        if (script != null) {
            if (script.getAssociatedPath() != null) {
                hasExternalFile = true;
            }
            // as well as the list of algorithms with results in the ta
            Iterator<IPredictionAlgo> algsIt = ta.getPredictionAlgs(script.getModelGenerator());
            while (algsIt.hasNext()) {
                IPredictionAlgo i = algsIt.next();
                if (i != null) {
                    algsWithResults.add(i);
                }
            }
        }
        // And whether or not ta has a visualizable result
        isVisualizable = ta.hasResultSteps(ta.determineActiveAlgorithm(project));
        algo = ta.getActiveAlgorithm();
    }
    //      that the following test will have to suffice.
    if (algo == null) {
        algo = ACTR6PredictionAlgo.ONLY;
    }
    contextMenus.setContextSelection(View.CONTEXT);
    if (undertaking.isTaskGroup()) {
        if (CogToolPref.HCIPA.getBoolean()) {
            return contextMenus.getMenu(HCIPA_GROUP_CELL_MENU);
        }
        if (CogToolPref.RESEARCH.getBoolean()) {
            return contextMenus.getMenu(RESEARCH_GROUP_CELL_MENU);
        }
        return contextMenus.getMenu(GROUP_CELL_MENU);
    }
    // create cascading menus for algorithm (now called "Usability Metric" BEJoh 25mar2011)
    MenuItemDefinition algCascade = null;
    if (CogToolPref.RESEARCH.getBoolean()) {
        List<MenuItemDefinition> algList = new ArrayList<MenuItemDefinition>();
        algList.add(ALG_ACTR6_ITEM);
        algList.add(ALG_SNIFACT_ITEM);
        if (algsWithResults.contains(HumanDataAlgo.ONLY)) {
            algList.add(ALG_HUMAN_DATA_ITEM);
        }
        algCascade = new CascadingMenuItemDefinition(L10N.get("MI.PV.AlgorithmType", //                                                        "Algorithm Type"),
        "Usability Metric"), algList.toArray(new MenuItemDefinition[algList.size()]));
        // test for active algorithm
        // TODO Do we really want to use this idiom? It seems nauseating on
        //      at least three counts:
        //      1) setting a field of a constant is confusing--no one
        //         looking at these constants elsewhere is going to be
        //         expecting them to mutate out from under them
        //      2) while it does seem unlikely re-entrancy will in practice
        //         be an issue here, using application-wide globals as a way
        //         of passing essentially local information is suspect
        //      3) it's a dangerously fragile idiom--for any of these
        //         fields you ever fiddle, you really, truly HAVE to set them to
        //         either true or false every time--can't count on any defaults,
        //         or you'll be using stale data from the previous time around
        ALG_ACTR6_ITEM.selectedInitially = (algo == ACTR6PredictionAlgo.ONLY);
        ALG_HUMAN_DATA_ITEM.selectedInitially = (algo == HumanDataAlgo.ONLY);
        ALG_SNIFACT_ITEM.selectedInitially = (algo == SNIFACTPredictionAlgo.ONLY);
        // TODO speaking of "it's a dangerously fragile idiom," the following
        //      code wasn't originally correct, as nothing ever got
        //      deselected, demonstrating my point #3, above!
        //      [while this is now fixed, I believe, I'm leaving a TODO here
        //       as a sort of continuation of the one above to which it refers]
        // test for background run
        Boolean computeInBkg = null;
        if (ta != null) {
            computeInBkg = ta.getComputeInBackground();
        }
        //      we do the following test will have to suffice.
        if (computeInBkg == null) {
            computeInBkg = TaskApplication.RUN_IN_FOREGROUND;
        }
        ALG_IN_BACKGROUND.selectedInitially = (computeInBkg == TaskApplication.RUN_IN_BACKGROUND);
        ALG_IN_FOREGROUND.selectedInitially = (computeInBkg == TaskApplication.RUN_IN_FOREGROUND);
    }
    // build custom menu
    List<MenuItemDefinition> taskCellContextMenuDef = new ArrayList<MenuItemDefinition>();
    SHOW_MODEL_VISUALIZATION.enabledInitially = isVisualizable;
    taskCellContextMenuDef.add(SHOW_MODEL_VISUALIZATION);
    taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
    taskCellContextMenuDef.add(EDIT_SCRIPT);
    IPredictionAlgo a = (algo != null ? algo : project.getDefaultAlgo());
    if (a instanceof ACTRPredictionAlgo || a instanceof SNIFACTPredictionAlgo) {
        if (CogToolPref.RESEARCH.getBoolean()) {
            // TODO it is disgusting the way this stuff is all cloned; when
            //      we have time we need to think through a consistent design
            //      to be shared across all backends
            EDIT_ACTR_MODEL_ITEM.enabledInitially = hasExternalFile;
            GENERATE_ACTR_MODEL_ITEM.enabledInitially = (ta != null);
            taskCellContextMenuDef.add(EDIT_ACTR_MODEL_ITEM);
            taskCellContextMenuDef.add(GENERATE_ACTR_MODEL_ITEM);
        }
        taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
    }
    taskCellContextMenuDef.add(RECOMPUTE_SCRIPT);
    if (algCascade != null) {
        taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
        taskCellContextMenuDef.add(algCascade);
        taskCellContextMenuDef.add(EXECUTE_CASCADE);
    }
    taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
    taskCellContextMenuDef.add(EDIT_DESIGN);
    taskCellContextMenuDef.add(RENAME_DESIGN);
    taskCellContextMenuDef.add(DUPLICATE_DESIGN);
    taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
    if (CogToolPref.RESEARCH.getBoolean()) {
        taskCellContextMenuDef.add(IMPORT_HUMAN_CSV);
        taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
        taskCellContextMenuDef.add(MenuFactory.EXPORT_DESIGN_TO_HTML);
    }
    taskCellContextMenuDef.add(MenuFactory.EXPORT_SCRIPT_TO_CSV);
    taskCellContextMenuDef.add(MenuFactory.EXPORT_RESULTS_TO_CSV);
    if (CogToolPref.RESEARCH.getBoolean()) {
        taskCellContextMenuDef.add(EXPORT_TO_XML);
        taskCellContextMenuDef.add(EXPORT_ACTR_MODEL);
        taskCellContextMenuDef.add(EXPORT_TRACELINES);
        taskCellContextMenuDef.add(EXPORT_FOR_SANLAB);
        if (CogToolPref.HCIPA.getBoolean()) {
            taskCellContextMenuDef.add(MenuFactory.EXPORT_TO_HCIPA);
        }
        taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
        taskCellContextMenuDef.add(DISPLAY_TRACELINES);
    }
    if (CogToolPref.RESEARCH.getBoolean()) {
        taskCellContextMenuDef.add(MenuUtil.SEPARATOR);
        taskCellContextMenuDef.add(GENERATE_DICT_ENTRIES);
        taskCellContextMenuDef.add(EDIT_DICT);
    }
    return contextMenus.createDynamicMenu(taskCellContextMenuDef);
}
Also used : Script(edu.cmu.cs.hcii.cogtool.model.Script) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) ArrayList(java.util.ArrayList) CascadingMenuItemDefinition(edu.cmu.cs.hcii.cogtool.util.MenuUtil.CascadingMenuItemDefinition) SimpleMenuItemDefinition(edu.cmu.cs.hcii.cogtool.util.MenuUtil.SimpleMenuItemDefinition) MenuItemDefinition(edu.cmu.cs.hcii.cogtool.util.MenuUtil.MenuItemDefinition) CascadingMenuItemDefinition(edu.cmu.cs.hcii.cogtool.util.MenuUtil.CascadingMenuItemDefinition) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) SNIFACTPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.SNIFACTPredictionAlgo) ACTRPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo) HashSet(java.util.HashSet)

Example 4 with ACTRPredictionAlgo

use of edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo in project cogtool by cogtool.

the class ProjectController method createExportActrModelFile.

// Action for ExportDesignFiles
// XXX: does this really belong in ProjectController? It seems like
//      something that's tied to whichever backend we're really using,
//      and so should be somewhere else
protected IListenerAction createExportActrModelFile() {
    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;
            }
            // Ask user for location of saved file.
            File exportFile = interaction.selectExportLocation("Exported ACT-R Model", CogToolFileTypes.LISP_FILE_EXT);
            // User canceled
            if (exportFile == null) {
                return 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 (" + 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 ACT-R Model 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 model file for task " + task.getName() + " in design " + design.getName()), ex);
                    }
                }
            }
            return true;
        }
    };
}
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)

Aggregations

ACTRPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo)4 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)4 Script (edu.cmu.cs.hcii.cogtool.model.Script)4 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)4 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)3 Design (edu.cmu.cs.hcii.cogtool.model.Design)3 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)3 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)3 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)3 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)3 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)3 RcvrUnimplementedFnException (edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException)3 File (java.io.File)3 IOException (java.io.IOException)3 SNIFACTPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.SNIFACTPredictionAlgo)1 CascadingMenuItemDefinition (edu.cmu.cs.hcii.cogtool.util.MenuUtil.CascadingMenuItemDefinition)1 MenuItemDefinition (edu.cmu.cs.hcii.cogtool.util.MenuUtil.MenuItemDefinition)1 SimpleMenuItemDefinition (edu.cmu.cs.hcii.cogtool.util.MenuUtil.SimpleMenuItemDefinition)1 RcvrIOSaveException (edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)1 FileOutputStream (java.io.FileOutputStream)1