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
28#include <filesystem>
29#include <fstream>
30
31#include <Ice/Properties.h>
32
35
37
38using namespace ScenarioManager;
39
40Data_Structure::Scenario::Scenario(const std::string& name,
41 const std::string& creationTime,
42 const std::string& lastChangedTime,
43 const PackagePtr& package,
44 const std::string& globalConfigName,
45 const std::string& subfolder) :
46 name(name),
47 creationTime(creationTime),
48 lastChangedTime(lastChangedTime),
49 globalConfigName(globalConfigName),
50 subfolder(subfolder),
51 nodeName("NodeMain"),
52 package(package),
53 applications(new std::vector<ApplicationInstancePtr>()),
54 globalConfig(new armarx::PropertyDefinitionContainer(name))
55{
57 auto* cfgInternal = dynamic_cast<armarx::IceProperties*>(cfgProperties.get());
58 cfgInternal->setInheritanceSolver(nullptr);
59
60 globalConfig->setProperties(cfgProperties);
61 globalConfig->setPrefix("");
62 globalConfig->setDescription("Global Config from Scenario " + name);
63}
64
65std::string
67{
68 return this->name;
69}
70
71void
72Data_Structure::Scenario::setName(std::string scenarioName)
73{
74 name = std::move(scenarioName);
75}
76
77std::string
79{
80 return this->creationTime;
81}
82
83std::string
85{
86 return this->lastChangedTime;
87}
88
89std::string
91{
92 return globalConfigName;
93}
94
95std::string
97{
98 return subfolder;
99}
100
101std::string
103{
104 std::string scenarioPath = package.lock()->getScenarioPath();
105
106 if (subfolder.empty())
107 {
108 scenarioPath.append("/").append(name).append("/").append(name).append(".scx");
109 }
110 else
111 {
112 scenarioPath.append("/")
113 .append(subfolder)
114 .append("/")
115 .append(name)
116 .append("/")
117 .append(name)
118 .append(".scx");
119 }
120
121 return scenarioPath;
122}
123
124std::string
126{
127 std::string scenarioPath = package.lock()->getScenarioPath();
128
129 scenarioPath.append("/").append(name);
130
131 return scenarioPath;
132}
133
134bool
136{
137 std::ofstream ofs;
138 ofs.open(getPath().c_str(), std::ofstream::out | std::ofstream::app);
139 ARMARX_DEBUG << getPath() << " is writeable: " << ofs.is_open();
140
141 return ofs.is_open();
142
143 std::filesystem::file_status s = std::filesystem::status(std::filesystem::path(getPath()));
144 if ((s.permissions() & std::filesystem::perms::owner_write) != std::filesystem::perms::none)
145 {
146 return true;
147 }
148 return false;
149}
150
153{
154 return package.lock();
155}
156
157std::string
159{
160 std::string status = ApplicationStatus::Unknown;
161 for (const auto& app : *applications)
162 {
163 if (!app->getEnabled())
164 {
165 continue;
166 }
167 if (status == ApplicationStatus::Unknown && app->getStatus() == ApplicationStatus::Running)
168 {
170 }
171 else if (status == ApplicationStatus::Unknown &&
172 app->getStatus() == ApplicationStatus::Stopped)
173 {
175 }
176 else if (status == ApplicationStatus::Unknown &&
177 app->getStatus() == ApplicationStatus::Inactive)
178 {
180 }
181 else if (status == ApplicationStatus::Running &&
182 app->getStatus() == ApplicationStatus::Running)
183 {
185 }
186 else if (status == ApplicationStatus::Stopped &&
187 app->getStatus() == ApplicationStatus::Stopped)
188 {
190 }
191 else if (status == ApplicationStatus::Inactive &&
192 app->getStatus() == ApplicationStatus::Inactive)
193 {
195 }
196 else if ((status == ApplicationStatus::Running &&
197 app->getStatus() == ApplicationStatus::Stopped) ||
198 (status == ApplicationStatus::Stopped &&
199 app->getStatus() == ApplicationStatus::Running) ||
200 (status == ApplicationStatus::Stopped &&
201 app->getStatus() == ApplicationStatus::Inactive) ||
202 (status == ApplicationStatus::Running &&
203 app->getStatus() == ApplicationStatus::Inactive) ||
204 (status == ApplicationStatus::Inactive &&
205 app->getStatus() == ApplicationStatus::Running) ||
206 (status == ApplicationStatus::Inactive &&
207 app->getStatus() == ApplicationStatus::Stopped))
208 {
210 break;
211 }
212 else if (app->getStatus() == ApplicationStatus::Waiting)
213 {
215 break;
216 }
217 else if (app->getStatus() == ApplicationStatus::Unknown)
218 {
220 break;
221 }
222 else
223 {
225 break;
226 }
227 }
228
229 return status;
230}
231
232void
234{
235 this->lastChangedTime = std::move(time);
236}
237
238void
240{
241 globalConfigName = std::move(configName);
242}
243
246{
247 return applications;
248}
249
252{
253 for (auto app : *applications)
254 {
255 if (app->getName() == appName)
256 {
257 return app;
258 }
259 }
260
261 return {nullptr};
262}
263
264void
266{
267 applications->push_back(application);
268}
269
270void
272 const Data_Structure::ApplicationInstancePtr& application) //?
273{
274 for (auto iter = applications->begin(); iter != applications->end(); ++iter)
275 {
276 if (*iter == application)
277 {
278 (*iter)->deleteConfig();
279 applications->erase(iter);
280 return;
281 }
282 }
283}
284
285void
286Data_Structure::Scenario::save(bool saveApplications)
287{
289 parser.saveScenario(shared_from_this(), saveApplications);
290}
291
292void
294{
295 if (!isReadOnly())
296 {
297 return;
298 }
299
300 if (!std::filesystem::is_symlink(getGlobalConfigPath()))
301 {
302 ARMARX_WARNING << "Make local called on non-linked global scenario config";
303 return;
304 }
305
306 std::filesystem::remove(getGlobalConfigPath());
307 this->save();
308 // Reload to register status changes
309 this->reloadGlobalConf();
310}
311
312bool
314{
315 return std::filesystem::is_symlink(getGlobalConfigPath());
316}
317
318void
320{
321 for (const auto& it : *applications)
322 {
323 it->load();
324 }
325}
326
327void
329{
331 {
332 globalConfig->getProperties()->load(getGlobalConfigPath());
333 }
334}
335
338{
339 return globalConfig;
340}
341
342std::string
344{
345 std::filesystem::path scenarioPath;
346 if (subfolder.empty())
347 {
348 scenarioPath = std::filesystem::path(
349 package.lock()->getScenarioPath().append("/").append(name).append("/"));
350 }
351 else
352 {
353 scenarioPath = std::filesystem::path(package.lock()
354 ->getScenarioPath()
355 .append("/")
356 .append(subfolder + "/")
357 .append(name)
358 .append("/"));
359 }
360
361 scenarioPath =
362 scenarioPath /
363 std::filesystem::path(globalConfigName.empty() ? "./config/global.cfg" : globalConfigName);
364 return scenarioPath.string();
365}
366
367bool
369{
370 std::ofstream ofs;
371 ofs.open(getGlobalConfigPath().c_str(), std::ofstream::out | std::ofstream::app);
372 ARMARX_DEBUG << getGlobalConfigPath() << " is writeable: " << ofs.is_open();
373
374 return ofs.is_open();
375}
376
377bool
379{
380 return std::filesystem::exists(std::filesystem::path(getGlobalConfigPath()));
381}
382
383bool
385{
386 for (const auto& app : *applications)
387 {
388 if (app->getStatusWriteBlock())
389 {
390 return true;
391 }
392 }
393 return false;
394}
395
396void
398{
399 for (const auto& app : *applications)
400 {
401 app->setStatusWriteBlock(state);
402 }
403}
404
405std::vector<std::string>
407{
408 std::vector<std::string> result;
409 for (const auto& app : *applications)
410 {
411 result.push_back(app->getEffectiveNodeName());
412 }
413 return result;
414}
415
416bool
418{
419 for (const auto& app : *applications)
420 {
421 app->updateFound();
422 if (!app->getFound())
423 {
424 return false;
425 }
426 }
427 return true;
428}
429
430std::string
432{
433 return nodeName;
434}
435
436void
438{
439 nodeName = value;
440}
ApplicationInstancePtr getApplicationByName(const std::string &name)
Definition Scenario.cpp:251
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:40
void removeApplication(const ApplicationInstancePtr &application)
Removes an Application from this scenario.
Definition Scenario.cpp:271
void setNodeName(const std::string &value)
Definition Scenario.cpp:437
std::vector< std::string > getAllDeploymendNodeNames()
Definition Scenario.cpp:406
void setLastChangedTime(std::string time)
Sets the last-changed-time to now.
Definition Scenario.cpp:233
void save(bool saveApplications=true)
Definition Scenario.cpp:286
void addApplication(const ApplicationInstancePtr &application)
Adds an Application to this scenario.
Definition Scenario.cpp:265
ApplicationInstanceVectorPtr getApplications()
Definition Scenario.cpp:245
void setGlobalConfigName(std::string name)
Definition Scenario.cpp:239
std::string getCreationTime()
SCENARIO_H.
Definition Scenario.cpp:78
armarx::PropertyDefinitionsPtr getGlobalConfig()
Definition Scenario.cpp:337
This class provides different methods to parse and save scenario data in XML-Files.
IceProperties stores ice properties and resolves property inheritance.
virtual void setInheritanceSolver(const InheritanceSolverPtr &inheritanceSolver)
Sets an inheritance solver in case of testing or using a non-default solver.
static Ice::PropertiesPtr create(const Ice::PropertiesPtr &iceProperties=nullptr)
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
std::shared_ptr< Package > PackagePtr
Definition Package.h:122
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
std::shared_ptr< std::vector< ApplicationInstancePtr > > ApplicationInstanceVectorPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.