Search in sources :

Example 11 with AUndertaking

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

the class ProjectController method createNewTaskAction.

// Action for NewTask
protected IListenerAction createNewTaskAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            TaskSelectionState selection = (TaskSelectionState) prms;
            // Figure out what the selection is
            AUndertaking[] selectedTasks = selection.getSelectedTasks(TaskSelectionState.ORDER_SELECTION);
            Task newTask = null;
            if ((selectedTasks == null) || (selectedTasks.length == 0)) {
                // No task selection -- add a new root task
                String newTaskName = computeNewTaskName(project, DEFAULT_TASK_PREFIX);
                newTask = addTask(project, project.getUndertakings().size(), newTaskName);
            } else {
                // At least one selected task: add a new sibling after
                // Multiple selected tasks:
                //   if all tasks have same parent,
                //          add new sibling after last selection
                //   if tasks do not have same parent,
                //          add new root task after last selected root
                //          but, if no selected root, add new root at end
                // Get parent
                TaskGroup parent = selection.getSelectedTaskParent();
                if (parent == null) {
                    String newTaskName = computeNewTaskName(project, DEFAULT_TASK_PREFIX);
                    newTask = addTask(project, findLastSelectedRoot(selectedTasks), newTaskName);
                } else {
                    String newTaskName = computeNewTaskName(parent, DEFAULT_TASK_PREFIX);
                    AUndertaking last = selectedTasks[selectedTasks.length - 1];
                    newTask = addTask(parent, parent.getUndertakings().indexOf(last) + 1, newTaskName);
                }
            }
            // Task is automatically selected when added -- rename
            // Cannot put this into the ui because of undo/redo
            ui.initiateTaskRename(newTask);
            return true;
        }
    };
}
Also used : Task(edu.cmu.cs.hcii.cogtool.model.Task) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskSelectionState(edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup)

Example 12 with AUndertaking

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

the class ProjectController method createChangeTaskPositionAction.

protected IListenerAction createChangeTaskPositionAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            ProjectUI.ChangeTaskPositionParms prms = (ProjectUI.ChangeTaskPositionParms) actionParms;
            if ((prms.placeBeforeTask != null) && prms.tasks.isInSelectedHierarchy(prms.placeBeforeTask)) {
                // Nothing to do
                return false;
            }
            if ((prms.tasks == null) || (prms.tasks.getSelectedTaskCount() == 0)) {
                interaction.protestNoSelection();
                return false;
            }
            final AUndertaking[] selectedTasks = prms.tasks.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
            final TaskParent[] oldParents = new TaskParent[selectedTasks.length];
            final int[] indexes = new int[selectedTasks.length];
            final String[] oldNames = new String[selectedTasks.length];
            final TaskParent placeBeforeTaskParent = (prms.placeBeforeTask != null) ? project.getTaskParent(prms.placeBeforeTask) : project;
            final List<AUndertaking> siblings = placeBeforeTaskParent.getUndertakings();
            // If the place-before-task is selected, find the next
            // sibling that is not selected.
            AUndertaking placeBefore = prms.placeBeforeTask;
            if (prms.tasks.isTaskSelected(placeBefore)) {
                int siblingIndex = siblings.indexOf(placeBefore);
                int siblingCount = siblings.size();
                while (++siblingIndex < siblingCount) {
                    placeBefore = siblings.get(siblingIndex);
                    if (!prms.tasks.isTaskSelected(placeBefore)) {
                        break;
                    }
                }
                if (siblingIndex >= siblingCount) {
                    placeBefore = null;
                }
            }
            // Remove first so that the atIndex computation works!
            removeChildTasks(selectedTasks, oldParents, indexes);
            final int atIndex = (placeBefore != null) ? siblings.indexOf(placeBefore) : siblings.size();
            // Add each selected task as siblings before the given task
            for (int i = 0; i < selectedTasks.length; i++) {
                oldNames[i] = selectedTasks[i].getName();
                String newName = NamedObjectUtil.makeNameUnique(oldNames[i], siblings);
                if (!newName.equals(oldNames[i])) {
                    selectedTasks[i].setName(newName);
                }
                placeBeforeTaskParent.addUndertaking(atIndex + i, selectedTasks[i]);
            }
            // Create undo/redo step and add to undo manager
            IUndoableEdit edit = new AUndoableEdit(ProjectLID.ChangeTaskPosition) {

                @Override
                public String getPresentationName() {
                    return (selectedTasks.length > 1) ? MOVE_TASKS : MOVE_TASK;
                }

                @Override
                public void redo() {
                    super.redo();
                    removeChildTasks(selectedTasks, null, null);
                    // before the given task
                    for (int i = 0; i < selectedTasks.length; i++) {
                        String newName = NamedObjectUtil.makeNameUnique(oldNames[i], siblings);
                        if (!newName.equals(oldNames[i])) {
                            selectedTasks[i].setName(newName);
                        }
                        placeBeforeTaskParent.addUndertaking(atIndex + i, selectedTasks[i]);
                    }
                }

                @Override
                public void undo() {
                    super.undo();
                    removeChildTasks(selectedTasks, null, null);
                    // Un-delete children; IMPORTANT: reverse order!
                    for (int i = selectedTasks.length - 1; 0 <= i; i--) {
                        if (!oldNames[i].equals(selectedTasks[i].getName())) {
                            selectedTasks[i].setName(oldNames[i]);
                        }
                        // Add to old parent at old index
                        oldParents[i].addUndertaking(indexes[i], selectedTasks[i]);
                    }
                }
            };
            undoMgr.addEdit(edit);
            return true;
        }
    };
}
Also used : DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) ProjectUI(edu.cmu.cs.hcii.cogtool.ui.ProjectUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 13 with AUndertaking

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

the class ProjectController method createPromoteTaskAction.

protected IListenerAction createPromoteTaskAction() {
    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) ? PROMOTE_TASKS : PROMOTE_TASK;
            CompoundUndoableEdit editSeq = new CompoundUndoableEdit(editLabel, ProjectLID.PromoteTask);
            for (AUndertaking task : tasks) {
                String promoteError = promoteTask(task, ProjectLID.PromoteTask, editSeq);
                if (promoteError != null) {
                    errors.add(promoteError);
                }
            }
            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 14 with AUndertaking

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

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

the class ProjectController method addTaskResults.

// Utility to help both copyResults and exportResultsToCSV
protected void addTaskResults(Iterator<AUndertaking> tasks, StringBuilder buffer, String separator) {
    while (tasks.hasNext()) {
        AUndertaking t = tasks.next();
        String[] resultStrs = ResultDisplayPolicy.getTaskRowStrings(project, t, ResultDisplayPolicy.NO_SECS);
        if (resultStrs.length > 0) {
            buffer.append(CSVSupport.quoteCell(resultStrs[0]));
            for (int i = 1; i < resultStrs.length; i++) {
                buffer.append(separator);
                buffer.append(CSVSupport.quoteCell(resultStrs[i]));
            }
        }
        CSVSupport.addLineEnding(buffer);
        if (t.isTaskGroup()) {
            addTaskResults(((TaskGroup) t).getUndertakings().iterator(), buffer, separator);
        }
    }
}
Also used : AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Aggregations

AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)89 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)38 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)35 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)30 Design (edu.cmu.cs.hcii.cogtool.model.Design)28 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)24 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)18 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)17 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)14 Script (edu.cmu.cs.hcii.cogtool.model.Script)13 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)13 IOException (java.io.IOException)12 TaskSelectionState (edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState)11 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)10 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)10 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)10 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)9 TaskParent (edu.cmu.cs.hcii.cogtool.model.TaskParent)9 TreeItem (org.eclipse.swt.widgets.TreeItem)9 ArrayList (java.util.ArrayList)7