Search in sources :

Example 1 with IPredictionAlgo

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

the class ProjectUI method createPredicates.

// setUpDragAndDrop
protected void createPredicates() {
    requiresRegenerationPredicate = new ProjectSelectionPredicate(project) {

        @Override
        protected boolean isSatisfiedBy(Design design, AUndertaking t) {
            TaskApplication ta = project.getTaskApplication(t, design);
            if (ta != null) {
                Demonstration demo = ta.getDemonstration();
                return demo.isObsolete();
            }
            return false;
        }
    };
    hasComputableScriptsPredicate = new ProjectSelectionPredicate(project) {

        @Override
        protected boolean isSatisfiedBy(Design design, AUndertaking t) {
            TaskApplication taskApp = project.getTaskApplication(t, design);
            if (taskApp != null) {
                IPredictionAlgo activeAlg = taskApp.determineActiveAlgorithm(project);
                APredictionResult result = taskApp.getResult(taskApp.getFirstModelGenerator(), activeAlg);
                if ((result != null) && !result.canBeRecomputed()) {
                    return false;
                }
                return taskApp.hasComputableScript() && !taskApp.getDemonstration().isInvalid();
            }
            return project.getDefaultAlgo() == SNIFACTPredictionAlgo.ONLY;
        }
    };
    hasComputedResultPredicate = new ProjectSelectionPredicate(project) {

        @Override
        protected boolean isSatisfiedBy(Design design, AUndertaking t) {
            TaskApplication taskApp = project.getTaskApplication(t, design);
            return ((taskApp != null) && taskApp.hasComputedResult());
        }
    };
    hasResultStepsPredicate = new ProjectSelectionPredicate(project) {

        @Override
        protected boolean isSatisfiedBy(Design design, AUndertaking t) {
            TaskApplication taskApp = project.getTaskApplication(t, design);
            return (taskApp != null) && taskApp.hasResultSteps();
        }
    };
    hasScriptsPredicate = new ProjectSelectionPredicate(project) {

        @Override
        protected boolean isSatisfiedBy(Design design, AUndertaking t) {
            TaskApplication taskApp = project.getTaskApplication(t, design);
            return (taskApp != null) && taskApp.hasScript();
        }
    };
    hasTracesPredicate = new ProjectSelectionPredicate(project) {

        @Override
        protected boolean isSatisfiedBy(Design design, AUndertaking t) {
            TaskApplication taskApp = project.getTaskApplication(t, design);
            return (taskApp != null) && taskApp.hasResultTraces();
        }
    };
    hasMultipleScriptsPredicate = new ProjectSelectionPredicate(project) {

        protected int numScripts = 0;

        @Override
        protected void resetState() {
            numScripts = 0;
        }

        @Override
        protected boolean isSatisfiedBy(Design design, AUndertaking t) {
            TaskApplication taskApp = project.getTaskApplication(t, design);
            if (taskApp != null) {
                Iterator<CognitiveModelGenerator> modelGens = taskApp.getModelGenerators();
                while (modelGens.hasNext()) {
                    CognitiveModelGenerator modelGen = modelGens.next();
                    Script script = taskApp.getScript(modelGen);
                    if (script != null) {
                        if (++numScripts > 1) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    };
}
Also used : Design(edu.cmu.cs.hcii.cogtool.model.Design) Script(edu.cmu.cs.hcii.cogtool.model.Script) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) Iterator(java.util.Iterator) APredictionResult(edu.cmu.cs.hcii.cogtool.model.APredictionResult) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) ProjectSelectionPredicate(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState.ProjectSelectionPredicate) Point(org.eclipse.swt.graphics.Point)

Example 2 with IPredictionAlgo

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

the class ProjectController method createSetAlgorithmHumanAction.

protected IListenerAction createSetAlgorithmHumanAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            ProjectSelectionState parms = (ProjectSelectionState) actionParms;
            final TaskApplication ta = project.getTaskApplication(parms.getSelectedTask(), parms.getSelectedDesign());
            final IPredictionAlgo oldAlgo = ta.determineActiveAlgorithm(project);
            ta.setActiveAlgorithm(HumanDataAlgo.ONLY);
            undoMgr.addEdit(new AUndoableEdit(ProjectLID.SetAlgorithmHuman) {

                @Override
                public String getPresentationName() {
                    return L10N.get("UNDO.PM.SetHumanAlgorithm", "Set Algorithm to Human Data");
                }

                @Override
                public void redo() {
                    super.redo();
                    ta.setActiveAlgorithm(HumanDataAlgo.ONLY);
                }

                @Override
                public void undo() {
                    super.undo();
                    ta.setActiveAlgorithm(oldAlgo);
                }
            });
            return true;
        }
    };
}
Also used : IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication)

Example 3 with IPredictionAlgo

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

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

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

the class ProjectController method recomputeScripts.

protected boolean recomputeScripts(AUndertaking task, Design design, DemoStateManager demoStateMgr, ComputeMessages computeMsgs, IUndoableEditSequence editSequence) {
    if (CogToolPref.isTracingOverride == null && !CogToolPref.IS_TRACING.getBoolean()) {
        Boolean answer = getInteraction().confirmNoTracing();
        if (answer == null) {
            // canceled
            return false;
        } else if (answer.booleanValue()) {
            CogToolPref.IS_TRACING.setBoolean(true);
        }
    }
    if (task.isTaskGroup()) {
        if (!NullSafe.equals(WidgetAttributes.NO_CONTEXT, task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR))) {
            return computeSnifAct(design, task, editSequence, null);
        }
        Iterator<AUndertaking> allTasks = ((TaskGroup) task).getUndertakings().iterator();
        CompoundUndoableEdit groupEditSeq = new CompoundUndoableEdit(RECOMPUTE_SCRIPTS, ProjectLID.RecomputeScript);
        while (allTasks.hasNext()) {
            if (!recomputeScripts(allTasks.next(), design, demoStateMgr, computeMsgs, groupEditSeq)) {
                return false;
            }
        }
        if (groupEditSeq.isSignificant()) {
            groupEditSeq.end();
            editSequence.addEdit(groupEditSeq);
        }
        return true;
    }
    TaskApplication ta = project.getTaskApplication(task, design);
    if (ta != null) {
        IPredictionAlgo activeAlg = ta.determineActiveAlgorithm(project);
        if (activeAlg == SNIFACTPredictionAlgo.ONLY) {
            return computeSnifAct(design, task, editSequence, null);
        }
        if (computeMsgs.canComputeScript(ta, demoStateMgr, editSequence)) {
            if (!activeAlg.allowsComputation()) {
                interaction.setStatusMessage(algDisallowsComputation);
                interaction.reportProblem(RECOMPUTE_SCRIPTS, algDisallowsComputation);
                return false;
            } else if (!ta.getDemonstration().isStartFrameChosen()) {
                // nothing to compute
                interaction.setStatusMessage(noStartFrameChosen);
                return false;
            }
            IUndoableEdit edit = ComputePredictionCmd.computeAllPredictions(project, ta, activeAlg, ta.determineComputeInBackground(project), interaction);
            if (edit != null) {
                ta.setActiveAlgorithm(activeAlg);
                editSequence.addEdit(edit);
            } else {
                interaction.setStatusMessage(computeHadNoResult);
            }
        } else {
            interaction.setStatusMessage(cannotRecomputeInvalid);
        }
    } else {
        if (project.getDefaultAlgo() == SNIFACTPredictionAlgo.ONLY) {
            return computeSnifAct(design, task, editSequence, null);
        }
        interaction.setStatusMessage(cannotRecomputeNoDemo);
    }
    return true;
}
Also used : IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Aggregations

IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)19 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)15 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)10 APredictionResult (edu.cmu.cs.hcii.cogtool.model.APredictionResult)7 Design (edu.cmu.cs.hcii.cogtool.model.Design)7 Script (edu.cmu.cs.hcii.cogtool.model.Script)7 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)7 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)7 CognitiveModelGenerator (edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator)6 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)5 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)5 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)5 ACTRPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo)4 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)4 RcvrUnimplementedFnException (edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException)4 File (java.io.File)4 IOException (java.io.IOException)4 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)3 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)3 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)3