VPTissue Reference Manual
ExplorationTask.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 "ExplorationTask.h"
21 
22 #include <cassert>
23 
24 using boost::property_tree::ptree;
25 
26 namespace SimPT_Parex {
27 
29  : m_state(TaskState::Waiting), m_start_time(), m_running_time(0), m_tries(0), m_node_ip("Unknown"), m_node_port(0)
30 {
31 }
32 
33 ExplorationTask::ExplorationTask(const boost::property_tree::ptree& pt)
34 {
35  ReadPtree(pt);
36 }
37 
39 {
40 }
41 
43 {
44  assert(m_state != TaskState::Finished
45  && m_state != TaskState::Failed
46  && "The task has finished or failed, so changing the state wouldn't make sense.");
47 
48  if (m_state != state) {
49  if (m_state == TaskState::Running) {
50  assert(m_start_time.isValid() && "The start time should be valid.");
51  int secs = m_start_time.secsTo(QDateTime::currentDateTime());
52  assert(secs >= 0 && "The start time can't be later than the current time.");
53  m_running_time += secs;
54  m_start_time = QDateTime();
55  }
56  else if (state == TaskState::Running) {
57  assert(!m_start_time.isValid()
58  && "The start time shouldn't have been set when the task wasn't running.");
59  m_start_time = QDateTime::currentDateTime();
60  }
61 
62  m_state = state;
63  }
64 }
65 
66 unsigned int ExplorationTask::GetRunningTime() const
67 {
68  if (m_state == TaskState::Running) {
69  assert(m_start_time.isValid() && "The start time should be valid.");
70  int secs = m_start_time.secsTo(QDateTime::currentDateTime());
71  assert(secs >= 0 && "The start time can't be later than the current time.");
72  return m_running_time + secs;
73  } else {
74  return m_running_time;
75  }
76 }
77 
79 {
80  m_tries += 1;
81 }
82 
83 void ExplorationTask::ReadPtree(const boost::property_tree::ptree& pt)
84 {
85  m_state = static_cast<TaskState>(pt.get<int>("state"));
86  if (pt.find("start_time") != pt.not_found()) {
87  m_start_time = QDateTime::fromString(QString::fromStdString(pt.get<std::string>("start_time")));
88  } else {
89  m_start_time = QDateTime();
90  }
91  m_running_time = pt.get<unsigned int>("running_time");
92  m_tries = pt.get<unsigned int>("tries");
93 
94  m_node_ip = pt.get<std::string>("node_ip");
95  m_node_port = pt.get<int>("node_port");
96 }
97 
99 {
100  ptree pt;
101  pt.put("state", static_cast<int>(m_state));
102  if (m_start_time.isValid()) {
103  pt.put("start_time", m_start_time.toString().toStdString());
104  }
105  pt.put("running_time", m_running_time);
106  pt.put("tries", m_tries);
107 
108  pt.put("node_ip", m_node_ip);
109  pt.put("node_port", m_node_port);
110 
111 
112  return pt;
113 }
114 
115 } // namespace
virtual ~ExplorationTask()
Destructor.
TaskState
Enumeration describing the state of a single task of the exploration.
void IncrementTries()
Increment the tries of running the task.
The task is waiting to be sent for the first time.
boost::property_tree::ptree ToPtree() const
Convert the task to a ptree.
Interface for ExplorationTask.
The task is being executed.
unsigned int GetRunningTime() const
Returns the time the task has run.
ExplorationTask()
Constructs a task in 'Waiting' state.
void ChangeState(const TaskState &state)
Change the state of the task.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
The task was cancelled or stopped.