VPTissue Reference Manual
Simulator.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 "Simulator.h"
21 
22 #include "../../cpp_simptshell/cli/CliSimulationTask.h"
23 #include "cli/CliController.h"
24 #include "workspace/Workspace.h"
25 #include "util/misc/Exception.h"
26 
27 #include <boost/property_tree/ptree.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
29 #include <QDateTime>
30 #include <QDir>
31 #include <QFile>
32 #include <QFileInfo>
33 #include <QString>
34 #include <QSystemSemaphore>
35 
36 #include <memory>
37 #include <sstream>
38 #include <string>
39 
40 namespace {
41  const QString C_SEMKEY = "__NODE_SIM_SEMAPHORE__";
42  const std::string C_WORKSPACE_PATH = "parex_result_";
43 }
44 
45 namespace SimPT_Parex {
46 
47 using namespace std;
48 using namespace SimPT_Sim::Util;
49 using namespace SimPT_Shell;
50 
51 // http://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/
52 void remDir(QString dir)
53 {
54  QDir d(dir);
55  auto files = d.entryInfoList(QDir::NoDotAndDotDot
56  | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files);
57  for (auto file : files){
58  if (file.isDir()) {
59  remDir(file.absoluteFilePath());
60  } else if (file.isFile()){
61  QFile::remove(file.absoluteFilePath());
62  }
63  }
64  d.rmdir(dir);
65 
66 }
67 
68 shared_ptr<Ws::CliWorkspace> SetupWorkspace(const SimTask& task, const std::string& project_name,
69  const std::string& workspace_path, const std::string& file)
70 {
71  // Create directory for workspace
72  QDir dir;
73  QString path = QString::fromStdString(workspace_path);
74  if (!dir.exists(path)) {
75  dir.mkdir(path);
76  }
77 
78  // Create workspace (if necessary)
79  shared_ptr<Ws::CliWorkspace> w;
80  try {
81  w = make_shared<Ws::CliWorkspace>(workspace_path); // may throw
82  } catch (Exception& e) {
83  Ws::CliWorkspace::Init(workspace_path);
84  w = make_shared<Ws::CliWorkspace>(workspace_path); // won't throw
85  }
86 
87  // Create project
88  Ws::CliWorkspace::ProjectIterator p;
89  try {
90  p = w->New("project", project_name);
91  } catch (Exception& e) {
92  cerr << e.what() << endl;
93  return nullptr;
94  }
95 
96  // Add tissue file to project
97  write_xml(workspace_path + "/" + project_name + "/" + file, task.ToPtree());
98  try {
99  p->second->Add(file);
100  } catch (Exception& e) {
101  cerr << e.what() << endl;
102  return nullptr;
103  }
104 
105  return w;
106 }
107 
109  : m_controller(nullptr), m_stopped(false)
110 {
111 }
112 
114 {
115 }
116 
117 void Simulator::Delete(const std::string& name)
118 {
119  QSystemSemaphore m(C_SEMKEY, 1);
120  if (!m.acquire()) {
121  std::cerr << "SEMAPHORE ERROR: " << QSystemSemaphore::SystemSemaphoreError() << std::endl;
122  }
123  std::string temp = C_WORKSPACE_PATH + name;
124  remDir(QDir(temp.c_str()).absolutePath());
125  m.release();
126 }
127 
128 void Simulator::SolveTask(const SimTask &task)
129 {
130 
131  std::cout << "Starting Task.." << std::endl;
132 
133  const unsigned int num_steps = 0;
134  const bool num_steps_set = false;
135  std::string workspace_path = C_WORKSPACE_PATH + task.GetExploration();
136  std::stringstream ss;
137  ss << "Simulation" << task.GetId();
138  std::string project_name = ss.str();
139  const std::string sim_file = "SimLeaf.xml";
140  const bool file_set = true;
141 
142  SimResult::ResultType success = SimResult::ResultType::Failure;
143  m_stopped = false;
144 
145  QSystemSemaphore m(C_SEMKEY, 1);
146  if (!m.acquire()) {
147  std::cerr << "SEMAPHORE ERROR: " << QSystemSemaphore::SystemSemaphoreError() << std::endl;
148  }
149  auto workspace = SetupWorkspace(task, project_name, workspace_path, sim_file);
150 
151  m.release();
152  if (!workspace) {
153  std::cerr << "ERROR: Unable to setup workspace. Aborting simulation" << std::endl;
154  emit TaskSolved(SimResult(task.GetExploration(), task.GetId(), SimResult::ResultType::Failure));
155  return;
156  }
157 
158  SimPT_Shell::CliSimulationTask ctask(project_name, file_set, sim_file, num_steps_set, num_steps);
159  m_controller = SimPT_Shell::CliController::Create(workspace, false);
160  if (m_controller) {
161  try {
162  int result = m_controller->Execute(ctask);
163  if (result == 0) {
164  success = SimResult::ResultType::Success;
165  }
166  }
167  catch (std::exception &e) {
168  std::cerr << "Error while simulating: " << e.what() << std::endl;
169  }
170  catch (...) {
171  std::cerr << "Problem" << std::endl;
172  }
173  } else {
174  std::cerr << "Error set-up Controller" << std::endl;
175  }
176 
177  if (m_stopped) {
178  success = SimResult::ResultType::Stopped;
179  std::string s = workspace_path + "/" + project_name;
180  QString d = s.c_str();
181  remDir(QDir(d).absolutePath());
182  }
183 
184  emit TaskSolved(SimResult(task.GetExploration(), task.GetId(), success));
185 
186  m_controller = nullptr;
187 }
188 
190 {
191  m_stopped = true;
192  if (m_controller) {
193  m_controller->Terminate();
194  }
195 }
196 
197 } // namespace
Interface for CliController.
STL namespace.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
A container class for the final result of a simulation.
Definition: SimResult.h:29
virtual const char * what() const noexcept
Return error message.
Definition: Exception.h:38
Namespace for SimPT shell package.
Definition: Client.cpp:50
void StopTask()
Stop the current task.
Definition: Simulator.cpp:189
void TaskSolved(const SimResult &)
Emitted when task is solved. If it was unsuccssful or interrupted a flag is set in SimResult...
static std::shared_ptr< CliController > Create(const std::string &workspace, bool quiet)
Create controller.
virtual ~Simulator()
Destructor.
Definition: Simulator.cpp:113
Interface for the Simulator.
Extremely simple Exception root class.
Definition: Exception.h:28
void Delete(const std::string &name)
Delete a folder on the disk.
Definition: Simulator.cpp:117
virtual void SolveTask(const SimTask &task)
Do the real task of solving a SimTask and saving the results as requested by that task...
Definition: Simulator.cpp:128
Simulator()
Constructor.
Definition: Simulator.cpp:108
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
std::string GetExploration() const
Get the name of the exploration.
Definition: SimTask.cpp:71
Header file for Exception class.
Contains all information needed for a transmitable simulation task.
Definition: SimTask.h:31
int GetId() const
Get the id of the task in the exploration.
Definition: SimTask.cpp:76
A CliTask represents an invocation of the program from the command line.