31 #include <boost/property_tree/ptree.hpp>
32 #include <boost/property_tree/xml_parser.hpp>
39 using boost::property_tree::ptree;
44 ExplorationManager::ExplorationManager()
49 connect(WorkerPool::globalInstance(), SIGNAL(NewWorkerAvailable()),
50 this, SLOT(NewWorkerAvailable()));
55 ExplorationManager::~ExplorationManager()
60 const std::deque<ExplorationProgress*>& explorations)
62 auto matching_iter = std::find_if(explorations.begin(), explorations.end(),
65 if (matching_iter != explorations.end()) {
66 return (*matching_iter);
72 void ExplorationManager::RemoveExploration(
const std::string& name,
73 std::deque<ExplorationProgress*>& explorations)
75 auto matching_iter = std::find_if(explorations.begin(), explorations.end(),
76 [&name] (ExplorationProgress* e) {
return name == e->GetExploration().GetName(); });
78 if (matching_iter != explorations.end()) {
79 explorations.erase(matching_iter);
83 std::vector<std::string> ExplorationManager::GetExplorationNames()
85 std::vector<std::string> names;
87 for (
auto progress : m_running_explorations) {
88 names.push_back(progress->GetExploration().GetName());
90 for (
auto progress : m_done_explorations) {
91 names.push_back(progress->GetExploration().GetName());
98 bool ExplorationManager::WorkAvailable()
100 if (m_running_explorations.empty()){
104 for (
auto progress : m_running_explorations) {
105 if (progress->GetTaskCount(TaskState::Waiting) != 0)
112 void ExplorationManager::RegisterExploration(
const Exploration* exploration)
120 m_running_explorations.push_back(progress);
121 connect(progress, SIGNAL(Updated()),
this, SIGNAL(Updated()));
122 connect(progress, SIGNAL(Updated()),
this, SLOT(BackUp()));
123 while (WorkerPool::globalInstance()->WorkerAvailable() && WorkAvailable()) {
124 NewWorkerAvailable();
130 void ExplorationManager::DeleteExploration(
const std::string& name)
132 auto exploration = GetExploration(name, m_running_explorations);
135 RemoveExploration(name, m_running_explorations);
138 exploration = GetExploration(name, m_done_explorations);
140 RemoveExploration(name, m_done_explorations);
144 WorkerPool::globalInstance()->Delete(name);
151 auto progress = GetExploration(explorationName, m_running_explorations);
152 if (progress ==
nullptr)
153 progress = GetExploration(explorationName, m_done_explorations);
154 if (progress ==
nullptr)
160 void ExplorationManager::NewWorkerAvailable()
162 if (m_running_explorations.empty()) {
167 ExplorationProgress* progress =
nullptr;
170 while (!done && i < m_running_explorations.size()) {
172 progress = m_running_explorations.at(i);
174 if (progress->GetTaskCount(TaskState::Waiting) > 0) {
186 auto task = progress->NextTask();
187 assert(task &&
"ExplorationProgress has waiting tasks, but NextTask returns a nullptr");
189 auto worker = WorkerPool::globalInstance()->getProcess();
192 progress->GiveBack(task);
196 connect(worker, SIGNAL(FinishedWork(
const SimResult&)),
197 progress, SLOT(HandleResult(
const SimResult&)), Qt::UniqueConnection);
198 connect(worker, SIGNAL(FinishedWork(
const SimResult&)),
199 this, SLOT(WorkerFinished()), Qt::UniqueConnection);
200 connect(worker, SIGNAL(FinishedWork(
const SimResult&)),
201 this, SLOT(BackUp()), Qt::UniqueConnection);
203 connect(worker, SIGNAL(Disconnected()),
204 this, SLOT(WorkerDisconnected()), Qt::UniqueConnection);
206 m_running_tasks[worker] = task;
207 worker->DoWork(*task);
212 void ExplorationManager::WorkerFinished()
214 WorkerRepresentative* worker =
dynamic_cast<WorkerRepresentative*
>(QObject::sender());
215 assert(worker &&
"QObject::sender() is not a WorkerRepresentative*");
217 auto progress = GetExploration(worker->GetExplName(), m_running_explorations);
221 if (progress->IsFinished()) {
222 m_done_explorations.push_back(progress);
223 RemoveExploration(worker->GetExplName(), m_running_explorations);
226 delete m_running_tasks[worker];
227 m_running_tasks.erase(worker);
230 void ExplorationManager::WorkerDisconnected()
232 WorkerRepresentative* worker =
dynamic_cast<WorkerRepresentative*
>(QObject::sender());
233 assert(worker &&
"QObject::sender() is not a WorkerRepresentative*");
235 SimTask* task = m_running_tasks[worker];
237 if (task ==
nullptr) {
238 m_running_tasks.erase(worker);
242 auto progress = GetExploration(task->GetExploration(), m_running_explorations);
247 progress->GiveBack(task);
248 m_running_tasks.erase(worker);
251 void ExplorationManager::WorkerReconnected(WorkerRepresentative* )
300 NewWorkerAvailable();
303 void ExplorationManager::BackUp()
307 writer.put(
"RunningExplorationsNr", m_running_explorations.size());
309 for (
auto progress : m_running_explorations) {
310 writer.put_child(
"RunningExplorations" + to_string(i), progress->ToPtree());
315 writer.put(
"DoneExplorationsNr", m_done_explorations.size());
316 for (
auto progress : m_done_explorations) {
317 writer.put_child(
"DoneExplorations" + to_string(i), progress->ToPtree());
321 write_xml(
"exploration_backup.xml", writer);
324 void ExplorationManager::Read_Backup()
328 read_xml(
"exploration_backup.xml", reader);
330 for (
int i = 0; i < reader.get<
int>(
"RunningExplorationsNr"); i++) {
331 ExplorationProgress* progress
332 =
new ExplorationProgress(
333 reader.get_child(
"RunningExplorations" + to_string(i)));
334 connect(progress, SIGNAL(Updated()),
this, SIGNAL(Updated()));
335 m_running_explorations.push_back(progress);
338 for (
int i = 0; i < reader.get<
int>(
"DoneExplorationsNr"); i++) {
339 ExplorationProgress* progress
340 =
new ExplorationProgress(
341 reader.get_child(
"DoneExplorations" + to_string(i)));
342 connect(progress, SIGNAL(Updated()),
this, SIGNAL(Updated()));
343 m_done_explorations.push_back(progress);
346 catch (std::exception& e) {
354 void ExplorationManager::StopTask(
const std::string& name,
int id)
356 auto progress = GetExploration(name, m_running_explorations);
360 auto state = progress->GetTask(
id).GetState();
361 if (state == TaskState::Running) {
362 for (
auto task : m_running_tasks) {
363 if (task.second->GetExploration() == name && task.second->GetId() == id) {
364 task.first->StopTask();
368 }
else if (state == TaskState::Waiting) {
369 progress->CancelWaitingTask(
id);
373 void ExplorationManager::RestartTask(
const std::string& name,
int id)
375 auto progress = GetExploration(name, m_running_explorations);
377 progress = GetExploration(name, m_done_explorations);
381 m_running_explorations.push_back(progress);
382 RemoveExploration(name, m_done_explorations);
385 if (progress->GetTask(
id).GetState() == TaskState::Cancelled) {
386 progress->ResendCancelledTask(
id);
389 while (WorkerPool::globalInstance()->WorkerAvailable() && WorkAvailable()) {
390 NewWorkerAvailable();
Interface for Exploration.
Interface for ExplorationTask.
Class describing the progress state of an exploration.
virtual unsigned int GetNumberOfTasks() const =0
Returns the number of tasks the exploration currently contains.
Interface for ExplorationManager.
Interface for WorkerPool.
String manipulation utilities.
Namespace for SimPT parameter explorer package.
Class describing a generic exploration.
Interface for ExplorationProgress.
A worker taken as representative for multiple workers (handles the communication with the node)...
Interface for WorkerRepresentative.