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;
}
};
}
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;
}
};
}
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();
}
}
}
}
}
}
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;
}
};
}
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;
}
Aggregations