Mantid
Loading...
Searching...
No Matches
AlgorithmRunner.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <Poco/ActiveResult.h>
10
11using namespace Mantid::Kernel;
12using namespace Mantid::API;
13
14namespace MantidQt::API {
15
16//----------------------------------------------------------------------------------------------
20 : QObject(parent), m_finishedObserver(*this, &AlgorithmRunner::handleAlgorithmFinishedNotification),
21 m_progressObserver(*this, &AlgorithmRunner::handleAlgorithmProgressNotification),
22 m_errorObserver(*this, &AlgorithmRunner::handleAlgorithmErrorNotification), m_asyncResult(nullptr) {}
23
24//----------------------------------------------------------------------------------------------
28 if (m_asyncAlg) {
29 m_asyncAlg->removeObserver(m_finishedObserver);
30 m_asyncAlg->removeObserver(m_errorObserver);
31 m_asyncAlg->removeObserver(m_progressObserver);
32 }
33 delete m_asyncResult;
34}
35
36//--------------------------------------------------------------------------------------
42 // Cancel any currently running algorithms
43 if (m_asyncAlg) {
44 if (m_asyncAlg->isRunning()) {
45 m_asyncAlg->cancel();
46 }
47 if (m_asyncResult) {
48 m_asyncResult->tryWait(1000);
49 delete m_asyncResult;
50 m_asyncResult = nullptr;
51 }
52 m_asyncAlg->removeObserver(m_finishedObserver);
53 m_asyncAlg->removeObserver(m_errorObserver);
54 m_asyncAlg->removeObserver(m_progressObserver);
55 m_asyncAlg.reset();
56 }
57}
58
59//--------------------------------------------------------------------------------------
66 if (!alg)
67 throw std::invalid_argument("AlgorithmRunner::startAlgorithm() given a NULL Algorithm");
68 if (!alg->isInitialized())
69 throw std::invalid_argument("AlgorithmRunner::startAlgorithm() given an uninitialized Algorithm");
70
72
73 // Observe the algorithm
74 alg->addObserver(m_finishedObserver);
75 alg->addObserver(m_errorObserver);
76 alg->addObserver(m_progressObserver);
77 // Start asynchronous execution
78 m_asyncAlg = alg;
80}
81
84
85//--------------------------------------------------------------------------------------
95void AlgorithmRunner::handleAlgorithmFinishedNotification(const Poco::AutoPtr<Algorithm::FinishedNotification> &pNf) {
96 UNUSED_ARG(pNf);
97 emit algorithmComplete(false);
98}
99
100//--------------------------------------------------------------------------------------
105void AlgorithmRunner::handleAlgorithmProgressNotification(const Poco::AutoPtr<Algorithm::ProgressNotification> &pNf) {
106 emit algorithmProgress(pNf->progress, pNf->message);
107}
108
109//--------------------------------------------------------------------------------------
115void AlgorithmRunner::handleAlgorithmErrorNotification(const Poco::AutoPtr<Algorithm::ErrorNotification> &pNf) {
116 UNUSED_ARG(pNf);
117 emit algorithmComplete(true);
118}
119
120} // namespace MantidQt::API
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
The AlgorithmRunner is a QObject that encapsulates methods for running an algorithm asynchronously (i...
void algorithmComplete(bool error)
Signal emitted when the algorithm has completed execution/encountered an error.
virtual void cancelRunningAlgorithm()
If an algorithm is already running, cancel it.
Mantid::API::IAlgorithm_sptr m_asyncAlg
Reference to the algorithm executing asynchronously.
Poco::ActiveResult< bool > * m_asyncResult
For the asynchronous call in dynamic rebinning.
virtual void startAlgorithm(Mantid::API::IAlgorithm_sptr alg)
Begin asynchronous execution of an algorithm and observe its execution.
void handleAlgorithmProgressNotification(const Poco::AutoPtr< Mantid::API::Algorithm::ProgressNotification > &pNf)
Observer called when the async algorithm has progress to report.
Poco::NObserver< AlgorithmRunner, Mantid::API::Algorithm::FinishedNotification > m_finishedObserver
void handleAlgorithmErrorNotification(const Poco::AutoPtr< Mantid::API::Algorithm::ErrorNotification > &pNf)
Observer called when the async algorithm has encountered an error.
~AlgorithmRunner() override
Destructor.
AlgorithmRunner(QObject *parent=nullptr)
Constructor.
Poco::NObserver< AlgorithmRunner, Mantid::API::Algorithm::ProgressNotification > m_progressObserver
Poco::NObserver< AlgorithmRunner, Mantid::API::Algorithm::ErrorNotification > m_errorObserver
virtual Mantid::API::IAlgorithm_sptr getAlgorithm() const
Get back a pointer to the running algorithm.
void algorithmProgress(double p, const std::string &msg)
Signal emitted when the algorithm reports progress.
void handleAlgorithmFinishedNotification(const Poco::AutoPtr< Mantid::API::Algorithm::FinishedNotification > &pNf)
Algorithm notification handlers.
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm