Scenario.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Dennis Weigelt
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
26 #include "Scenario.h"
27 #include "../parser/XMLScenarioParser.h"
30 #include <filesystem>
31 #include <Ice/Properties.h>
32 #include <fstream>
33 
34 using namespace ScenarioManager;
35 
36 
37 Data_Structure::Scenario::Scenario(const std::string &name, const std::string& creationTime, const std::string& lastChangedTime,
38  const PackagePtr &package, const std::string& globalConfigName, const std::string& subfolder)
39  : name(name), creationTime(creationTime),
40  lastChangedTime(lastChangedTime),
41  globalConfigName(globalConfigName),
42  subfolder(subfolder),
43  nodeName("NodeMain"), package(package),
44  applications(new std::vector<ApplicationInstancePtr>()),
45  globalConfig(new armarx::PropertyDefinitionContainer(name))
46 {
48  auto* cfgInternal = dynamic_cast<armarx::IceProperties*>(cfgProperties.get());
49  cfgInternal->setInheritanceSolver(nullptr);
50 
51  globalConfig->setProperties(cfgProperties);
52  globalConfig->setPrefix("");
53  globalConfig->setDescription("Global Config from Scenario " + name);
54 }
55 
57 {
58  return this->name;
59 }
60 
61 void Data_Structure::Scenario::setName(std::string scenarioName)
62 {
63  name = std::move(scenarioName);
64 }
65 
67 {
68  return this->creationTime;
69 }
70 
72 {
73  return this->lastChangedTime;
74 }
75 
77 {
78  return globalConfigName;
79 }
80 
82 {
83  return subfolder;
84 }
85 
87 {
88  std::string scenarioPath = package.lock()->getScenarioPath();
89 
90  if (subfolder.empty())
91  {
92  scenarioPath.append("/").append(name).append("/").append(name).append(".scx");
93  }
94  else
95  {
96  scenarioPath.append("/").append(subfolder).append("/").append(name).append("/").append(name).append(".scx");
97  }
98 
99  return scenarioPath;
100 }
101 
103 {
104  std::string scenarioPath = package.lock()->getScenarioPath();
105 
106  scenarioPath.append("/").append(name);
107 
108  return scenarioPath;
109 }
110 
112 {
113  std::ofstream ofs;
114  ofs.open(getPath().c_str(), std::ofstream::out | std::ofstream::app);
115  ARMARX_DEBUG << getPath() << " is writeable: " << ofs.is_open();
116 
117  return ofs.is_open();
118 
119  std::filesystem::file_status s = std::filesystem::status(std::filesystem::path(getPath()));
120  if ((s.permissions() & std::filesystem::perms::owner_write) != std::filesystem::perms::none)
121  {
122  return true;
123  }
124  return false;
125 }
126 
128 {
129  return package.lock();
130 }
131 
133 {
134  std::string status = ApplicationStatus::Unknown;
135  for (const auto& app : *applications)
136  {
137  if (!app->getEnabled())
138  {
139  continue;
140  }
141  if (status == ApplicationStatus::Unknown && app->getStatus() == ApplicationStatus::Running)
142  {
144  }
145  else if (status == ApplicationStatus::Unknown && app->getStatus() == ApplicationStatus::Stopped)
146  {
148  }
149  else if (status == ApplicationStatus::Unknown && app->getStatus() == ApplicationStatus::Inactive)
150  {
152  }
153  else if (status == ApplicationStatus::Running && app->getStatus() == ApplicationStatus::Running)
154  {
156  }
157  else if (status == ApplicationStatus::Stopped && app->getStatus() == ApplicationStatus::Stopped)
158  {
160  }
161  else if (status == ApplicationStatus::Inactive && app->getStatus() == ApplicationStatus::Inactive)
162  {
164  }
165  else if ((status == ApplicationStatus::Running && app->getStatus() == ApplicationStatus::Stopped)
166  || (status == ApplicationStatus::Stopped && app->getStatus() == ApplicationStatus::Running)
167  || (status == ApplicationStatus::Stopped && app->getStatus() == ApplicationStatus::Inactive)
168  || (status == ApplicationStatus::Running && app->getStatus() == ApplicationStatus::Inactive)
169  || (status == ApplicationStatus::Inactive && app->getStatus() == ApplicationStatus::Running)
170  || (status == ApplicationStatus::Inactive && app->getStatus() == ApplicationStatus::Stopped))
171  {
173  break;
174  }
175  else if (app->getStatus() == ApplicationStatus::Waiting)
176  {
178  break;
179  }
180  else if (app->getStatus() == ApplicationStatus::Unknown)
181  {
183  break;
184  }
185  else
186  {
188  break;
189  }
190  }
191 
192  return status;
193 }
194 
196 {
197  this->lastChangedTime = std::move(time);
198 }
199 
201 {
202  globalConfigName = std::move(configName);
203 }
204 
206 {
207  return applications;
208 }
209 
211 {
212  for (auto app : *applications)
213  {
214  if (app->getName() == appName)
215  {
216  return app;
217  }
218  }
219 
220  return {nullptr};
221 }
222 
224 {
225  applications->push_back(application);
226 }
227 
229 {
230  for (auto iter = applications->begin(); iter != applications->end(); ++iter)
231  {
232  if (*iter == application)
233  {
234  (*iter)->deleteConfig();
235  applications->erase(iter);
236  return;
237  }
238  }
239 }
240 
241 void Data_Structure::Scenario::save(bool saveApplications)
242 {
244  parser.saveScenario(shared_from_this(), saveApplications);
245 }
246 
248 {
249  if (!isReadOnly())
250  {
251  return;
252  }
253 
254  if (!std::filesystem::is_symlink(getGlobalConfigPath()))
255  {
256  ARMARX_WARNING << "Make local called on non-linked global scenario config";
257  return;
258  }
259 
260  std::filesystem::remove(getGlobalConfigPath());
261  this->save();
262  // Reload to register status changes
263  this->reloadGlobalConf();
264 }
265 
267 {
268  return std::filesystem::is_symlink(getGlobalConfigPath());
269 }
270 
272 {
273  for (const auto& it : *applications)
274  {
275  it->load();
276  }
277 }
278 
280 {
281  if (isGlobalConfigFileexistent())
282  {
283  globalConfig->getProperties()->load(getGlobalConfigPath());
284  }
285 }
286 
288 {
289  return globalConfig;
290 }
291 
293 {
294  std::filesystem::path scenarioPath;
295  if (subfolder.empty())
296  {
297  scenarioPath = std::filesystem::path(package.lock()->getScenarioPath().append("/").append(name).append("/"));
298  }
299  else
300  {
301  scenarioPath = std::filesystem::path(package.lock()->getScenarioPath().append("/").append(subfolder + "/").append(name).append("/"));
302  }
303 
304  scenarioPath = scenarioPath / std::filesystem::path(globalConfigName.empty() ? "./config/global.cfg" : globalConfigName);
305  return scenarioPath.string();
306 }
307 
309 {
310  std::ofstream ofs;
311  ofs.open(getGlobalConfigPath().c_str(), std::ofstream::out | std::ofstream::app);
312  ARMARX_DEBUG << getGlobalConfigPath() << " is writeable: " << ofs.is_open();
313 
314  return ofs.is_open();
315 }
316 
318 {
319  return std::filesystem::exists(std::filesystem::path(getGlobalConfigPath()));
320 }
321 
323 {
324  for (const auto& app : *applications)
325  {
326  if (app->getStatusWriteBlock())
327  {
328  return true;
329  }
330  }
331  return false;
332 }
333 
335 {
336  for (const auto& app : *applications)
337  {
338  app->setStatusWriteBlock(state);
339  }
340 }
341 
343 {
344  std::vector<std::string> result;
345  for (const auto& app : *applications)
346  {
347  result.push_back(app->getEffectiveNodeName());
348  }
349  return result;
350 }
351 
353 {
354  for (const auto& app : *applications)
355  {
356  app->updateFound();
357  if (!app->getFound())
358  {
359  return false;
360  }
361  }
362  return true;
363 }
364 
365 
367 {
368  return nodeName;
369 }
370 
372 {
373  nodeName = value;
374 }
ScenarioManager::Data_Structure::Scenario::getName
std::string getName()
Definition: Scenario.cpp:56
ScenarioManager::Data_Structure::Scenario::getGlobalConfigPath
std::string getGlobalConfigPath()
Definition: Scenario.cpp:292
ScenarioManager::Data_Structure::ApplicationInstancePtr
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
Definition: ApplicationInstance.h:33
ScenarioManager::Data_Structure::Scenario::getGlobalConfig
armarx::PropertyDefinitionsPtr getGlobalConfig()
Definition: Scenario.cpp:287
ScenarioManager::Data_Structure::Scenario::addApplication
void addApplication(const ApplicationInstancePtr &application)
Adds an Application to this scenario.
Definition: Scenario.cpp:223
Scenario.h
ScenarioManager::Data_Structure::ApplicationInstanceVectorPtr
std::shared_ptr< std::vector< ApplicationInstancePtr > > ApplicationInstanceVectorPtr
Definition: ApplicationInstance.h:35
ScenarioManager::Data_Structure::Scenario::setNodeName
void setNodeName(const std::string &value)
Definition: Scenario.cpp:371
ScenarioManager::Data_Structure::Scenario::setName
void setName(std::string name)
Definition: Scenario.cpp:61
ScenarioManager::Data_Structure::Scenario::setLastChangedTime
void setLastChangedTime(std::string time)
Sets the last-changed-time to now.
Definition: Scenario.cpp:195
ScenarioManager::Parser::XMLScenarioParser
This class provides different methods to parse and save scenario data in XML-Files....
Definition: XMLScenarioParser.h:56
ScenarioManager::Data_Structure::ApplicationStatus::Stopped
static const std::string Stopped
Definition: ApplicationInstance.h:47
ScenarioManager::Data_Structure::Scenario::getPath
std::string getPath()
Definition: Scenario.cpp:86
ScenarioManager::Data_Structure::Scenario::makeLocalGlobalConfig
void makeLocalGlobalConfig()
Definition: Scenario.cpp:247
ScenarioManager::Data_Structure::Scenario::reloadGlobalConf
void reloadGlobalConf()
Definition: Scenario.cpp:279
ScenarioManager::Data_Structure::ApplicationStatus::Running
static const std::string Running
Definition: ApplicationInstance.h:48
ScenarioManager::Data_Structure::Scenario::getStatus
std::string getStatus()
Definition: Scenario.cpp:132
ScenarioManager::Data_Structure::Scenario::allApplicationsFound
bool allApplicationsFound()
Definition: Scenario.cpp:352
IceInternal::Handle< ::Ice::Properties >
armarx::navigation::algorithms::save
bool save(const Costmap &costmap, const std::filesystem::path &directory)
Definition: persistence.cpp:101
ScenarioManager::Data_Structure::Scenario::save
void save(bool saveApplications=true)
Definition: Scenario.cpp:241
ScenarioManager::Data_Structure::Scenario::getApplications
ApplicationInstanceVectorPtr getApplications()
Definition: Scenario.cpp:205
armarx::status
status
Definition: FiniteStateMachine.h:259
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
ScenarioManager::Data_Structure::Scenario::getFolder
std::string getFolder()
Definition: Scenario.cpp:102
ScenarioManager::Data_Structure::Scenario::reloadAppInstances
void reloadAppInstances()
Definition: Scenario.cpp:271
ScenarioManager::Data_Structure::PackagePtr
std::shared_ptr< Package > PackagePtr
Definition: Package.h:121
ScenarioManager::Data_Structure::Scenario::getNodeName
std::string getNodeName() const
Definition: Scenario.cpp:366
ScenarioManager::Data_Structure::Scenario::getSubfolder
std::string getSubfolder()
Definition: Scenario.cpp:81
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
ScenarioManager::Data_Structure::Scenario::removeApplication
void removeApplication(const ApplicationInstancePtr &application)
Removes an Application from this scenario.
Definition: Scenario.cpp:228
ScenarioManager::Data_Structure::ApplicationStatus::Waiting
static const std::string Waiting
Definition: ApplicationInstance.h:49
armarx::IceProperties::create
static Ice::PropertiesPtr create(const Ice::PropertiesPtr &iceProperties=nullptr)
Definition: IceProperties.cpp:32
ScenarioManager::Data_Structure::ApplicationStatus::Inactive
static const std::string Inactive
Definition: ApplicationInstance.h:52
ScenarioManager::Data_Structure::ApplicationStatus::Mixed
static const std::string Mixed
Definition: ApplicationInstance.h:50
ScenarioManager::Data_Structure::ApplicationStatus::Unknown
static const std::string Unknown
Definition: ApplicationInstance.h:51
ScenarioManager::Data_Structure::Scenario::getCreationTime
std::string getCreationTime()
SCENARIO_H.
Definition: Scenario.cpp:66
ScenarioManager::Data_Structure::Scenario::getGlobalConfigName
std::string getGlobalConfigName()
Definition: Scenario.cpp:76
ScenarioManager::Data_Structure::Scenario::getStatusWriteBlock
bool getStatusWriteBlock()
Definition: Scenario.cpp:322
armarx::IceProperties::setInheritanceSolver
virtual void setInheritanceSolver(const InheritanceSolverPtr &inheritanceSolver)
Sets an inheritance solver in case of testing or using a non-default solver.
Definition: IceProperties.cpp:217
ScenarioManager::Data_Structure::Scenario::isGlobalConfigFileexistent
bool isGlobalConfigFileexistent()
Definition: Scenario.cpp:317
armarx::IceProperties
IceProperties stores ice properties and resolves property inheritance.
Definition: IceProperties.h:120
IceStorm::parser
Parser * parser
Definition: Parser.cpp:33
ScenarioManager::Data_Structure::Scenario::getAllDeploymendNodeNames
std::vector< std::string > getAllDeploymendNodeNames()
Definition: Scenario.cpp:342
ScenarioManager::Data_Structure::Scenario::isScenarioFileWriteable
bool isScenarioFileWriteable()
Definition: Scenario.cpp:111
ScenarioManager::Data_Structure::Scenario::isReadOnly
bool isReadOnly()
Definition: Scenario.cpp:266
ScenarioManager
Definition: Application.cpp:166
ScenarioManager::Data_Structure::Scenario::getPackage
PackagePtr getPackage()
Definition: Scenario.cpp:127
std
Definition: Application.h:66
IceUtil::Handle< class PropertyDefinitionContainer >
ScenarioManager::Data_Structure::Scenario::isGlobalConfigWritable
bool isGlobalConfigWritable()
Definition: Scenario.cpp:308
ScenarioManager::Data_Structure::Scenario::getLastChangedTime
std::string getLastChangedTime()
Definition: Scenario.cpp:71
ScenarioManager::Data_Structure::Scenario::setGlobalConfigName
void setGlobalConfigName(std::string name)
Definition: Scenario.cpp:200
ScenarioManager::Data_Structure::Scenario::setStatusWriteBlock
void setStatusWriteBlock(bool state)
Definition: Scenario.cpp:334
ScenarioManager::Data_Structure::Scenario::Scenario
Scenario(const std::string &name, const std::string &creationTime, const std::string &lastChangedTime, const PackagePtr &package, const std::string &globalConfigName="./config/global.cfg", const std::string &subfolder="")
Constructor that sets some base information about the scenario.
Definition: Scenario.cpp:37
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
IceProperties.h
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
ScenarioManager::Data_Structure::Scenario::getApplicationByName
ApplicationInstancePtr getApplicationByName(const std::string &name)
Definition: Scenario.cpp:210
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28