Skip to content

Commit ea05dde

Browse files
Leverage tesseract_common loadYamlFile and loadYamlString
1 parent 11d935b commit ea05dde

File tree

12 files changed

+322
-240
lines changed

12 files changed

+322
-240
lines changed

tesseract_collision/core/include/tesseract_collision/core/contact_managers_plugin_factory.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
3333
#include <map>
3434
TESSERACT_COMMON_IGNORE_WARNINGS_POP
3535

36+
#include <tesseract_common/fwd.h>
3637
#include <tesseract_common/filesystem.h>
3738
#include <tesseract_common/plugin_loader.h>
3839
#include <tesseract_common/plugin_info.h>
@@ -109,19 +110,20 @@ class ContactManagersPluginFactory
109110
* @brief Load plugins from yaml node
110111
* @param config The config node
111112
*/
112-
ContactManagersPluginFactory(YAML::Node config);
113+
ContactManagersPluginFactory(YAML::Node config, const tesseract_common::ResourceLocator& locator);
113114

114115
/**
115116
* @brief Load plugins from file path
116117
* @param config The config file path
117118
*/
118-
ContactManagersPluginFactory(const tesseract_common::fs::path& config);
119+
ContactManagersPluginFactory(const tesseract_common::fs::path& config,
120+
const tesseract_common::ResourceLocator& locator);
119121

120122
/**
121123
* @brief Load plugins from string
122124
* @param config The config string
123125
*/
124-
ContactManagersPluginFactory(const std::string& config);
126+
ContactManagersPluginFactory(const std::string& config, const tesseract_common::ResourceLocator& locator);
125127

126128
/**
127129
* @brief Add location for the plugin loader to search
@@ -281,6 +283,8 @@ class ContactManagersPluginFactory
281283
tesseract_common::PluginInfoContainer discrete_plugin_info_;
282284
tesseract_common::PluginInfoContainer continuous_plugin_info_;
283285
tesseract_common::PluginLoader plugin_loader_;
286+
287+
void loadConfig(const YAML::Node& config);
284288
};
285289
} // namespace tesseract_collision
286290
#endif // TESSERACT_COLLISION_CONTACT_MANAGERS_PLUGIN_FACTORY_H

tesseract_collision/core/src/contact_managers_plugin_factory.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
3131

3232
#include <tesseract_collision/core/discrete_contact_manager.h>
3333
#include <tesseract_collision/core/continuous_contact_manager.h>
34+
#include <tesseract_common/resource_locator.h>
3435
#include <tesseract_common/plugin_loader.hpp>
3536
#include <tesseract_common/yaml_utils.h>
3637
#include <tesseract_common/yaml_extenstions.h>
@@ -58,7 +59,7 @@ ContactManagersPluginFactory::ContactManagersPluginFactory()
5859
boost::token_compress_on);
5960
}
6061

61-
ContactManagersPluginFactory::ContactManagersPluginFactory(YAML::Node config) : ContactManagersPluginFactory()
62+
void ContactManagersPluginFactory::loadConfig(const YAML::Node& config)
6263
{
6364
if (const YAML::Node& plugin_info = config[ContactManagersPluginInfo::CONFIG_KEY])
6465
{
@@ -71,14 +72,26 @@ ContactManagersPluginFactory::ContactManagersPluginFactory(YAML::Node config) :
7172
}
7273
}
7374

74-
ContactManagersPluginFactory::ContactManagersPluginFactory(const tesseract_common::fs::path& config)
75-
: ContactManagersPluginFactory(YAML::LoadFile(config.string()))
75+
ContactManagersPluginFactory::ContactManagersPluginFactory(YAML::Node config,
76+
const tesseract_common::ResourceLocator& locator)
77+
: ContactManagersPluginFactory()
7678
{
79+
config = tesseract_common::processYamlIncludeDirective(config, locator);
80+
loadConfig(config);
7781
}
7882

79-
ContactManagersPluginFactory::ContactManagersPluginFactory(const std::string& config)
80-
: ContactManagersPluginFactory(YAML::Load(config))
83+
ContactManagersPluginFactory::ContactManagersPluginFactory(const tesseract_common::fs::path& config,
84+
const tesseract_common::ResourceLocator& locator)
85+
: ContactManagersPluginFactory()
8186
{
87+
loadConfig(tesseract_common::loadYamlFile(config.string(), locator));
88+
}
89+
90+
ContactManagersPluginFactory::ContactManagersPluginFactory(const std::string& config,
91+
const tesseract_common::ResourceLocator& locator)
92+
: ContactManagersPluginFactory()
93+
{
94+
loadConfig(tesseract_common::loadYamlString(config, locator));
8295
}
8396

8497
// This prevents it from being defined inline.

tesseract_collision/test/contact_managers_factory_static_unit.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
3232
#include <tesseract_collision/core/contact_managers_plugin_factory.h>
3333
#include <tesseract_collision/core/discrete_contact_manager.h>
3434
#include <tesseract_collision/bullet/bullet_factories.h>
35+
#include <tesseract_common/yaml_utils.h>
36+
#include <tesseract_common/resource_locator.h>
3537

3638
using namespace tesseract_collision;
3739

@@ -63,10 +65,11 @@ TEST(TesseractContactManagersFactoryUnit, StaticLoadPlugin) // NOLINT
6365
BulletCastSimpleManager:
6466
class: BulletCastSimpleManagerFactory)";
6567

66-
ContactManagersPluginFactory factory(config);
68+
tesseract_common::GeneralResourceLocator locator;
69+
ContactManagersPluginFactory factory(config, locator);
6770
factory.clearSearchLibraries();
6871
factory.clearSearchPaths();
69-
YAML::Node plugin_config = YAML::Load(config);
72+
YAML::Node plugin_config = tesseract_common::loadYamlString(config, locator);
7073

7174
DiscreteContactManager::UPtr cm = factory.createDiscreteContactManager("BulletDiscreteBVHManager");
7275
EXPECT_TRUE(cm != nullptr);

tesseract_collision/test/contact_managers_factory_unit.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ TESSERACT_COMMON_IGNORE_WARNINGS_POP
3232
#include <tesseract_collision/core/contact_managers_plugin_factory.h>
3333
#include <tesseract_collision/core/discrete_contact_manager.h>
3434
#include <tesseract_collision/core/continuous_contact_manager.h>
35+
#include <tesseract_common/yaml_utils.h>
36+
#include <tesseract_common/resource_locator.h>
3537

3638
using namespace tesseract_collision;
3739

3840
void runContactManagersFactoryTest(const tesseract_common::fs::path& config_path)
3941
{
40-
ContactManagersPluginFactory factory(config_path);
41-
YAML::Node plugin_config = YAML::LoadFile(config_path.string());
42+
tesseract_common::GeneralResourceLocator locator;
43+
ContactManagersPluginFactory factory(config_path, locator);
44+
YAML::Node plugin_config =
45+
tesseract_common::processYamlIncludeDirective(YAML::LoadFile(config_path.string()), locator);
4246

4347
const YAML::Node& plugin_info = plugin_config["contact_manager_plugins"];
4448
const YAML::Node& search_paths = plugin_info["search_paths"];
@@ -152,8 +156,9 @@ TEST(TesseractContactManagersFactoryUnit, LoadStringPluginTest) // NOLINT
152156
BulletCastSimpleManager:
153157
class: BulletCastSimpleManagerFactory)";
154158

155-
ContactManagersPluginFactory factory(config);
156-
YAML::Node plugin_config = YAML::Load(config);
159+
tesseract_common::GeneralResourceLocator locator;
160+
ContactManagersPluginFactory factory(config, locator);
161+
YAML::Node plugin_config = tesseract_common::loadYamlString(config, locator);
157162

158163
const YAML::Node& plugin_info = plugin_config["contact_manager_plugins"];
159164
const YAML::Node& search_paths = plugin_info["search_paths"];
@@ -318,8 +323,9 @@ TEST(TesseractContactManagersFactoryUnit, LoadOnlyDiscretePluginTest) // NOLINT
318323
FCLDiscreteBVHManager:
319324
class: FCLDiscreteBVHManagerFactory)";
320325

321-
ContactManagersPluginFactory factory(config);
322-
YAML::Node plugin_config = YAML::Load(config);
326+
tesseract_common::GeneralResourceLocator locator;
327+
ContactManagersPluginFactory factory(config, locator);
328+
YAML::Node plugin_config = tesseract_common::loadYamlString(config, locator);
323329

324330
const YAML::Node& plugin_info = plugin_config["contact_manager_plugins"];
325331
const YAML::Node& search_paths = plugin_info["search_paths"];
@@ -372,8 +378,9 @@ TEST(TesseractContactManagersFactoryUnit, LoadOnlyContinuousPluginTest) // NOLI
372378
BulletCastSimpleManager:
373379
class: BulletCastSimpleManagerFactory)";
374380

375-
ContactManagersPluginFactory factory(config);
376-
YAML::Node plugin_config = YAML::Load(config);
381+
tesseract_common::GeneralResourceLocator locator;
382+
ContactManagersPluginFactory factory(config, locator);
383+
YAML::Node plugin_config = tesseract_common::loadYamlString(config, locator);
377384

378385
const YAML::Node& plugin_info = plugin_config["contact_manager_plugins"];
379386
const YAML::Node& search_paths = plugin_info["search_paths"];

tesseract_common/include/tesseract_common/yaml_utils.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,30 @@ namespace tesseract_common
4040
{
4141
class ResourceLocator;
4242

43+
/**
44+
* @brief Recursively processes a YAML node to resolve `!include` directives.
45+
*
46+
* This function replaces any node tagged with `!include` with the content of the
47+
* specified file. It also recursively processes maps and sequences to handle nested
48+
* `!include` directives.
49+
*
50+
* @param node The YAML node to process.
51+
* @param locator The locator used to resolve urls and relative file paths.
52+
* @return A YAML::Node with all `!include` directives resolved.
53+
*
54+
* @throws std::runtime_error if an `!include` tag is used improperly (e.g., not scalar),
55+
* or if a file specified in an `!include` directive cannot be loaded.
56+
*/
57+
YAML::Node processYamlIncludeDirective(const YAML::Node& node, const ResourceLocator& locator);
58+
4359
/**
4460
* @brief Loads a YAML file and processes `!include` directives recursively.
4561
*
4662
* This function loads a YAML file and replaces any node tagged with `!include`
4763
* with the content of the specified file. It handles nested `!include` directives
4864
* and works with both maps and sequences.
4965
*
50-
* @param file_path The path to the YAML file to be loaded. Relative paths should be
51-
* resolved based on the caller's context.
66+
* @param file_path The path to the YAML file/url to be loaded.
5267
* @param locator The locator used to resolve urls and relative file paths.
5368
* @return A YAML::Node object containing the fully processed YAML structure.
5469
*

tesseract_common/src/yaml_utils.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace tesseract_common
4949
* @throws std::runtime_error if an `!include` tag is used improperly (e.g., not scalar),
5050
* or if a file specified in an `!include` directive cannot be loaded.
5151
*/
52-
YAML::Node handleIncludeDirective(const YAML::Node& node, const ResourceLocator& locator)
52+
YAML::Node processYamlIncludeDirective(const YAML::Node& node, const ResourceLocator& locator)
5353
{
5454
if (node.Tag() == "!include")
5555
{
@@ -63,15 +63,15 @@ YAML::Node handleIncludeDirective(const YAML::Node& node, const ResourceLocator&
6363
if (resource == nullptr)
6464
throw std::runtime_error("Unable to locate resource: " + included_file);
6565

66-
return handleIncludeDirective(YAML::LoadFile(resource->getFilePath()), *resource);
66+
return processYamlIncludeDirective(YAML::LoadFile(resource->getFilePath()), *resource);
6767
}
6868

6969
if (node.IsMap())
7070
{
7171
// Create a new map and process each key-value pair
7272
YAML::Node processed_map = YAML::Node(YAML::NodeType::Map);
7373
for (auto it = node.begin(); it != node.end(); ++it)
74-
processed_map[it->first] = handleIncludeDirective(it->second, locator);
74+
processed_map[it->first] = processYamlIncludeDirective(it->second, locator);
7575

7676
return processed_map;
7777
}
@@ -81,7 +81,7 @@ YAML::Node handleIncludeDirective(const YAML::Node& node, const ResourceLocator&
8181
// Create a new sequence and process each element
8282
YAML::Node processed_sequence = YAML::Node(YAML::NodeType::Sequence);
8383
for (const auto& child : node)
84-
processed_sequence.push_back(handleIncludeDirective(child, locator));
84+
processed_sequence.push_back(processYamlIncludeDirective(child, locator));
8585

8686
return processed_sequence;
8787
}
@@ -92,17 +92,15 @@ YAML::Node handleIncludeDirective(const YAML::Node& node, const ResourceLocator&
9292

9393
YAML::Node loadYamlFile(const std::string& file_path, const ResourceLocator& locator)
9494
{
95-
// Load the base YAML file
9695
auto resource = locator.locateResource(file_path);
9796
YAML::Node root = YAML::LoadFile(resource->getFilePath());
98-
return handleIncludeDirective(root, *resource);
97+
return processYamlIncludeDirective(root, *resource);
9998
}
10099

101100
YAML::Node loadYamlString(const std::string& yaml_string, const ResourceLocator& locator)
102101
{
103-
// Load the base YAML file
104102
YAML::Node root = YAML::Load(yaml_string);
105-
return handleIncludeDirective(root, locator);
103+
return processYamlIncludeDirective(root, locator);
106104
}
107105

108106
void writeYamlToFile(const YAML::Node& node, const std::string& file_path)

0 commit comments

Comments
 (0)