VPTissue Reference Manual
ExplorationProgress.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "ExplorationProgress.h"
21 
22 #include "Exploration.h"
23 #include "ExplorationTask.h"
24 #include "FileExploration.h"
25 #include "ParameterExploration.h"
26 #include "SimResult.h"
27 #include "SimTask.h"
28 
29 #include <cassert>
30 
31 using boost::property_tree::ptree;
32 
33 namespace SimPT_Parex {
34 
35 const unsigned int ExplorationProgress::g_max_tries;
36 
38  : m_exploration(exploration), m_tasks(m_exploration->GetNumberOfTasks())
39 {
40  assert(exploration != nullptr && "The given exploration isn't initialized.");
41 
42  m_task_counts[TaskState::Waiting] = m_tasks.size();
43  for (unsigned int i = 0; i < m_tasks.size(); ++i) {
44  m_send_queue.push_back(i);
45  }
46 }
47 
49  : m_exploration(nullptr)
50 {
51  ReadPtree(pt);
52 }
53 
55 {
56  delete m_exploration;
57 }
58 
60 {
61  assert(id < m_tasks.size() && "This task doesn't exist.");
62  assert(m_tasks[id].GetState() == TaskState::Waiting && "This task isn't in a 'Waiting' state.");
63 
64  m_send_queue.remove(id);
65  ChangeState(m_tasks[id], TaskState::Cancelled);
66 }
67 
68 void ExplorationProgress::ChangeState(ExplorationTask& task, TaskState state)
69 {
70  m_task_counts[state] += 1;
71  m_task_counts[task.GetState()] -= 1;
72  task.ChangeState(state);
73 
74  emit Updated();
75 }
76 
77 const ExplorationTask& ExplorationProgress::GetTask(unsigned int id) const
78 {
79  assert(id < m_exploration->GetNumberOfTasks() && "Id/Index out of bounds");
80 
81  return m_tasks[id];
82 }
83 
84 unsigned int ExplorationProgress::GetTaskCount(const TaskState& state) const
85 {
86  auto found = m_task_counts.find(state);
87 
88  if (found == m_task_counts.end()) {
89  return 0;
90  }
91  else {
92  return found->second;
93  }
94 }
95 
97 {
98  assert(m_tasks[task->GetId()].GetState() == TaskState::Running && "This task isn't in a 'Running' state.");
99 
100  unsigned int id = task->GetId();
101  ChangeState(m_tasks[id], TaskState::Waiting);
102  m_running_tasks.remove(id);
103  m_send_queue.push_front(id);
104 
105  delete task;
106 }
107 
109 {
110  ExplorationTask& taskInfo = m_tasks.at(result.GetTaskId());
111 
112  switch (result.GetResult()) {
113  case SimResult::ResultType::Success:
114  taskInfo.setNodeThatContainsResult(result.GetNodeIP(), result.GetNodePort());
115 
116  ChangeState(taskInfo, TaskState::Finished);
117  break;
118 
119  case SimResult::ResultType::Failure:
120  if (taskInfo.GetNumberOfTries() < g_max_tries) {
121  m_send_queue.push_back(result.GetTaskId());
122  ChangeState(taskInfo, TaskState::Waiting);
123  taskInfo.IncrementTries();
124  } else {
125  ChangeState(taskInfo, TaskState::Failed);
126  }
127  break;
128 
129  case SimResult::ResultType::Stopped:
130  ChangeState(taskInfo, TaskState::Cancelled);
131  break;
132  }
133 }
134 
136 {
137  return m_send_queue.empty() && m_running_tasks.empty();
138 }
139 
141 {
142  if (m_send_queue.size() == 0)
143  return nullptr;
144 
145  unsigned int id = m_send_queue.front();
146  m_send_queue.pop_front();
147  m_running_tasks.push_back(id);
148 
149  SimTask* task = m_exploration->CreateTask(id);
150  ChangeState(m_tasks[id], TaskState::Running);
151 
152  return task;
153 }
154 
155 void ExplorationProgress::ReadPtree(const ptree& pt)
156 {
157  delete m_exploration;
158 
159  m_exploration = nullptr;
160  if (pt.find("parameter_exploration") != pt.not_found()) {
161  m_exploration = new ParameterExploration(pt.get_child("parameter_exploration"));
162  } else if (pt.find("file_exploration") != pt.not_found()) {
163  m_exploration = new FileExploration(pt.get_child("file_exploration"));
164  } else {
165  assert(false && "Exploration type unknown.");
166  }
167 
168  ptree tasks_pt = pt.get_child("tasks");
169  m_tasks = std::vector<ExplorationTask>(tasks_pt.size());
170  m_task_counts.clear();
171 
172  unsigned int i = 0;
173  for (const auto& pair : tasks_pt) {
174  ExplorationTask task(pair.second);
175 
176  m_tasks[i] = task;
177  m_task_counts[task.GetState()] += 1;
178 
179  if (task.GetState() == TaskState::Running) {
180  m_running_tasks.push_back(i);
181  }
182  i++;
183  }
184 
185  m_send_queue.clear();
186  const auto &send_queue_pt = pt.get_child_optional("send_queue");
187  if (send_queue_pt) {
188  for (const auto& pair : send_queue_pt.get()) {
189  m_send_queue.push_back(pair.second.get_value<unsigned int>());
190  }
191  }
192 }
193 
195 {
196  assert(id < m_tasks.size() && "This task doesn't exist.");
197  assert(m_tasks[id].GetState() == TaskState::Cancelled && "This task isn't in a 'Cancelled' state.");
198 
199  m_send_queue.push_back(id);
200  ChangeState(m_tasks[id], TaskState::Waiting);
201 }
202 
204 {
205  ptree pt;
206 
207  if (dynamic_cast<const ParameterExploration*>(m_exploration) != nullptr) {
208  pt.put_child("parameter_exploration", m_exploration->ToPtree());
209  } else if (dynamic_cast<const FileExploration*>(m_exploration) != nullptr) {
210  pt.put_child("file_exploration", m_exploration->ToPtree());
211  } else {
212  assert(false && "Exploration type unknown.");
213  }
214 
215  for (const ExplorationTask& task : m_tasks) {
216  pt.add_child("tasks.task", task.ToPtree());
217  }
218  for (unsigned int task_id : m_send_queue) {
219  pt.add("send_queue.task_id", task_id);
220  }
221 
222  return pt;
223 }
224 
225 } // namespace
TaskState
Enumeration describing the state of a single task of the exploration.
void IncrementTries()
Increment the tries of running the task.
bool IsFinished() const
Return true if the exploration has finished.
Interface for Exploration.
The task is waiting to be sent for the first time.
TaskState GetState() const
Gets the state of the task.
ExplorationProgress(const Exploration *exploration)
Constructor.
boost::property_tree::ptree ToPtree() const
Convert the task to a ptree.
Interface for ExplorationTask.
A container class for the final result of a simulation.
Definition: SimResult.h:29
The task is being executed.
unsigned int GetNumberOfTries() const
Returns the number of tries.
std::string GetNodeIP() const
Returns the IP of the node that has the result.
Definition: SimResult.h:56
boost::property_tree::ptree ToPtree() const
Convert the exploration and progress to a ptree.
void GiveBack(const SimTask *task)
Give a task back to exploration (when it could not have been sent).
int GetTaskId() const
Return the id of the finished task.
Definition: SimResult.h:50
ResultType GetResult() const
Check if the simulation was successfully completed.
Definition: SimResult.h:53
void ResendCancelledTask(unsigned int id)
Resends a cancelled task.
void Updated()
Signal called when Exploration has changed.
virtual boost::property_tree::ptree ToPtree() const
Convert the exploration to a ptree.
Definition: Exploration.cpp:51
The task was finished.
virtual SimTask * CreateTask(unsigned int index) const =0
Creates a task with the parameters on the given index.
unsigned int GetTaskCount(const TaskState &state) const
Returns the number of tasks with the specified state.
Interface for SimResult.
virtual ~ExplorationProgress()
Destructor.
void HandleResult(const SimResult &result)
Handles the result of a simulation task.
void ChangeState(const TaskState &state)
Change the state of the task.
Interface for SimTask.
SimTask * NextTask()
Return the next task ready to be simulated.
Contains information about task in an exploration.
const ExplorationTask & GetTask(unsigned int id) const
Returns the state of the task with the specified id.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
Contains all information needed for a transmitable simulation task.
Definition: SimTask.h:31
Class describing a parameter exploration and its simulation tasks.
int GetId() const
Get the id of the task in the exploration.
Definition: SimTask.cpp:76
Class describing a generic exploration.
Definition: Exploration.h:33
int GetNodePort() const
Returns the Port of the TCP server of the node that has the result.
Definition: SimResult.h:59
Interface for ParameterExploration.
Interface for ExplorationProgress.
void CancelWaitingTask(unsigned int id)
Cancels a waiting task.
The task was cancelled or stopped.
Interface for FileExploration.