Mantid
Loading...
Searching...
No Matches
BatchAlgorithmRunner.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2021 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 +
7#pragma once
8
9#include "DllOption.h"
11#include "MantidAPI/Algorithm.h"
12
13#include <QObject>
14
15#include <Poco/ActiveMethod.h>
16#include <Poco/ActiveResult.h>
17#include <Poco/NObserver.h>
18#include <Poco/Void.h>
19
20#include <QMetaType>
21#include <deque>
22#include <mutex>
23#include <utility>
24
25namespace MantidQt::API {
26
27class BatchCompleteNotification : public Poco::Notification {
28public:
29 BatchCompleteNotification(bool inProgress, bool error)
30 : Poco::Notification(), m_inProgress(inProgress), m_error(error) {}
31
32 bool isInProgress() const { return m_inProgress; }
33 bool hasError() const { return m_error; }
34
35private:
37 bool m_error;
38};
39
40class BatchCancelledNotification : public Poco::Notification {
41public:
42 BatchCancelledNotification() : Poco::Notification() {}
43};
44
45class AlgorithmCompleteNotification : public Poco::Notification {
46public:
48 : Poco::Notification(), m_algorithm(std::move(algorithm)) {}
49
51
52private:
54};
55
56class AlgorithmStartedNotification : public Poco::Notification {
57public:
59 : Poco::Notification(), m_algorithm(std::move(algorithm)) {}
60
62
63private:
65};
66
67class AlgorithmErrorNotification : public Poco::Notification {
68public:
70 : Poco::Notification(), m_algorithm(std::move(algorithm)), m_errorMessage(errorMessage) {}
71
73 std::string errorMessage() const { return m_errorMessage; }
74
75private:
77 std::string m_errorMessage;
78};
79
87 Q_OBJECT
88
89public:
90 explicit BatchAlgorithmRunner(QObject *parent = nullptr);
91 ~BatchAlgorithmRunner() override;
92
94 void addAlgorithm(const Mantid::API::IAlgorithm_sptr &algo);
95 void addAlgorithm(const Mantid::API::IAlgorithm_sptr &algo, std::unique_ptr<IAlgorithmRuntimeProps> props);
96
97 void setQueue(std::deque<IConfiguredAlgorithm_sptr> algorithm);
99 void clearQueue();
101 size_t queueLength();
102
104 bool executeBatch();
106 void executeBatchAsync();
108 void cancelBatch();
109
111 void stopOnFailure(bool stopOnFailure);
112
113signals:
119 void algorithmError(MantidQt::API::IConfiguredAlgorithm_sptr algorithm, std::string errorMessage);
120
121private:
123 bool executeBatchAsyncImpl(const Poco::Void & /*unused*/);
125 bool executeAlgo(const IConfiguredAlgorithm_sptr &algorithm);
126
128 void handleBatchComplete(const Poco::AutoPtr<BatchCompleteNotification> &pNf);
129 void handleBatchCancelled(const Poco::AutoPtr<BatchCancelledNotification> &pNf);
130 void handleAlgorithmStarted(const Poco::AutoPtr<AlgorithmStartedNotification> &pNf);
131 void handleAlgorithmComplete(const Poco::AutoPtr<AlgorithmCompleteNotification> &pNf);
132 void handleAlgorithmError(const Poco::AutoPtr<AlgorithmErrorNotification> &pNf);
133
135 std::deque<IConfiguredAlgorithm_sptr> m_algorithms;
136
139
142
145 std::mutex m_mutex;
146 void resetState();
147 bool cancelRequested();
148
150 mutable Poco::NotificationCenter m_notificationCenter;
157
162
163 void addAllObservers();
164 void removeAllObservers();
165};
166} // namespace MantidQt::API
#define EXPORT_OPT_MANTIDQT_COMMON
Definition: DllOption.h:15
double error
Definition: IndexPeaks.cpp:133
AlgorithmCompleteNotification(IConfiguredAlgorithm_sptr algorithm)
IConfiguredAlgorithm_sptr algorithm() const
AlgorithmErrorNotification(IConfiguredAlgorithm_sptr algorithm, std::string const &errorMessage)
IConfiguredAlgorithm_sptr algorithm() const
IConfiguredAlgorithm_sptr algorithm() const
AlgorithmStartedNotification(IConfiguredAlgorithm_sptr algorithm)
Algorithm runner for execution of a queue of algorithms.
bool m_cancelRequested
User has requested to cancel processing.
void algorithmStarted(MantidQt::API::IConfiguredAlgorithm_sptr algorithm)
Mantid::API::IAlgorithm_sptr m_currentAlgorithm
The current algorithm being executed.
std::deque< IConfiguredAlgorithm_sptr > m_algorithms
The queue of algorithms to be executed.
bool m_stopOnFailure
If execution should be stopped on algorithm failure.
Poco::NObserver< BatchAlgorithmRunner, AlgorithmStartedNotification > m_algorithmStartedObserver
Poco::NObserver< BatchAlgorithmRunner, BatchCompleteNotification > m_batchCompleteObserver
Observer for notifications.
Poco::NotificationCenter m_notificationCenter
Notification center used to handle notifications from active method.
Poco::ActiveResult< bool > executeAsync()
Holds result of async execution.
Poco::ActiveMethod< bool, Poco::Void, BatchAlgorithmRunner, Poco::ActiveStarter< BatchAlgorithmRunner > > m_executeAsync
Active method to run batch runner on separate thread.
Poco::NObserver< BatchAlgorithmRunner, AlgorithmErrorNotification > m_algorithmErrorObserver
void algorithmError(MantidQt::API::IConfiguredAlgorithm_sptr algorithm, std::string errorMessage)
void algorithmComplete(MantidQt::API::IConfiguredAlgorithm_sptr algorithm)
Poco::NObserver< BatchAlgorithmRunner, AlgorithmCompleteNotification > m_algorithmCompleteObserver
void batchComplete(bool error)
Emitted when a batch has finished executing.
Poco::NObserver< BatchAlgorithmRunner, BatchCancelledNotification > m_batchCancelledObserver
BatchCompleteNotification(bool inProgress, bool error)
std::shared_ptr< IConfiguredAlgorithm > IConfiguredAlgorithm_sptr
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
Definition: Algorithm.h:30
STL namespace.