45 #include "../data_structure/Application.h"
46 #include "../data_structure/ApplicationInstance.h"
49 #define SCENARIOMIMETYPE ".scx"
53 using namespace Data_Structure;
54 using namespace Parser;
63 tstruct = *localtime(&now);
66 ss << std::put_time(&tstruct,
"%Y-%m-%d.%X");
74 auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
75 chronotp - T::clock::now() + std::chrono::system_clock::now());
76 time_t time = std::chrono::system_clock::to_time_t(sctp);
78 tstruct = *localtime(&time);
81 ss << std::put_time(&tstruct,
"%Y-%m-%d.%X");
85 std::vector<std::string>
86 XMLScenarioParser::getScenariosFromFolder(
const std::string& folder)
88 namespace fs = std::filesystem;
90 std::vector<std::string> result;
91 if (exists(scenarioDir))
95 for (std::filesystem::recursive_directory_iterator end, dir(scenarioDir); dir != end;
98 if (dir->path().extension() ==
".scx")
100 result.push_back(fs::relative(dir->path(), scenarioDir).parent_path().string());
104 catch (std::exception& e)
113 XMLScenarioParser::parseScenario(
PackagePtr package, std::string scenName, std::string subfolder)
115 if (package ==
nullptr)
121 std::string appFolder = package->getScenarioPath();
122 appFolder.append(
"/");
123 if (!subfolder.empty())
125 appFolder.append(subfolder +
"/");
127 appFolder.append(scenName);
128 appFolder.append(
"/");
130 std::string path = appFolder;
131 path.append(scenName);
134 if (!std::filesystem::exists(std::filesystem::path(path)))
145 std::string globalConfigName;
152 globalConfigName =
"./config/global.cfg";
158 std::string lastWriteTime =
159 formatTTime(std::filesystem::last_write_time(std::filesystem::path(path)));
162 std::map<std::string, std::string> iceEnvVariables;
163 if (root_node.
has_node(
"iceEnvVariablesNode"))
169 std::string
value = iceEnvVariableNode.value();
170 iceEnvVariables[varName] =
value;
175 new Scenario(name, creation, lastWriteTime, package, globalConfigName, subfolder));
178 std::string appInstanceStr = application_node.attribute_value(
"instance");
179 std::string appName = application_node.attribute_value(
"name");
180 std::string packageName = application_node.attribute_value(
"package");
181 std::string appNodeName;
184 appNodeName = application_node.attribute_value(
"appNodeName");
194 enabled = application_node.attribute_as_bool(
"enabled",
"true",
"false");
201 CMakePackageFinder pFinder = CMakePackageFinderCache::GlobalCache.findPackage(packageName);
203 namespace fs = std::filesystem;
208 const std::string appExecutableName = [&]() -> std::string
212 std::string executableName = application_node.attribute_value(
214 return executableName;
218 if (fs::exists(executableDir / (appName +
"Run")))
220 return appName +
"Run";
223 if (fs::exists(executableDir / (appName +
"AppRun")))
225 return appName +
"AppRun";
228 if (fs::exists(executableDir / (appName +
"_run")))
230 return appName +
"_run";
234 "` could not be found.";
244 if (appInstanceStr.empty())
247 std::make_shared<ApplicationInstance>(*application,
249 appFolder +
"config/" + appName +
".cfg",
256 appInstance = std::make_shared<ApplicationInstance>(*application,
258 appFolder +
"config/" + appName +
259 "." + appInstanceStr +
".cfg",
264 appInstance->setNodeName(appNodeName);
266 appInstance->updateFound();
267 result->addApplication(appInstance);
275 if (result->isGlobalConfigFileexistent())
277 cfgProperties->load(result->getGlobalConfigPath());
280 Ice::PropertyDict dict = cfgProperties->getPropertiesForPrefix(
"");
282 for (
auto const& property : dict)
284 result->getGlobalConfig()->defineOptionalProperty<std::string>(
285 property.first,
"::NOT_DEFINED::",
"Custom Property");
286 result->getGlobalConfig()->getProperties()->setProperty(property.first, property.second);
293 XMLScenarioParser::parseScenario(
ScenarioPtr scenario)
295 return parseScenario(scenario->getPackage(), scenario->getName(), scenario->getSubfolder());
299 XMLScenarioParser::isScenarioexistent(
const std::string& name,
301 const std::string& subPath)
303 std::vector<std::string> paths = getScenariosFromFolder(package->getScenarioPath());
305 return std::find(paths.begin(), paths.end(), subPath + name) != paths.end();
309 XMLScenarioParser::createNewScenario(
const std::string& name,
PackagePtr package)
311 std::string scenarioDir = package->getScenarioPath();
312 if (scenarioDir.empty())
314 throw LocalException(
"The scenarios directory variable of " + package->getName() +
315 " is empty - did you run CMake successfully?");
317 if (!package->isScenarioPathWritable())
320 <<
"Warning: Scenario Path is not writable. Unable to create a new Scenario.";
334 if (!std::filesystem::create_directories(scenarioDir.append(
"/").append(name)) ||
335 !std::filesystem::create_directories(scenarioDir +
"/config"))
337 ARMARX_ERROR_S <<
"Can not create Scenario in package " << package->getName();
346 std::ofstream out2(scenarioDir.append(
"/config/global.cfg"));
350 return parseScenario(package, name);
354 XMLScenarioParser::saveScenario(
const ScenarioWPtr& wScenario,
bool saveApplications)
366 scenario->setLastChangedTime(currentDateStr);
369 scenarioNode.
append_attribute(
"globalConfigName", scenario->getGlobalConfigName());
370 scenarioNode.
append_attribute(
"package", scenario->getPackage()->getName());
374 saveScenarioApplications(scenario, scenarioNode, saveApplications);
377 std::string path = scenario->getPath();
378 if (scenario->isScenarioFileWriteable())
384 saveScenarioGlobalConfig(scenario);
390 for (
auto package : *packages)
393 CMakePackageFinderCache::GlobalCache.findPackage(package->getName());
395 if (scenario->getPath().find(scenarioDir) != std::string::npos)
404 XMLScenarioParser::getPackageNameFromScx(
const std::string& path)
408 if (!reader->getRoot(
"scenario").has_attribute(
"package"))
413 return reader->getRoot(
"scenario").attribute_value(
"package");
417 XMLScenarioParser::saveScenarioApplications(
ScenarioPtr scenario,
419 bool saveApplications)
421 std::vector<ApplicationInstancePtr> applicationInstances = *scenario->getApplications();
422 for (
const auto& app : applicationInstances)
424 if (saveApplications && app->isConfigWritable())
440 XMLScenarioParser::saveScenarioGlobalConfig(
ScenarioPtr scenario)
443 if (std::filesystem::is_symlink(scenario->getGlobalConfigPath()))
453 std::string resultStr;
457 pdcFormatter.setProperties(props->getProperties());
459 resultStr += pdcFormatter.formatPropertyDefinitionContainer(props);
462 size_t begin = resultStr.rfind(
"# Ice.Config:");
463 std::string ice_set_value =
"Ice.Config = <set value!>";
464 std::string ice_not_defined =
"Ice.Config = ::NOT_DEFINED::";
465 size_t end = resultStr.rfind(ice_set_value) + ice_set_value.length();
467 if (begin != std::string::npos)
469 if (end - ice_set_value.length() != std::string::npos)
471 resultStr.erase(begin, end - begin);
475 end = resultStr.rfind(ice_not_defined) + ice_not_defined.length();
476 if (end - ice_not_defined.length() != std::string::npos)
478 resultStr.erase(begin, end - begin);
483 std::ofstream cfgFile;
484 cfgFile.open(scenario->getGlobalConfigPath(), std::ofstream::out | std::ofstream::trunc);
485 if (cfgFile.fail() && scenario->isGlobalConfigWritable())
488 << scenario->getGlobalConfigPath();
491 cfgFile << resultStr;