Search in sources :

Example 51 with IListenerAction

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

Example 52 with IListenerAction

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

the class ProjectController method createDemoteTaskAction.

protected IListenerAction createDemoteTaskAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            TaskSelectionState seln = (TaskSelectionState) prms;
            AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
            if ((tasks == null) || (tasks.length == 0)) {
                interaction.protestNoSelection();
                return false;
            }
            List<String> errors = new ArrayList<String>();
            String editLabel = (tasks.length > 1) ? DEMOTE_TASKS : DEMOTE_TASK;
            CompoundUndoableEdit editSeq = new CompoundUndoableEdit(editLabel, ProjectLID.DemoteTask);
            for (AUndertaking task : tasks) {
                String demoteError = demoteTask(task, editSeq);
                if (demoteError != null) {
                    errors.add(demoteError);
                }
            }
            if (errors.size() > 0) {
                interaction.reportProblems(editLabel, errors);
            }
            if (editSeq.isSignificant()) {
                editSeq.end();
                undoMgr.addEdit(editSeq);
            }
            return true;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskSelectionState(edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState) ArrayList(java.util.ArrayList) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)

Example 53 with IListenerAction

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

the class ProjectController method createExportDictionaryAction.

protected IListenerAction createExportDictionaryAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            ProjectSelectionState seln = (ProjectSelectionState) actionParms;
            Design design = seln.getSelectedDesign();
            if (design == null) {
                interaction.reportProblem("Export Dictionary", "No design is selected");
                return false;
            }
            ISimilarityDictionary dict = (ISimilarityDictionary) design.getAttribute(WidgetAttributes.DICTIONARY_ATTR);
            if (dict == null) {
                interaction.reportProblem("Export Dictionary", "No dictionary exists for the selected design");
                return false;
            }
            return DictionaryEditorCmd.exportDictionaryToCSV(dict, interaction);
        }
    };
}
Also used : 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) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) ISimilarityDictionary(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary)

Example 54 with IListenerAction

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

the class ProjectController method createImportAction.

protected IListenerAction createImportAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            boolean computeScripts = CogToolPref.COMPSCR.getBoolean();
            if (importFile == null) {
                importFile = interaction.selectXMLFile(true, null);
            } else {
                computeScripts = importFileComputes;
            }
            if (importFile == null) {
                return false;
            }
            CompoundUndoableEdit editSeq = new CompoundUndoableEdit(importXMLPresentation, ProjectLID.ImportXML);
            Map<Design, Collection<Demonstration>> parsedDesigns = null;
            Set<AUndertaking> newUndertakings = null;
            TaskParent parent = project;
            try {
                if (CogToolPref.HCIPA.getBoolean()) {
                    TaskGroup importGroup = new TaskGroup(importFile.getName(), GroupNature.SUM);
                    parent = importGroup;
                    project.addUndertaking(importGroup);
                    editSeq.addEdit(createNewTaskUndo(project, Project.AT_END, importGroup, ProjectLID.ImportXML, importXMLPresentation));
                }
                ImportCogToolXML importer = new ImportCogToolXML();
                if (!importer.importXML(importFile, parent, MODELGEN_ALG) || (importer.getDesigns().size() == 0)) {
                    List<String> errors = importer.getObjectFailures();
                    if ((errors != null) && (errors.size() > 0)) {
                        interaction.reportProblems(importXMLPresentation, errors);
                    } else {
                        interaction.reportProblem(importXMLPresentation, xmlParseFailed);
                    }
                    return false;
                }
                List<String> warnings = importer.getGenerationWarnings();
                if (warnings.size() > 0) {
                    interaction.reportWarnings(importXMLPresentation, warnings);
                }
                parsedDesigns = importer.getDesigns();
                newUndertakings = importer.getNewUndertakings();
            } catch (ImportCogToolXML.ImportFailedException ex) {
                throw new RcvrXMLParsingException("Missing XML component", ex);
            } catch (GraphicsUtil.ImageException ex) {
                throw new RcvrImageException("Image error during loading XML", ex);
            } catch (IOException ex) {
                throw new RcvrXMLParsingException("IO error loading XML", ex);
            } catch (SAXException ex) {
                throw new RcvrXMLParsingException("Error parsing XML", ex);
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                importFile = null;
                importFileComputes = false;
            }
            for (AUndertaking u : newUndertakings) {
                parent.addUndertaking(u);
                final AUndertaking uu = u;
                final TaskParent pp = parent;
                editSeq.addEdit(new AUndoableEdit(ProjectLID.ImportXML) {

                    @Override
                    public void redo() {
                        super.redo();
                        pp.addUndertaking(uu);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        pp.removeUndertaking(uu);
                    }
                });
            }
            Iterator<Map.Entry<Design, Collection<Demonstration>>> designDemos = parsedDesigns.entrySet().iterator();
            while (designDemos.hasNext()) {
                Map.Entry<Design, Collection<Demonstration>> entry = designDemos.next();
                importDesign(parent, (DesignSelectionState) prms, entry.getKey(), entry.getValue(), newUndertakings, editSeq);
            }
            editSeq.end();
            undoMgr.addEdit(editSeq);
            if (computeScripts) {
                //compute predictions for imported project
                ProjectContextSelectionState seln = new ProjectContextSelectionState(project);
                seln.addSelectedDesigns(project.getDesigns());
                ui.selectAllTasks();
                recomputeScripts(seln);
                ui.deselectAllTasks();
            }
            return true;
        }
    };
}
Also used : ProjectContextSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectContextSelectionState) SAXException(org.xml.sax.SAXException) Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) URLCrawlEntry(edu.cmu.cs.hcii.cogtool.model.URLCrawlEntry) ImportCogToolXML(edu.cmu.cs.hcii.cogtool.model.ImportCogToolXML) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) Collection(java.util.Collection) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) RcvrXMLParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrXMLParsingException) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) Map(java.util.Map) HashMap(java.util.HashMap)

Example 55 with IListenerAction

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

the class SEDemoController method createInsertSelfTransitionAction.

protected IListenerAction createInsertSelfTransitionAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            SEDemoUI.SelfTransition prms = (SEDemoUI.SelfTransition) actionParms;
            if (prms.target != null) {
                TaskApplication taskApp = script.getDemonstration().getTaskApplication();
                Set<DeviceType> deviceTypeSet = taskApp.getDesign().getDeviceTypes();
                int deviceTypes = DeviceType.buildDeviceSet(deviceTypeSet);
                int limitMode = ActionProperties.determineChangeActionMode(prms.target);
                AAction action = prms.action;
                if (prms.action == null) {
                    properties.resetValues();
                    properties.setInitialActionType(prms.target, deviceTypeSet);
                    if (!interaction.determineNewAction(properties, deviceTypes, limitMode, L10N.get("DE.SetActionType", "Set Action Type"))) {
                        return false;
                    }
                    action = EditActionCmd.buildActionFromProperties(properties, deviceTypes, limitMode, interaction);
                    if (action == null) {
                        return false;
                    }
                }
                action = EditActionCmd.ensureActionIsUnique(prms.target, action, properties, deviceTypes, limitMode, null, interaction);
                if (action == null) {
                    return false;
                }
                return performSelfTransition(prms.selection, prms.target, action, properties.delayInSecs, properties.delayLabel);
            }
            return false;
        }
    };
}
Also used : DeviceType(edu.cmu.cs.hcii.cogtool.model.DeviceType) SEDemoUI(edu.cmu.cs.hcii.cogtool.ui.SEDemoUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) AAction(edu.cmu.cs.hcii.cogtool.model.AAction)

Aggregations

IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)94 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)30 Design (edu.cmu.cs.hcii.cogtool.model.Design)29 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)29 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)23 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)23 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)21 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)19 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)17 IOException (java.io.IOException)14 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)13 TaskSelectionState (edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState)12 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)10 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)10 DesignSelectionState (edu.cmu.cs.hcii.cogtool.ui.DesignSelectionState)10 FrameEditorSelectionState (edu.cmu.cs.hcii.cogtool.ui.FrameEditorSelectionState)9 FrameSelectionState (edu.cmu.cs.hcii.cogtool.ui.FrameSelectionState)9 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)8 FrameEditorUI (edu.cmu.cs.hcii.cogtool.ui.FrameEditorUI)8 File (java.io.File)8