Search in sources :

Example 1 with ResultStep

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

the class ProjectController method createImportHumanCSVFileAction.

// Action for ImportHumanCSVFile
protected IListenerAction createImportHumanCSVFileAction() {
    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);
            if ((design == null) || (tasks == null) || (tasks.length == 0)) {
                return false;
            }
            List<String> outputLines = new LinkedList<String>();
            List<String> errorLines = new LinkedList<String>();
            // Get csv file
            File dataFile = interaction.selectCSVFile();
            if (dataFile == null) {
                interaction.setStatusMessage(IMPORT_FAIL_NOFILE_MSG);
                return false;
            }
            // Read lines out of file
            FileReader reader = null;
            try {
                reader = new FileReader(dataFile);
                FileUtil.readLines(reader, outputLines);
            } catch (FileNotFoundException e) {
                interaction.setStatusMessage(IMPORT_FAIL_NOFILE_MSG);
                throw new RcvrIOTempException("Error in parsing csv", e);
            } catch (IOException e) {
                interaction.setStatusMessage(IMPORT_FAIL_NOREAD_MSG);
                throw new RcvrParsingException("Error in parsing csv", e);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    // ignore
                    }
                    reader = null;
                }
            }
            // parse ResultSteps out of trace lines
            TraceParser<ResultStep> parser = new HumanCSVParser();
            List<ResultStep> steps;
            try {
                steps = parser.parseTrace(outputLines);
            } catch (TraceParser.ParseException e) {
                interaction.setStatusMessage(IMPORT_FAIL_PARSE_IMPL_MSG);
                throw new RcvrParsingException("Error in parsing implementation", e);
            }
            System.out.println(steps);
            if (steps.size() < 1) {
                interaction.setStatusMessage(IMPORT_FAIL_PARSE_MSG);
                return false;
            }
            double taskTime = -1.0;
            Iterator<ResultStep> stepIt = steps.iterator();
            while (stepIt.hasNext()) {
                ResultStep step = stepIt.next();
                taskTime = Math.max(taskTime, step.startTime + step.duration);
            }
            // Scale time to seconds.
            taskTime /= 1000.0;
            // -------------- Done parsing
            DemoStateManager demoMgr = DemoStateManager.getStateManager(project, design);
            final TaskApplication[] taskApps = new TaskApplication[tasks.length];
            final APredictionResult[] oldResults = new APredictionResult[tasks.length];
            final APredictionResult[] results = new APredictionResult[tasks.length];
            final IPredictionAlgo[] oldActiveAlgos = new IPredictionAlgo[tasks.length];
            for (int i = 0; i < tasks.length; i++) {
                taskApps[i] = ensureTaskApplication(tasks[i], design, MODELGEN_ALG, demoMgr);
                // If for some reason there are no result steps,
                // create a TimePredictionResult that
                // reflects COMPUTATION_FAILED.
                // Otherwise, create a normal one.
                Script script = taskApps[i].getScript(MODELGEN_ALG);
                if (taskTime < 0.0) {
                    results[i] = new TimePredictionResult(dataFile.getName(), script, HumanDataAlgo.ONLY, outputLines, errorLines, steps);
                } else {
                    results[i] = new TimePredictionResult(dataFile.getName(), script, HumanDataAlgo.ONLY, outputLines, errorLines, steps, taskTime);
                }
                oldResults[i] = taskApps[i].getResult(MODELGEN_ALG, HumanDataAlgo.ONLY);
                taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, results[i]);
                oldActiveAlgos[i] = taskApps[i].getActiveAlgorithm();
                taskApps[i].setActiveAlgorithm(HumanDataAlgo.ONLY);
            }
            IUndoableEdit edit = new AUndoableEdit(ProjectLID.RecomputeScript) {

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

                @Override
                public void redo() {
                    super.redo();
                    for (int i = 0; i < taskApps.length; i++) {
                        taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, results[i]);
                        taskApps[i].setActiveAlgorithm(HumanDataAlgo.ONLY);
                    }
                }

                @Override
                public void undo() {
                    super.undo();
                    for (int i = 0; i < taskApps.length; i++) {
                        taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, oldResults[i]);
                        taskApps[i].setActiveAlgorithm(oldActiveAlgos[i]);
                    }
                }
            };
            undoMgr.addEdit(edit);
            interaction.setStatusMessage(IMPORT_SUCCESS_MSG);
            return true;
        }
    };
}
Also used : TraceParser(edu.cmu.cs.hcii.cogtool.model.TraceParser) FileNotFoundException(java.io.FileNotFoundException) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) ResultStep(edu.cmu.cs.hcii.cogtool.model.ResultStep) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) HumanCSVParser(edu.cmu.cs.hcii.cogtool.model.HumanCSVParser) TimePredictionResult(edu.cmu.cs.hcii.cogtool.model.TimePredictionResult) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FileReader(java.io.FileReader) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) Script(edu.cmu.cs.hcii.cogtool.model.Script) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) APredictionResult(edu.cmu.cs.hcii.cogtool.model.APredictionResult) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) LinkedList(java.util.LinkedList) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) RcvrIOTempException(edu.cmu.cs.hcii.cogtool.util.RcvrIOTempException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) File(java.io.File)

Example 2 with ResultStep

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

the class PERTPanel method observeSelectionState.

/**
     * Associates this panel with a PERTChartSelectionState so that when a new
     * operator is selected, this panel will redraw itself accordingly.
     *
     * @param selectionState
     */
public void observeSelectionState(PERTChartSelectionState selectionState) {
    chartSelectionState = selectionState;
    AlertHandler handler = new AlertHandler() {

        public void handleAlert(EventObject alert) {
            // TODO: Change this to map or set
            List<ResultStep> selectedSteps = ((PERTChartSelectionState.SelectionChange) alert).selectedSteps;
            // clear selection boxes
            Iterator<SelectionHalo> haloIterator = selectionBoxes.iterator();
            SelectionHalo deadHalo = null;
            while (haloIterator.hasNext()) {
                deadHalo = haloIterator.next();
                contents.remove(deadHalo);
                deadHalo.dispose();
            }
            selectionBoxes.clear();
            Iterator<PERTChartOperatorBar> barIterator = bars.iterator();
            PERTChartOperatorBar bar;
            while (barIterator.hasNext()) {
                bar = barIterator.next();
                if (selectedSteps.contains(bar.getStep())) {
                    bar.setSelected(true);
                    SelectionHalo halo = new SelectionHalo();
                    halo.setTarget(bar);
                    contents.add(halo);
                    selectionBoxes.add(halo);
                } else {
                    bar.setSelected(false);
                }
            }
            //redraw();
            contents.repaint();
        }
    };
    chartSelectionState.addHandler(this, PERTChartSelectionState.SelectionChange.class, handler);
}
Also used : ResultStep(edu.cmu.cs.hcii.cogtool.model.ResultStep) PERTChartSelectionState(edu.cmu.cs.hcii.cogtool.ui.PERTChartSelectionState) PERTChartOperatorBar(edu.cmu.cs.hcii.cogtool.uimodel.PERTChartOperatorBar) AlertHandler(edu.cmu.cs.hcii.cogtool.util.AlertHandler) EventObject(java.util.EventObject)

Example 3 with ResultStep

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

the class PERTPanel method setResultSteps.

public void setResultSteps(List<ResultStep> steps, List<String> resourceLabels) {
    // First remove seleciton halos
    Iterator<SelectionHalo> haloIterator = selectionBoxes.iterator();
    SelectionHalo deadHalo = null;
    while (haloIterator.hasNext()) {
        deadHalo = haloIterator.next();
        contents.remove(deadHalo);
        deadHalo.dispose();
    }
    selectionBoxes.clear();
    // Then remove existing operator bars
    Iterator<PERTChartOperatorBar> barIt = bars.iterator();
    PERTChartOperatorBar deadBar = null;
    while (barIt.hasNext()) {
        deadBar = barIt.next();
        Iterator<PERTStepDependency> depIt = deadBar.getDependencies().iterator();
        while (depIt.hasNext()) {
            PERTStepDependency deadDependency = depIt.next();
            contents.remove(deadDependency);
        }
        contents.remove(deadBar);
    //deadBar.dispose();
    }
    bars.clear();
    Iterator<ResultStep> stepIterator = steps.iterator();
    ResultStep step;
    PERTChartOperatorBar bar;
    // create a map so that we can copy the dependency structure from the
    // ResultSteps to the PERTChartOperatorBars
    // TODO: save this state, and use this instead of bars
    Map<ResultStep, PERTChartOperatorBar> barMap = new HashMap<ResultStep, PERTChartOperatorBar>();
    List<PERTChartOperatorBar> operators = new ArrayList<PERTChartOperatorBar>();
    // iterate through once and create operator bars
    while (stepIterator.hasNext()) {
        step = stepIterator.next();
        bar = new PERTChartOperatorBar(step, resourceLabels, barHeight);
        bar.setColorWithMap(colorMap);
        barMap.put(step, bar);
        bars.add(bar);
        if (bar.isCascade()) {
            contents.add(bar);
        } else {
            operators.add(bar);
        }
        // coompare to find the total time
        totalTime = Math.max(totalTime, step.startTime + step.duration);
    }
    // iterator through operators, and add them now:
    Iterator<PERTChartOperatorBar> opIter = operators.iterator();
    while (opIter.hasNext()) {
        contents.add(opIter.next());
    }
    // iterate through again setting dependencies on each bar object
    //   based on the dependencies in each ResultStep object
    ResultStep.ResultStepDependency rStepDep;
    Iterator<ResultStep.ResultStepDependency> depStepIter;
    stepIterator = steps.iterator();
    double ntvTotalTime = 0.0d;
    while (stepIterator.hasNext()) {
        step = stepIterator.next();
        bar = barMap.get(step);
        depStepIter = step.getDependencies().iterator();
        ntvTotalTime = Math.max(ntvTotalTime, step.startTime + step.duration);
        while (depStepIter.hasNext()) {
            rStepDep = depStepIter.next();
            PERTChartOperatorBar otherBar = barMap.get(rStepDep.dependency);
            PERTStepDependency dep = bar.addDependency(otherBar, rStepDep);
            contents.add(dep);
            connections.add(dep);
        }
    }
    nativeTotalTime = ntvTotalTime;
    totalTime = nativeTotalTime;
    numRows = resourceLabels.size();
    resizeVisualization(null);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PERTChartOperatorBar(edu.cmu.cs.hcii.cogtool.uimodel.PERTChartOperatorBar) PERTStepDependency(edu.cmu.cs.hcii.cogtool.uimodel.PERTChartOperatorBar.PERTStepDependency) ResultStep(edu.cmu.cs.hcii.cogtool.model.ResultStep)

Example 4 with ResultStep

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

the class PERTChartPanel method observeSelectionState.

/**
     * Associate this view with a PERTChartSelectionState so that, when a new
     * operator is selected, this view adds the operator info to the
     * operatorInfoPanel.
     *
     * @param selection
     */
public void observeSelectionState(PERTChartSelectionState selection) {
    chartSelectionState = selection;
    visPanel.observeSelectionState(chartSelectionState);
    scrollBar.observeSelectionState(chartSelectionState);
    AlertHandler handler = new AlertHandler() {

        public void handleAlert(EventObject alert) {
            List<ResultStep> selectedStepList = ((PERTChartSelectionState.SelectionChange) alert).selectedSteps;
            String trace = traceText.getText();
            int startPos = -1;
            int endPos = -1;
            operatorInfoText.setText("");
            Iterator<ResultStep> stepIt = selectedStepList.iterator();
            while (stepIt.hasNext()) {
                ResultStep step = stepIt.next();
                operatorInfoText.append(step.toString() + "\n\n\n\n");
                if (startPos < 0) {
                    startPos = Math.max(0, StringUtil.getCharPosFromLineNum(trace, step.traceStart));
                } else {
                    startPos = Math.min(startPos, StringUtil.getCharPosFromLineNum(trace, step.traceStart));
                }
                endPos = Math.max(endPos, StringUtil.getCharPosFromLineNum(trace, step.traceEnd));
            }
            if (selectedStepList.size() == 0) {
                operatorInfoText.setText(NO_OPERATOR_SELECTED);
            }
            if ((startPos > -1) && (endPos > -1)) {
                traceText.setSelection(startPos, endPos);
                traceText.showSelection();
            }
        }
    };
    chartSelectionState.addHandler(this, PERTChartSelectionState.SelectionChange.class, handler);
}
Also used : ResultStep(edu.cmu.cs.hcii.cogtool.model.ResultStep) PERTChartSelectionState(edu.cmu.cs.hcii.cogtool.ui.PERTChartSelectionState) AlertHandler(edu.cmu.cs.hcii.cogtool.util.AlertHandler) EventObject(java.util.EventObject)

Example 5 with ResultStep

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

the class PERTChartPanel method setResult.

public void setResult(APredictionResult res) {
    result = res;
    List<ResultStep> steps = res.getModelSteps();
    Iterator<ResultStep> stepIterator = steps.iterator();
    ResultStep thisStep;
    resourceLabels = new ArrayList<String>();
    while (stepIterator.hasNext()) {
        thisStep = stepIterator.next();
        if (!resourceLabels.contains(thisStep.resource)) {
            resourceLabels.add(thisStep.resource);
        }
        totalTime = Math.max(totalTime, thisStep.startTime + thisStep.duration);
    }
    resourceLabels = PERTChartUIModel.orderResourceLabels(resourceLabels);
    scrollBar.setResultSteps(steps, resourceLabels);
    visPanel.setResultSteps(steps, resourceLabels);
    labelPanel.setResourceLabels(resourceLabels);
    Iterator<String> traceIt = result.getTraceLines().iterator();
    StringBuilder tt = new StringBuilder();
    //TODO: Really long traces cause the system to hang here. Fix it!
    int lineCount = 0;
    while ((traceIt.hasNext()) && (lineCount < 10000)) {
        lineCount++;
        tt.append(traceIt.next() + "\n");
    }
    traceText.setText(tt.toString());
}
Also used : ResultStep(edu.cmu.cs.hcii.cogtool.model.ResultStep)

Aggregations

ResultStep (edu.cmu.cs.hcii.cogtool.model.ResultStep)6 PERTChartSelectionState (edu.cmu.cs.hcii.cogtool.ui.PERTChartSelectionState)2 PERTChartOperatorBar (edu.cmu.cs.hcii.cogtool.uimodel.PERTChartOperatorBar)2 AlertHandler (edu.cmu.cs.hcii.cogtool.util.AlertHandler)2 EventObject (java.util.EventObject)2 APredictionResult (edu.cmu.cs.hcii.cogtool.model.APredictionResult)1 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)1 Design (edu.cmu.cs.hcii.cogtool.model.Design)1 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)1 HumanCSVParser (edu.cmu.cs.hcii.cogtool.model.HumanCSVParser)1 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)1 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)1 Script (edu.cmu.cs.hcii.cogtool.model.Script)1 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)1 TimePredictionResult (edu.cmu.cs.hcii.cogtool.model.TimePredictionResult)1 TraceParser (edu.cmu.cs.hcii.cogtool.model.TraceParser)1 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)1 PERTStepDependency (edu.cmu.cs.hcii.cogtool.uimodel.PERTChartOperatorBar.PERTStepDependency)1 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)1 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)1