Search in sources :

Example 1 with TaskStatusListener

use of net.sf.mzmine.taskcontrol.TaskStatusListener in project mzmine2 by mzmine.

the class DataPointProcessingController method execute.

/**
 * This will execute the modules associated with the plot. It will start with the first one and
 * execute the following ones afterwards automatically. This method is called by the public method
 * execute(). The status listener in this method starts the next task recursively after the
 * previous one has finished.
 *
 * @param dp
 * @param module
 * @param plot
 */
private void execute(DataPoint[] dp, MZmineProcessingStep<DataPointProcessingModule> step, SpectraPlot plot) {
    if (queue == null || queue.isEmpty() || plot == null) {
        logger.warning("execute called, without queue or plot being set.");
        setStatus(ControllerStatus.FINISHED);
        return;
    }
    if (getForcedStatus() == ForcedControllerStatus.CANCEL || getStatus() == ControllerStatus.CANCELED) {
        setResults(ProcessedDataPoint.convert(dp));
        logger.finest("Canceled controller, not starting new tasks. Results are set to latest array.");
        setStatus(ControllerStatus.CANCELED);
        return;
    }
    List<String> err = new ArrayList<>();
    if (!step.getParameterSet().checkParameterValues(err)) {
        setResults(ProcessedDataPoint.convert(dp));
        setStatus(ControllerStatus.CANCELED);
        logger.warning(step.getModule().getName() + " - Not all parameters set." + Arrays.toString(err.toArray(new String[0])));
        return;
    }
    if (step.getModule() instanceof DataPointProcessingModule) {
        DataPointProcessingModule inst = step.getModule();
        ParameterSet parameters = step.getParameterSet();
        Task t = ((DataPointProcessingModule) inst).createTask(dp, parameters, plot, this, new TaskStatusListener() {

            @Override
            public void taskStatusChanged(Task task, TaskStatus newStatus, TaskStatus oldStatus) {
                if (!(task instanceof DataPointProcessingTask)) {
                    // TODO: Throw exception?
                    logger.warning("This should have been a DataPointProcessingTask.");
                    return;
                }
                // logger.finest("Task status changed to " + newStatus.toString());
                switch(newStatus) {
                    case FINISHED:
                        if (queue.hasNextStep(step)) {
                            if (DataPointProcessingManager.getInst().isRunning(((DataPointProcessingTask) task).getController())) {
                                MZmineProcessingStep<DataPointProcessingModule> next = queue.getNextStep(step);
                                // pass results to next task and start recursively
                                ProcessedDataPoint[] result = ((DataPointProcessingTask) task).getResults();
                                ((DataPointProcessingTask) task).displayResults();
                                execute(result, next, plot);
                            } else {
                                logger.warning("This controller was already removed from the running list, although it " + "had not finished processing. Exiting");
                                break;
                            }
                        } else {
                            setResults(((DataPointProcessingTask) task).getResults());
                            ((DataPointProcessingTask) task).displayResults();
                            setStatus(ControllerStatus.FINISHED);
                        }
                        break;
                    case PROCESSING:
                        setStatus(ControllerStatus.PROCESSING);
                        break;
                    case WAITING:
                        // should we even set to WAITING here?
                        break;
                    case ERROR:
                        setStatus(ControllerStatus.ERROR);
                        break;
                    case CANCELED:
                        setStatus(ControllerStatus.CANCELED);
                        break;
                }
            }
        });
        logger.finest("Start processing of " + t.getClass().getName());
        MZmineCore.getTaskController().addTask(t);
        // maybe we need this some time
        setCurrentTask((DataPointProcessingTask) t);
        setCurrentStep(step);
    }
}
Also used : ParameterSet(net.sf.mzmine.parameters.ParameterSet) Task(net.sf.mzmine.taskcontrol.Task) ArrayList(java.util.ArrayList) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) TaskStatusListener(net.sf.mzmine.taskcontrol.TaskStatusListener) MZmineProcessingStep(net.sf.mzmine.modules.MZmineProcessingStep)

Example 2 with TaskStatusListener

use of net.sf.mzmine.taskcontrol.TaskStatusListener in project mzmine2 by mzmine.

the class MultiThreadPeakFinderMainTask method run.

@Override
public void run() {
    setStatus(TaskStatus.PROCESSING);
    logger.info("Running multithreaded gap filler on " + peakList);
    // Create new results feature list
    processedPeakList = createResultsPeakList();
    progress = 0.5;
    // split raw data files into groups for each thread (task)
    // Obtain the settings of max concurrent threads
    // as this task uses one thread
    int maxRunningThreads = getMaxThreads();
    // raw files
    int raw = peakList.getNumberOfRawDataFiles();
    // create consumer of resultpeaklist
    SubTaskFinishListener listener = new SubTaskFinishListener(project, parameters, peakList, removeOriginal, maxRunningThreads);
    // Submit the tasks to the task controller for processing
    Task[] tasks = createSubTasks(raw, maxRunningThreads, listener);
    // listener for status change: Cancel / error
    TaskStatusListener list = new TaskStatusListener() {

        @Override
        public void taskStatusChanged(Task task, TaskStatus newStatus, TaskStatus oldStatus) {
            if (newStatus.equals(TaskStatus.CANCELED) || newStatus.equals(TaskStatus.ERROR)) {
                // if one is cancelled, stop all
                synchronized (this) {
                    // cancel all
                    for (Task t : tasks) {
                        if (t instanceof AbstractTask)
                            ((AbstractTask) t).removeTaskStatusListener(this);
                        t.cancel();
                    }
                }
            }
        }
    };
    // add listener to all sub tasks
    for (Task t : tasks) {
        if (t instanceof AbstractTask)
            ((AbstractTask) t).addTaskStatusListener(list);
        // add to batchMode collection
        if (batchTasks != null)
            batchTasks.add(t);
    }
    // start
    MZmineCore.getTaskController().addTasks(tasks);
    // listener will take care of adding the final list
    progress = 1;
    // end
    logger.info("All sub tasks started for multithreaded gap-filling on " + peakList);
    setStatus(TaskStatus.FINISHED);
}
Also used : Task(net.sf.mzmine.taskcontrol.Task) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) AbstractTask(net.sf.mzmine.taskcontrol.AbstractTask) TaskStatus(net.sf.mzmine.taskcontrol.TaskStatus) TaskStatusListener(net.sf.mzmine.taskcontrol.TaskStatusListener)

Aggregations

Task (net.sf.mzmine.taskcontrol.Task)2 TaskStatus (net.sf.mzmine.taskcontrol.TaskStatus)2 TaskStatusListener (net.sf.mzmine.taskcontrol.TaskStatusListener)2 ArrayList (java.util.ArrayList)1 MZmineProcessingStep (net.sf.mzmine.modules.MZmineProcessingStep)1 ParameterSet (net.sf.mzmine.parameters.ParameterSet)1 AbstractTask (net.sf.mzmine.taskcontrol.AbstractTask)1