Search in sources :

Example 11 with TaskApplication

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

the class ProjectController method regenerateScripts.

protected boolean regenerateScripts(AUndertaking task, Design design, DemoStateManager demoStateMgr, IUndoableEditSequence editSequence) {
    if (task.isTaskGroup()) {
        Iterator<AUndertaking> allTasks = ((TaskGroup) task).getUndertakings().iterator();
        CompoundUndoableEdit groupEditSeq = new CompoundUndoableEdit(REGENERATE_SCRIPTS, ProjectLID.RegenerateScript);
        while (allTasks.hasNext()) {
            if (!regenerateScripts(allTasks.next(), design, demoStateMgr, groupEditSeq)) {
                return false;
            }
        }
        if (groupEditSeq.isSignificant()) {
            groupEditSeq.end();
            editSequence.addEdit(groupEditSeq);
        }
        return true;
    }
    TaskApplication ta = project.getTaskApplication(task, design);
    if (ta != null) {
        Demonstration demo = ta.getDemonstration();
        return DemoScriptCmd.regenerateScripts(project, demo, demoStateMgr, interaction, editSequence);
    }
    return true;
}
Also used : AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration)

Example 12 with TaskApplication

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

the class ProjectController method computeSnifAct.

// assignActions
protected boolean computeSnifAct(Design design, AUndertaking task, IUndoableEditSequence editSequence, SNIFACTGroupParameters defaults) {
    // TODO: L10N required for error titles and messages.
    if (design == null) {
        interaction.reportProblem("SNIF-ACT", "No design is selected.");
        return false;
    }
    if (design.getFrames().size() == 0) {
        interaction.reportProblem("SNIF-ACT", "Selected design is empty.");
        return false;
    }
    if (task.isTaskGroup()) {
        // We can only recompute with this algorithm if the
        // group was generated by a previous SNIF-ACT
        // computation (and thus has the execution
        // context attribute)
        Object contextAttr = task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
        if (NullSafe.equals(contextAttr, WidgetAttributes.NO_CONTEXT)) {
            interaction.reportProblem("SNIF-ACT", "Can't recompute with this algorithm from this cell.");
            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;
    }
    List<Frame> sortedFrames = NamedObjectUtil.getSortedList(design.getFrames());
    SNIFACTParameters parms;
    int addGroupMode;
    boolean hasScript = false;
    final SNIFACTExecContext prevContext;
    if (task.isTaskGroup()) {
        // Since we got this far, we know the group already has
        // parameters associated with it, so use those values as
        // defaults in the dialog box
        prevContext = (SNIFACTExecContext) task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
        parms = prevContext.getParameters();
        addGroupMode = SNIFACTDialog.ENABLED;
    } else {
        // Otherwise, create a new set of default values for this
        // execution
        String defaultName = sortedFrames.get(0).getName();
        List<String> targets = new ArrayList<String>();
        targets.add(defaultName);
        parms = new SNIFACTParameters(task.getName(), NUM_SNIF_ACT_TRIALS, SNIF_ACT_K_VALUE, defaultName, targets, ITermSimilarity.ALL);
        addGroupMode = SNIFACTDialog.NONE;
        prevContext = null;
        TaskApplication ta = project.getTaskApplication(task, design);
        hasScript = (ta != null) && ta.hasScript();
    }
    SNIFACTGroupParameters groupParms = null;
    if (defaults == null) {
        groupParms = interaction.requestSNIFACTParameters(hasScript, sortedFrames, parms, dict.getAlgorithmsInUse(), addGroupMode);
    } else {
        groupParms = new SNIFACTGroupParameters(defaults.taskName, defaults.numRuns, defaults.kValue, defaults.startFrame, defaults.targetFrames, defaults.algorithm, ((addGroupMode != SNIFACTDialog.NONE) && defaults.addToGroup));
    }
    if (groupParms == null) {
        return false;
    }
    SNIFACTPredictionAlgo.ONLY.setParameters(groupParms);
    TaskParent parent = task.getTaskGroup();
    if (parent == null) {
        parent = project;
    }
    final TaskGroup group;
    if (groupParms.addToGroup) {
        // user wants to add new trial tasks to the same group
        group = (TaskGroup) task;
    } else {
        Collection<AUndertaking> siblings = parent.getUndertakings();
        group = new TaskGroup(SNIFACTCmd.getGroupName(groupParms, siblings), GroupNature.MEAN);
    }
    final SNIFACTExecContext context = SNIFACTCmd.computeInBackground(project, design, interaction, group, groupParms);
    if (context == null) {
        return true;
    }
    group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, context);
    CompoundUndoableEdit snifActEditSeq = new CompoundUndoableEdit(SNIFACT_COMPUTATION, ProjectLID.RecomputeScript);
    if (!task.isTaskGroup()) {
        snifActEditSeq.addEdit(HCIPACmd.replaceTask(project, parent, task, group, SNIFACT_COMPUTATION));
    } else {
        if (groupParms.addToGroup) {
            snifActEditSeq.addEdit(SNIFACTCmd.addTasksToGroup(project, group, context, SNIFACT_COMPUTATION));
        } else {
            snifActEditSeq.addEdit(SNIFACTCmd.addGroup(project, parent, group, SNIFACT_COMPUTATION));
        }
    }
    snifActEditSeq.addEdit(new AUndoableEdit(ProjectLID.RecomputeScript) {

        @Override
        public String getPresentationName() {
            return SNIFACT_COMPUTATION;
        }

        @Override
        public void redo() {
            super.redo();
            group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, context);
        }

        @Override
        public void undo() {
            super.undo();
            group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, prevContext);
        }
    });
    snifActEditSeq.end();
    if (editSequence != null) {
        editSequence.addEdit(snifActEditSeq);
    }
    return true;
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) ArrayList(java.util.ArrayList) SNIFACTGroupParameters(edu.cmu.cs.hcii.cogtool.model.SNIFACTPredictionAlgo.SNIFACTGroupParameters) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) SNIFACTParameters(edu.cmu.cs.hcii.cogtool.model.SNIFACTPredictionAlgo.SNIFACTParameters) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) SNIFACTExecContext(edu.cmu.cs.hcii.cogtool.model.SNIFACTExecContext) ISimilarityDictionary(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup)

Example 13 with TaskApplication

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

the class ProjectController method displayTraces.

protected void displayTraces(AUndertaking task, Design design) {
    if (task.isTaskGroup()) {
        Iterator<AUndertaking> allSubtasks = ((TaskGroup) task).getUndertakings().iterator();
        while (allSubtasks.hasNext()) {
            displayTraces(allSubtasks.next(), design);
        }
    } else {
        TaskApplication taskApp = project.getTaskApplication(task, design);
        if (taskApp != null) {
            StringBuilder labelText = new StringBuilder();
            Iterator<CognitiveModelGenerator> modelGens = taskApp.getModelGenerators();
            while (modelGens.hasNext()) {
                CognitiveModelGenerator modelGen = modelGens.next();
                Iterator<IPredictionAlgo> computeAlgs = taskApp.getPredictionAlgs(modelGen);
                while (computeAlgs.hasNext()) {
                    IPredictionAlgo computeAlg = computeAlgs.next();
                    APredictionResult result = taskApp.getResult(modelGen, computeAlg);
                    int resultState = result.getResultState();
                    if ((result != null) && ((resultState == APredictionResult.IS_COMPUTED) || (resultState == APredictionResult.COMPUTE_FAILED))) {
                        labelText.delete(0, labelText.length());
                        labelText.append(tracesWindowLabel);
                        labelText.append(": ");
                        labelText.append(project.getName());
                        labelText.append(" > ");
                        labelText.append(design.getName());
                        labelText.append(" > ");
                        labelText.append(task.getName());
                        String labelStr = labelText.toString();
                        ITraceWindow traceWin = interaction.createTraceWindow(labelStr + (OSUtils.MACOSX ? "" : UI.WINDOW_TITLE), null, labelStr);
                        traceWin.appendOutputLines(result.getTraceLines());
                        traceWin.scrollToTop();
                        traceWin.appendErrorLines(result.getErrorLines());
                        traceWin.scrollToTop();
                    }
                }
            }
        }
    }
}
Also used : IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) ITraceWindow(edu.cmu.cs.hcii.cogtool.ui.Interaction.ITraceWindow) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) APredictionResult(edu.cmu.cs.hcii.cogtool.model.APredictionResult) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Example 14 with TaskApplication

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

the class ProjectController method duplicateTaskApplications.

protected void duplicateTaskApplications(Design designCopy, Map<ITaskDesign, TaskApplication> associatedTAs) {
    // The undo edit for adding the Design will remove and restore
    // the task-applications; all we need to do here is duplicate
    // the task applications.
    Iterator<TaskApplication> taskApps = associatedTAs.values().iterator();
    while (taskApps.hasNext()) {
        TaskApplication ta = taskApps.next();
        AUndertaking task = ta.getTask();
        project.setTaskApplication(ta.duplicate(task, designCopy));
    }
}
Also used : AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication)

Example 15 with TaskApplication

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

Aggregations

TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)63 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)38 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)22 Design (edu.cmu.cs.hcii.cogtool.model.Design)20 Script (edu.cmu.cs.hcii.cogtool.model.Script)20 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)17 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)16 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)15 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)14 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)12 IOException (java.io.IOException)12 CognitiveModelGenerator (edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator)11 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)11 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)10 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)10 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)10 APredictionResult (edu.cmu.cs.hcii.cogtool.model.APredictionResult)8 File (java.io.File)8 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)7 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)5