49#define SCENARIOMIMETYPE ".scx"
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");
85std::vector<std::string>
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)
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"))
168 std::string varName = iceEnvVariableNode.attribute_value(
"name");
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");
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);
295 return parseScenario(scenario->getPackage(), scenario->getName(), scenario->getSubfolder());
301 const std::string& subPath)
305 return std::find(paths.begin(), paths.end(), subPath + name) != paths.end();
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"));
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)
395 if (scenario->getPath().find(scenarioDir) != std::string::npos)
408 if (!reader->getRoot(
"scenario").has_attribute(
"package"))
413 return reader->getRoot(
"scenario").attribute_value(
"package");
417XMLScenarioParser::saveScenarioApplications(
ScenarioPtr scenario,
419 bool saveApplications)
421 std::vector<ApplicationInstancePtr> applicationInstances = *scenario->getApplications();
422 for (
const auto& app : applicationInstances)
424 if (saveApplications && app->isConfigWritable())
440XMLScenarioParser::saveScenarioGlobalConfig(
ScenarioPtr scenario)
443 if (std::filesystem::is_symlink(scenario->getGlobalConfigPath()))
449 armarx::IceProperties* propsInternals =
450 dynamic_cast<armarx::IceProperties*
>(props->getProperties().get());
453 std::string resultStr;
455 PropertyDefinitionConfigFormatter defFormatter;
456 PropertyDefinitionContainerFormatter pdcFormatter(defFormatter);
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;
std::string currentDateTime()
std::string formatTTime(T chronotp)
Class containing data about a scenario and its applications.
static std::string getPackageNameFromScx(const std::string &path)
static ScenarioManager::Data_Structure::ScenarioPtr parseScenario(Data_Structure::PackagePtr package, std::string name, std::string subfolder="")
Parses a .xml scenario file and creates a Scenario object out of it.
static void saveScenario(const Data_Structure::ScenarioWPtr &scenario, bool saveApplications)
Saves a Scenario by recreating its .xml file based on its data.
static ScenarioManager::Data_Structure::PackagePtr getScenarioPackage(const ScenarioManager::Data_Structure::ScenarioPtr &scenario, const ScenarioManager::Data_Structure::PackageVectorPtr &packages)
parseScenarioByFile parses an scenario file and returns the curresponding ScenarioPtr.
static ScenarioManager::Data_Structure::ScenarioPtr createNewScenario(const std::string &name, ScenarioManager::Data_Structure::PackagePtr package)
Creates a new scenario with the given name within the given package.
static bool isScenarioexistent(const std::string &name, ScenarioManager::Data_Structure::PackagePtr package, const std::string &subPath="")
isScenarioexistent
static std::vector< std::string > getScenariosFromFolder(const std::string &folder)
Finds all .xml scenario files in a folder and returns a list of paths to them.
Baseclass for all ArmarX applications.
static CMakePackageFinderCache GlobalCache
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
std::string getBinaryDir() const
std::string getScenariosDir() const
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)
RapidXmlReaderNode first_node(const char *name=nullptr) const
std::vector< RapidXmlReaderNode > nodes(const char *name=nullptr) const
bool has_attribute(const char *attrName) const
bool has_node(const char *nodeName) const
std::string attribute_value(const char *attrName) const
static RapidXmlReaderPtr FromFile(const std::string &path)
RapidXmlWriterNode & append_bool_attribute(const std::string &name, const std::string &trueValue, const std::string &falseValue, bool value)
RapidXmlWriterNode append_node(const std::string &name)
RapidXmlWriterNode & append_attribute(const std::string &name, const std::string &value)
void saveToFile(const std::string &path, bool indent)
RapidXmlWriterNode createRootNode(const std::string &name)
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
std::shared_ptr< Scenario > ScenarioPtr
std::shared_ptr< std::vector< ScenarioManager::Data_Structure::PackagePtr > > PackageVectorPtr
std::weak_ptr< Scenario > ScenarioWPtr
std::shared_ptr< Package > PackagePtr
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
fs::path remove_trailing_separator(fs::path p)
IceUtil::Handle< Application > ApplicationPtr
std::shared_ptr< RapidXmlReader > RapidXmlReaderPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.