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::vector<std::string> stati;
161 stati.reserve(applications->size());
162 for (const auto& app : *applications)
163 {
164 if (!app->getEnabled())
165 {
166 continue;
167 }
168 stati.push_back(app->getStatus());
169 }
170
171 return ApplicationStatus::Aggregate(stati);
172}
173
174void
176{
177 this->lastChangedTime = std::move(time);
178}
179
180void
182{
183 globalConfigName = std::move(configName);
184}
185
188{
189 return applications;
190}
191
194{
195 for (auto app : *applications)
196 {
197 if (app->getName() == appName)
198 {
199 return app;
200 }
201 }
202
203 return {nullptr};
204}
205
206void
208{
209 applications->push_back(application);
210}
211
212void
214 const Data_Structure::ApplicationInstancePtr& application) //?
215{
216 for (auto iter = applications->begin(); iter != applications->end(); ++iter)
217 {
218 if (*iter == application)
219 {
220 (*iter)->deleteConfig();
221 applications->erase(iter);
222 return;
223 }
224 }
225}
226
227void
228Data_Structure::Scenario::save(bool saveApplications)
229{
231 parser.saveScenario(shared_from_this(), saveApplications);
232}
233
234void
236{
237 if (!isReadOnly())
238 {
239 return;
240 }
241
242 if (!std::filesystem::is_symlink(getGlobalConfigPath()))
243 {
244 ARMARX_WARNING << "Make local called on non-linked global scenario config";
245 return;
246 }
247
248 std::filesystem::remove(getGlobalConfigPath());
249 this->save();
250 // Reload to register status changes
251 this->reloadGlobalConf();
252}
253
254bool
256{
257 return std::filesystem::is_symlink(getGlobalConfigPath());
258}
259
260void
262{
263 for (const auto& it : *applications)
264 {
265 it->load();
266 }
267}
268
269void
271{
273 {
274 globalConfig->getProperties()->load(getGlobalConfigPath());
275 }
276}
277
280{
281 return globalConfig;
282}
283
284std::string
286{
287 std::filesystem::path scenarioPath;
288 if (subfolder.empty())
289 {
290 scenarioPath = std::filesystem::path(
291 package.lock()->getScenarioPath().append("/").append(name).append("/"));
292 }
293 else
294 {
295 scenarioPath = std::filesystem::path(package.lock()
296 ->getScenarioPath()
297 .append("/")
298 .append(subfolder + "/")
299 .append(name)
300 .append("/"));
301 }
302
303 scenarioPath =
304 scenarioPath /
305 std::filesystem::path(globalConfigName.empty() ? "./config/global.cfg" : globalConfigName);
306 return scenarioPath.string();
307}
308
309bool
311{
312 std::ofstream ofs;
313 ofs.open(getGlobalConfigPath().c_str(), std::ofstream::out | std::ofstream::app);
314 ARMARX_DEBUG << getGlobalConfigPath() << " is writeable: " << ofs.is_open();
315
316 return ofs.is_open();
317}
318
319bool
321{
322 return std::filesystem::exists(std::filesystem::path(getGlobalConfigPath()));
323}
324
325std::vector<std::string>
327{
328 std::vector<std::string> result;
329 for (const auto& app : *applications)
330 {
331 result.push_back(app->getEffectiveNodeName());
332 }
333 return result;
334}
335
336bool
338{
339 for (const auto& app : *applications)
340 {
341 app->updateFound();
342 if (!app->getFound())
343 {
344 return false;
345 }
346 }
347 return true;
348}
349
350std::string
352{
353 return nodeName;
354}
355
356void
358{
359 nodeName = value;
360}
ApplicationInstancePtr getApplicationByName(const std::string &name)
Definition Scenario.cpp:193
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:213
void setNodeName(const std::string &value)
Definition Scenario.cpp:357
std::vector< std::string > getAllDeploymendNodeNames()
Definition Scenario.cpp:326
void setLastChangedTime(std::string time)
Sets the last-changed-time to now.
Definition Scenario.cpp:175
void save(bool saveApplications=true)
Definition Scenario.cpp:228
void addApplication(const ApplicationInstancePtr &application)
Adds an Application to this scenario.
Definition Scenario.cpp:207
ApplicationInstanceVectorPtr getApplications()
Definition Scenario.cpp:187
void setGlobalConfigName(std::string name)
Definition Scenario.cpp:181
std::string getCreationTime()
SCENARIO_H.
Definition Scenario.cpp:78
armarx::PropertyDefinitionsPtr getGlobalConfig()
Definition Scenario.cpp:279
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:182
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:191
::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.
static std::string Aggregate(const std::vector< std::string > &stati)
Combines the stati of several applications into the status of their common parent (a scenario or the ...