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
55
66
67class AlgorithmErrorNotification : public Poco::Notification {
68public:
71
73 const 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,
96 std::unique_ptr<Mantid::API::IAlgorithmRuntimeProps> props);
97
98 void setQueue(std::deque<IConfiguredAlgorithm_sptr> algorithm);
100 void clearQueue();
102 size_t queueLength();
103
105 bool executeBatch();
107 void executeBatchAsync();
109 void executeAlgorithmAsync(IConfiguredAlgorithm_sptr algorithm);
111 void cancelBatch();
112
114 void stopOnFailure(bool stopOnFailure);
115
116signals:
122 void algorithmError(MantidQt::API::IConfiguredAlgorithm_sptr algorithm, std::string errorMessage);
123
124private:
126 bool executeBatchAsyncImpl(const Poco::Void & /*unused*/);
128 bool executeAlgo(const IConfiguredAlgorithm_sptr &algorithm);
130 void postNotification(Poco::Notification *notification);
131 // Sets cancel requested flag
132 void setCancelRequested(bool const cancel);
133
135 void handleBatchComplete(const Poco::AutoPtr<BatchCompleteNotification> &pNf);
136 void handleBatchCancelled(const Poco::AutoPtr<BatchCancelledNotification> &pNf);
137 void handleAlgorithmStarted(const Poco::AutoPtr<AlgorithmStartedNotification> &pNf);
138 void handleAlgorithmComplete(const Poco::AutoPtr<AlgorithmCompleteNotification> &pNf);
139 void handleAlgorithmError(const Poco::AutoPtr<AlgorithmErrorNotification> &pNf);
140
142 std::deque<IConfiguredAlgorithm_sptr> m_algorithms;
143
146
149
152 std::recursive_mutex m_executeMutex;
153 std::recursive_mutex m_cancelMutex;
154 std::recursive_mutex m_notificationMutex;
155 void resetState();
156 bool cancelRequested();
157
159 mutable Poco::NotificationCenter m_notificationCenter;
166
171
172 void addAllObservers();
173 void removeAllObservers();
174};
175} // namespace MantidQt::API
#define EXPORT_OPT_MANTIDQT_COMMON
Definition DllOption.h:15
double error
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
STL namespace.