use of edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo in project cogtool by cogtool.
the class ResultDisplayPolicy method getTaskApplicationCell.
public static String getTaskApplicationCell(Project project, TaskApplication taskApp, CognitiveModelGenerator gen, boolean forCell, String withSecs) {
if ((gen == null) || (taskApp == null) || ((!taskApp.hasComputedResult()) && (!taskApp.hasComputableScript())) || (taskApp.getScript(gen) == null)) {
// has a TaskApp, but no Script for the current algorithm
return "";
}
Demonstration demo = taskApp.getDemonstration();
IPredictionAlgo alg = taskApp.determineActiveAlgorithm(project);
APredictionResult r = taskApp.getResult(gen, alg);
int resultState = (r == null) ? APredictionResult.NOT_COMPUTED : r.getResultState();
// ?? means the demonstration is invalid or script needs regeneration
if (forCell) {
if (demo.isInvalid() || demo.isObsolete()) {
return "?? ";
}
if ((resultState == APredictionResult.NOT_COMPUTED) || (alg.requiresDemonstration() && !demo.isStartFrameChosen())) {
// No result yet computed for the associated script
return "-- ";
}
if (resultState == APredictionResult.COMPUTATION_IN_PROGRESS) {
// Result is being computed
return "(><) ";
}
if (resultState == APredictionResult.COMPUTE_FAILED) {
// Has a result for this algorithm, but it failed
return "XX ";
}
double timing = getComputationResult(r);
if (timing < 0.0) {
return "~~";
}
updateDigits();
String result = cellNumberFormat.format(timing);
if (CogToolPref.KLM_RESULT_RANGE.getBoolean() && withSecs != null && withSecs.length() > 0) {
result += " (" + cellNumberFormat.format(0.9 * timing);
result += ", " + cellNumberFormat.format(1.1 * timing);
result += ")";
}
result += withSecs;
return result;
// return (timing >= 0.0)
//// ? (cellNumberFormat.format(timing) + withSecs)
// ? (cellNumberFormat.format(timing) + withSecs + " ±10%")
// : "~~";
}
// Long form task application state descriptions, used for tooltips
// "" means no task application or no computable script for alg
// -- / "" means computable, not computed, and demo is valid
// -- / X means not computed and demo is invalid
// -- / ? means not computed and script needs regeneration
// NN / "" means computed and demo is valid
// NN / X means computed and demo is invalid
// NN / ? means computed and script needs regeneration
// ## / "" means computation failed and demo is valid
// ## / X means computation failed and demo is invalid
// ## / ? means computation failed, script needs regeneration
// (><) / ... means result is being computed
// ~~ / ... means result resulted in a negative number (??)
String demoState;
if (demo.isInvalid()) {
// " / X ";
demoState = " / INVALID";
} else if (demo.isObsolete()) {
// " / ? ";
demoState = " / OBSOLETE";
} else {
// " / ";
demoState = "";
}
if ((resultState == APredictionResult.NOT_COMPUTED) || !demo.isStartFrameChosen()) {
// No result yet computed for the associated script
return "NOT COMPUTED" + /* "--" */
demoState;
}
if (resultState == APredictionResult.COMPUTE_FAILED) {
// Has a result for this algo, but it failed
return "COMPUTE FAILED" + /* "##" */
demoState;
}
if (resultState == APredictionResult.COMPUTATION_IN_PROGRESS) {
// Result is being computed
return "BEING COMPUTED" + /* "(><)" */
demoState;
}
double timing = getComputationResult(r);
updateDigits();
return (timing >= 0.0) ? (cellNumberFormat.format(timing) + withSecs + demoState) : ("~~" + demoState);
}
use of edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo in project cogtool by cogtool.
the class ComputePredictionCmd method computeInBackground.
/**
* Perform the analysis in the background. Set the result when done.
*/
public static APredictionResult computeInBackground(IPredictionAlgo computeAlg, Script s, Interaction interact) {
try {
DefaultAnalysisWorkThread workThread = new DefaultAnalysisWorkThread(computeAlg, s, null, interact);
ITraceWindow traceWin = interact.createTraceWindow("Computation trace", workThread, "Trace output: stdout (top) and stderr (bottom)");
workThread.setTraceWindow(traceWin);
ThreadManager.startNewThread(workThread);
return workThread.getResultProxy();
} catch (IPredictionAlgo.ComputationException ex) {
throw new RcvrComputationException(ex);
} catch (IllegalStateException ex) {
throw new RcvrIllegalStateException(ex);
} catch (UnsupportedOperationException ex) {
throw new RcvrUnimplementedFnException(ex);
} catch (Exception ex) {
throw new RcvrComputationException(ex);
}
}
use of edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo 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;
}
};
}
use of edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo in project cogtool by cogtool.
the class HCIPACmd method initHCIPATaskDesign.
// For each design
protected static void initHCIPATaskDesign(Project project, String taskName, AUndertaking[] subtasks, Design design, CognitiveModelGenerator modelGen) {
Frame f = getStartFrame(design);
DemoStateManager demoMgr = DemoStateManager.getStateManager(project, design);
TaskApplication ta = DemoStateManager.ensureTaskApplication(project, subtasks[0], design, modelGen, demoMgr);
Script script = ta.getScript(modelGen);
Demonstration demo = script.getDemonstration();
demo.setStartFrame(f);
demo.setStartFrameChosen(true);
IPredictionAlgo computeAlg = ta.determineActiveAlgorithm(project);
ThinkScriptStep thinkStep = new ThinkScriptStep(f, RECOGNIZE_NEED + taskName);
demo.appendStep(thinkStep);
List<String> warnings = new ArrayList<String>();
List<DefaultModelGeneratorState> states = modelGen.generateScriptSteps(thinkStep, demo.getInitialState(), warnings);
script.replaceStepStates(0, states);
APredictionResult result = ComputePredictionCmd.computePrediction(computeAlg, script);
ta.setResult(modelGen, computeAlg, PredictionResultProxy.getLatestResult(result));
ta = DemoStateManager.ensureTaskApplication(project, subtasks[1], design, modelGen, demoMgr);
script = ta.getScript(modelGen);
demo = ta.getDemonstration();
demo.setStartFrame(f);
demo.setStartFrameChosen(true);
thinkStep = new ThinkScriptStep(f, "Select Function Step");
demo.appendStep(thinkStep);
states = modelGen.generateScriptSteps(thinkStep, demo.getInitialState(), warnings);
script.replaceStepStates(0, states);
result = ComputePredictionCmd.computePrediction(computeAlg, script);
ta.setResult(modelGen, computeAlg, PredictionResultProxy.getLatestResult(result));
}
use of edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo in project cogtool by cogtool.
the class ProjectController method createGenerateACTRModelAction.
// createImportHumanCSVFileAction
protected IListenerAction createGenerateACTRModelAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return ProjectSelectionState.class;
}
public boolean performAction(Object prms) {
ProjectSelectionState seln = (ProjectSelectionState) prms;
// Must have selected tasks and design
Design design = seln.getSelectedDesign();
AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION);
for (AUndertaking task : tasks) {
TaskApplication ta = project.getTaskApplication(task, design);
Script s = DemoStateManager.ensureScript(ta, KLMCognitiveGenerator.ONLY);
// TODO There's too much algorithm specific code
// in here; but for now it seems the expedient
// thing to do -- all this needs to be thought
// through for all back ends, and restructured
String path = s.getAssociatedPath();
String filename = null;
if (path == null) {
filename = design.getName() + "-" + task.getName();
} else {
filename = (new File(path)).getName();
if ((filename != null) && filename.endsWith(CogToolFileTypes.LISP_FILE_EXT)) {
filename = filename.substring(0, filename.length() - CogToolFileTypes.LISP_FILE_EXT.length());
}
}
File file = interaction.selectExportLocation(filename, CogToolFileTypes.LISP_FILE_EXT);
if (file == null) {
return false;
}
s.setAssociatedPath(file.getAbsolutePath());
// so we have to delete it.
if (file.length() == 0) {
file.delete();
}
try {
IPredictionAlgo taAlg = ta.determineActiveAlgorithm(project);
if (!(taAlg instanceof ACTRPredictionAlgo)) {
throw new RcvrIllegalStateException("Can't generate ACT-R Model from a non ACT-R task.");
}
ACTRPredictionAlgo algo = (ACTRPredictionAlgo) taAlg;
algo.outputModel(design, task, s.getDemonstration().getStartFrame(), s, file, null);
} catch (UnsupportedOperationException ex) {
throw new RcvrUnimplementedFnException(ex);
} catch (IOException ex) {
throw new RcvrIOException(("IOException generating model file for task " + task.getName() + " in design " + design.getName()), ex);
}
}
return false;
}
};
}
Aggregations