Skip to content

Commit 4e739b8

Browse files
committed
Add entity and sdf parameters to Server's AddSystem interface
Signed-off-by: Gabriel Arjones <arjones@arjones.com>
1 parent 633ce72 commit 4e739b8

File tree

3 files changed

+119
-7
lines changed

3 files changed

+119
-7
lines changed

include/gz/sim/Server.hh

+48
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <gz/sim/Export.hh>
2727
#include <gz/sim/ServerConfig.hh>
2828
#include <gz/sim/SystemPluginPtr.hh>
29+
#include <sdf/Element.hh>
30+
#include <sdf/Plugin.hh>
2931

3032
namespace gz
3133
{
@@ -225,14 +227,60 @@ namespace gz
225227
const SystemPluginPtr &_system,
226228
const unsigned int _worldIndex = 0);
227229

230+
/// \brief Add a System to the server. The server must not be running when
231+
/// calling this.
232+
/// \param[in] _system system to be added
233+
/// \param[in] _entity Entity of system to be added.
234+
/// If _entity is std::nullopt, it will be added to the world entity.
235+
/// \param[in] _sdf Pointer to the SDF element of a <plugin> tag with
236+
/// configuration options for the system being added.
237+
/// \param[in] _worldIndex Index of the world to query.
238+
/// \return Whether the system was added successfully, or std::nullopt
239+
/// if _worldIndex is invalid.
240+
public: std::optional<bool> AddSystem(
241+
const SystemPluginPtr &_system,
242+
std::optional<Entity> _entity,
243+
std::optional<std::shared_ptr<const sdf::Element>> _sdf,
244+
const unsigned int _worldIndex = 0);
245+
246+
/// \brief Add a System to the server. The server must not be running when
247+
/// calling this.
248+
/// \param[in] _plugin system plugin to be added with any additional XML
249+
/// contents.
250+
/// \param[in] _entity Entity of system to be added.
251+
/// If _entity is std::nullopt, it will be added to the world entity.
252+
/// \param[in] _worldIndex Index of the world to query.
253+
/// \return Whether the system was added successfully, or std::nullopt
254+
/// if _worldIndex is invalid.
255+
public: std::optional<bool> AddSystem(
256+
const sdf::Plugin &_plugin,
257+
std::optional<Entity> _entity,
258+
const unsigned int _worldIndex = 0);
259+
260+
/// \brief Add a System to the server. The server must not be running when
261+
/// calling this.
262+
/// \param[in] _system System to be added
263+
/// \param[in] _worldIndex Index of the world to add to.
264+
/// \return Whether the system was added successfully, or std::nullopt
265+
/// if _worldIndex is invalid.
266+
public: std::optional<bool> AddSystem(
267+
const std::shared_ptr<System> &_system,
268+
const unsigned int _worldIndex = 0);
269+
228270
/// \brief Add a System to the server. The server must not be running when
229271
/// calling this.
230272
/// \param[in] _system System to be added
273+
/// \param[in] _entity Entity of system to be added.
274+
/// If _entity is std::nullopt, it will be added to the world entity.
275+
/// \param[in] _sdf Pointer to the SDF element of a <plugin> tag with
276+
/// configuration options for the system being added
231277
/// \param[in] _worldIndex Index of the world to add to.
232278
/// \return Whether the system was added successfully, or std::nullopt
233279
/// if _worldIndex is invalid.
234280
public: std::optional<bool> AddSystem(
235281
const std::shared_ptr<System> &_system,
282+
std::optional<Entity> _entity,
283+
std::optional<std::shared_ptr<const sdf::Element>> _sdf,
236284
const unsigned int _worldIndex = 0);
237285

238286
/// \brief Get an Entity based on a name.

src/Server.cc

+35-2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,29 @@ std::optional<size_t> Server::SystemCount(const unsigned int _worldIndex) const
352352
//////////////////////////////////////////////////
353353
std::optional<bool> Server::AddSystem(const SystemPluginPtr &_system,
354354
const unsigned int _worldIndex)
355+
{
356+
return this->AddSystem(_system, std::nullopt, std::nullopt, _worldIndex);
357+
}
358+
359+
//////////////////////////////////////////////////
360+
std::optional<bool> Server::AddSystem(const sdf::Plugin &_plugin,
361+
std::optional<Entity> _entity,
362+
const unsigned int _worldIndex)
363+
{
364+
auto system = this->dataPtr->systemLoader->LoadPlugin(_plugin);
365+
if (system)
366+
{
367+
return this->AddSystem(*system, _entity, _plugin.ToElement(), _worldIndex);
368+
}
369+
return false;
370+
}
371+
372+
//////////////////////////////////////////////////
373+
std::optional<bool> Server::AddSystem(
374+
const SystemPluginPtr &_system,
375+
std::optional<Entity> _entity,
376+
std::optional<std::shared_ptr<const sdf::Element>> _sdf,
377+
const unsigned int _worldIndex)
355378
{
356379
// Check the current state, and return early if preconditions are not met.
357380
std::lock_guard<std::mutex> lock(this->dataPtr->runMutex);
@@ -364,7 +387,7 @@ std::optional<bool> Server::AddSystem(const SystemPluginPtr &_system,
364387

365388
if (_worldIndex < this->dataPtr->simRunners.size())
366389
{
367-
this->dataPtr->simRunners[_worldIndex]->AddSystem(_system);
390+
this->dataPtr->simRunners[_worldIndex]->AddSystem(_system, _entity, _sdf);
368391
return true;
369392
}
370393

@@ -374,6 +397,16 @@ std::optional<bool> Server::AddSystem(const SystemPluginPtr &_system,
374397
//////////////////////////////////////////////////
375398
std::optional<bool> Server::AddSystem(const std::shared_ptr<System> &_system,
376399
const unsigned int _worldIndex)
400+
{
401+
return this->AddSystem(_system, std::nullopt, std::nullopt, _worldIndex);
402+
}
403+
404+
//////////////////////////////////////////////////
405+
std::optional<bool> Server::AddSystem(
406+
const std::shared_ptr<System> &_system,
407+
std::optional<Entity> _entity,
408+
std::optional<std::shared_ptr<const sdf::Element>> _sdf,
409+
const unsigned int _worldIndex)
377410
{
378411
std::lock_guard<std::mutex> lock(this->dataPtr->runMutex);
379412
if (this->dataPtr->running)
@@ -384,7 +417,7 @@ std::optional<bool> Server::AddSystem(const std::shared_ptr<System> &_system,
384417

385418
if (_worldIndex < this->dataPtr->simRunners.size())
386419
{
387-
this->dataPtr->simRunners[_worldIndex]->AddSystem(_system);
420+
this->dataPtr->simRunners[_worldIndex]->AddSystem(_system, _entity, _sdf);
388421
return true;
389422
}
390423

src/Server_TEST.cc

+36-5
Original file line numberDiff line numberDiff line change
@@ -625,17 +625,48 @@ TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(RunOnceUnpaused))
625625
auto mockSystemPlugin = systemLoader.LoadPlugin(sdfPlugin);
626626
ASSERT_TRUE(mockSystemPlugin.has_value());
627627

628-
// Check that it was loaded
629-
const size_t systemCount = *server.SystemCount();
630-
EXPECT_TRUE(*server.AddSystem(mockSystemPlugin.value()));
631-
EXPECT_EQ(systemCount + 1, *server.SystemCount());
632-
633628
// Query the interface from the plugin
634629
auto system = mockSystemPlugin.value()->QueryInterface<sim::System>();
635630
EXPECT_NE(system, nullptr);
636631
auto mockSystem = dynamic_cast<sim::MockSystem*>(system);
637632
EXPECT_NE(mockSystem, nullptr);
638633

634+
Entity entity = server.EntityByName("default").value();
635+
size_t configureCallCount = 0;
636+
auto configureCallback = [&sdfPlugin, &entity, &configureCallCount](
637+
const Entity &_entity,
638+
const std::shared_ptr<const sdf::Element> &_sdf,
639+
EntityComponentManager &,
640+
EventManager &)
641+
{
642+
configureCallCount++;
643+
EXPECT_EQ(entity, _entity);
644+
EXPECT_EQ(sdfPlugin.ToElement()->ToString(""), _sdf->ToString(""));
645+
};
646+
647+
mockSystem->configureCallback = configureCallback;
648+
const size_t systemCount = *server.SystemCount();
649+
EXPECT_TRUE(
650+
*server.AddSystem(
651+
mockSystemPlugin.value(), entity, sdfPlugin.ToElement()
652+
));
653+
654+
// Add pointer
655+
auto mockSystemPtr = std::make_shared<MockSystem>();
656+
mockSystemPtr->configureCallback = configureCallback;
657+
EXPECT_TRUE(*server.AddSystem(mockSystemPtr, entity, sdfPlugin.ToElement()));
658+
659+
// Add an sdf::Plugin
660+
EXPECT_TRUE(*server.AddSystem(sdfPlugin, entity));
661+
662+
// Fail if plugin is invalid
663+
sdf::Plugin invalidPlugin("foo_plugin", "foo::systems::FooPlugin");
664+
EXPECT_FALSE(*server.AddSystem(invalidPlugin, entity));
665+
666+
// Check that it was loaded
667+
EXPECT_EQ(systemCount + 3, *server.SystemCount());
668+
EXPECT_EQ(2u, configureCallCount);
669+
639670
// No steps should have been executed
640671
EXPECT_EQ(0u, mockSystem->preUpdateCallCount);
641672
EXPECT_EQ(0u, mockSystem->updateCallCount);

0 commit comments

Comments
 (0)